Search in sources :

Example 6 with IProblemLocationCore

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;
}
Also used : JsonArray(com.google.gson.JsonArray) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) ProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.ProblemLocationCore) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore)

Example 7 with IProblemLocationCore

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) StringUtils(org.apache.commons.lang3.StringUtils) DefaultCodeFormatterConstants(org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants) QuickFixProcessor(org.eclipse.jdt.ls.core.internal.corrections.QuickFixProcessor) DiagnosticsHelper(org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) ProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.ProblemLocationCore) Collectors(java.util.stream.Collectors) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) QuickAssistProcessor(org.eclipse.jdt.ls.core.internal.text.correction.QuickAssistProcessor) IJavaModelMarker(org.eclipse.jdt.core.IJavaModelMarker) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) List(java.util.List) JsonArray(com.google.gson.JsonArray) Command(org.eclipse.lsp4j.Command) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) Optional(java.util.Optional) ChangeUtil(org.eclipse.jdt.ls.core.internal.ChangeUtil) RefactoringCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) HashMap(java.util.HashMap) Diagnostic(org.eclipse.lsp4j.Diagnostic) RefactorProcessor(org.eclipse.jdt.ls.core.internal.corrections.RefactorProcessor) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) SourceAssistProcessor(org.eclipse.jdt.ls.core.internal.text.correction.SourceAssistProcessor) JsonElement(com.google.gson.JsonElement) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) CUCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) NonProjectFixProcessor(org.eclipse.jdt.ls.core.internal.text.correction.NonProjectFixProcessor) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) AssignToVariableAssistCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ForkJoinPool(java.util.concurrent.ForkJoinPool) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) CoreException(org.eclipse.core.runtime.CoreException)

Example 8 with IProblemLocationCore

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;
}
Also used : IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore)

Aggregations

IProblemLocationCore (org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore)8 ArrayList (java.util.ArrayList)5 CoreException (org.eclipse.core.runtime.CoreException)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 CoreASTProvider (org.eclipse.jdt.core.manipulation.CoreASTProvider)2 ProblemLocationCore (org.eclipse.jdt.internal.ui.text.correction.ProblemLocationCore)2 ChangeUtil (org.eclipse.jdt.ls.core.internal.ChangeUtil)2 JDTUtils (org.eclipse.jdt.ls.core.internal.JDTUtils)2 JavaCodeActionKind (org.eclipse.jdt.ls.core.internal.JavaCodeActionKind)2