use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getSourceActionCommands.
public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParams params, IInvocationContext context, IProblemLocationCore[] locations, IProgressMonitor monitor) {
List<Either<Command, CodeAction>> $ = new ArrayList<>();
ICompilationUnit cu = context.getCompilationUnit();
IType type = getSelectionType(context);
boolean isInTypeDeclaration = isInTypeDeclaration(context);
boolean isInImportDeclaration = isInImportDeclaration(context);
try {
// Generate Constructor QuickAssist
IJavaElement element = JDTUtils.findElementAtSelection(cu, params.getRange().getEnd().getLine(), params.getRange().getEnd().getCharacter(), this.preferenceManager, new NullProgressMonitor());
if (element instanceof IField || isInTypeDeclaration) {
Optional<Either<Command, CodeAction>> quickAssistGenerateConstructors = getGenerateConstructorsAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, monitor);
addSourceActionCommand($, params.getContext(), quickAssistGenerateConstructors);
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException(e);
}
// Generate Constructor Source Action
Optional<Either<Command, CodeAction>> sourceGenerateConstructors = getGenerateConstructorsAction(params, context, type, JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS, monitor);
addSourceActionCommand($, params.getContext(), sourceGenerateConstructors);
// Organize Imports
if (preferenceManager.getClientPreferences().isAdvancedOrganizeImportsSupported()) {
// Generate QuickAssist
if (isInImportDeclaration) {
Optional<Either<Command, CodeAction>> quickAssistOrganizeImports = getOrganizeImportsAction(params, JavaCodeActionKind.QUICK_ASSIST);
addSourceActionCommand($, params.getContext(), quickAssistOrganizeImports);
}
// Generate Source Action
Optional<Either<Command, CodeAction>> sourceOrganizeImports = getOrganizeImportsAction(params, CodeActionKind.SourceOrganizeImports);
addSourceActionCommand($, params.getContext(), sourceOrganizeImports);
} else {
CodeActionProposal organizeImportsProposal = (pm) -> {
TextEdit edit = getOrganizeImportsProposal(context, pm);
return convertToWorkspaceEdit(cu, edit);
};
// Generate QuickAssist
if (isInImportDeclaration) {
Optional<Either<Command, CodeAction>> sourceOrganizeImports = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description, JavaCodeActionKind.QUICK_ASSIST, organizeImportsProposal);
addSourceActionCommand($, params.getContext(), sourceOrganizeImports);
}
// Generate Source Action
Optional<Either<Command, CodeAction>> sourceOrganizeImports = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description, CodeActionKind.SourceOrganizeImports, organizeImportsProposal);
addSourceActionCommand($, params.getContext(), sourceOrganizeImports);
}
if (!UNSUPPORTED_RESOURCES.contains(cu.getResource().getName())) {
// Override/Implement Methods QuickAssist
if (isInTypeDeclaration) {
Optional<Either<Command, CodeAction>> quickAssistOverrideMethods = getOverrideMethodsAction(params, JavaCodeActionKind.QUICK_ASSIST);
addSourceActionCommand($, params.getContext(), quickAssistOverrideMethods);
}
// Override/Implement Methods Source Action
Optional<Either<Command, CodeAction>> sourceOverrideMethods = getOverrideMethodsAction(params, JavaCodeActionKind.SOURCE_OVERRIDE_METHODS);
addSourceActionCommand($, params.getContext(), sourceOverrideMethods);
}
// Generate Getter and Setter QuickAssist
if (isInTypeDeclaration) {
Optional<Either<Command, CodeAction>> quickAssistGetterSetter = getGetterSetterAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, isInTypeDeclaration);
addSourceActionCommand($, params.getContext(), quickAssistGetterSetter);
}
// Generate Getter and Setter Source Action
Optional<Either<Command, CodeAction>> sourceGetterSetter = getGetterSetterAction(params, context, type, JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS, isInTypeDeclaration);
addSourceActionCommand($, params.getContext(), sourceGetterSetter);
// Generate hashCode() and equals()
if (supportsHashCodeEquals(context, type, monitor)) {
// Generate QuickAssist
if (isInTypeDeclaration) {
Optional<Either<Command, CodeAction>> quickAssistHashCodeEquals = getHashCodeEqualsAction(params, JavaCodeActionKind.QUICK_ASSIST);
addSourceActionCommand($, params.getContext(), quickAssistHashCodeEquals);
}
// Generate Source Action
Optional<Either<Command, CodeAction>> sourceActionHashCodeEquals = getHashCodeEqualsAction(params, JavaCodeActionKind.SOURCE_GENERATE_HASHCODE_EQUALS);
addSourceActionCommand($, params.getContext(), sourceActionHashCodeEquals);
}
// Generate toString()
if (supportsGenerateToString(type)) {
boolean nonStaticFields = true;
try {
nonStaticFields = hasFields(type, false);
} catch (JavaModelException e) {
// do nothing.
}
if (nonStaticFields) {
// Generate QuickAssist
if (isInTypeDeclaration) {
Optional<Either<Command, CodeAction>> generateToStringQuickAssist = getGenerateToStringAction(params, JavaCodeActionKind.QUICK_ASSIST);
addSourceActionCommand($, params.getContext(), generateToStringQuickAssist);
}
// Generate Source Action
Optional<Either<Command, CodeAction>> generateToStringCommand = getGenerateToStringAction(params, JavaCodeActionKind.SOURCE_GENERATE_TO_STRING);
addSourceActionCommand($, params.getContext(), generateToStringCommand);
} else {
CodeActionProposal generateToStringProposal = (pm) -> {
IJavaElement insertPosition = isInTypeDeclaration ? CodeGenerationUtils.findInsertElement(type, null) : CodeGenerationUtils.findInsertElement(type, context.getSelectionOffset());
TextEdit edit = GenerateToStringHandler.generateToString(type, new LspVariableBinding[0], insertPosition, pm);
return convertToWorkspaceEdit(cu, edit);
};
// Generate QuickAssist
if (isInTypeDeclaration) {
Optional<Either<Command, CodeAction>> generateToStringQuickAssist = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateToStringAction_label, JavaCodeActionKind.QUICK_ASSIST, generateToStringProposal);
addSourceActionCommand($, params.getContext(), generateToStringQuickAssist);
}
// Generate Source Action
Optional<Either<Command, CodeAction>> generateToStringCommand = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateToStringAction_label, JavaCodeActionKind.SOURCE_GENERATE_TO_STRING, generateToStringProposal);
addSourceActionCommand($, params.getContext(), generateToStringCommand);
}
}
// Generate Delegate Methods
Optional<Either<Command, CodeAction>> generateDelegateMethods = getGenerateDelegateMethodsAction(params, context, type);
addSourceActionCommand($, params.getContext(), generateDelegateMethods);
// Add final modifiers where possible
Optional<Either<Command, CodeAction>> generateFinalModifiers = addFinalModifierWherePossibleAction(context);
addSourceActionCommand($, params.getContext(), generateFinalModifiers);
return $;
}
use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getGenerateConstructorsAction.
private Optional<Either<Command, CodeAction>> getGenerateConstructorsAction(CodeActionParams params, IInvocationContext context, IType type, String kind, IProgressMonitor monitor) {
try {
if (type == null || type.isAnnotation() || type.isInterface() || type.isAnonymous() || type.getCompilationUnit() == null) {
return Optional.empty();
}
} catch (JavaModelException e) {
return Optional.empty();
}
if (preferenceManager.getClientPreferences().isGenerateConstructorsPromptSupported()) {
CheckConstructorsResponse status = GenerateConstructorsHandler.checkConstructorStatus(type, monitor);
if (status.constructors.length == 0) {
return Optional.empty();
}
if (status.constructors.length == 1 && status.fields.length == 0) {
CodeActionProposal generateConstructorsProposal = (pm) -> {
TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields, params.getRange(), pm);
return convertToWorkspaceEdit(type.getCompilationUnit(), edit);
};
return getCodeActionFromProposal(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, generateConstructorsProposal);
}
Command command = new Command(ActionMessages.GenerateConstructorsAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, Collections.singletonList(params));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS)) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateConstructorsAction_ellipsisLabel);
codeAction.setKind(kind);
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.emptyList());
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
}
return Optional.empty();
}
use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getGetterSetterAction.
private Optional<Either<Command, CodeAction>> getGetterSetterAction(CodeActionParams params, IInvocationContext context, IType type, String kind, boolean isInTypeDeclaration) {
try {
AccessorField[] accessors = GenerateGetterSetterOperation.getUnimplementedAccessors(type);
if (accessors == null || accessors.length == 0) {
return Optional.empty();
} else if (accessors.length == 1 || !preferenceManager.getClientPreferences().isAdvancedGenerateAccessorsSupported()) {
CodeActionProposal getAccessorsProposal = (pm) -> {
// If cursor position is not specified, then insert to the last by default.
IJavaElement insertBefore = isInTypeDeclaration ? CodeGenerationUtils.findInsertElement(type, null) : CodeGenerationUtils.findInsertElement(type, params.getRange());
GenerateGetterSetterOperation operation = new GenerateGetterSetterOperation(type, context.getASTRoot(), preferenceManager.getPreferences().isCodeGenerationTemplateGenerateComments(), insertBefore);
TextEdit edit = operation.createTextEdit(pm, accessors);
return convertToWorkspaceEdit(context.getCompilationUnit(), edit);
};
return getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateGetterSetterAction_label, kind, getAccessorsProposal);
} else {
Command command = new Command(ActionMessages.GenerateGetterSetterAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATEACCESSORSPROMPT, Collections.singletonList(params));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS)) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateGetterSetterAction_ellipsisLabel);
codeAction.setKind(kind);
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.emptyList());
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
}
} catch (OperationCanceledException | CoreException e) {
JavaLanguageServerPlugin.logException("Failed to generate Getter and Setter source action", e);
return Optional.empty();
}
}
Aggregations