【问题标题】:Writing a code template for Eclipse PDT with context=php_new_file_context使用 context=php_new_file_context 为 Eclipse PDT 编写代码模板
【发布时间】:2011-04-01 14:45:06
【问题描述】:

我有一个基本类,我经常扩展它。我认为在创建新文件时添加扩展模板作为选择会很好。我去了 Eclipse->Preferences->PHP->Code Style->Code Templates 并决定复制和修改“简单的 php 文件”。所以我导出了那个模板并打开了它。它看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
    <template autoinsert="true" context="php_new_file_context"
        deleted="false" description="Simple php file" enabled="true"
        id="org.eclipse.php.ui.editor.templates.php.author" name="New simple PHP file">
    &lt;?php
            ${cursor}
    ?&gt;
    </template>
</templates>

我将文件编辑为如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
    <template autoinsert="true" context="php_new_file_context"
        deleted="false" description="PHP Item subclass file" enabled="true"
        id="org.eclipse.php.ui.editor.templates.php.itemclass" name="Item subclass file">
        &lt;?php
        /**
        * ${enter description}
        * @author: My Name
        * @version: 
        **/
        class ${classname} extends Item {
            const PKEY='${pkey}'; //name of primary key variable
            const TABLE='${table}'; //name of db table

            //db table vars
            $${pkey};
            $${name};

            //history vars


            public static function who() {
                return __CLASS__;
            }

        }
        ?&gt;
    </template>
</templates>

然后我将它保存为 item.xml 并尝试导入它。导入未引发错误,但新模板未显示在列表中。我唯一能想到的是,我为 id 属性选择的字符串是有问题的。但我找不到任何关于正确创作模板的参考资料。要么我的 google-fu 失败了,要么我正在尝试做一些我不应该做的事情;我不知道。

想法?

【问题讨论】:

    标签: xml eclipse templates eclipse-pdt


    【解决方案1】:

    这里的问题是模板第三行的变量名称中有一个空格,${enter description},这是 Eclipse 不允许的,但这些变量不会被考虑用于自动填充。

    尽管如此,它不会让您导入它们,因为它是一个不再受支持的功能,如 stated in their bugtracker。您可以使用当前的模板系统,它可以让您将变量添加到自动填充中。要使用它,你必须去PHP &gt; Editor &gt; Templates。然后,您将创建一个具有所需名称的新模板,当您开始在新的 php 文件中键入它时,如果您从自动完成中选择它。

    【讨论】:

    • 感谢该错误的链接。我最终会按照你的建议去做,但我认为这是一种解决方法,而不是做事的新方法。我希望他们尽快消除以旧方式做事的可能性;它只会让事情变得混乱。
    • 谢谢。我一直在想为什么我不能添加自己的模板。
    • 希望看到此功能重新添加的用户,请在 eclipse 网站上对这个错误报告进行投票。
    猜你喜欢
    • 2016-06-17
    • 2011-01-09
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    相关资源
    最近更新 更多