PHP Komodo getter/setter 自动生成

发布于 2024-09-13 06:42:30 字数 81 浏览 2 评论 0原文

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

Does Komodo support getter/setter auto generation a la NetBeans or Eclipse? If so how do I use it? I can't seem to find it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柠栀 2024-09-20 06:42:30

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

This is a modified/improved version with a more readable code. Also will remove the default values from property declaration, like in 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)
夏夜暖风 2024-09-20 06:42:30

我不认为 Komodo [编辑/打开] 支持它,不确定 Komodo IDE。

I don't think Komodo [Edit/Open] supports it, not sure about Komodo IDE.

时光暖心i 2024-09-20 06:42:30

这是 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)

This is a modified version of David's code and works with the correct line endings:

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)
救星 2024-09-20 06:42:30

Komodo IDE 和 Edit 都不支持它。

对于 PHP,您希望从什么生成代码?

  • 埃里克

Neither Komodo IDE nor Edit support it.

With PHP, what would you want to generate the code from?

  • Eric
策马西风 2024-09-20 06:42:30

这绝不是完美或完成的,但这是我编写的一个与 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;
    }

这对于我的使用来说已经足够好了(我可以很快地重新创建所有内容),但如果我对脚本有显着改进,我会更新这个答案。

This is in no way perfect or finished, but here's a Komodo 6 compatible python macro script I wrote to auto-generate setters/getters for an entire PHP class.

    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)

give a file like foo.php with Class Bar as the only thing present

class Bar {
   private $id;
   private $name_first;
}

It would inject

    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;
    }

That's good enough for my uses ( I can retab everything pretty quickly ) but I'll update this answer if I improve significantly on the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文