0
votes

I'm using MulgaSoft's fantastic Emacs+ Eclipse Plug-in to make Eclipse usable for my emacs'd fingers.

However, I am unable to setup a working Alt+q binding for 'Format Element'. When writing a comment, for example in Java code, I'd like to be able to hit Alt+q from time-to-time to re-flow the current paragraph of text. (Mostly I just want the comment to wrap at ~80 columns.)

So, I went into Preferences -> General -> Keys and bound Format Element to Alt+Q "when" Editing in Structured Text Editors (category Source).

If I type Alt+x format-element with the cursor in a long Java comment the correct thing happens (the text reflows). But hitting Alt+q does not do anything.

If I ask Eclipse what Alt+q is bound to (by typing Ctrl+h k Alt+q), I get:

Alt+Q runs format-element
    Id -org.eclipse.wst.jsdt.ui.edit.text.java.quick.format
 Key Bindings
    ALT+Q [org.eclipse.ui.contexts.dialogAndWindow]
 Description
    Format enclosing text element

As far as I can tell (by sorting on 'Binding') in the Key preferences nothing else is bound to Alt+Q. (Well, I tried binding it in some additional "When" contexts, but that didn't help ...)

screenshot of Alt+Q keybindings

I'm not sure if this is a problem with the Emacs+ plugin or with Eclipse (since "Format Element" is an eclipse primitive?). I've restarted Eclipse more than once with these bindings defined.

Update

With @mfeber's suggestions I've tweaked the key bindings, but am still not having success. I've reduced to just one binding with a "when" of just Editing Text.

Now when I run Ctrl+h k Alt+q I see:

Alt+Q runs format-element
    Id -org.eclipse.wst.jsdt.ui.edit.text.java.quick.format
 Key Bindings
    ALT+Q [org.eclipse.ui.textEditorScope]
 Description
    Format enclosing text element

The difference is the "[org.eclipse.ui.textEditorScope]" in the Key Bindings section. The new value does better match what other key bindings like "Ctrl+O" show.

Oddly, if I run Ctrl+h w format-element I get:

format-element is unbound

But running Ctrl+h w for other commands like forward-character or insert-line-above-current-line does print the correct list of key bindings for that function. So there is something a bit wacky still.

One other thing I noticed is that there is a 'Format Element' entry in the key bindings that has a blank 'When' and 'Binding' (the above screenshot was sorted on 'Binding' so it didn't show this, but I'm pretty sure it was there before):

screenshot #2 of Alt+Q keybindings

Are there any tools or logs for debugging key bindings in Eclipse that I can turn on?

Update #2

To figure out which underlying id is being bound (as @mfeber's update suggested), I've tried a couple more things.

I've setup multiple bindings of Alt-Q to format-element in different contexts:

screenshot #3 of Alt+Q keybindings

(I've tried with and without the 'Editing Text' binding --- I'm not sure if 'Editing Text' might apply/conflict when editing .java or .py files, or if its purely for .txt files... Anyway I can't see much of a difference....)

The only interesting information I've been able to gather is that the Ctrl+h k Alt+q shows very slightly different results when run in a .java editor and when run in a .js editor:

Here is what I get from Ctrl+h k Alt+q in a Java source editor:

Alt+Q runs format-element
    Id -org.eclipse.wst.jsdt.ui.edit.text.java.quick.format
 Key Bindings
    ALT+Q [org.eclipse.jdt.ui.javaEditorScope]
 Description
    Format enclosing text element

And here is what I get from Ctrl+h k Alt+q in a Javascript source editor:

Alt+Q runs format-element
    Id -org.eclipse.jdt.ui.edit.text.java.quick.format
 Key Bindings
    ALT+Q [org.eclipse.wst.jsdt.ui.javaEditorScope]
 Description
    Format enclosing text element

The difference is the wst.jsdt vs jdt in the package paths. I don't really understand this, but I think it means I'm running the java formatter when in a javascript window, and the javascript formatter in a java window?

Oh ... that's it! If I switch the 'When' for each of those bindings, now Alt-Q works! So, despite all the key bindings looking identical in the UI, there are several different functions being bound underneath....

1

1 Answers

1
votes

First: you should set the 'When' element to 'Editing Text' ('Structured Text' is the province of things like the XML editors which provide different editing views for their content). You should see something like:

Alt+Q runs format-element Id -org.eclipse.jdt.ui.edit.text.java.quick.format Key Bindings ALT+Q [org.eclipse.ui.textEditorScope] Description Format enclosing text element

Second: you can find out the bindings for a specific key by calling describe-key (C-h k). If there is more than one binding for a given key sequence they are all listed, and the ones in bold are the ones that will be enabled in the corresponding 'When' context, assuming that you are not running into...

Three: there are key bindings in Eclipse that are not exposed in the bindings preference (these are typically bindings siphoned off by the OS, so they are different in different installs). I've never found an easy way to disable these in Eclipse.

Finally: glad to hear you find the Emacs+ plugin useful...

Hope this helps, Mark

Further info... My bad... format-element is one of those eclipse commands that has the same 'external' name, but multiple ids. The one you have bound is, I believe, appropriate for javascript, but not other contexts. You can suss out which one you need for java by binding each one in turn and checking the id in C-h k. For java it should be: Id -org.eclipse.jdt.ui.edit.text.java.quick.format. Then set the binding 'When' for 'Editing Java Source' and it should work in that context. You can set any others appropriately as desired.