| /******************************************************************************* |
| * Copyright (c) 2016, 2017 Red Hat Inc. and others. |
| * This program and the accompanying materials are made |
| * available under the terms of the Eclipse Public License 2.0 |
| * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Mickael Istria (Red Hat Inc.) - initial implementation |
| * Miro Spoenemann (TypeFox) - extracted to separate file |
| *******************************************************************************/ |
| package org.eclipse.lsp4e; |
| |
| import java.util.Arrays; |
| import java.util.List; |
| import java.util.concurrent.CompletableFuture; |
| import java.util.stream.Collectors; |
| |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.core.runtime.jobs.Job; |
| import org.eclipse.lsp4e.operations.diagnostics.LSPDiagnosticsToMarkers; |
| import org.eclipse.lsp4e.ui.Messages; |
| import org.eclipse.lsp4j.ApplyWorkspaceEditParams; |
| import org.eclipse.lsp4j.ApplyWorkspaceEditResponse; |
| import org.eclipse.lsp4j.MessageActionItem; |
| import org.eclipse.lsp4j.MessageParams; |
| import org.eclipse.lsp4j.PublishDiagnosticsParams; |
| import org.eclipse.lsp4j.RegistrationParams; |
| import org.eclipse.lsp4j.ShowMessageRequestParams; |
| import org.eclipse.lsp4j.UnregistrationParams; |
| import org.eclipse.lsp4j.WorkspaceFolder; |
| import org.eclipse.lsp4j.services.LanguageClient; |
| import org.eclipse.lsp4j.services.LanguageServer; |
| |
| public class LanguageClientImpl implements LanguageClient { |
| |
| private LSPDiagnosticsToMarkers diagnosticHandler; |
| |
| private LanguageServer server; |
| private LanguageServerWrapper wrapper; |
| |
| public final void connect(LanguageServer server, LanguageServerWrapper wrapper) { |
| this.server = server; |
| this.wrapper = wrapper; |
| this.diagnosticHandler = new LSPDiagnosticsToMarkers(wrapper.serverDefinition.id); |
| } |
| |
| protected final LanguageServer getLanguageServer() { |
| return server; |
| } |
| |
| @Override |
| public void telemetryEvent(Object object) { |
| // TODO |
| } |
| |
| @Override |
| public final CompletableFuture<MessageActionItem> showMessageRequest(ShowMessageRequestParams requestParams) { |
| return ServerMessageHandler.showMessageRequest(wrapper, requestParams); |
| } |
| |
| @Override |
| public final void showMessage(MessageParams messageParams) { |
| ServerMessageHandler.showMessage(wrapper.serverDefinition.label, messageParams); |
| } |
| |
| @Override |
| public final void publishDiagnostics(PublishDiagnosticsParams diagnostics) { |
| this.diagnosticHandler.accept(diagnostics); |
| } |
| |
| @Override |
| public final void logMessage(MessageParams message) { |
| CompletableFuture.runAsync(() -> ServerMessageHandler.logMessage(wrapper, message)); |
| } |
| |
| @Override |
| public final CompletableFuture<ApplyWorkspaceEditResponse> applyEdit(ApplyWorkspaceEditParams params) { |
| return CompletableFuture.supplyAsync(() -> { |
| Job job = new Job(Messages.serverEdit) { |
| @Override |
| public IStatus run(IProgressMonitor monitor) { |
| LSPEclipseUtils.applyWorkspaceEdit(params.getEdit()); |
| return Status.OK_STATUS; |
| } |
| }; |
| job.schedule(); |
| try { |
| job.join(); |
| return new ApplyWorkspaceEditResponse(true); |
| } catch (InterruptedException e) { |
| LanguageServerPlugin.logError(e); |
| Thread.currentThread().interrupt(); |
| return new ApplyWorkspaceEditResponse(Boolean.FALSE); |
| } |
| }); |
| } |
| |
| @Override |
| public CompletableFuture<Void> registerCapability(RegistrationParams params) { |
| return CompletableFuture.runAsync(() -> wrapper.registerCapability(params)); |
| } |
| |
| @Override |
| public CompletableFuture<Void> unregisterCapability(UnregistrationParams params) { |
| return CompletableFuture.runAsync(() -> wrapper.unregisterCapability(params)); |
| } |
| |
| @Override |
| public CompletableFuture<List<WorkspaceFolder>> workspaceFolders() { |
| return CompletableFuture.completedFuture(Arrays.stream(ResourcesPlugin.getWorkspace().getRoot().getProjects()) // |
| .filter(IProject::isAccessible) // |
| .map(LSPEclipseUtils::toWorkspaceFolder) // |
| .collect(Collectors.toList())); |
| } |
| } |