Sublimetext3 是否可以将一个代码段的输出包含在另一个代码段中?
生命是短暂的,所以我正在创建一组片段。其中一个名为Sublimetext3 是否可以将一个代码段的输出包含在另一个代码段中?,sublimetext3,code-snippets,Sublimetext3,Code Snippets,生命是短暂的,所以我正在创建一组片段。其中一个名为Muletilla.sublime snippet,如下所示 <snippet> <content><![CDATA[ if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); ${1:} ]]></content> <!-- Optional: Set a tabTrigger to defi
Muletilla.sublime snippet
,如下所示
<snippet>
<content><![CDATA[
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
${1:}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>muletilla</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.php</scope>
<description>Create the mulelitlla!</description>
</snippet>
另一个名为CreateCustomController.sublime snippet
,如下所示:
<snippet>
<content><![CDATA[
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class Custom${1:Module}Controller extends SugarController
{
public function action_${2:name}()
{
${3:// add logic here}
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>controller</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.php</scope>
<description>create a custom controller</description>
</snippet>
控制器
source.php
创建自定义控制器
正如您所见,我需要再次使用muletilla
输出,在其他snippete中也需要这样做。是否可以将一个代码段的输出包含在另一个代码段中
在本例中,在我创建的任何其他代码段中使用muletilla
的输出
提前感谢这类事情不可能直接实现,因为我猜您需要它,您可以在第二个代码段中添加一些内容,告诉它应该拉入第一个代码段的内容,并将其与其他内容一起内联插入 有人可能会认为,当一个代码片段即将扩展并动态重写时,插件可能会捕获。然而,在一般情况下,当一个代码段即将插入时,插件没有真正的方法来捕获,因为缓冲区完成、
sublime snippet
和sublime completions
文件都混合在可以完成的项目列表中
一个可能的解决方案是一个插件,该插件从其他地方获取代码片段数据,并将其作为定制的完成项提供,而不是直接使用代码片段。在这种情况下,插件本身将负责提供插入的内容,它可以根据需要重写这些内容
另一个解决方案是一个包,它允许您以中间格式指定代码段,以便它可以为您生成最终的代码段,在这种情况下,它将减轻在子代码段更改时手动更新所有代码段的负担
也就是说,不幸的是,我不知道有任何现有的包可以做这两件事
对于问题中概述的情况,如果将第一个代码段的结尾指定为${0}
而不是${1}
,Sublime将展开它并将光标放在{$0}
点,而不提供任何展开字段。这将允许您触发它,然后立即触发第二个,而无需先按tab键
然而,这只是保存一个按键
虽然这不是一个通用的解决方案,但这里有另一种方法可以使用core Sublime实现您想要的功能 首先,您需要在
User
包中创建一个tmPreferences
文件,该文件如下所示:
<snippet>
<content><![CDATA[
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class Custom${1:Module}Controller extends SugarController
{
public function action_${2:name}()
{
${3:// add logic here}
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>controller</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.php</scope>
<description>create a custom controller</description>
</snippet>
范围
source.php
设置
外壳变量
名称
穆莱蒂拉
价值
uuid
75888874-b1ed-460d-ab0e-9d1741d50188
文件名无关紧要(只有扩展名很重要),您需要将范围更改为适当的范围
在shellVariables
中定义自定义变量及其内容。本例创建了一个名为muletilla
的变量,其内容是第一个代码段(公共文本)的第一行
请注意,CDATA
的内容是按原样插入的;这意味着任何前导或尾随的换行符都将进入,并且不能在值中使用任何变量替换,因为它们将按原样插入,而不会进一步展开
这样,您的第一个代码片段就变成:
<snippet>
<content><![CDATA[
${muletilla}
class Custom${1:Module}Controller extends SugarController
{
public function action_${2:name}()
{
${0:// add logic here}
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>controller</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.php</scope>
<description>create a custom controller</description>
</snippet>
穆莱蒂拉
source.php
创造穆莱特拉!
第二个片段变成:
<snippet>
<content><![CDATA[
${muletilla}
class Custom${1:Module}Controller extends SugarController
{
public function action_${2:name}()
{
${0:// add logic here}
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>controller</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.php</scope>
<description>create a custom controller</description>
</snippet>
控制器
source.php
创建自定义控制器
这种方法适用于这样的情况,但在您希望公共文本包含某种占位符的情况下就不适用了。如果您想更改变量,那么您必须记住您在哪里定义了变量,而不是简单地引用另一个代码段,这可能会带来更大的麻烦
谢谢你的回答。非常好的回应,谢谢你的时间