Eclipse caret jumps to constructor while typing - eclipse

While typing in Eclipse (Java) I often have the problem that when I begin to type accessors, the caret jumps down to the beginning of the constructor definition. So in a document like this:
private int mSomeInt;
public
in|public MyClass(){
}
I would like to manually type out the accessor (getter/setter) for mSomeInt, but when I press space after 'public' above, the caret jumps to the beginning of 'public MyClass'.
I often type complete lines to look up and find my methods jumbled with the constructor (like above).
Any help would be appreciated.
Note - this isn't only with accessors but rather any access modifiers that I define before the constructor or another method.
Edit
After unsuccessfully trying Deco's solution below, I've managed to narrow it down a little further.
The problem only happens if I have all the blocks in the file in a collapsed state (ctrl+shift+numPadDivide). I can see the problem is now that the new access modifier I type is then (quickly) collapsed into the below method. i.e. Eclipse is actually taking the first accessor modifier and collapsing everything from there, even though my intention is actually to write a new method.
The only solution I've been able to find is to only edit the source with all the 'fold' elements unfolded.

Under Window -> Preferences -> <Language> (e.g. Java) -> Editor there is a Content Assist menu item where you can configure auto completion and caret placement as well as auto-activation of it and the delay it uses.
Edit:
After your update to the original question I was able to successfully replicate this in Eclipse Indigo. When you have all of the code blocks collapsed it looks like Eclipse assumes that the code you are writing needs to be in that block (rather than as a variable declaration). I'm not sure if this is expected behaviour or not - but the only way around it I've found is to edit the code with the main block open, and then close it after the fact - or turn folding off altogether.
From what I can tell there are various folding plugins/addons that you can get for Eclipse which override the default behaviour and might function better? A quick Google search will be able to get you a list of them quickly.
I'd probably also suggest posting this as an issue on the Eclipse support site for their official answer.

Unfortunately this issue still exists for me in the latest Elcipse version (Kepler).
As the issue only occurs when the document is 'folded', the work around this is to either disable folding in the editor - or disable folding on 'Members' from the :
Preferences -> Java -> Editor -> Folding

Related

Method reference shortcut transformation in Eclipse Luna

In Spring Tool Suite
(Version: 3.7.0.RELEASE
Build Id: 201506290649
Platform: Eclipse Luna SR2 (4.4.2))
Is there a shortcut to go from something like:
Function<String, Integer> func = str -> Integer.valueOf(str);
to something like this:
Function<String, Integer> func = Integer::valueOf;
(and backwards) via a keyboard shortcut?
I am aware of Ctrl+1 key combo, but it doesn't seem to offer the method reference conversion, hence this inquiry:
First, you need Eclipse 4.5 or greater, because that's when this particular assist was first published.
Next you should avoid the "unused" warning on the variable, because otherwise content assist will jump to the nearest warning / error rather than proposing a change unrelated to any problem in the current code (this is not strictly necessary, but makes the next step easier).
Finally, you need to position the cursor on the ->, because that's the only piece of syntax definitely identifying the lambda.
See also the first item in the 4.5 "New-&-Noteworthy", Section on JDT.
Select the lambda expression "str -> Integer.valueOf(str)" and press Ctrl-1. The first entry in the quick-fix list should be something like "convert to method reference".

How to disable Eclipse's "auto-folding during typing"?

Eclipse (any version AFAIK) has some weird behavior related to folding in Java code. Suppose I’m editing this class:
class A {
String field;
#Nonnull
Object method(){
// whatever
}
}
If folding is enabled and I tell it to collapse everything (it’s Control-NumSlash in mine, but that might be customized), the method is correctly folded, i.e. it shows only Object method()... for the method. All good ’till now.
The part that bothers me is that if I move the cursor right after field;, press Enter, and type something like “public”, and then stop for a second, Eclipse automatically folds that word into the method below.
That might seem reasonable (presumably it assumes I wanted to add that qualifier to the method); but in practice what I’m actually doing is trying to add a new method, and paused for a moment to think about its return type or maybe its name. (If I wanted to modify the method I’d unfold it first, since it might already have that qualifier, folded.)
I hate this “feature” with passion, but I can’t for the life of me find out how to disable it, nor even which of the damned mess of plugins (that Eclipse keeps insisting I should not be allowed to remove) is responsible for it so I can file a bug report.
So, does anyone know (1) where does that behavior come from, and hopefully (2) how can I get rid of it but keep manual folding? Thanks!
(For the record, I’m using Kepler SR1, but this behavior goes back a really long time, at least five years or so.)
I don't believe there's any way to prevent it from doing that unless you just make a habit of putting a semicolon ; or closing curly brace } after public which prevents the Object method(){.. from 'folding' it up. I believe it's written to fold everything up until the closest semicolon which is why #Nonnull is also included.
The only options for folding I can find are located in Window > Preferences > Java > Editor > folding
I would consider this to be a bug, or just a feature that had unintended side effects.
Funny enough, if you put almost any symbol or misspell public it wont fold it.

Eclipse auto completion with method parameter type name

Currently I get this result with Eclipse auto completion.
System.console().printf(format, args)
Of course, Eclipse shows the parameter types as popup, but I want the types to be displayed in front of parameter names too. Like this.
System.console().printf(String format, Object ... args) // `String`, `Object` inserted automatically.
Can I configure Eclipse to show the parameter types too?
Of course not. Eclipse auto completion only suggests valid syntax, while your demand is not valid java code.
An alternative for you might be to open the "Declaration" view or the "JavaDoc" view. Both will always show the declaration/JavaDoc of the currently selected element while you are typing. Therefore you could see the method declaration/javadoc when writing that method call.
Update
Whoever came across this old question. You can use eclipse code mining now:
parameter name hint for Eclipse

Is there a way to get eclipse code completion to filter options on text typed anywhere in a word rather than just the start?

I have just started using eclipse Indigo for Scala development. Is there a setting anywhere in eclipse to change the filter behavior to "anywhere" when I type X. and invoke completion?
If I have an object foo with a field foo.name and methods foo.fullName, foo.capAllNames I would like all three to remain in the selection list when I type foo.name.
Currently the list is filtering on the start of the word so only the foo.name property would remain in the example given. I would like the filter to be based on the text being present anywhere in the word, can this be done in eclipse?
Thanks
John
Looks like Code Recommenders as suggested by moeTi will do the job but I might have to wait for a while until the latest bug fixes filter through to a release that will play nicely with the Scala plug in (it/eclipse threw exceptions when I installed it):
https://bugs.eclipse.org/bugs/show_bug.cgi?id=383395

Auto fix common typo in eclipse

Lets say for example I write many times priavte instead private.
Is there a way to let Eclipse automatically fix my common typo?
Something like construct a map of my common typo to its desire fix,
and then just let Eclipse fix it without asking me about that.
Are there any other IDE\editors that have such support?
There is no builtin support for automatically changing strings. The closest to your request are the templates of the Java editor, but even those must explicitly be activated using CtrlSpace.
To get around your problem, I suggest simply not to write that much yourself. If you want to declare a private field, type just "pr" and hit CtrlSpace to invoke code completion. Eclipse can do code completion quite well, often even without any trigger characters (try it with an empty class file).