Reputation: 548
I do a lot of JavaScript projects, and I miss a great feature from PHPStorm. Now I don't understand Python that much. So hope you can help me.
This is what I want:
'test'.log => console.log('test');
test.log => console.log(test);
So with a single tabtrigger .log
I want to retrieve anything before the .log
. And then I will transform it. How can I do this?
Upvotes: 3
Views: 373
Reputation: 4847
You can just create a plugin to retrieve the text before .log
and replace it in the view:
import re
import sublime
import sublime_plugin
class PostSnippetLogCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
for sel in view.sel():
pos = sel.b
text_before = view.substr(sublime.Region(view.line(pos).a, pos))
# match the text before in reversed order
m = re.match(r"gol\.(\S*)", text_before[::-1])
if not m:
continue
# retrieve the text before .log and reestablish the correct order
text_content = m.group(1)[::-1]
# create the replacements text and region
replace_text = "console.log({});".format(text_content)
replace_reg = sublime.Region(pos - len(m.group(0)), pos)
# replace the text
view.replace(edit, replace_reg, replace_text)
Afterwards add this keybinding to trigger the command if it is prefixed with .log
inside a javascript document.
{
"keys": ["tab"],
"command": "post_snippet_log",
"context":
[
{ "key": "selector", "operand": "source.js" },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\.log$" },
],
},
Upvotes: 4