Bug 575422 - [content assist] completion parser should not "eat" into subsequent statement
Summary: [content assist] completion parser should not "eat" into subsequent statement
Status: CLOSED DUPLICATE of bug 575631
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.21   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.22 M2   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 575599
  Show dependency tree
 
Reported: 2021-08-15 12:55 EDT by Stephan Herrmann CLA
Modified: 2021-10-21 14:45 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Herrmann CLA 2021-08-15 12:55:22 EDT
From bug 575397:

When parsing smth like
	if (true) {
		Thread.|
		someMethod();
	}
the completion scanner currently recognizes a dubious LocalDeclaration from 
    Thread.<emptyId> someMethod

This prematurely determines "Thread.<emptyId>" as a QualifiedTypeReference.

While bug 575397 resolves this for one kind of statement, we should find a general solution to steer the parser into a more likely AST.


Adding the following method in Scanner would allow us to recognize the part "the subsequent code can be interpreted as a legal statement":

public boolean isAtStatementStart() {
	VanguardParser vp = getVanguardParser();
	return vp.parse(Goal.BlockStatementoptGoal) == VanguardParser.SUCCESS;
}

We could use some additional routine for recognizing what's left of the cursor.

Finally, I'm thinking of synthesizing one or more tokens to allow reducing the part up until the completionIdentifier. Perhaps some speculative insertion could allow us to check if that would be accepted as a statement by the parser.

All this must bear in mind, that the prefix can legally be any name, resolving to variable or type.

In a quick-n-dirty experiment I had some success even by just scheduling a ';' right after the (empty) completion identifier (using Scanner.unget()), which suffices to separate both statements. It even helps to recover an enclosing if statement (see the example from bug 575397).
Comment 1 Eclipse Genie CLA 2021-08-15 13:23:05 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184031
Comment 2 Gayan Perera CLA 2021-08-15 13:41:42 EDT
I have some tests added in https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184001/3/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/CompletionParserTest18.java Let me know if you would like to move them to this gerrit, i can add more tests like including the method invocation with type parameters for example.
Comment 3 Stephan Herrmann CLA 2021-08-15 16:33:01 EDT
(In reply to Gayan Perera from comment #2)
> I have some tests added in
> https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184001/3/org.eclipse.jdt.
> core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/
> CompletionParserTest18.java Let me know if you would like to move them to
> this gerrit, i can add more tests like including the method invocation with
> type parameters for example.

Yes, more tests and specifically some which explicitly check parser output would be valuable here (as opposed to bug 575397 where I focused on the effective result from CompletionEngine, ignoring details of the recovered AST)
Comment 4 Stephan Herrmann CLA 2021-10-21 14:45:54 EDT
(In reply to Stephan Herrmann from comment #0)
> Finally, I'm thinking of synthesizing one or more tokens to allow reducing
> the part up until the completionIdentifier. Perhaps some speculative
> insertion could allow us to check if that would be accepted as a statement
> by the parser.

Bug 575631 contains a refined version of this strategy, see bug 575631 comment 14

*** This bug has been marked as a duplicate of bug 575631 ***