Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faulty/Undesired CodeCompletion behaviour #2620 #3350

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

angelickite
Copy link

What it does

Add cursor awareness to various auto-completion situations.

Completions for CompletionOnMessageSendName as well as for QualifiedNameReference did not take the cursor position into consideration, leading to various downstream issues like failing to complete at all, not properly overwriting a source range or overwriting too eagerly.

fixes #2620

How to test

I've included some tests. A thorough description can be found in #2620.

Author checklist

@@ -3562,7 +3567,41 @@ private void completionOnQualifiedNameReference(ASTNode astNode, ASTNode enclosi
private void internalCompletionOnQualifiedReference(ASTNode ref, ASTNode enclosingNode, Binding qualifiedBinding, Scope scope,
boolean insideTypeAnnotation, boolean isInsideAnnotationAttribute, InvocationSite site, char[][] tokens, long[] sourcePositions)
{
long completionPosition = sourcePositions[sourcePositions.length - 1];
// we take the cursor location into consideration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not qualified to make a in-depth review, but i can help with some formals. it would help here if you start the code example with "Example:" otherwise it looks like out commented code

// in the above example, bestPosition will match against the cursor after 'ch', i.e. sourcePositions[1].
// (sourcePositions[0] points to 'node', sourcePositions[4] to 'parent')
long bestPosition;
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why start a codeblock here?

long bestPosition;
{
bestPosition = -1L;
for (int i = 0; i < sourcePositions.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a for-each loop here? would be easier to read.

int end = (int) (p);
{ // in specific cases like "node.|.child" the start might be larger than the end (see GH-2620)
if (start > end) {
int tmp = start;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this code should swap start and end, you could add it as comment.

}
}
if (bestPosition == -1L) {
// fallback to the previous behaviour, just in case.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In future it will not be clear what "previous" refers to. Please instead describe what the previous behavior was.

this(selector, start, end, setup -> {/* use defaults */});
}

public CompletionOnMessageSendName(char[] selector, int start, int end, Consumer<CompletionOnMessageSendName> setup) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using a Consumer for setup it would be easier to understand and maintain if you directly pass the initial values to the constructor. probably "cursorIsToTheLeftOfTheLParen" misses a "t" at the end and could be final.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to unblock the change I'm applying some of these suggestions, but here's one correction: LParen is correct compiler lingo to refer to '(' (short for left parenthesis), nothing to do with parents :)

angelickite and others added 2 commits January 18, 2025 14:38
Completions for CompletionOnMessageSendName as well as for
QualifiedNameReference did not take the cursor position into
consideration, leading to various downstream issues like failing to
complete at all, not properly overwriting a source range or overwriting
too eagerly.

fixes eclipse-jdt#2620
Changes during review:
+ revert overhead that was used just to set one addition boolean field
+ code simplification also in CompletionEngine
Tests:
+ make expected relevance explicit in terms of individual constants
+ two more tests to observe incremental differences
  1a: when cursor is immediately after '('
  1b: immediately before '(' when the selected method has parameters

fixes eclipse-jdt#2620
@stephan-herrmann
Copy link
Contributor

Trying to unblock this issue I ...

  • rebased the existing commit on HEAD (without any changes)
  • added a commit of code simplifications (also considering @jukzi's comments) and additional tests.

One more observations, and one remaining task:

(1) We should call out that the change in CompletionTest.testCamelCaseLocalVariable1() fixed a test bug, which would affect proposals of Object.clone() and Object.finalize() in unrelated tests, if run after this one.

(2) I'm seeing a regression when trying the following in the IDE:

public class GH2620_1 {
	public static void main (String[] args) {
		int j = 1;
		Test local;
		if (true) { // the if-statement is crucial for this test.
			/*x*/local.ref.test|();
		}
	}
	public static class Test {
		public Test ref;
		public static void test(int i) {}
	}
}

Cursor location is indicated by |.

  • Old behavior: argument j is proposed.
  • New behavior: argument j is proposed only when completion operates in 'insert' mode. When selecting 'overwrite' no argument is proposed.

@angelickite can you find out, if this is a bug in JDT/UI, or can/should be fixed here?

@stephan-herrmann stephan-herrmann added this to the 4.35 M3 milestone Jan 18, 2025
@angelickite
Copy link
Author

@stephan-herrmann Are we on the same page that this regression is foremost a side effect of the new approach? What I mean by that:

  • The new approach is generally well behaved
  • Specifically when the completion is operating on any part names (i.e.. local, ref and test in the above example), then the completion should only affect these parts names.
  • If the method name is the target, only the method name should be operated on and the argument list not be touched, i.e existing arguments should remain as they are. (e.g. System.out.print|("Hello") -> System.out.println("Hello"))

So the regression then manifests itself in the case where we have an empty argument list and can potentially safely modify the argument list too as a bonus feature as there is "nothing to break"?

If this is the situation you refer to as the regression, looking back I did see this case appear but never gave it any attention or thought. Reason being that the completion behaviour around arguments always felt janky anyways as well as it not negatively impacting my workflow.

If this feature is truly highly important, I will take a look where it originates and whether there is a sensible solution.

Lastly, I personally would prefer this sidegrade every time over having the old behaviour back.

@stephan-herrmann
Copy link
Contributor

  • The new approach is generally well behaved

I generally agree.

So the regression then manifests itself in the case where we have an empty argument list and can potentially safely modify the argument list too as a bonus feature as there is "nothing to break"?

I didn't investigate the exact conditions under which arguments are proposed, perhaps my scenario is the only one, perhaps the feature applies much more broadly. I didn't test.

If this is the situation you refer to as the regression, looking back I did see this case appear but never gave it any attention or thought. Reason being that the completion behaviour around arguments always felt janky anyways as well as it not negatively impacting my workflow.

I agree that the behaviour around arguments has room for improvement. But that even more motivates me to be careful that we don't break behaviour that exists at present and may be very important to some users. A somewhat messy situation is no excuse for piling up more inconsistencies - and as I said the current state in this PR is inconsistent between insertion / overwrite modes.

For me personally, support for arguments in calls to methods with long parameter lists is actually very relevant.

If this feature is truly highly important, I will take a look where it originates and whether there is a sensible solution.

I'd appreciate. It would already help if we only find out if the problem is in jdt.core or jdt.ui.

Lastly, I personally would prefer this sidegrade every time over having the old behaviour back.

I wouldn't wont to judge one user's needs against those of another :)

@angelickite
Copy link
Author

angelickite commented Jan 19, 2025

The regression is here: https://github.com/angelickite/eclipse.jdt.core/blob/aa0c66d368586cdc30c18511f260519c80c59ff7/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java#L3263-L3270

So it stems directly from the new behaviour where "cursor to left of lparen" is given special consideration.

Something like this just might do the trick (limited testing):

// we take the cursor location into consideration
if (messageSend.cursorIsToTheLeftOfTheLParen && messageSend.arguments != null) {
  setSourceAndTokenRange(messageSend.sourceStart, messageSend.sourceEnd);
else {
  setTokenRange(messageSend.sourceStart, messageSend.sourceEnd);
  if (messageSend.statementEnd > messageSend.sourceStart)
    setSourceRange(messageSend.sourceStart, messageSend.statementEnd);
}

(note the added messageSend.arguments != null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Faulty/Undesired CodeCompletion behaviour
3 participants