gpt4 book ai didi

PHP Komodo getter/setter 自动生成

转载 作者:可可西里 更新时间:2023-10-31 22:49:06 27 4
gpt4 key购买 nike

Komodo 是否像 NetBeans 或 Eclipse 一样支持 getter/setter 自动生成?如果是这样,我该如何使用它?我好像找不到。

最佳答案

这是一个修改/改进的版本,具有更易读的代码。还将从属性声明中删除默认值,如 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)

关于PHP Komodo getter/setter 自动生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3410149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com