下面列出了org.eclipse.lsp4j.CodeActionParams# setTextDocument ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testCodeAction_removeUnusedImport() throws Exception{
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"import java.sql.*; \n" +
"public class Foo {\n"+
" void foo() {\n"+
" }\n"+
"}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "java.sql");
params.setRange(range);
params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnusedImport), range))));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertTrue(codeActions.size() >= 3);
Assert.assertEquals(codeActions.get(0).getRight().getKind(), CodeActionKind.QuickFix);
Assert.assertEquals(codeActions.get(1).getRight().getKind(), CodeActionKind.QuickFix);
Assert.assertEquals(codeActions.get(2).getRight().getKind(), CodeActionKind.SourceOrganizeImports);
Command c = codeActions.get(0).getRight().getCommand();
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
}
@Test
public void testCodeAction_sourceActionsOnly() throws Exception {
//@formatter:off
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"import java.sql.*; \n" +
"public class Foo {\n"+
" void foo() {\n"+
" }\n"+
"}\n");
//@formatter:on
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "foo()");
params.setRange(range);
params.setContext(new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Source)));
List<Either<Command, CodeAction>> sourceActions = getCodeActions(params);
Assert.assertNotNull(sourceActions);
Assert.assertFalse("No source actions were found", sourceActions.isEmpty());
for (Either<Command, CodeAction> codeAction : sourceActions) {
Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.Source));
}
}
@Test
public void testCodeAction_refactorActionsOnly() throws Exception {
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class Foo {\n"+
" void foo() {\n"+
" String bar = \"astring\";"+
" }\n"+
"}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
CodeActionContext context = new CodeActionContext(
Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)),
Collections.singletonList(CodeActionKind.Refactor)
);
params.setContext(context);
List<Either<Command, CodeAction>> refactorActions = getCodeActions(params);
Assert.assertNotNull(refactorActions);
Assert.assertFalse("No refactor actions were found", refactorActions.isEmpty());
for (Either<Command, CodeAction> codeAction : refactorActions) {
Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.Refactor));
}
}
@Test
public void testCodeAction_quickfixActionsOnly() throws Exception {
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class Foo {\n"+
" void foo() {\n"+
" String bar = \"astring\";"+
" }\n"+
"}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
CodeActionContext context = new CodeActionContext(
Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)),
Collections.singletonList(CodeActionKind.QuickFix)
);
params.setContext(context);
List<Either<Command, CodeAction>> quickfixActions = getCodeActions(params);
Assert.assertNotNull(quickfixActions);
Assert.assertFalse("No quickfix actions were found", quickfixActions.isEmpty());
for (Either<Command, CodeAction> codeAction : quickfixActions) {
Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.QuickFix));
}
}
@Test
public void testCodeAction_removeUnterminatedString() throws Exception{
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class Foo {\n"+
" void foo() {\n"+
"String s = \"some str\n" +
" }\n"+
"}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "some str");
params.setRange(range);
params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnterminatedString), range))));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertFalse(codeActions.isEmpty());
Assert.assertEquals(codeActions.get(0).getRight().getKind(), CodeActionKind.QuickFix);
Command c = codeActions.get(0).getRight().getCommand();
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
}
@Test
public void testCodeAction_exception() throws JavaModelException {
URI uri = project.getFile("nopackage/Test.java").getRawLocationURI();
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
try {
cu.becomeWorkingCopy(new NullProgressMonitor());
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(uri.toString()));
final Range range = new Range();
range.setStart(new Position(0, 17));
range.setEnd(new Position(0, 17));
params.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Collections.emptyList());
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
} finally {
cu.discardWorkingCopy();
}
}
protected List<Either<Command, CodeAction>> getCodeActions(ICompilationUnit cu) throws JavaModelException {
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Range range = getRange(cu, problems);
CodeActionParams parms = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
parms.setTextDocument(textDocument);
parms.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true));
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
parms.setContext(context);
return new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
}
@Override
protected void performTest(Project project, String moduleName, N4JSTestCodeActionConfiguration tcac)
throws InterruptedException, ExecutionException {
CodeActionParams codeActionParams = new CodeActionParams();
Range range = new Range();
Position posStart = new Position(tcac.getLine(), tcac.getColumn());
Position posEnd = tcac.getEndLine() >= 0 && tcac.getEndColumn() >= 0
? new Position(tcac.getEndLine(), tcac.getEndColumn())
: posStart;
range.setStart(posStart);
range.setEnd(posEnd);
codeActionParams.setRange(range);
CodeActionContext context = new CodeActionContext();
FileURI uri = getFileURIFromModuleName(moduleName);
context.setDiagnostics(Lists.newArrayList(getIssues(uri)));
codeActionParams.setContext(context);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(uri.toString());
codeActionParams.setTextDocument(textDocument);
CompletableFuture<List<Either<Command, CodeAction>>> future = languageServer.codeAction(codeActionParams);
List<Either<Command, CodeAction>> result = future.get();
if (tcac.getAssertCodeActions() != null) {
tcac.getAssertCodeActions().apply(result);
} else {
String resultStr = result.stream()
.map(cmdOrAction -> getStringLSP4J().toString3(cmdOrAction))
.collect(Collectors.joining("\n-----\n"));
assertEquals(tcac.getExpectedCodeActions().trim(), resultStr.trim());
}
}
public static CodeActionParams constructCodeActionParams(ICompilationUnit unit, Range range) {
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
params.setRange(range);
params.setContext(new CodeActionContext(Collections.emptyList()));
return params;
}
@Test
public void testCodeAction_organizeImportsSourceActionOnly() throws Exception {
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"import java.util.List;\n"+
"public class Foo {\n"+
" void foo() {\n"+
" String bar = \"astring\";"+
" }\n"+
"}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
CodeActionContext context = new CodeActionContext(
Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)),
Collections.singletonList(CodeActionKind.SourceOrganizeImports)
);
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertFalse("No organize imports actions were found", codeActions.isEmpty());
for (Either<Command, CodeAction> codeAction : codeActions) {
Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.SourceOrganizeImports));
}
}
@Test
public void testCodeAction_allKindsOfActions() throws Exception {
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class Foo {\n"+
" void foo() {\n"+
" String bar = \"astring\";"+
" }\n"+
"}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
CodeActionContext context = new CodeActionContext(
Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range))
);
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertFalse("No code actions were found", codeActions.isEmpty());
boolean hasQuickFix = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.QuickFix));
assertTrue("No quickfix actions were found", hasQuickFix);
boolean hasRefactor = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Refactor));
assertTrue("No refactor actions were found", hasRefactor);
boolean hasSource = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Source));
assertTrue("No source actions were found", hasSource);
List<String> baseKinds = codeActions.stream().map(codeAction -> getBaseKind(codeAction.getRight().getKind())).collect(Collectors.toList());
assertTrue("quickfix actions should be ahead of refactor actions", baseKinds.lastIndexOf(CodeActionKind.QuickFix) < baseKinds.indexOf(CodeActionKind.Refactor));
assertTrue("refactor actions should be ahead of source actions", baseKinds.lastIndexOf(CodeActionKind.Refactor) < baseKinds.indexOf(CodeActionKind.Source));
}
@Test
@Ignore
public void testCodeAction_superfluousSemicolon() throws Exception{
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class Foo {\n"+
" void foo() {\n"+
";" +
" }\n"+
"}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, ";");
params.setRange(range);
params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.SuperfluousSemicolon), range))));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertEquals(1, codeActions.size());
Assert.assertEquals(codeActions.get(0), CodeActionKind.QuickFix);
Command c = getCommand(codeActions.get(0));
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
Assert.assertNotNull(c.getArguments());
Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
WorkspaceEdit we = (WorkspaceEdit) c.getArguments().get(0);
List<org.eclipse.lsp4j.TextEdit> edits = we.getChanges().get(JDTUtils.toURI(unit));
Assert.assertEquals(1, edits.size());
Assert.assertEquals("", edits.get(0).getNewText());
Assert.assertEquals(range, edits.get(0).getRange());
}
@Test
public void test_noUnnecessaryCodeActions() throws Exception{
//@formatter:off
ICompilationUnit unit = getWorkingCopy(
"src/org/sample/Foo.java",
"package org.sample;\n"+
"\n"+
"public class Foo {\n"+
" private String foo;\n"+
" public String getFoo() {\n"+
" return foo;\n"+
" }\n"+
" \n"+
" public void setFoo(String newFoo) {\n"+
" foo = newFoo;\n"+
" }\n"+
"}\n");
//@formatter:on
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "String foo;");
params.setRange(range);
params.setContext(new CodeActionContext(Collections.emptyList()));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertFalse("No need for organize imports action", containsKind(codeActions, CodeActionKind.SourceOrganizeImports));
Assert.assertFalse("No need for generate getter and setter action", containsKind(codeActions, JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS));
}
@Test
public void test_filterTypes() throws Exception {
//@formatter:off
ICompilationUnit unit = getWorkingCopy(
"src/org/sample/Foo.java",
"package org.sample;\n"+
"\n"+
"public class Foo {\n"+
" List foo;\n"+
"}\n");
//@formatter:on
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "List");
params.setRange(range);
params.setContext(new CodeActionContext(Collections.emptyList()));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertTrue("No organize imports action", containsKind(codeActions, CodeActionKind.SourceOrganizeImports));
try {
List<String> filteredTypes = new ArrayList<>();
filteredTypes.add("java.util.*");
PreferenceManager.getPrefs(null).setFilteredTypes(filteredTypes);
codeActions = getCodeActions(params);
assertNotNull(codeActions);
Assert.assertFalse("No need for organize imports action", containsKind(codeActions, CodeActionKind.SourceOrganizeImports));
} finally {
PreferenceManager.getPrefs(null).setFilteredTypes(Collections.emptyList());
}
}
private List<Either<Command, CodeAction>> getCodeActions(ICompilationUnit cu, IProblem problem) throws JavaModelException {
CodeActionParams parms = new CodeActionParams();
Range range = JDTUtils.toRange(cu, problem.getSourceStart(), 0);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
parms.setTextDocument(textDocument);
parms.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problem), true));
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
parms.setContext(context);
return new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
}