Komodo 是否支持像 NetBeans 或 Eclipse 那样自动生成 getter/setter?如果是这样,我该如何使用它?我似乎找不到它。
问问题
1213 次
5 回答
4
这是一个修改/改进的版本,具有更易读的代码。还将从属性声明中删除默认值,例如public $prop = array();
from xpcom import components
import re
viewSvc = components.classes["@activestate.com/koViewService;1"]\
.getService(components.interfaces.koIViewService)
view = viewSvc.currentView.queryInterface(components.interfaces.koIScintillaView)
sm = view.scimoz
sm.currentPos # current position in the editor
sm.text # editor text
# sm.selText # the selected text
output = u"\n"
setterTemplate = """
/**
* Sets %s
*
* @param mixed $value
* @return $this
*/
public function set%s($value) {
$this->%s = $value;
return $this;
}"""
getterTemplate = """
/**
* Gets %s
*
* @return string
*/
public function get%s() {
return $this->%s;
}
"""
propertyTemplate = """%s
%s"""
prefixSizePv = len(u"private $")
prefixSizePu = len(u"public $")
prefixSizePr = len(u"protected $")
def formalName(rawName):
return u"%s%s" % (rawName[0:1].upper(), rawName[1:])
#todo find a better way to split lines, what if its Mac or Windows format?
for line in sm.text.split("\n"):
tmpLine = line.strip()
hasPriv = tmpLine.startswith("private $")
hasPublic = tmpLine.startswith("public $")
hasProt = tmpLine.startswith('protected $')
if hasPriv or hasPublic or hasProt:
if hasPriv:
realName = tmpLine[prefixSizePv:-1]
elif hasPublic:
realName = tmpLine[prefixSizePu:-1]
else:
realName = tmpLine[prefixSizePr:-1]
realName = re.sub('\s?=.*', '', realName);
formal = formalName(realName)
output += propertyTemplate % ( setterTemplate %(realName, formal, realName), getterTemplate % (realName, formal, realName))
sm.insertText(sm.currentPos, output)
于 2013-06-12T02:31:38.503 回答
1
我不认为 Komodo [Edit/Open] 支持它,不确定 Komodo IDE。
于 2010-08-05T02:10:05.317 回答
1
这是 David 代码的修改版本,可以使用正确的行尾:
from xpcom import components
import re
viewSvc = components.classes["@activestate.com/koViewService;1"]\
.getService(components.interfaces.koIViewService)
view = viewSvc.currentView.queryInterface(components.interfaces.koIScintillaView)
sm = view.scimoz
sm.currentPos # current position in the editor
sm.text # editor text
sm.selText # the selected text
output = u"\n"
setterTemplate = """
function set%s($value){
$this->%s = $value;
}
"""
getterTemplate = """
/**
*@return string
*/
function get%s(){
return $this->%s;
}
"""
propertyTemplate = """
%s
%s
"""
prefixSize = len(u"private $")
def formalName(rawName):
return u"%s" % "".join([part.title() for part in rawName.split("_")])
eol = u"\n" #UNIX \n (default) sm.eOLMode == 2
if sm.eOLMode == 0: #DOS/Windows \r\n
eol = u"\r\n"
elif sm.eOLMode == 1: #Mac Classic \r
eol = u"\r"
for line in sm.text.split(eol):
if line.strip().startswith("private $"):
#trim of the private $ and trailing semi-colon
realName = line.strip()[prefixSize:-1]
output += propertyTemplate % ( setterTemplate %(formalName(realName), realName), getterTemplate % (formalName(realName), realName))
output = output.replace("\n", eol)
sm.insertText(sm.currentPos, output)
于 2013-03-25T12:51:01.137 回答
0
Komodo IDE 和 Edit 都不支持它。
使用 PHP,您想从什么生成代码?
- 埃里克
于 2010-08-12T17:25:56.430 回答
0
这绝不是完美的或完成的,但这是我编写的一个与 Komodo 6 兼容的 python 宏脚本,用于为整个 PHP 类自动生成 setter/getter。
from xpcom import components
import re
viewSvc = components.classes["@activestate.com/koViewService;1"]\
.getService(components.interfaces.koIViewService)
view = viewSvc.currentView.queryInterface(components.interfaces.koIScintillaView)
sm = view.scimoz
sm.currentPos # current position in the editor
sm.text # editor text
sm.selText # the selected text
#sm.text = "Hello World!"
output = u"\n"
setterTemplate = """
function set%s($value){
$this->%s = $value;
}
"""
getterTemplate = """
/**
*@return string
*/
function get%s(){
return $this->%s;
}
"""
propertyTemplate = """
%s
%s
"""
prefixSize = len(u"private $")
def formalName(rawName):
return u"%s" % "".join([part.title() for part in rawName.split("_")])
#todo find a better way to split lines, what if its Mac or Windows format?
for line in sm.text.split("\n"):
if line.strip().startswith("private $"):
#trim of the private $ and trailing semi-colon
realName = line.strip()[prefixSize:-1]
output += propertyTemplate % ( setterTemplate %(formalName(realName), realName), getterTemplate % (formalName(realName), realName))
sm.insertText(sm.currentPos, output)
给出一个像 foo.php 这样的文件,其中只有 Class Bar
class Bar {
private $id;
private $name_first;
}
它会注入
function setId($value){
$this->id = $value;
}
/**
*@return string
*/
function getId(){
return $this->id;
}
function setNameFirst($value){
$this->name_first = $value;
}
/**
*@return string
*/
function getNameFirst(){
return $this->name_first;
}
这对我的使用来说已经足够了(我可以很快地重新标记所有内容),但如果我对脚本进行了显着改进,我会更新这个答案。
于 2011-06-01T22:12:05.810 回答