Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing code snippets in eclipse

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(); 

and then "bp would paste

  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?

like image 443
Paul Tomblin Avatar asked Oct 26 '08 13:10

Paul Tomblin


People also ask

How do I add a code to snippets?

With a code file open in the editor, choose Snippets > Insert Snippet from the right-click menu, then My Code Snippets. You should see a snippet named Square Root. Double-click it. The snippet code is inserted in the code file.

How do I autocomplete in eclipse?

Step 1: Open your Eclipse or Spring Tool Suite, then go to the Window > Preferences as shown in the below image. Step 2: In the next screen go to the Java > Editor > Content Assist > Auto activation triggers for Java as shown in the below image.

What are code snippets in Java?

A Snippet represents a snippet of Java source code as passed to JShell. eval . It is associated only with the JShell instance that created it. An instance of Snippet (including its subclasses) is immutable: an access to any of its methods will always return the same result.


2 Answers

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 adds (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.

like image 126
VonC Avatar answered Oct 13 '22 09:10

VonC


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

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.

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

like image 37
David Easley Avatar answered Oct 13 '22 09:10

David Easley