Skip to content

Instantly share code, notes, and snippets.

@andytill
Last active March 21, 2018 05:00
Show Gist options
  • Select an option

  • Save andytill/7856573 to your computer and use it in GitHub Desktop.

Select an option

Save andytill/7856573 to your computer and use it in GitHub Desktop.
A Sublime Text command to take some selected text and introduce an Erlang "variable" with the selection as the assigned value. It doesn't understand the text of course, I thought you were supposed to!
import sublime
import sublime_plugin
import re
class IntroduceVariableCommand(sublime_plugin.TextCommand):
var_name = "NewVar"
def run(self, edit):
sels = self.view.sel()
for sel in sels:
if not sel.empty():
self.introduce_variable(edit, sel)
def introduce_variable(self, edit, sel):
sel_text = self.view.substr(sel)
top_line = self.view.lines(sel)[0]
top_line_text = self.view.substr(top_line)
indentation = ""
match = re.search(r"\W*", top_line_text, re.UNICODE)
if match:
indentation = match.group(0)
var_declaration = indentation + self.var_name + " = " + sel_text + ",\n"
self.view.replace(edit, sel, self.var_name)
self.view.insert(edit, top_line.a, var_declaration)
@patcorwin

Copy link
Copy Markdown

Turns out my desire to share code isn't enough to get me to actually learn how to use github, so I gave up and made a new repo: https://github.com/patcorwin/ExtractAsVariable, starting from yours. I changed it so it instead puts the cursor at all locations, handling multiple selection. Thanks for the jumpstart!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment