Skip to content

Instantly share code, notes, and snippets.

@migajek
Last active August 29, 2015 14:04
Show Gist options
  • Save migajek/40f9d3e7fbd39787edb3 to your computer and use it in GitHub Desktop.
Save migajek/40f9d3e7fbd39787edb3 to your computer and use it in GitHub Desktop.
Sublime Text 3 plugin for quick modification of dependencies
import sublime, sublime_plugin
import re
class AngularPromptDepsCommand(sublime_plugin.WindowCommand):
def run(self):
defstr = ""
if self.window.active_view():
cmd = AngularInjectGetDepsCommand(view = self.window.active_view())
result = cmd.run(None)
if result:
defstr = ", ".join(result['lst'])
self.result = result
self.window.show_input_panel("Modify dependencies list:", defstr, self.on_done, None, None)
pass
def on_done(self, text):
self.result['new_lst'] = [x.strip() for x in text.split(',')]
try:
if self.window.active_view():
self.window.active_view().run_command("angular_inject_set_deps", {"result": self.result} )
except ValueError:
pass
class AngularInjectGetDepsCommand(sublime_plugin.TextCommand):
REGEX = '(?P<prefix>.*\[\s*)(?P<deps>([\'"]([$\w]+)[\'"]\s*\,?\s*){,})?(?P<funcspec>function\s*\(([\w\d\s\,\$\-\_\'"]*)\))'
def run(self, edit, **kwargs):
for region in self.view.sel():
if region.empty():
line = self.view.line(region)
line_contents = self.view.substr(line)
else:
line_contents = self.view.substr(region)
return self.parse_deps(line_contents)
def parse_deps(self, txt):
matches = re.match(self.REGEX, txt, re.M)
if not matches:
self.view.run_command('angular_goto_controller')
matches = re.match(self.REGEX, txt, re.M)
if matches:
return {'str': matches.group('deps'), 'lst': [x.strip() for x in matches.groups()[-1].split(',')]}
class AngularInjectSetDepsCommand(sublime_plugin.TextCommand):
def run(self, edit, result, **kwargs):
for region in self.view.sel():
reg_org = region
region = self.view.line(region) if region.empty() else region
line_contents = self.view.substr(region)
matches = re.search(AngularInjectGetDepsCommand.REGEX, line_contents)
new_textual = self.build_textual(result['str'], result['new_lst'])
if result['str']:
line_contents = line_contents.replace(result['str'], new_textual)
elif matches:
line_contents = line_contents.replace(matches.group('prefix'), matches.group('prefix') + new_textual)
if result['new_lst']:
if matches:
funcspec = matches.group('funcspec')
repl = re.sub('(.*function\s*\().*(\).*)', '\\1%s\\2' % ", ".join(result['new_lst']), funcspec)
line_contents = line_contents.replace(funcspec, repl)
print (region)
self.view.replace(edit, region, line_contents)
def build_textual(self, old_textual, new_list):
result = []
for item in new_list:
match = re.search('(\'|")%s' % item, old_textual)
quote = match.groups()[0] if match else '\''
result.append('%s%s%s' % (quote, item, quote))
if not result:
return ""
return (", ".join(result)) + ", "
class AngularGotoControllerCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
text = self.view.substr(sublime.Region(0, self.view.sel()[0].end()))
match = list(re.finditer(r"\.\s*controller\s*\(\s*['\"]", text))[-1]
if match:
pos = match.end()
self.view.sel().clear()
self.view.sel().add(sublime.Region(pos, pos))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment