-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemma.py
More file actions
69 lines (52 loc) · 2.05 KB
/
Copy pathgemma.py
File metadata and controls
69 lines (52 loc) · 2.05 KB
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
from urllib import request
import json, re, sublime, sublime_plugin, threading
class GemmaReplaceLineCommand(sublime_plugin.TextCommand):
def run(self, edit, text):
selection = self.view.sel()[0]
line = self.view.line(selection)
self.view.replace(edit, line, text)
class GemmaCommand(sublime_plugin.TextCommand):
items = []
def search_rubygems(self, query):
status = "Searching RubyGems for '%s'..." % (query)
self.view.set_status('gemma', status)
url = "https://rubygems.org/api/v1/search.json?query=%s" % (query)
req = request.urlopen(url)
self.view.erase_status('gemma')
encoding = req.headers.get_content_charset()
obj = json.loads(req.read().decode(encoding))
self.handle_rubygems_result(query, obj)
def handle_rubygems_result(self, query, matches):
if len(matches) is 0:
message = "Rubygems had no matches for '%s'" % (query)
sublime.error_message(message)
return
self.items.clear()
for match in matches:
self.items.append([match['name'], match['version']])
window = sublime.active_window()
window.show_quick_panel(self.items, self.gem_selected)
def gem_selected(self, selected_index):
if selected_index < 0:
return
item = self.items[selected_index]
text = "gem '%s', '~> %s'" % (item[0], item[1])
self.view.run_command('gemma_replace_line', { 'text': text })
def run(self, edit):
selection = self.view.sel()[0]
line = self.view.line(selection)
if selection.empty() is False:
gem_name = self.view.substr(selection)
elif line.empty() is False:
gem_name = self.view.substr(line).strip()
else:
sublime.error_message("Place the cursor on a new line, type a gem name and run the command again.")
return
pattern = re.compile('^gem [\'"]([\d\w_-]+)[\'"].*')
match = pattern.match(gem_name)
if match is not None:
gem_name = match.group(1)
else:
gem_name = re.sub('[^\d\w_-]', '', gem_name)
thread = threading.Thread(target=self.search_rubygems, args=(gem_name,))
thread.start()