use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation in project eclipse.jdt.ls by eclipse.
the class GenerateAccessorsHandler method generateAccessors.
public static TextEdit generateAccessors(IType type, AccessorField[] accessors, boolean generateComments, Range cursor, IProgressMonitor monitor) {
if (type == null || type.getCompilationUnit() == null) {
return null;
}
try {
ASTNode declarationNode = null;
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
if (astRoot != null && cursor != null) {
ASTNode node = NodeFinder.perform(astRoot, DiagnosticsHelper.getStartOffset(type.getCompilationUnit(), cursor), DiagnosticsHelper.getLength(type.getCompilationUnit(), cursor));
declarationNode = SourceAssistProcessor.getTypeDeclarationNode(node);
}
// If cursor position is not specified, then insert to the last by default.
IJavaElement insertPosition = (declarationNode != null) ? CodeGenerationUtils.findInsertElement(type, null) : CodeGenerationUtils.findInsertElement(type, cursor);
GenerateGetterSetterOperation operation = new GenerateGetterSetterOperation(type, null, generateComments, insertPosition);
return operation.createTextEdit(null, accessors);
} catch (OperationCanceledException | CoreException e) {
JavaLanguageServerPlugin.logException("Failed to generate the accessors.", e);
return null;
}
}
use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation 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