use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getAssists.
public List<ChangeCorrectionProposal> getAssists(CodeActionParams params, IInvocationContext context, IProblemLocationCore[] locations) throws CoreException {
ASTNode coveringNode = context.getCoveringNode();
if (coveringNode != null) {
// ArrayList<ASTNode> coveredNodes = getFullyCoveredNodes(context, coveringNode);
ArrayList<ChangeCorrectionProposal> resultingCollections = new ArrayList<>();
// quick assists that show up also if there is an error/warning
// getRenameLocalProposals(context, coveringNode, locations, resultingCollections);
// getRenameRefactoringProposal(context, coveringNode, locations, resultingCollections);
// getAssignToVariableProposals(context, coveringNode, locations, resultingCollections);
getAssignParamToFieldProposals(context, coveringNode, resultingCollections);
getAssignAllParamsToFieldsProposals(context, coveringNode, resultingCollections);
// getInferDiamondArgumentsProposal(context, coveringNode, locations, resultingCollections);
// getGenerateForLoopProposals(context, coveringNode, locations, resultingCollections);
// boolean noErrorsAtLocation = noErrorsAtLocation(locations);
// if (noErrorsAtLocation) {
// boolean problemsAtLocation = locations.length != 0;
// getCatchClauseToThrowsProposals(context, coveringNode, resultingCollections);
// getPickoutTypeFromMulticatchProposals(context, coveringNode, coveredNodes, resultingCollections);
// getConvertToMultiCatchProposals(context, coveringNode, resultingCollections);
// getUnrollMultiCatchProposals(context, coveringNode, resultingCollections);
// getUnWrapProposals(context, coveringNode, resultingCollections);
// getJoinVariableProposals(context, coveringNode, resultingCollections);
// getSplitVariableProposals(context, coveringNode, resultingCollections);
// getAddFinallyProposals(context, coveringNode, resultingCollections);
// getAddElseProposals(context, coveringNode, resultingCollections);
// getAddBlockProposals(context, coveringNode, resultingCollections);
// getInvertEqualsProposal(context, coveringNode, resultingCollections);
// getArrayInitializerToArrayCreation(context, coveringNode, resultingCollections);
// getCreateInSuperClassProposals(context, coveringNode, resultingCollections);
// getConvertLocalToFieldProposal(context, coveringNode, resultingCollections);
// getChangeLambdaBodyToBlockProposal(context, coveringNode, resultingCollections);
// getChangeLambdaBodyToExpressionProposal(context, coveringNode, resultingCollections);
// getAddInferredLambdaParameterTypes(context, coveringNode, resultingCollections);
getConvertMethodReferenceToLambdaProposal(context, coveringNode, resultingCollections);
getConvertLambdaToMethodReferenceProposal(context, coveringNode, resultingCollections);
// getFixParenthesesInLambdaExpression(context, coveringNode, resultingCollections);
// if (!getConvertForLoopProposal(context, coveringNode, resultingCollections)) {
// getConvertIterableLoopProposal(context, coveringNode, resultingCollections);
// }
// getConvertEnhancedForLoopProposal(context, coveringNode, resultingCollections);
// getRemoveBlockProposals(context, coveringNode, resultingCollections);
// getConvertStringConcatenationProposals(context, resultingCollections);
// getMissingCaseStatementProposals(context, coveringNode, resultingCollections);
// }
getAddMethodDeclaration(context, coveringNode, resultingCollections);
getTryWithResourceProposals(locations, context, coveringNode, resultingCollections);
getConvertToSwitchExpressionProposals(context, coveringNode, resultingCollections);
return resultingCollections;
}
return Collections.emptyList();
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class CodeActionHandler method getCodeActionCommands.
public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams params, IProgressMonitor monitor) {
if (monitor.isCanceled()) {
return Collections.emptyList();
}
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit == null || monitor.isCanceled()) {
return Collections.emptyList();
}
Map<String, Object> formattingOptions = ConfigurationHandler.getFormattingOptions(params.getTextDocument().getUri());
if (formattingOptions != null && !formattingOptions.isEmpty()) {
Object tabSizeValue = formattingOptions.get(Preferences.JAVA_CONFIGURATION_TABSIZE);
Object insertSpacesValue = formattingOptions.get(Preferences.JAVA_CONFIGURATION_INSERTSPACES);
Map<String, String> customOptions = new HashMap<>();
if (tabSizeValue != null) {
try {
int tabSize = Integer.parseInt(String.valueOf(tabSizeValue));
if (tabSize > 0) {
customOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, Integer.toString(tabSize));
}
} catch (Exception ex) {
// do nothing
}
}
if (insertSpacesValue != null) {
boolean insertSpaces = Boolean.parseBoolean(String.valueOf(insertSpacesValue));
customOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, insertSpaces ? JavaCore.SPACE : JavaCore.TAB);
}
if (!customOptions.isEmpty()) {
unit.setOptions(customOptions);
}
}
CompilationUnit astRoot = getASTRoot(unit, monitor);
if (astRoot == null || monitor.isCanceled()) {
return Collections.emptyList();
}
int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
InnovationContext context = new InnovationContext(unit, start, end - start);
context.setASTRoot(astRoot);
List<Diagnostic> diagnostics = params.getContext().getDiagnostics().stream().filter((d) -> {
return JavaLanguageServerPlugin.SERVER_SOURCE_ID.equals(d.getSource());
}).collect(Collectors.toList());
IProblemLocationCore[] locations = getProblemLocationCores(unit, diagnostics);
if (monitor.isCanceled()) {
return Collections.emptyList();
}
List<String> codeActionKinds = new ArrayList<>();
if (params.getContext().getOnly() != null && !params.getContext().getOnly().isEmpty()) {
codeActionKinds.addAll(params.getContext().getOnly());
} else {
List<String> defaultCodeActionKinds = Arrays.asList(CodeActionKind.QuickFix, CodeActionKind.Refactor, JavaCodeActionKind.QUICK_ASSIST, CodeActionKind.Source);
codeActionKinds.addAll(defaultCodeActionKinds);
}
List<Either<Command, CodeAction>> codeActions = new ArrayList<>();
List<ChangeCorrectionProposal> proposals = new ArrayList<>();
ChangeCorrectionProposalComparator comparator = new ChangeCorrectionProposalComparator();
if (containsKind(codeActionKinds, CodeActionKind.QuickFix)) {
try {
codeActions.addAll(nonProjectFixProcessor.getCorrections(params, context, locations));
List<ChangeCorrectionProposal> quickfixProposals = this.quickFixProcessor.getCorrections(context, locations);
quickfixProposals.sort(comparator);
proposals.addAll(quickfixProposals);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem resolving quick fix code actions", e);
}
}
if (monitor.isCanceled()) {
return Collections.emptyList();
}
if (containsKind(codeActionKinds, CodeActionKind.Refactor)) {
try {
List<ChangeCorrectionProposal> refactorProposals = this.refactorProcessor.getProposals(params, context, locations);
refactorProposals.sort(comparator);
proposals.addAll(refactorProposals);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem resolving refactor code actions", e);
}
}
if (monitor.isCanceled()) {
return Collections.emptyList();
}
if (containsKind(codeActionKinds, JavaCodeActionKind.QUICK_ASSIST)) {
try {
List<ChangeCorrectionProposal> quickassistProposals = this.quickAssistProcessor.getAssists(params, context, locations);
quickassistProposals.sort(comparator);
proposals.addAll(quickassistProposals);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem resolving quick assist code actions", e);
}
}
if (monitor.isCanceled()) {
return Collections.emptyList();
}
try {
for (ChangeCorrectionProposal proposal : proposals) {
Optional<Either<Command, CodeAction>> codeActionFromProposal = getCodeActionFromProposal(proposal, params.getContext());
if (codeActionFromProposal.isPresent() && !codeActions.contains(codeActionFromProposal.get())) {
codeActions.add(codeActionFromProposal.get());
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem converting proposal to code actions", e);
}
if (monitor.isCanceled()) {
return Collections.emptyList();
}
if (containsKind(codeActionKinds, CodeActionKind.Source)) {
codeActions.addAll(sourceAssistProcessor.getSourceActionCommands(params, context, locations, monitor));
}
if (monitor.isCanceled()) {
return Collections.emptyList();
}
populateDataFields(codeActions);
return codeActions;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getProposals.
public List<ChangeCorrectionProposal> getProposals(CodeActionParams params, IInvocationContext context, IProblemLocationCore[] locations) throws CoreException {
ASTNode coveringNode = context.getCoveringNode();
if (coveringNode != null) {
ArrayList<ChangeCorrectionProposal> proposals = new ArrayList<>();
InvertBooleanUtility.getInverseConditionProposals(params, context, coveringNode, proposals);
getInverseLocalVariableProposals(params, context, coveringNode, proposals);
getMoveRefactoringProposals(params, context, coveringNode, proposals);
boolean noErrorsAtLocation = noErrorsAtLocation(locations, coveringNode);
if (noErrorsAtLocation) {
boolean problemsAtLocation = locations.length != 0;
getExtractVariableProposal(params, context, problemsAtLocation, proposals);
getExtractMethodProposal(params, context, coveringNode, problemsAtLocation, proposals);
getExtractFieldProposal(params, context, problemsAtLocation, proposals);
getInlineProposal(context, coveringNode, proposals);
getConvertAnonymousToNestedProposals(params, context, coveringNode, proposals);
getConvertAnonymousClassCreationsToLambdaProposals(context, coveringNode, proposals);
getConvertLambdaToAnonymousClassCreationsProposals(context, coveringNode, proposals);
getConvertVarTypeToResolvedTypeProposal(context, coveringNode, proposals);
getConvertResolvedTypeToVarTypeProposal(context, coveringNode, proposals);
getAddStaticImportProposals(context, coveringNode, proposals);
getConvertForLoopProposal(context, coveringNode, proposals);
getAssignToVariableProposals(context, coveringNode, locations, proposals, params);
getIntroduceParameterProposals(params, context, coveringNode, locations, proposals);
}
return proposals;
}
return Collections.emptyList();
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getConvertToSwitchExpressionProposals.
private static boolean getConvertToSwitchExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
if (covering instanceof Block) {
List<Statement> statements = ((Block) covering).statements();
int startIndex = QuickAssistProcessorUtil.getIndex(context.getSelectionOffset(), statements);
if (startIndex == -1 || startIndex >= statements.size()) {
return false;
}
covering = statements.get(startIndex);
} else {
while (covering instanceof SwitchCase || covering instanceof SwitchExpression) {
covering = covering.getParent();
}
}
SwitchStatement switchStatement;
if (covering instanceof SwitchStatement) {
switchStatement = (SwitchStatement) covering;
} else {
return false;
}
SwitchExpressionsFixCore fix = SwitchExpressionsFixCore.createConvertToSwitchExpressionFix(switchStatement);
if (fix == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
// add correction proposal
try {
CompilationUnitChange change = fix.createChange(null);
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, change, IProposalRelevance.CONVERT_TO_SWITCH_EXPRESSION);
resultingCollections.add(proposal);
} catch (CoreException e) {
// continue
}
return true;
}
Aggregations