This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuseIt.py
45 lines (35 loc) · 1.37 KB
/
useIt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sublime
import sublime_plugin
import sys
import re
import webbrowser
# Outside pattern compilation to have better performance for multi
# selection
CLEAN_CSS_PATTERN = re.compile(r'([a-z-]+)', re.IGNORECASE)
BASE_URL = 'http://caniuse.com/#search='
class UseItCommand(sublime_plugin.TextCommand):
"""
This will search a word or a selection.
Default binding recommendation: "ctrl + alt + d"
"""
def run(self, edit):
s = sublime.load_settings("Can I Use.sublime-settings")
default_browser = s.get('default_browser', '')
if not default_browser:
platform = sublime.platform()
if platform == 'windows':
default_browser = 'windows-default'
elif platform == "osx":
default_browser = 'macosx'
for region in self.view.sel():
# Get the start point of the region of the selection
point = region.begin()
scope = self.view.extract_scope(point)
search = self.view.substr(scope)
# Clean the selection on css syntax
if "/CSS" in self.view.settings().get('syntax'):
re_search = CLEAN_CSS_PATTERN.search(search)
if re_search:
search = re_search.group()
browser = webbrowser.get(default_browser)
browser.open_new_tab(BASE_URL + search)