Skip to content

Commit

Permalink
Fix AbstractQuickFixTest.evaluateCodeActions(ICompilationUnit) logic.
Browse files Browse the repository at this point in the history
- Code actions belonging to diagnostics need to be resolved by setting
  the invocation range for the particular diagnostic in the evalutation
  request

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Aug 16, 2024
1 parent 2f37f45 commit 365d396
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,8 @@ public void assertEquivalent(Either<Command, CodeAction> action) throws Exceptio
}
}

protected Range getRange(ICompilationUnit cu, IProblem[] problems) throws JavaModelException {
if (problems.length == 0) {
return new Range(new Position(), new Position());
}
IProblem problem = problems[0];
return JDTUtils.toRange(cu, problem.getSourceStart(), 0);
protected Range getRange(ICompilationUnit cu, IProblem problem) throws JavaModelException {
return (problem == null) ? new Range(new Position(), new Position()) : JDTUtils.toRange(cu, problem.getSourceStart(), 0);
}

protected void setIgnoredCommands(String... ignoredCommands) {
Expand All @@ -246,12 +242,21 @@ protected void setOnly(String... onlyKinds) {
}

protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit cu) throws JavaModelException {

List<Either<Command, CodeAction>> result = new ArrayList<>();
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();

Range range = getRange(cu, problems);
return evaluateCodeActions(cu, range);
if (problems.length == 0) {
Range range = getRange(cu, null);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
result.addAll(codeActions);
} else {
for (IProblem problem : problems) {
Range range = getRange(cu, problem);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
result.addAll(codeActions);
}
}
return result;
}

protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit cu, Range range) throws JavaModelException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected int[] getSelection(String source) {
}

@Override
protected Range getRange(ICompilationUnit cu, IProblem[] problems) throws JavaModelException {
protected Range getRange(ICompilationUnit cu, IProblem problem) throws JavaModelException {
return CodeActionUtil.getRange(cu);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public void testExtractToField_standaloneStatement() throws Exception {
protected void failHelper(ICompilationUnit cu) throws OperationCanceledException, CoreException {
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Range range = getRange(cu, problems);
Range range = getRange(cu, problems.length > 0 ? problems[0] : null);
int start = DiagnosticsHelper.getStartOffset(cu, range);
int end = DiagnosticsHelper.getEndOffset(cu, range);
ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(astRoot, start, end - start);
Expand All @@ -549,7 +549,7 @@ protected void failHelper(ICompilationUnit cu) throws OperationCanceledException
protected boolean helper(ICompilationUnit cu, InitializeScope initializeIn, String expected) throws Exception {
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Range range = getRange(cu, problems);
Range range = getRange(cu, problems.length > 0 ? problems[0] : null);
int start = DiagnosticsHelper.getStartOffset(cu, range);
int end = DiagnosticsHelper.getEndOffset(cu, range);
ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(astRoot, start, end - start);
Expand Down

0 comments on commit 365d396

Please sign in to comment.