use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_AnnotationQuickFixes.
// See https://github.com/redhat-developer/vscode-java/issues/1992
@Test
public void testResolveCodeAction_AnnotationQuickFixes() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
importProjects("maven/salut4");
IProject proj = WorkspaceHelper.getProject("salut4");
IJavaProject javaProject = JavaCore.create(proj);
assertTrue(javaProject.exists());
IType type = javaProject.findType("org.sample.MyTest");
ICompilationUnit unit = type.getCompilationUnit();
assertFalse(unit.getSource().contains("import org.junit.jupiter.api.Test"));
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
Position position = new Position(3, 4);
final Range range = new Range(position, position);
params.setRange(range);
CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UndefinedType), range)), Collections.singletonList(CodeActionKind.QuickFix));
params.setContext(context);
List<Either<Command, CodeAction>> quickfixActions = server.codeAction(params).join();
assertNotNull(quickfixActions);
assertFalse("No quickfix actions were found", quickfixActions.isEmpty());
Optional<Either<Command, CodeAction>> importTest = quickfixActions.stream().filter(codeAction -> {
return "Import 'Test' (org.junit.jupiter.api)".equals(codeAction.getRight().getTitle());
}).findFirst();
CodeAction codeAction = importTest.get().getRight();
assertEquals(1, codeAction.getDiagnostics().size());
CodeAction resolvedCodeAction = server.resolveCodeAction(codeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
assertTrue(actual.contains("import org.junit.jupiter.api.Test"));
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testAssignAllParamsToFields.
@Test
public void testAssignAllParamsToFields() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class App {\n");
buf.append(" private String s;\n");
buf.append("\n");
buf.append(" public App(String s, String s3, String s2) {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("App.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
Range range = CodeActionUtil.getRange(unit, "s3");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(JavaCodeActionKind.QUICK_ASSIST));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No quickassist actions were found", codeActions.isEmpty());
Optional<Either<Command, CodeAction>> assignAllToNewFieldsResponse = codeActions.stream().filter(codeAction -> {
return "Assign all parameters to new fields".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quick assist 'Assign all parameters to new fields'", assignAllToNewFieldsResponse.isPresent());
CodeAction unresolvedCodeAction = assignAllToNewFieldsResponse.get().getRight();
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class App {\n");
buf.append(" private String s;\n");
buf.append(" private String s4;\n");
buf.append(" private String s3;\n");
buf.append(" private String s2;\n");
buf.append("\n");
buf.append(" public App(String s, String s3, String s2) {\n");
buf.append(" s4 = s;\n");
buf.append(" this.s3 = s3;\n");
buf.append(" this.s2 = s2;\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_SourceActions.
@Test
public void testResolveCodeAction_SourceActions() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" private void hello() {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("E.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "hello");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Source));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No source actions were found", codeActions.isEmpty());
for (Either<Command, CodeAction> codeAction : codeActions) {
Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
}
Optional<Either<Command, CodeAction>> generateConstructorResponse = codeActions.stream().filter(codeAction -> {
return "Generate Constructors".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quick assist 'Convert to lambda expression'", generateConstructorResponse.isPresent());
CodeAction unresolvedCodeAction = generateConstructorResponse.get().getRight();
Assert.assertNotNull("Should preserve the data property for the unresolved code action", unresolvedCodeAction.getData());
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" private void hello() {\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" /**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append(" public E() {\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class StandardProjectsManager method fileChanged.
@Override
public void fileChanged(String uriString, CHANGE_TYPE changeType) {
if (uriString == null) {
return;
}
boolean configureNeeded = false;
String formatterUrl = preferenceManager.getPreferences().getFormatterUrl();
if (formatterUrl != null && JavaLanguageServerPlugin.getInstance().getProtocol() != null) {
URI uri = JDTUtils.toURI(uriString);
List<URI> uris = getURIs(formatterUrl);
boolean changed = false;
for (URI formatterUri : uris) {
if (URIUtil.sameURI(formatterUri, uri)) {
changed = true;
break;
}
}
if (changed) {
if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CREATED) {
registerWatchers();
}
configureNeeded = true;
}
}
String settingsUrl = preferenceManager.getPreferences().getSettingsUrl();
if (settingsUrl != null && JavaLanguageServerPlugin.getInstance().getProtocol() != null) {
URI uri = JDTUtils.toURI(uriString);
List<URI> uris = getURIs(settingsUrl);
boolean changed = false;
for (URI settingsURI : uris) {
if (URIUtil.sameURI(settingsURI, uri)) {
changed = true;
break;
}
}
if (changed) {
if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CREATED) {
registerWatchers();
}
configureNeeded = true;
}
}
if (configureNeeded) {
configureSettings(preferenceManager.getPreferences());
}
IResource resource = JDTUtils.getFileOrFolder(uriString);
if (resource == null) {
return;
}
try {
Optional<IBuildSupport> bs = getBuildSupport(resource.getProject());
if (bs.isPresent()) {
IBuildSupport buildSupport = bs.get();
if (JDTUtils.isExcludedFile(buildSupport.getExcludedFilePatterns(), uriString)) {
return;
}
boolean requireConfigurationUpdate = buildSupport.fileChanged(resource, changeType, new NullProgressMonitor());
if (requireConfigurationUpdate) {
FeatureStatus status = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus();
switch(status) {
case automatic:
// do not force the build, because it's not started by user and should be done only if build file has changed
updateProject(resource.getProject(), false);
break;
case disabled:
break;
default:
if (client != null) {
String cmd = "java.projectConfiguration.status";
TextDocumentIdentifier uri = new TextDocumentIdentifier(uriString);
ActionableNotification updateProjectConfigurationNotification = new ActionableNotification().withSeverity(MessageType.Info).withMessage("A build file was modified. Do you want to synchronize the Java classpath/configuration?").withCommands(asList(new Command("Never", cmd, asList(uri, FeatureStatus.disabled)), new Command("Now", cmd, asList(uri, FeatureStatus.interactive)), new Command("Always", cmd, asList(uri, FeatureStatus.automatic))));
client.sendActionableNotification(updateProjectConfigurationNotification);
}
}
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem refreshing workspace", e);
}
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class AbstractQuickFixTest method evaluateCodeActionCommand.
protected String evaluateCodeActionCommand(Either<Command, CodeAction> codeAction) throws BadLocationException, JavaModelException {
Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
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);
return evaluateWorkspaceEdit(we);
}
Aggregations