Search in sources :

Example 1 with CodeActionOptions

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

the class JDTLanguageServer method getCodeActionOptions.

private CodeActionOptions getCodeActionOptions() {
    String[] kinds = { CodeActionKind.QuickFix, CodeActionKind.Refactor, CodeActionKind.RefactorExtract, CodeActionKind.RefactorInline, CodeActionKind.RefactorRewrite, CodeActionKind.Source, CodeActionKind.SourceOrganizeImports };
    List<String> codeActionKinds = new ArrayList<>();
    for (String kind : kinds) {
        if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) {
            codeActionKinds.add(kind);
        }
    }
    CodeActionOptions options = new CodeActionOptions(codeActionKinds);
    options.setResolveProvider(Boolean.valueOf(preferenceManager.getClientPreferences().isResolveCodeActionSupported()));
    return options;
}
Also used : CodeActionOptions(org.eclipse.lsp4j.CodeActionOptions) ArrayList(java.util.ArrayList)

Example 2 with CodeActionOptions

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

the class InitHandler method registerCapabilities.

@Override
public void registerCapabilities(InitializeResult initializeResult) {
    ServerCapabilities capabilities = new ServerCapabilities();
    if (!preferenceManager.getClientPreferences().isCompletionDynamicRegistered()) {
        capabilities.setCompletionProvider(CompletionHandler.DEFAULT_COMPLETION_OPTIONS);
    }
    if (!preferenceManager.getClientPreferences().isFormattingDynamicRegistrationSupported()) {
        capabilities.setDocumentFormattingProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isRangeFormattingDynamicRegistrationSupported()) {
        capabilities.setDocumentRangeFormattingProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isOnTypeFormattingDynamicRegistrationSupported()) {
        capabilities.setDocumentOnTypeFormattingProvider(new DocumentOnTypeFormattingOptions(";", Arrays.asList("\n", "}")));
    }
    if (!preferenceManager.getClientPreferences().isCodeLensDynamicRegistrationSupported()) {
        capabilities.setCodeLensProvider(new CodeLensOptions(true));
    }
    if (!preferenceManager.getClientPreferences().isSignatureHelpDynamicRegistrationSupported()) {
        capabilities.setSignatureHelpProvider(SignatureHelpHandler.createOptions());
    }
    if (!preferenceManager.getClientPreferences().isRenameDynamicRegistrationSupported()) {
        capabilities.setRenameProvider(RenameHandler.createOptions());
    }
    if (!preferenceManager.getClientPreferences().isCodeActionDynamicRegistered()) {
        if (preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
            CodeActionOptions codeActionOptions = new CodeActionOptions();
            codeActionOptions.setResolveProvider(Boolean.TRUE);
            capabilities.setCodeActionProvider(codeActionOptions);
        } else {
            capabilities.setCodeActionProvider(Boolean.TRUE);
        }
    }
    if (!preferenceManager.getClientPreferences().isExecuteCommandDynamicRegistrationSupported()) {
        Set<String> commands = commandHandler.getAllCommands();
        if (!commands.isEmpty()) {
            capabilities.setExecuteCommandProvider(new ExecuteCommandOptions(new ArrayList<>(commands)));
        }
    } else {
        // Send static command at the startup - they remain registered all the time
        Set<String> staticCommands = commandHandler.getStaticCommands();
        if (!staticCommands.isEmpty()) {
            capabilities.setExecuteCommandProvider(new ExecuteCommandOptions(new ArrayList<>(staticCommands)));
        }
    }
    if (!preferenceManager.getClientPreferences().isWorkspaceSymbolDynamicRegistered()) {
        capabilities.setWorkspaceSymbolProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isClientDocumentSymbolProviderRegistered() && !preferenceManager.getClientPreferences().isDocumentSymbolDynamicRegistered()) {
        capabilities.setDocumentSymbolProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isDefinitionDynamicRegistered()) {
        capabilities.setDefinitionProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isTypeDefinitionDynamicRegistered()) {
        capabilities.setTypeDefinitionProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isClientHoverProviderRegistered() && !preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
        capabilities.setHoverProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isReferencesDynamicRegistered()) {
        capabilities.setReferencesProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isDocumentHighlightDynamicRegistered()) {
        capabilities.setDocumentHighlightProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isFoldgingRangeDynamicRegistered()) {
        capabilities.setFoldingRangeProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isImplementationDynamicRegistered()) {
        capabilities.setImplementationProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isSelectionRangeDynamicRegistered()) {
        capabilities.setSelectionRangeProvider(Boolean.TRUE);
    }
    capabilities.setCallHierarchyProvider(Boolean.TRUE);
    TextDocumentSyncOptions textDocumentSyncOptions = new TextDocumentSyncOptions();
    textDocumentSyncOptions.setOpenClose(Boolean.TRUE);
    textDocumentSyncOptions.setSave(new SaveOptions(Boolean.TRUE));
    textDocumentSyncOptions.setChange(TextDocumentSyncKind.Incremental);
    if (preferenceManager.getClientPreferences().isWillSaveRegistered()) {
        textDocumentSyncOptions.setWillSave(Boolean.TRUE);
    }
    if (preferenceManager.getClientPreferences().isWillSaveWaitUntilRegistered()) {
        textDocumentSyncOptions.setWillSaveWaitUntil(Boolean.TRUE);
    }
    capabilities.setTextDocumentSync(textDocumentSyncOptions);
    WorkspaceServerCapabilities wsCapabilities = new WorkspaceServerCapabilities();
    WorkspaceFoldersOptions wsFoldersOptions = new WorkspaceFoldersOptions();
    wsFoldersOptions.setSupported(Boolean.TRUE);
    wsFoldersOptions.setChangeNotifications(Boolean.TRUE);
    wsCapabilities.setWorkspaceFolders(wsFoldersOptions);
    capabilities.setWorkspace(wsCapabilities);
    SemanticTokensWithRegistrationOptions semanticTokensOptions = new SemanticTokensWithRegistrationOptions();
    semanticTokensOptions.setFull(new SemanticTokensServerFull(false));
    semanticTokensOptions.setRange(false);
    semanticTokensOptions.setDocumentSelector(List.of(new DocumentFilter("java", "file", null), new DocumentFilter("java", "jdt", null)));
    semanticTokensOptions.setLegend(SemanticTokensHandler.legend());
    capabilities.setSemanticTokensProvider(semanticTokensOptions);
    initializeResult.setCapabilities(capabilities);
}
Also used : TextDocumentSyncOptions(org.eclipse.lsp4j.TextDocumentSyncOptions) DocumentOnTypeFormattingOptions(org.eclipse.lsp4j.DocumentOnTypeFormattingOptions) ArrayList(java.util.ArrayList) DocumentFilter(org.eclipse.lsp4j.DocumentFilter) WorkspaceFoldersOptions(org.eclipse.lsp4j.WorkspaceFoldersOptions) WorkspaceServerCapabilities(org.eclipse.lsp4j.WorkspaceServerCapabilities) CodeActionOptions(org.eclipse.lsp4j.CodeActionOptions) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) WorkspaceServerCapabilities(org.eclipse.lsp4j.WorkspaceServerCapabilities) CodeLensOptions(org.eclipse.lsp4j.CodeLensOptions) SaveOptions(org.eclipse.lsp4j.SaveOptions) SemanticTokensServerFull(org.eclipse.lsp4j.SemanticTokensServerFull) ExecuteCommandOptions(org.eclipse.lsp4j.ExecuteCommandOptions) SemanticTokensWithRegistrationOptions(org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions)

Example 3 with CodeActionOptions

use of org.eclipse.lsp4j.CodeActionOptions in project lsp4e by eclipse.

the class LSPCodeActionMarkerResolution method checkMarkerResoultion.

private void checkMarkerResoultion(IMarker marker) throws IOException, CoreException, InterruptedException, ExecutionException, TimeoutException {
    if (marker.getAttribute(LSP_REMEDIATION) == null) {
        IResource res = marker.getResource();
        if (res != null && res.getType() == IResource.FILE) {
            IFile file = (IFile) res;
            String languageServerId = marker.getAttribute(LSPDiagnosticsToMarkers.LANGUAGE_SERVER_ID, null);
            List<CompletableFuture<LanguageServer>> languageServerFutures = new ArrayList<>();
            if (languageServerId != null) {
                // try to use same LS as the one that created the marker
                LanguageServerDefinition definition = LanguageServersRegistry.getInstance().getDefinition(languageServerId);
                if (definition != null) {
                    CompletableFuture<LanguageServer> serverFuture = LanguageServiceAccessor.getInitializedLanguageServer(file, definition, serverCapabilities -> serverCapabilities == null || providesCodeActions(serverCapabilities));
                    if (serverFuture != null) {
                        languageServerFutures.add(serverFuture);
                    }
                }
            }
            if (languageServerFutures.isEmpty()) {
                // if it's not there, try any other server
                languageServerFutures.addAll(LanguageServiceAccessor.getInitializedLanguageServers(file, capabilities -> {
                    Either<Boolean, CodeActionOptions> codeActionProvider = capabilities.getCodeActionProvider();
                    if (codeActionProvider == null) {
                        return false;
                    } else if (codeActionProvider.isLeft()) {
                        return Boolean.TRUE.equals(codeActionProvider.getLeft());
                    } else if (codeActionProvider.isRight()) {
                        return true;
                    }
                    return false;
                }));
            }
            List<CompletableFuture<?>> futures = new ArrayList<>();
            for (CompletableFuture<LanguageServer> lsf : languageServerFutures) {
                marker.setAttribute(LSP_REMEDIATION, COMPUTING);
                Diagnostic diagnostic = (Diagnostic) marker.getAttribute(MarkerAttributeComputer.LSP_DIAGNOSTIC);
                CodeActionContext context = new CodeActionContext(Collections.singletonList(diagnostic));
                CodeActionParams params = new CodeActionParams();
                params.setContext(context);
                params.setTextDocument(new TextDocumentIdentifier(LSPEclipseUtils.toUri(marker.getResource()).toString()));
                params.setRange(diagnostic.getRange());
                CompletableFuture<List<Either<Command, CodeAction>>> codeAction = lsf.thenComposeAsync(ls -> ls.getTextDocumentService().codeAction(params));
                futures.add(codeAction);
                codeAction.thenAcceptAsync(actions -> {
                    try {
                        marker.setAttribute(LSP_REMEDIATION, actions);
                    } catch (CoreException e) {
                        LanguageServerPlugin.logError(e);
                    }
                });
            }
            // wait a bit to avoid showing too much "Computing" without looking like a freeze
            CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).get(300, TimeUnit.MILLISECONDS);
        }
    }
}
Also used : TimeoutException(java.util.concurrent.TimeoutException) Image(org.eclipse.swt.graphics.Image) CompletableFuture(java.util.concurrent.CompletableFuture) CoreException(org.eclipse.core.runtime.CoreException) JFaceResources(org.eclipse.jface.resource.JFaceResources) Diagnostic(org.eclipse.lsp4j.Diagnostic) LanguageServer(org.eclipse.lsp4j.services.LanguageServer) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) LSPEclipseUtils(org.eclipse.lsp4e.LSPEclipseUtils) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IMarkerResolution2(org.eclipse.ui.IMarkerResolution2) LanguageServersRegistry(org.eclipse.lsp4e.LanguageServersRegistry) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) IFile(org.eclipse.core.resources.IFile) LSPDiagnosticsToMarkers(org.eclipse.lsp4e.operations.diagnostics.LSPDiagnosticsToMarkers) Messages(org.eclipse.lsp4e.ui.Messages) IMarker(org.eclipse.core.resources.IMarker) LanguageServerDefinition(org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition) CodeAction(org.eclipse.lsp4j.CodeAction) Collection(java.util.Collection) IMarkerResolutionGenerator2(org.eclipse.ui.IMarkerResolutionGenerator2) IOException(java.io.IOException) ProgressInfoItem(org.eclipse.ui.internal.progress.ProgressInfoItem) CodeActionOptions(org.eclipse.lsp4j.CodeActionOptions) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Command(org.eclipse.lsp4j.Command) IResource(org.eclipse.core.resources.IResource) MarkerAttributeComputer(org.eclipse.lsp4e.MarkerAttributeComputer) Collections(java.util.Collections) NonNull(org.eclipse.jdt.annotation.NonNull) LanguageServerPlugin(org.eclipse.lsp4e.LanguageServerPlugin) LanguageServiceAccessor(org.eclipse.lsp4e.LanguageServiceAccessor) IMarkerResolution(org.eclipse.ui.IMarkerResolution) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) LanguageServerDefinition(org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) IFile(org.eclipse.core.resources.IFile) CodeAction(org.eclipse.lsp4j.CodeAction) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) LanguageServer(org.eclipse.lsp4j.services.LanguageServer) CompletableFuture(java.util.concurrent.CompletableFuture) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ArrayList(java.util.ArrayList) List(java.util.List) IResource(org.eclipse.core.resources.IResource)

Example 4 with CodeActionOptions

use of org.eclipse.lsp4j.CodeActionOptions in project camel-language-server by camel-tooling.

the class CamelLanguageServer method createServerCapabilities.

private ServerCapabilities createServerCapabilities() {
    ServerCapabilities capabilities = new ServerCapabilities();
    capabilities.setTextDocumentSync(TextDocumentSyncKind.Full);
    capabilities.setCompletionProvider(new CompletionOptions(Boolean.TRUE, Arrays.asList(".", "?", "&", "\"", "=")));
    capabilities.setHoverProvider(Boolean.TRUE);
    capabilities.setDocumentSymbolProvider(Boolean.TRUE);
    capabilities.setReferencesProvider(Boolean.TRUE);
    capabilities.setDefinitionProvider(Boolean.TRUE);
    capabilities.setCodeActionProvider(new CodeActionOptions(Arrays.asList(CodeActionKind.QuickFix)));
    capabilities.setFoldingRangeProvider(Boolean.TRUE);
    return capabilities;
}
Also used : CompletionOptions(org.eclipse.lsp4j.CompletionOptions) CodeActionOptions(org.eclipse.lsp4j.CodeActionOptions) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities)

Example 5 with CodeActionOptions

use of org.eclipse.lsp4j.CodeActionOptions in project lxtk by lxtk-org.

the class CodeActionFeature method initialize.

@Override
void initialize(ServerCapabilities capabilities, List<DocumentFilter> documentSelector) {
    if (documentSelector == null)
        return;
    Either<Boolean, CodeActionOptions> capability = capabilities.getCodeActionProvider();
    if (capability == null || !(capability.isRight() || Boolean.TRUE.equals(capability.getLeft())))
        return;
    CodeActionRegistrationOptions registerOptions = new CodeActionRegistrationOptions();
    registerOptions.setDocumentSelector(documentSelector);
    CodeActionOptions options = capability.getRight();
    if (options != null) {
        registerOptions.setWorkDoneProgress(options.getWorkDoneProgress());
        registerOptions.setCodeActionKinds(options.getCodeActionKinds());
        registerOptions.setResolveProvider(options.getResolveProvider());
    }
    register(new Registration(UUID.randomUUID().toString(), METHOD, registerOptions));
}
Also used : CodeActionOptions(org.eclipse.lsp4j.CodeActionOptions) CodeActionRegistrationOptions(org.eclipse.lsp4j.CodeActionRegistrationOptions) Registration(org.eclipse.lsp4j.Registration)

Aggregations

CodeActionOptions (org.eclipse.lsp4j.CodeActionOptions)9 ServerCapabilities (org.eclipse.lsp4j.ServerCapabilities)5 ArrayList (java.util.ArrayList)4 CompletionOptions (org.eclipse.lsp4j.CompletionOptions)4 ExecuteCommandOptions (org.eclipse.lsp4j.ExecuteCommandOptions)4 List (java.util.List)3 SignatureHelpOptions (org.eclipse.lsp4j.SignatureHelpOptions)3 JsonObject (com.google.gson.JsonObject)2 Collections (java.util.Collections)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CodeAction (org.eclipse.lsp4j.CodeAction)2 InitializeResult (org.eclipse.lsp4j.InitializeResult)2 RenameOptions (org.eclipse.lsp4j.RenameOptions)2 WorkspaceFolder (org.eclipse.lsp4j.WorkspaceFolder)2 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)2 Objects (com.google.common.base.Objects)1 Optional (com.google.common.base.Optional)1 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1