use of org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore in project eclipse.jdt.ls by eclipse.
the class CodeActionHandler method getProblemLocationCores.
public static IProblemLocationCore[] getProblemLocationCores(ICompilationUnit unit, List<Diagnostic> diagnostics) {
IProblemLocationCore[] locations = new IProblemLocationCore[diagnostics.size()];
for (int i = 0; i < diagnostics.size(); i++) {
Diagnostic diagnostic = diagnostics.get(i);
int start = DiagnosticsHelper.getStartOffset(unit, diagnostic.getRange());
int end = DiagnosticsHelper.getEndOffset(unit, diagnostic.getRange());
boolean isError = diagnostic.getSeverity() == DiagnosticSeverity.Error;
int problemId = getProblemId(diagnostic);
List<String> arguments = new ArrayList<>();
if (diagnostic.getData() instanceof JsonArray) {
final JsonArray data = (JsonArray) diagnostic.getData();
for (JsonElement e : data) {
arguments.add(e.getAsString());
}
}
locations[i] = new ProblemLocationCore(start, end - start, problemId, arguments.toArray(new String[0]), isError, IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
}
return locations;
}
use of org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore 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.internal.ui.text.correction.IProblemLocationCore in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method noErrorsAtLocation.
static boolean noErrorsAtLocation(IProblemLocationCore[] locations, ASTNode coveringNode) {
if (locations != null) {
int start = coveringNode.getStartPosition();
int length = coveringNode.getLength();
for (int i = 0; i < locations.length; i++) {
IProblemLocationCore location = locations[i];
if (location.getOffset() > start + length || (location.getOffset() + location.getLength()) < start) {
continue;
}
if (location.isError()) {
if (IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER.equals(location.getMarkerType()) && JavaCore.getOptionForConfigurableSeverity(location.getProblemId()) != null) {
// continue (only drop out for severe (non-optional) errors)
} else {
return false;
}
}
}
}
return true;
}
Aggregations