Search in sources :

Example 1 with LanguageServerDefinition

use of org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition 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 2 with LanguageServerDefinition

use of org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition in project lsp4e by eclipse.

the class CommandExecutor method executeCommandServerSide.

private static CompletableFuture<Object> executeCommandServerSide(@NonNull Command command, @Nullable String languageServerId, @Nullable IDocument document) {
    @Nullable LanguageServerDefinition languageServerDefinition = languageServerId == null ? null : LanguageServersRegistry.getInstance().getDefinition(languageServerId);
    try {
        CompletableFuture<LanguageServer> languageServerFuture = getLanguageServerForCommand(command, document, languageServerDefinition);
        if (languageServerFuture == null) {
            return null;
        }
        // Server can handle command
        return languageServerFuture.thenApplyAsync(server -> {
            ExecuteCommandParams params = new ExecuteCommandParams();
            params.setCommand(command.getCommand());
            params.setArguments(command.getArguments());
            return server.getWorkspaceService().executeCommand(params);
        });
    } catch (IOException e) {
        // log and let the code fall through for LSPEclipseUtils to handle
        LanguageServerPlugin.logError(e);
        return null;
    }
}
Also used : LanguageServer(org.eclipse.lsp4j.services.LanguageServer) LanguageServerDefinition(org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) IOException(java.io.IOException) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with LanguageServerDefinition

use of org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition in project lsp4e by eclipse.

the class IncompleteCompletionTest method testAssistForUnknownButConnectedType.

/*
	 * This tests the not-so-official way to associate a LS to a file programmatically, and then to retrieve the LS
	 * for the file independently of the content-types. Although doing it programatically isn't recommended, consuming
	 * file-specific LS already associated is something we want to support.
	 */
@Test
public void testAssistForUnknownButConnectedType() throws CoreException, InvocationTargetException, IOException, InterruptedException {
    List<CompletionItem> items = new ArrayList<>();
    items.add(createCompletionItem("FirstClass", CompletionItemKind.Class));
    MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(true, items));
    IFile testFile = TestUtils.createUniqueTestFileOfUnknownType(project, "");
    ITextViewer viewer = TestUtils.openTextViewer(testFile);
    LanguageServerDefinition serverDefinition = LanguageServersRegistry.getInstance().getDefinition("org.eclipse.lsp4e.test.server");
    assertNotNull(serverDefinition);
    LanguageServerWrapper lsWrapperForConnection = LanguageServiceAccessor.getLSWrapperForConnection(testFile.getProject(), serverDefinition);
    URI fileLocation = testFile.getLocationURI();
    // force connection (that's what LSP4E should be designed to prevent 3rd party from having to use it).
    lsWrapperForConnection.connect(testFile, null);
    new DisplayHelper() {

        @Override
        protected boolean condition() {
            return lsWrapperForConnection.isConnectedTo(fileLocation);
        }
    }.waitForCondition(Display.getCurrent(), 3000);
    ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, 0);
    assertEquals(items.size(), proposals.length);
    // TODO compare both structures
    LSCompletionProposal LSIncompleteCompletionProposal = (LSCompletionProposal) proposals[0];
    LSIncompleteCompletionProposal.apply(viewer.getDocument());
    assertEquals(new Point("FirstClass".length(), 0), LSIncompleteCompletionProposal.getSelection(viewer.getDocument()));
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) LSCompletionProposal(org.eclipse.lsp4e.operations.completion.LSCompletionProposal) LanguageServerDefinition(org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) URI(java.net.URI) ITextViewer(org.eclipse.jface.text.ITextViewer) CompletionItem(org.eclipse.lsp4j.CompletionItem) LanguageServerWrapper(org.eclipse.lsp4e.LanguageServerWrapper) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) DisplayHelper(org.eclipse.ui.tests.harness.util.DisplayHelper) Test(org.junit.Test)

Example 4 with LanguageServerDefinition

use of org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition in project jbosstools-quarkus by jbosstools.

the class SchemaRegistry method sendInitialize.

private void sendInitialize() {
    List<LanguageServer> servers = LanguageServiceAccessor.getLanguageServers(null, null, true);
    for (LanguageServer server : servers) {
        Optional<LanguageServerDefinition> definition = LanguageServiceAccessor.resolveServerDefinition(server);
        if (definition.isPresent()) {
            if ("org.eclipse.wildwebdeveloper.yaml".equals(definition.get().id)) {
                Map<String, Object> prefs = new HashMap<>();
                prefs.put("schemas", schemas2YAMLLSMap());
                prefs.put("completion", true);
                prefs.put("hover", true);
                prefs.put("validate", true);
                DidChangeConfigurationParams params = new DidChangeConfigurationParams(Collections.singletonMap("yaml", prefs));
                server.getWorkspaceService().didChangeConfiguration(params);
            }
        }
    }
}
Also used : LanguageServer(org.eclipse.lsp4j.services.LanguageServer) LanguageServerDefinition(org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DidChangeConfigurationParams(org.eclipse.lsp4j.DidChangeConfigurationParams)

Example 5 with LanguageServerDefinition

use of org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition in project lsp4e by eclipse.

the class LanguageServiceAccessor method getLSWrappers.

@NonNull
private static Collection<LanguageServerWrapper> getLSWrappers(@NonNull IDocument document) {
    LinkedHashSet<LanguageServerWrapper> res = new LinkedHashSet<>();
    IFile file = LSPEclipseUtils.getFile(document);
    URI uri = LSPEclipseUtils.toUri(document);
    if (uri == null) {
        return Collections.emptyList();
    }
    IPath path = new Path(uri.getPath());
    // look for running language servers via content-type
    Queue<IContentType> contentTypes = new LinkedList<>();
    Set<IContentType> processedContentTypes = new HashSet<>();
    contentTypes.addAll(LSPEclipseUtils.getDocumentContentTypes(document));
    synchronized (startedServers) {
        // already started compatible servers that fit request
        res.addAll(startedServers.stream().filter(wrapper -> {
            try {
                return wrapper.isConnectedTo(uri) || LanguageServersRegistry.getInstance().matches(document, wrapper.serverDefinition);
            } catch (Exception e) {
                LanguageServerPlugin.logError(e);
                return false;
            }
        }).filter(wrapper -> wrapper.canOperate(document)).collect(Collectors.toList()));
        while (!contentTypes.isEmpty()) {
            IContentType contentType = contentTypes.poll();
            if (contentType == null || processedContentTypes.contains(contentType)) {
                continue;
            }
            for (ContentTypeToLanguageServerDefinition mapping : LanguageServersRegistry.getInstance().findProviderFor(contentType)) {
                if (mapping == null || !mapping.isEnabled()) {
                    continue;
                }
                LanguageServerDefinition serverDefinition = mapping.getValue();
                if (serverDefinition == null) {
                    continue;
                }
                if (startedServers.stream().anyMatch(wrapper -> wrapper.serverDefinition.equals(serverDefinition) && wrapper.canOperate(document))) {
                    // we already checked a compatible LS with this definition
                    continue;
                }
                final IProject fileProject = file != null ? file.getProject() : null;
                LanguageServerWrapper wrapper = fileProject != null ? new LanguageServerWrapper(fileProject, serverDefinition) : new LanguageServerWrapper(serverDefinition, path);
                startedServers.add(wrapper);
                res.add(wrapper);
            }
            if (contentType.getBaseType() != null) {
                contentTypes.add(contentType.getBaseType());
            }
            processedContentTypes.add(contentType);
        }
        return res;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) StreamConnectionProvider(org.eclipse.lsp4e.server.StreamConnectionProvider) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CoreException(org.eclipse.core.runtime.CoreException) LanguageServer(org.eclipse.lsp4j.services.LanguageServer) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IDocument(org.eclipse.jface.text.IDocument) IProject(org.eclipse.core.resources.IProject) IPath(org.eclipse.core.runtime.IPath) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) IFile(org.eclipse.core.resources.IFile) URI(java.net.URI) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) LanguageServerDefinition(org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition) FileEditorInput(org.eclipse.ui.part.FileEditorInput) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IContentType(org.eclipse.core.runtime.content.IContentType) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Path(org.eclipse.core.runtime.Path) IEditorReference(org.eclipse.ui.IEditorReference) Optional(java.util.Optional) Queue(java.util.Queue) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) Collections(java.util.Collections) NonNull(org.eclipse.jdt.annotation.NonNull) LanguageServerDefinition(org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IContentType(org.eclipse.core.runtime.content.IContentType) URI(java.net.URI) LinkedList(java.util.LinkedList) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IProject(org.eclipse.core.resources.IProject) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NonNull(org.eclipse.jdt.annotation.NonNull)

Aggregations

LanguageServerDefinition (org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition)7 IFile (org.eclipse.core.resources.IFile)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 URI (java.net.URI)3 CoreException (org.eclipse.core.runtime.CoreException)3 LanguageServer (org.eclipse.lsp4j.services.LanguageServer)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)2 IContentType (org.eclipse.core.runtime.content.IContentType)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 ITextViewer (org.eclipse.jface.text.ITextViewer)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 LanguageServerWrapper (org.eclipse.lsp4e.LanguageServerWrapper)2