forked from evandroforks/SublimePackageDefault
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_context_url.py
54 lines (40 loc) · 1.45 KB
/
open_context_url.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
46
47
48
49
50
51
52
53
54
import re
import webbrowser
import sublime_plugin
rex = re.compile(
r'''(?x)
\b(?:
https?://(?:(?:[\w\d\-]+(?:\.[\w\d\-.]+)+)|localhost)| # http://
www\.[\w\d\-]+(?:\.[\w\d\-.]+)+ # www.
)
/?[\w\d\-.?,!'(){}\[\]/+&@%$#=:"|~;]* # url path and query string
[\w\d\-~:/#@$*+=] # allowed end chars
''')
class OpenContextUrlCommand(sublime_plugin.TextCommand):
def run(self, edit, event):
url = self.find_url(event)
webbrowser.open_new_tab(url)
def is_visible(self, event):
return self.find_url(event) is not None
def find_url(self, event):
pt = self.view.window_to_text((event["x"], event["y"]))
line = self.view.line(pt)
line.a = max(line.a, pt - 1024)
line.b = min(line.b, pt + 1024)
text = self.view.substr(line)
it = rex.finditer(text)
for match in it:
if match.start() <= (pt - line.a) and match.end() >= (pt - line.a):
url = text[match.start():match.end()]
if url[0:3] == "www":
return "http://" + url
else:
return url
return None
def description(self, event):
url = self.find_url(event)
if len(url) > 64:
url = url[0:64] + "..."
return "Open " + url
def want_event(self):
return True