Skip to content

Instantly share code, notes, and snippets.

@V13Axel
Created August 6, 2015 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save V13Axel/e6a09be8a59cfde2a982 to your computer and use it in GitHub Desktop.
Save V13Axel/e6a09be8a59cfde2a982 to your computer and use it in GitHub Desktop.
(Naively) Simplifies injecting a property into the constructer as a property, setting it in the constructor, and defining the class property in PHP using Sublime Text. Add {"keys": ["ctrl+i"], "command": "insert_property"} to your sublime keymap, then simply place your cursor on a constructor variable and hit "Ctrl+I"! In-action: http://i.imgur.…
import sublime, sublime_plugin
class InsertPropertyCommand(sublime_plugin.TextCommand):
def run(self, edit):
beginningOfClass = self.view.find_all(r"<\?php(.*\n)*class(.*\n*)\{")[0].end()
constructorFirstLinePos = self.view.find_all(r"__construct(.*\n*).*\{")[0]
constructorPos = self.view.find_all(r"__construct(.*\n*){1,10}?\}")[0]
selectionWordPosition = self.view.word(self.view.sel()[0])
constructorFirstLineStr = self.view.substr(constructorFirstLinePos)
constructorStr = self.view.substr(constructorPos)
symbol = self.view.substr(selectionWordPosition)
# nextchar = self.view.substr(constructorFirstLinePos.end())
if symbol in constructorFirstLineStr and ("$" + symbol + ";" not in constructorStr):
self.view.insert(edit, constructorFirstLinePos.end(), "\n\t\t$this->" + symbol + " = $" + symbol + ';')
self.view.insert(edit, beginningOfClass, "\n\n\tprotected $" + symbol + ";")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment