在eclipse中存储代码段 [英] Storing code snippets in eclipse

查看:140
本文介绍了在eclipse中存储代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在使用vi和gvim 20年后半转换为Eclipse。关于gvim的其中一件事是,我可以将一大堆不同的代码段剪切成命名的缓冲区,并在做一些类似重复常用习语的事情时随意粘贴。例如我会这样ap 将粘贴

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 "ap would paste

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

然后bp 将粘贴

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

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

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?

推荐答案

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

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)

Newtopian 在评论中添加

事实上,模板通过添加变量和tabstops变得更加强大,所以上面的例子会变成 dbHandle ctrl + space 。它将复制两部分的片段,并将光标放在中间。

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.

这篇关于在eclipse中存储代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆