Last active
September 9, 2020 21:03
-
-
Save korinVR/3542836 to your computer and use it in GitHub Desktop.
Sublime Text plugin : Open the URL under the cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
import webbrowser | |
class OpenUrlCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
s = self.view.sel()[0] | |
# Expand selection to possible URL | |
start = s.a | |
end = s.b | |
view_size = self.view.size() | |
terminator = ['\t', ' ', '\"', '\'', '(', ')'] | |
while (start > 0 | |
and not self.view.substr(start - 1) in terminator | |
and self.view.classify(start) & sublime.CLASS_LINE_START == 0): | |
start -= 1 | |
while (end < view_size | |
and not self.view.substr(end) in terminator | |
and self.view.classify(end) & sublime.CLASS_LINE_END == 0): | |
end += 1 | |
# Check if this is URL | |
url = self.view.substr(sublime.Region(start, end)) | |
print("URL : " + url) | |
if url.startswith(('http://', 'https://')): | |
webbrowser.open_new_tab(url) | |
else: | |
print("not URL") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is possible to get similar functionality but with files. I mean: From a text containing a path to a file with a line number like 'path/to/a/file.extension line 22' on click goto that file on that line????
Thanks.