在 Eclipse 中存储代码片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/237987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:30:14  来源:igfitidea点击:

Storing code snippets in eclipse

eclipsecode-snippets

提问by Paul Tomblin

I'm a recent semi-convert to Eclipse after 20 years of using vi and gvim. One of the things I miss about gvim is that I could cut a bunch of different snippets of code into named buffers, and paste them at will when doing something like repeating a common idiom. For instance I'd have it so "apwould paste

在使用 vi 和 gvim 20 年后,我最近半转换为 Eclipse。我想念 gvim 的一件事是,我可以将一堆不同的代码片段剪切到命名缓冲区中,并在执行诸如重复常见习语之类的操作时随意粘贴它们。例如我有它所以"ap会粘贴

DatabaseHandle handle = null;
try
{
  handle = DatabaseConnectionPool.newHandle();

and then "bpwould paste

然后"bp将粘贴

  handle.commit();
}
finally
{
  handle.rollback();
  DatabaseConnectionPool.returnHandle(handle);
}

And I could repeat both of them over and over in the course of a day. In an answer to another question, somebody mentioned that you could "manage code snippets" in Eclipse, but didn't mention how. So now I'm asking: how do you manage code snippets in Eclipse?

我可以在一天中一遍又一遍地重复它们。在另一个问题的回答中,有人提到您可以在 Eclipse 中“管理代码片段”,但没有提到如何。所以现在我问:你如何在 Eclipse 中管理代码片段?

回答by VonC

You might want to store those two snippets into a code template, as explained in this tutorial.

您可能希望将这两个片段存储到一个代码模板中,如本教程中所述

And do not forget about the possibility to quickly execute any kind of java code snippets in a scrapbook(not exactly what you want, but it can come in handy at times)

并且不要忘记在剪贴簿中快速执行任何类型的 Java 代码片段的可能性(不完全是你想要的,但它有时会派上用场)

Newtopianadds (in the comments)

新托邦补充(在评论中)

In fact templates become much more powerful by adding variables and tabstops within, so your example above would become dbHandle ctrl+space. It would copy snippets from both parts and place your cursor right in the middle.

事实上,通过在其中添加变量和制表符,模板变得更加强大,因此上面的示例将变为dbHandle ctrl+space. 它会从两个部分复制片段并将光标放在中间。

回答by David Easley

Eclipse also offers something very similar to the templates feature described by VonC called (would you believe) snippets. Window > Show view > Snippets.

Eclipse 还提供了一些与 VonC 描述的模板特性非常相似的东西,称为(你相信吗)片段。窗口 > 显示视图 > 片段。

To add a new snippet category: Right click in the Snippets window and click Customize... Click New > New Category. Enter a category name if necessary (e.g. "Java"). Click Apply. With your chosen category selected, click New > New Item. Enter your snippet.

添加新的代码段类别: 在 Snippets 窗口中单击鼠标右键,然后单击自定义... 单击新建 > 新建类别。如有必要,输入类别名称(例如“Java”)。单击应用。选择您选择的类别后,单击新建 > 新建项目。输入您的代码段。

To use a snippet, put the cursor where you want to insert the snippet, then double click on a snippet in the Snippets window.

要使用代码段,请将光标放在要插入代码段的位置,然后双击代码段窗口中的代码段。

回答by Naveen Bhagwati

I ran into the Snip2Codeplugin recently. It did the job, and I can collect and search snippets in a quick way.

我最近遇到了Snip2Code插件。它完成了这项工作,我可以快速收集和搜索片段。

回答by Sauvik Dolui

Well a picture worths a thousand words, what about this one?

一张图值一千字,这个呢?

enter image description here

在此处输入图片说明

回答by David Bruchmann

The question is old but the link of the answere is older ;)

问题很旧,但答案的链接较旧;)

Here is a nice tutorial: http://www.dansshorts.com/post/creating-snippets-in-eclipse

这是一个不错的教程:http: //www.dansshorts.com/post/creating-snippets-in-eclipse

回答by jamesTheProgrammer

I have used snippets in some IDEs, like Dreamweaver and Homesite, an old Coldfusion IDE. I also use a lot of snippets in MySQL Workbench - where i type a lot of SQL, very handy there.

我在一些 IDE 中使用过片段,比如 Dreamweaver 和 Homesite,一个旧的 Coldfusion IDE。我还在 MySQL Workbench 中使用了很多片段——我在那里输入了很多 SQL,在那里非常方便。

I am now using Eclipse Java EE IDE for Web Developers Version Indigo Releaseand found the snippets panel in Window|Show View|Other...|General|Snippets. I was able to manipulate it and figure out how to add the code I wanted as snippets and how to use it efficiently.

我现在使用Eclipse Java EE IDE for Web Developers Version Indigo Release并在Window|Show View|Other...|General|Snippets 中找到了代码段面板。我能够操纵它并弄清楚如何将我想要的代码添加为片段以及如何有效地使用它。

回答by Marcel

Use Eclipse Snipmatch (Part of Eclipse for Java Developers Package).

使用 Eclipse Snipmatch(Eclipse for Java Developers Package 的一部分)。

  • Works very well for Java code snippets but also works for any other language like HTML, ABABP, PHP etc.
  • You can convert any code fragment from your editor directly to a code template. Highlight the code you'd like to convert to a snippet, context menu "create snippet", complete the form and done.
  • snippets can be shared via Git repositories with your team members
  • 适用于 Java 代码片段,但也适用于任何其他语言,如 HTML、ABABP、PHP 等。
  • 您可以将编辑器中的任何代码片段直接转换为代码模板。突出显示您要转换为代码段的代码,上下文菜单“创建代码段”,完成表单并完成。
  • 片段可以通过 Git 存储库与您的团队成员共享

Manual: https://www.eclipse.org/recommenders/manual/#snipmatch

手册:https: //www.eclipse.org/recommenders/manual/#snipmatch

Installation: https://marketplace.eclipse.org/content/snipmatch

安装:https: //marketplace.eclipse.org/content/snipmatch