Search in sources :

Example 56 with Command

use of org.eclipse.lsp4j.Command in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method createCodeActionsForImport.

private void createCodeActionsForImport(Diagnostic diagnostic, List<Command> commands) {
    String message = diagnostic.getMessage();
    int start = message.lastIndexOf(" ") + 1;
    int end = message.length() - 1;
    String typeString = message.substring(start, end);
    ArrayList<IDefinition> types = new ArrayList<>();
    for (ICompilationUnit unit : compilationUnits) {
        if (unit == null) {
            continue;
        }
        try {
            Collection<IDefinition> definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
            if (definitions == null) {
                continue;
            }
            for (IDefinition definition : definitions) {
                if (definition instanceof ITypeDefinition) {
                    ITypeDefinition typeDefinition = (ITypeDefinition) definition;
                    String baseName = typeDefinition.getBaseName();
                    if (typeDefinition.getQualifiedName().equals(baseName)) {
                        //this definition is top-level. no import required.
                        continue;
                    }
                    if (baseName.equals(typeString)) {
                        types.add(typeDefinition);
                    }
                }
            }
        } catch (Exception e) {
        //safe to ignore
        }
    }
    for (IDefinition definitionToImport : types) {
        Command command = createImportCommand(definitionToImport);
        if (command != null) {
            commands.add(command);
        }
    }
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) Command(org.eclipse.lsp4j.Command) ArrayList(java.util.ArrayList) IDefinition(org.apache.flex.compiler.definitions.IDefinition) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 57 with Command

use of org.eclipse.lsp4j.Command in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method codeAction.

/**
     * Can be used to "quick fix" an error or warning.
     */
@Override
public CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params) {
    List<? extends Diagnostic> diagnostics = params.getContext().getDiagnostics();
    TextDocumentIdentifier textDocument = params.getTextDocument();
    Path path = LanguageServerUtils.getPathFromLanguageServerURI(textDocument.getUri());
    if (path == null || !sourceByPath.containsKey(path)) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    ArrayList<Command> commands = new ArrayList<>();
    for (Diagnostic diagnostic : diagnostics) {
        //I don't know why this can be null
        String code = diagnostic.getCode();
        if (code == null) {
            continue;
        }
        switch(code) {
            case //AccessUndefinedPropertyProblem
            "1120":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
            case //UnknownTypeProblem
            "1046":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
            case //InaccessiblePropertyReferenceProblem
            "1178":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
            case //CallUndefinedMethodProblem
            "1180":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
        }
    }
    return CompletableFuture.completedFuture(commands);
}
Also used : Path(java.nio.file.Path) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Command(org.eclipse.lsp4j.Command) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic)

Example 58 with Command

use of org.eclipse.lsp4j.Command in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method addDefinitionAutoCompleteActionScript.

private void addDefinitionAutoCompleteActionScript(IDefinition definition, CompletionList result) {
    if (definition.getBaseName().startsWith(VECTOR_HIDDEN_PREFIX)) {
        return;
    }
    CompletionItem item = new CompletionItem();
    item.setKind(getDefinitionKind(definition));
    item.setDetail(getDefinitionDetail(definition));
    item.setLabel(definition.getBaseName());
    boolean isInPackage = !definition.getQualifiedName().equals(definition.getBaseName());
    if (isInPackage) {
        Command command = createImportCommand(definition);
        if (command != null) {
            item.setCommand(command);
        }
    }
    result.getItems().add(item);
}
Also used : CompletionItem(org.eclipse.lsp4j.CompletionItem) Command(org.eclipse.lsp4j.Command)

Example 59 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class DocumentLifeCycleHandler method handleOpen.

public void handleOpen(DidOpenTextDocumentParams params) {
    String uri = params.getTextDocument().getUri();
    ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
    if (unit == null || unit.getResource() == null) {
        return;
    }
    try {
        // checks if the underlying resource exists and refreshes to sync the newly created file.
        if (!unit.getResource().isAccessible()) {
            try {
                unit.getResource().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
                if (unit.getResource().exists()) {
                    IJavaElement parent = unit.getParent();
                    if (parent instanceof IPackageFragment) {
                        IPackageFragment pkg = (IPackageFragment) parent;
                        unit = pkg.createCompilationUnit(unit.getElementName(), unit.getSource(), true, new NullProgressMonitor());
                    }
                }
            } catch (CoreException e) {
            // ignored
            }
        }
        IProject project = unit.getResource().getProject();
        // Resources belonging to the default project can only report syntax errors, because the project classpath is incomplete
        boolean isDefaultProject = project.equals(JavaLanguageServerPlugin.getProjectsManager().getDefaultProject());
        if (isDefaultProject || !JDTUtils.isOnClassPath(unit)) {
            Severity severity = preferenceManager.getPreferences(project).getIncompleteClasspathSeverity();
            String msg;
            if (isDefaultProject) {
                msg = "Classpath is incomplete. Only syntax errors will be reported";
            } else {
                msg = unit.getElementName() + " isn't on the classpath. Only syntax errors will be reported";
            }
            JavaLanguageServerPlugin.logInfo(msg + " for " + uri);
            if (severity.compareTo(Preferences.Severity.ignore) > 0) {
                ActionableNotification ignoreIncompleteClasspath = new ActionableNotification().withSeverity(severity.toMessageType()).withMessage(msg).withCommands(Arrays.asList(new Command("More Information", "java.ignoreIncompleteClasspath.help", null), new Command("Don't Show Again", "java.ignoreIncompleteClasspath", null)));
                connection.sendActionableNotification(ignoreIncompleteClasspath);
            }
        }
        // DiagnosticsHandler problemRequestor = new DiagnosticsHandler(connection, unit.getResource(), reportOnlySyntaxErrors);
        unit.becomeWorkingCopy(new NullProgressMonitor());
        IBuffer buffer = unit.getBuffer();
        String newContent = params.getTextDocument().getText();
        if (buffer != null && !buffer.getContents().equals(newContent)) {
            buffer.setContents(newContent);
        }
        triggerValidation(unit);
        // see https://github.com/redhat-developer/vscode-java/issues/274
        checkPackageDeclaration(uri, unit);
    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("Error while opening document", e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) ActionableNotification(org.eclipse.jdt.ls.core.internal.ActionableNotification) Severity(org.eclipse.jdt.ls.core.internal.preferences.Preferences.Severity) IProject(org.eclipse.core.resources.IProject) IBuffer(org.eclipse.jdt.core.IBuffer) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command)

Example 60 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class ProjectsManager method fileChanged.

public void fileChanged(String uriString, CHANGE_TYPE changeType) {
    if (uriString == null) {
        return;
    }
    IResource resource = JDTUtils.findFile(uriString);
    if (resource == null) {
        return;
    }
    try {
        if (changeType == CHANGE_TYPE.DELETED) {
            resource = resource.getParent();
        }
        if (resource != null) {
            resource.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
        }
        if (isBuildFile(resource)) {
            FeatureStatus status = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus();
            switch(status) {
                case automatic:
                    updateProject(resource.getProject());
                    break;
                case disabled:
                    break;
                default:
                    if (client != null) {
                        String cmd = "java.projectConfiguration.status";
                        TextDocumentIdentifier uri = new TextDocumentIdentifier(uriString);
                        ActionableNotification updateProjectConfigurationNotification = new ActionableNotification().withSeverity(MessageType.Info).withMessage("A build file was modified. Do you want to synchronize the Java classpath/configuration?").withCommands(asList(new Command("Never", cmd, asList(uri, FeatureStatus.disabled)), new Command("Now", cmd, asList(uri, FeatureStatus.interactive)), new Command("Always", cmd, asList(uri, FeatureStatus.automatic))));
                        client.sendActionableNotification(updateProjectConfigurationNotification);
                    }
            }
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem refreshing workspace", e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CoreException(org.eclipse.core.runtime.CoreException) FeatureStatus(org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus) Command(org.eclipse.lsp4j.Command) ActionableNotification(org.eclipse.jdt.ls.core.internal.ActionableNotification) IResource(org.eclipse.core.resources.IResource)

Aggregations

Command (org.eclipse.lsp4j.Command)95 CodeAction (org.eclipse.lsp4j.CodeAction)58 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 Test (org.junit.Test)46 Range (org.eclipse.lsp4j.Range)36 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)35 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)26 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)23 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)22 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)21 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)17 Diagnostic (org.eclipse.lsp4j.Diagnostic)17 Arrays (java.util.Arrays)14 Collections (java.util.Collections)14 JDTUtils (org.eclipse.jdt.ls.core.internal.JDTUtils)13 CodeActionKind (org.eclipse.lsp4j.CodeActionKind)13 Optional (java.util.Optional)12