-
-
Save justinrainbow/3638538 to your computer and use it in GitHub Desktop.
Sublime Text 2 PHP Getter Setter Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime, sublime_plugin, re, os | |
# TODO | |
# check indention (spaces/tabs/size, current indention ) | |
# insert right where the pointer is, or insert right before the closing } of the class? | |
# check of these generator getter/setters already exist? make it idempotent? | |
class GeneratorCommand(sublime_plugin.TextCommand): | |
def fixup(self, string): | |
return re.sub(r'\r\n|\r', '\n', string.decode('utf-8')) | |
def formalName(self, rawName): | |
return u"%s" % "".join([part.title() for part in rawName.split("_")]) | |
def run(self, edit): | |
output = u"\n" | |
setterTemplate = """ | |
public function set%s( $x ) { | |
$this->%s = $x; | |
} | |
""" | |
getterTemplate = """ | |
public function get%s() { | |
return $this->%s; | |
} | |
""" | |
propertyTemplate = """%s | |
%s | |
""" | |
prefixPrivateSize = len(u"private $") | |
prefixPublicSize = len(u"public $") | |
prefixProtectedSize = len(u"protected $") | |
bufferLength = sublime.Region(0, self.view.size()) | |
bufferContent = self.view.substr(bufferLength).encode('utf-8') | |
for line in bufferContent.split("\n"): | |
if line.strip().startswith("private $") or line.strip().startswith("protected $") or line.strip().startswith("public $"): | |
if line.strip().startswith("private $"): | |
realName = line.strip()[prefixPrivateSize:-1] | |
elif line.strip().startswith("public $"): | |
realName = line.strip()[prefixPublicSize:-1] | |
elif line.strip().startswith("protected $"): | |
realName = line.strip()[prefixProtectedSize:-1] | |
formalName = self.formalName(realName) | |
setTemplate = setterTemplate %( formalName, realName) | |
getTemplate = getterTemplate %( formalName, realName) | |
output += propertyTemplate % (setTemplate, getTemplate) | |
self.view.insert(edit, self.view.size(), self.fixup(output)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment