Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

Java, the OLPC, and community responsibility

The "One Laptop Per Child" project has a great device ready to ship, but there's no Java on there. Let's think about working together to put Java on OLPC!
Replies: 5 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Configure Tab Completion in Eclipse (almost)

At 2:20 PM on Aug 16, 2007, Daniel Spiewak Javalobby Junkies wrote:

Numerous text editors these days are capable of so called "tab completion". That is, where you type three characters, hit the Tab key and wham, 200 lines appear. Obviously this can be a considerable time saver at times.

Now, Eclipse has long had this capability in the form of templates. You've probably seen them if you've ever used the Ctrl+Space code completion. For example, in Eclipse JDT you can type "sysout" followed by Ctrl+Space, and the sysout will be replaced with System.out.println(|); This is grand and all very useful, but Ctrl+Space has its issues.

Ctrl+Space is a nice shortcut, but it takes too long to press. Think about it, you have to move your left pinky down to the Ctrl button, which depending on your keyboard may or may not be a challenging affair. At the same time, your right thumb (or left thumb I suppose) has to find the space key and press it. Technically speaking the space key takes a bit more force to press, since it has two springs (on most keyboards). So all together, this motion might take a fast typist maybe 200-300 ms. That's pretty quick, but when you're talking about a shortcut you press maybe once per line (if you're using templating), it becomes a bit of an issue.

A much faster shortcut (as seen by text editors like jEdit and TextMate) is the Tab key. Tab can be pressed with one hand (that's the usual route anyway), is very quick to find, and only has one spring fighting against you. In fact, tabbing can be a 100-200 ms affair, usually less. Obviously, if we can assign this as a template-complete shortcut, it would cut quite a bit of time off of our development. On top of that (and this is important here), it would separate the templates from the rest of the code completions, meaning that Eclipse could auto-fill the template rather than forcing us to page through a popup.

The major problem with this is that Eclipse doesn't support key-bind passing. Meaning, if a key is mapped to a command, even if the command is unable to execute (such as not matching a template), the key event will still be eaten by the UI. This means that if we try to map the Tab key to the Eclipse template auto-complete, we lose the ability to input the Tab character into the editor. Until this sort of "key passing" is supported by the platform, true Tab completion will be impossible in Eclipse.

The most promising "live with it" solution to this problem is to bind the template insertion to a different key combo. Since much of the workflow increase which comes from rebinding the template-insert command comes from its separation from the generic content assist, we can still expect to see some fraction of the same improvements to our workflow. Having dug through the key bindings and experimented with different combos, I recommend using the Ctrl+Enter combination. It's not bound to another command, and it's fairly easy to hit. Not as easy as Tab mind you, but it'll do.

So the first (and actually only) step to this tip is to open up Preferences, and then limit down the panes available by filtering on "Keys". In fact, for most plugin sets there should be only one preference pane remaining. Select this, and then check the "Include unbound commands" checkbox. (note: these instructions are aimed at Eclipse 3.3 users. While they will work with Eclipse 3.2 and below, they will have to be adapted).

Now select the "type filter text" field, and enter the word "template". Expand the "Command" column until you can read all of the text in the first two hits. You should see "Content Assist (type: Template Proposals (Mylyn))" and "Content Assist (type: Template Proposals)" (obviously you won't see the first if you don't have Mylyn installed). Select the second hit (the one without "Mylyn"), and then select the "Binding" field below.

Now, here's where we choose the shortcut we want to assign to the command. You can use the binding I selected (Ctrl+Enter, or Ctrl+Return on Mac), or you can select a combination of your own. To set the combo, select the "Binding" field, and then type the combination. In my case, I hold down the Ctrl key and press Enter.

With the keyboard shortcut set, we can now click "OK" to apply the setting change, and we're done!

You can try this out by opening an editor with templates enabled (the Java editor is a good bet). Once open, you can type a template and punch in the combination you've chosen (Ctrl+Enter in my case). If it's a valid template, it should auto-fill replacing the template keyword. Templates can be configured from the relevant preference panes for each editor (hint: find them by filtering preferences by "template").

Templates can be a little hard to get used to if you're accustomed to typing everything out by hand. But trust me, once you're comfortable with them, you'll never look back.

Update: I apologize if the title of this article mislead anyone. The original article did in fact give instructions on getting tab completion in Eclipse. However, a mistake in my testing lead me to miss the fact that it also disabled the Tab key in the editor. I have edited the article to correct for this error and I've opened a feature request to allow for this in the future: 200292.
  Click to reply to this thread Reply
1. At 7:58 PM on Aug 16, 2007, Prashant Deva Occasional Javalobby Visitor wrote:

Re: Configure Tab Completion in Eclipse

This seems to render the tab key useless, since everytime you try to tab, it says 'no completions available' and doesnt tab.
Virtual Ant - Revolutionary new GUI for Apache Ant
  Click to reply to this thread Reply
2. At 10:42 PM on Aug 16, 2007, Daniel Spiewak Javalobby Junkies wrote:

Re: Configure Tab Completion in Eclipse

> This seems to render the tab key useless, since
> everytime you try to tab, it says 'no completions
> available' and doesnt tab.

Hmm, it seems you're right. This does of course lead to complications. Working to correct...
Daniel Spiewak
ActiveObjects: an Easier Java ORM; Fuse: Resource Injection for Java
  Click to reply to this thread Reply
3. At 3:09 AM on Aug 17, 2007, Thomas Kappler Javalobby Newcomers wrote:

Re: Configure Tab Completion in Eclipse (almost)

As a developer, you probably should change your caps lock key into an additional ctrl key. You hardly use caps lock anyway, and it will make all the ctrl-X key combos, including ctrl-space, much easier and faster.
  Click to reply to this thread Reply
4. At 4:01 PM on Aug 17, 2007, Daniel Spiewak Javalobby Junkies wrote:

Re: Configure Tab Completion in Eclipse (almost)

> As a developer, you probably should change your caps
> lock key into an additional ctrl key. You hardly use
> caps lock anyway, and it will make all the ctrl-X key
> combos, including ctrl-space, much easier and faster.

I've considered that, but it drives me nuts, turning the indicator light on, and then off again with no relation to what's actually going on.
Daniel Spiewak
ActiveObjects: an Easier Java ORM; Fuse: Resource Injection for Java
  Click to reply to this thread Reply
5. At 2:11 PM on Sep 30, 2007, Chris Torrence Javalobby Newcomers wrote:

Re: Configure Tab Completion in Eclipse (almost)

Is there any way to configure key bindings for individual code templates? That seems like it would be a quick way to insert particular templates. You would need a way to add a key binding to each Template within the Template Preference page. Should I log a feature request?

thread.rss_message