-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownloadSourceCommand.py
36 lines (28 loc) · 1.14 KB
/
DownloadSourceCommand.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
import sublime
import sublime_plugin
urlopen = None
try:
import urllib.request
urlopen = urllib.request.urlopen
except (ImportError) as e:
import urllib2
urlopen = urllib2.urlopen
class DownloadSourceCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel('Url:', '', self.on_done, None, None)
def on_done(self, text):
content = urlopen(text).read().decode('utf-8')
view = self.window.new_file()
view.run_command('insert_source', {'text': content})
class DownloadInsertSourceCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel('Url:', '', self.on_done, None, None)
def on_done(self, text):
content = urlopen(text).read().decode('utf-8')
self.view.run_command('insert_source', {'text': content})
class InsertSourceCommand(sublime_plugin.TextCommand):
def run(self, edit, text):
self.view.replace(edit, self.view.sel()[0], text)
current_sel = self.view.sel()[0]
self.view.sel().clear()
self.view.sel().add(sublime.Region(current_sel.begin(), current_sel.begin()))