简体   繁体   English

如何让“sout”速记在 Sublime Text 3 中工作?

[英]How to get "sout" shorthand to work in Sublime Text 3?

I am trying to get the "sout" shorthand to work in Sublime Text 3 for Java.我试图让“sout”速记在 Java 的 Sublime Text 3 中工作。 In vscode and other editors typing "sout + [tab]" will fill in "System.out.println".在 vscode 和其他编辑器中键入“sout + [tab]”将填写“System.out.println”。 When I try this in Sublime Text it instead prints "southPane".当我在 Sublime Text 中尝试此操作时,它会打印“southPane”。

This is something that can be done via a snippet or a completion ;这可以通过 片段或完成来完成 both can do this but which you use depends largely on the complexity of the text you want to insert and how many you have.两者都可以做到这一点,但您使用的主要取决于您要插入的文本的复杂性以及您拥有的文本数量。

The main difference is that a snippet is a XML based format where each file contains a single completion whereas a sublime-completions file is JSON formatted file that can contain many completions at once.主要区别在于, snippet是基于 XML 的格式,其中每个文件都包含一个完成,而sublime-completions文件是 JSON 格式的文件,可以一次包含多个完成。 Additionally, all snippets are automatically added to the command palette and made available only in files to which they apply.此外,所有片段都会自动添加到命令选项板中,并且仅在它们应用的文件中可用。

Thus the XML based snippet is good for larger stretches of code (eg blocks) or for any text that needs to contain characters that would be a pain to encode as JSON, whereas the JSON based completions are favored for shorter sequences of text, since you can pack more of them into a file.因此,基于 XML 的代码片段适用于较大的代码段(例如块)或任何需要包含编码为 JSON 会很痛苦的字符的文本,而基于 JSON 的补全更适合较短的文本序列,因为您可以将更多它们打包到一个文件中。

To demonstrate a snippet, use Tools > Developer > New Snippet to generate a stub, then replace the stub with this content and save it as a file in the default offered location (your User package) as a sublime-snippet file;为了演示一个片段,使用Tools > Developer > New Snippet来生成一个存根,然后用这个内容替换存根,并将它作为一个文件保存在默认提供的位置(你的User包)作为一个sublime-snippet文件; the name doesn't matter, but the extension does:名称无关紧要,但扩展名:

<snippet>
    <content><![CDATA[
System.out.println($0);
]]></content>
    <tabTrigger>sout</tabTrigger>
    <scope>source.java</scope>
</snippet>

This says that in a Java file the abbreviation sout Tab will expand out to the text System.out.println();这表示在 Java 文件中,缩写sout Tab将扩展为文本System.out.println(); with the cursor left inside the parenthesis.光标留在括号内。

Alternately, create a file with the following content and save it in your User package as a sublime-completions file (name doesn't matter, only extension, and you can use Preferences > Browse Packages to find the User package`:或者,创建一个包含以下内容的文件并将其作为sublime-completions文件保存在您的User包中(名称无关紧要,只有扩展名,您可以使用Preferences > Browse Packages来查找User包`:

{
    "scope": "source.java",

    "completions": [
        { "trigger": "sout", "contents": "System.out.println($0);" },
    ]
}

This does the same as the above example, but the file is smaller, and you can include multiple items in it, say for example by also adding:这与上面的示例相同,但文件更小,您可以在其中包含多个项目,例如还添加:

        { "trigger": "serr", "contents": "System.err.println($0);" },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM