This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
clickable_urls.py
146 lines (118 loc) · 5.68 KB
/
clickable_urls.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sublime
import sublime_plugin
import webbrowser
import threading
class UrlHighlighter(sublime_plugin.EventListener):
# Thanks Jeff Atwood http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html
# ^ that up here is a URL that should be matched
URL_REGEX = "\\bhttps?://[-A-Za-z0-9+&@#/%?=~_()|!:,.;']*[-A-Za-z0-9+&@#/%=~_(|]"
DEFAULT_MAX_URLS = 200
SETTINGS_FILENAME = 'ClickableUrls.sublime-settings'
urls_for_view = {}
scopes_for_view = {}
ignored_views = []
browser = None
highlight_semaphore = threading.Semaphore()
def on_activated(self, view):
self.update_url_highlights(view)
# Blocking handlers for ST2
def on_load(self, view):
if sublime.version() < '3000':
self.update_url_highlights(view)
def on_modified(self, view):
if sublime.version() < '3000':
self.update_url_highlights(view)
# Async listeners for ST3
def on_load_async(self, view):
self.update_url_highlights_async(view)
def on_modified_async(self, view):
self.update_url_highlights_async(view)
def on_close(self, view):
for map in [self.urls_for_view, self.scopes_for_view, self.ignored_views]:
if view.id() in map:
del map[view.id()]
"""The logic entry point. Find all URLs in view, store and highlight them"""
def update_url_highlights(self, view):
settings = sublime.load_settings(UrlHighlighter.SETTINGS_FILENAME)
should_highlight_urls = settings.get('highlight_urls', True)
max_url_limit = settings.get('max_url_limit', UrlHighlighter.DEFAULT_MAX_URLS)
if view.id() in UrlHighlighter.ignored_views:
return
urls = view.find_all(UrlHighlighter.URL_REGEX)
# Avoid slowdowns for views with too much URLs
if len(urls) > max_url_limit:
print("UrlHighlighter: ignoring view with %u URLs" % len(urls))
UrlHighlighter.ignored_views.append(view.id())
return
UrlHighlighter.urls_for_view[view.id()] = urls
should_highlight_urls = sublime.load_settings(UrlHighlighter.SETTINGS_FILENAME).get('highlight_urls', True)
if (should_highlight_urls):
self.highlight_urls(view, urls)
"""Same as update_url_highlights, but avoids race conditions with a
semaphore."""
def update_url_highlights_async(self, view):
UrlHighlighter.highlight_semaphore.acquire()
try:
self.update_url_highlights(view)
finally:
UrlHighlighter.highlight_semaphore.release()
"""Creates a set of regions from the intersection of urls and scopes,
underlines all of them."""
def highlight_urls(self, view, urls):
# We need separate regions for each lexical scope for ST to use a proper color for the underline
scope_map = {}
for url in urls:
scope_name = view.scope_name(url.a)
scope_map.setdefault(scope_name, []).append(url)
for scope_name in scope_map:
self.underline_regions(view, scope_name, scope_map[scope_name])
self.update_view_scopes(view, scope_map.keys())
"""Apply underlining with provided scope name to provided regions.
Uses the empty region underline hack for Sublime Text 2 and native
underlining for Sublime Text 3."""
def underline_regions(self, view, scope_name, regions):
if sublime.version() >= '3019':
# in Sublime Text 3, the regions are just underlined
view.add_regions(
u'clickable-urls ' + scope_name,
regions,
scope_name,
flags=sublime.DRAW_NO_FILL|sublime.DRAW_NO_OUTLINE|sublime.DRAW_SOLID_UNDERLINE)
else:
# in Sublime Text 2, the 'empty region underline' hack is used
char_regions = [sublime.Region(pos, pos) for region in regions for pos in range(region.a, region.b)]
view.add_regions(
u'clickable-urls ' + scope_name,
char_regions,
scope_name,
sublime.DRAW_EMPTY_AS_OVERWRITE)
"""Store new set of underlined scopes for view. Erase underlining from
scopes that were used but are not anymore."""
def update_view_scopes(self, view, new_scopes):
old_scopes = UrlHighlighter.scopes_for_view.get(view.id(), None)
if old_scopes:
unused_scopes = set(old_scopes) - set(new_scopes)
for unused_scope_name in unused_scopes:
view.erase_regions(u'clickable-urls ' + unused_scope_name)
UrlHighlighter.scopes_for_view[view.id()] = new_scopes
def open_url(url):
browser = sublime.load_settings(UrlHighlighter.SETTINGS_FILENAME).get('clickable_urls_browser')
try:
webbrowser.get(browser).open(url, autoraise=True)
except(webbrowser.Error):
sublime.error_message('Failed to open browser. See "Customizing the browser" in the README.')
class OpenUrlUnderCursorCommand(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.id() in UrlHighlighter.urls_for_view:
for selection in self.view.sel():
if selection.empty():
selection = next((url for url in UrlHighlighter.urls_for_view[self.view.id()] if url.contains(selection)), None)
if not selection:
return
url = self.view.substr(selection)
open_url(url)
class OpenAllUrlsCommand(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.id() in UrlHighlighter.urls_for_view:
for url in set([self.view.substr(url_region) for url_region in UrlHighlighter.urls_for_view[self.view.id()]]):
open_url(url)