eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Faulty/Undesired CodeCompletion behaviour

Open
#2,620 12 comments 0 reactions 1 assignee Claimed by @stephan-herrmann View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

Hello. This situation has been driving me ([and others](https://bugs.eclipse.org/bugs/show_bug.cgi?id=575025)) crazy every minute(!) I am coding with eclipse since forever, so I finally took some time out to at least pinpoint the offending plugin and commit. Hope this helps!

This behaviour started back with eclipe 2021-06 while 2021-03 still behaved "well".

Code completion is behaving in an undesired manner in the following situation:
```Java
public class Demo {
public static void main(String[] args) {
System.out.println("hello");
if (true) {
System.out.print|cursor|ln("world");
}
}
}
```
Triggering code completion (ctrl+space) while the cursor is located as indicated gives a list of proposals, so far so good.
Now, after selecting the first proposal (in my case the "print" method), the code gets transformed as follows:
```Java
public class Demo {
public static void main(String[] args) {
System.out.println("hello");
if (true) {
System.out.print(false);
}
}
}
```
The expected behaviour would be that the existing parameter "world" remains in place and only the "ln" part of "println" gets removed, i.e. the method call is updated from "println" to "print", like so:
```Java
public class Demo {

public static void main(String[] args) {
System.out.println("hello");
if (true) {
System.out.print("world");
}
}
}
```
Note that I have enclosed the second println statement in an if block (the true doesnt matter). An empty block does not show the bad behaviour. Here is an (incomplete) overview:

```Java
public class Test {

public static void main (String[] args) {
// good behaviour
System.out.println("hello");

{ // good behaviour
System.out.println("world");
}

if (true) { // bad behaviour
System.out.println("world");
}

while (true) { // bad behaviour
System.out.println("world");
break;
}

for (int i = 0; i < 10; i++) { // bad behaviour
System.out.println("world");
}

Runnable a = () -> { // bad behaviour
System.out.println("world");
};

Nested n = new Nested();
// good behaviour for deeper1, bad behaviour for deeper2
n.deeper1(1, 2).deeper2(3, 4);
}

public static class Nested {
public Nested deeper;

public Nested deeper1 (int a, int b) {
return deeper;
}

public Nested deeper2 (int a, int b) {
return deeper;
}
}

}
```

My content assist settings:
![settings](https://github.com/eclipse-jdt/eclipse.jdt.core/assets/24558048/5657c5c6-7ae1-416f-b4fc-97f4659c92ad)

I was able to pinpoint the issue to the jdt.core, specifically the following commit: e3517ddc41f3c9536a29ef9be4e7dd3104993ab2

To arrive at this commit I used a git bisect:
- endpoints: 18d780b and 1e898ec
- last good: 1e8d2c330d6c9f355e1893333382d51809c457c3
- first bad: e3517ddc41f3c9536a29ef9be4e7dd3104993ab2

Due to the complexity of that specific commit, me looking into the eclipse source code for the first time, and my lack of time, I was hoping someone more experienced could pick it up from here.

There are some other completion issues I have been encountering which are possibly related to this, so looking into it is hopefully time well spent.

Additionally possibly related:

#1770
#1016

Thank you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.