use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getGenerateToStringAction.
private Optional<Either<Command, CodeAction>> getGenerateToStringAction(CodeActionParams params, String kind) {
if (!preferenceManager.getClientPreferences().isGenerateToStringPromptSupported()) {
return Optional.empty();
}
Command command = new Command(ActionMessages.GenerateToStringAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATETOSTRINGPROMPT, Collections.singletonList(params));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_TO_STRING)) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateToStringAction_ellipsisLabel);
codeAction.setKind(kind);
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.emptyList());
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method addFinalModifierWherePossibleAction.
private Optional<Either<Command, CodeAction>> addFinalModifierWherePossibleAction(IInvocationContext context) {
IProposableFix fix = (IProposableFix) VariableDeclarationFixCore.createCleanUp(context.getASTRoot(), true, true, true);
if (fix == null) {
return Optional.empty();
}
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, null, IProposalRelevance.MAKE_VARIABLE_DECLARATION_FINAL, context, JavaCodeActionKind.SOURCE_GENERATE_FINAL_MODIFIERS);
if (this.preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateFinalModifiersAction_label);
codeAction.setKind(proposal.getKind());
codeAction.setData(proposal);
codeAction.setDiagnostics(Collections.EMPTY_LIST);
return Optional.of(Either.forRight(codeAction));
} else {
WorkspaceEdit edit;
try {
edit = ChangeUtil.convertToWorkspaceEdit(proposal.getChange());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem converting proposal to code actions", e);
return Optional.empty();
}
if (!ChangeUtil.hasChanges(edit)) {
return Optional.empty();
}
Command command = new Command(ActionMessages.GenerateFinalModifiersAction_label, CodeActionHandler.COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(proposal.getKind())) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateFinalModifiersAction_label);
codeAction.setKind(proposal.getKind());
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.EMPTY_LIST);
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
}
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getCodeActionFromProposal.
private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(CodeActionContext context, ICompilationUnit cu, String name, String kind, CodeActionProposal proposal) {
if (preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
CodeAction codeAction = new CodeAction(name);
codeAction.setKind(kind);
codeAction.setData(proposal);
codeAction.setDiagnostics(Collections.EMPTY_LIST);
return Optional.of(Either.forRight(codeAction));
}
try {
WorkspaceEdit edit = proposal.resolveEdit(new NullProgressMonitor());
if (!ChangeUtil.hasChanges(edit)) {
return Optional.empty();
}
Command command = new Command(name, CodeActionHandler.COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) {
CodeAction codeAction = new CodeAction(name);
codeAction.setKind(kind);
codeAction.setCommand(command);
codeAction.setDiagnostics(context.getDiagnostics());
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
} catch (OperationCanceledException | CoreException e) {
JavaLanguageServerPlugin.logException("Problem converting proposal to code actions", e);
}
return null;
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_organizeImportsSourceActionOnly.
@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));
}
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_quickfixActionsOnly.
@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));
}
}
Aggregations