Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.tools.lsp.server;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.function.Supplier;
import java.util.logging.Level;
import org.graalvm.collections.Pair;
import org.graalvm.tools.api.lsp.LSPServerAccessor;
import org.graalvm.tools.lsp.exceptions.DiagnosticsNotification;
import org.graalvm.tools.lsp.exceptions.UnknownLanguageException;
import org.graalvm.tools.lsp.server.types.CodeAction;
import org.graalvm.tools.lsp.server.types.CodeActionParams;
import org.graalvm.tools.lsp.server.types.CodeLens;
import org.graalvm.tools.lsp.server.types.CodeLensOptions;
import org.graalvm.tools.lsp.server.types.CodeLensParams;
import org.graalvm.tools.lsp.server.types.CompletionItem;
import org.graalvm.tools.lsp.server.types.CompletionList;
import org.graalvm.tools.lsp.server.types.CompletionOptions;
import org.graalvm.tools.lsp.server.types.CompletionParams;
import org.graalvm.tools.lsp.server.types.Coverage;
import org.graalvm.tools.lsp.server.types.DidChangeTextDocumentParams;
import org.graalvm.tools.lsp.server.types.DidCloseTextDocumentParams;
import org.graalvm.tools.lsp.server.types.DidOpenTextDocumentParams;
import org.graalvm.tools.lsp.server.types.DidSaveTextDocumentParams;
import org.graalvm.tools.lsp.server.types.DocumentFormattingParams;
import org.graalvm.tools.lsp.server.types.DocumentHighlight;
import org.graalvm.tools.lsp.server.types.DocumentOnTypeFormattingParams;
import org.graalvm.tools.lsp.server.types.DocumentRangeFormattingParams;
import org.graalvm.tools.lsp.server.types.DocumentSymbolParams;
import org.graalvm.tools.lsp.server.types.ExecuteCommandOptions;
import org.graalvm.tools.lsp.server.types.ExecuteCommandParams;
import org.graalvm.tools.lsp.server.types.Hover;
import org.graalvm.tools.lsp.server.types.InitializeParams;
import org.graalvm.tools.lsp.server.types.InitializeResult;
import org.graalvm.tools.lsp.server.types.LanguageClient;
import org.graalvm.tools.lsp.server.types.LanguageServer;
import org.graalvm.tools.lsp.server.types.Location;
import org.graalvm.tools.lsp.server.types.MessageType;
import org.graalvm.tools.lsp.server.types.PublishDiagnosticsParams;
import org.graalvm.tools.lsp.server.types.ReferenceParams;
import org.graalvm.tools.lsp.server.types.RenameParams;
import org.graalvm.tools.lsp.server.types.ServerCapabilities;
import org.graalvm.tools.lsp.server.types.ShowMessageParams;
import org.graalvm.tools.lsp.server.types.SignatureHelp;
import org.graalvm.tools.lsp.server.types.SignatureHelpOptions;
import org.graalvm.tools.lsp.server.types.SymbolInformation;
import org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent;
import org.graalvm.tools.lsp.server.types.TextDocumentPositionParams;
import org.graalvm.tools.lsp.server.types.TextDocumentSyncKind;
import org.graalvm.tools.lsp.server.types.TextEdit;
import org.graalvm.tools.lsp.server.types.WorkspaceEdit;
import org.graalvm.tools.lsp.server.types.WorkspaceSymbolParams;
import com.oracle.truffle.api.source.Source;
import org.graalvm.shadowed.org.json.JSONObject;
/**
* A LSP4J {@link LanguageServer} implementation using TCP sockets as transportation layer for the
* JSON-RPC requests. It delegates all requests to {@link TruffleAdapter}.
*/
public final class LanguageServerImpl extends LanguageServer {
private static final String DRY_RUN = "dry_run";
private static final String GET_COVERAGE = "get_coverage";
private static final TextDocumentSyncKind TEXT_DOCUMENT_SYNC_KIND = TextDocumentSyncKind.Incremental;
private final TruffleAdapter truffleAdapter;
private final PrintWriter err;
private final PrintWriter info;
private LanguageClient client;
private final Map openedFileUri2LangId = new HashMap<>();
private ExecutorService clientConnectionExecutor;
private final Hover emptyHover = Hover.create(Collections.emptyList());
private final SignatureHelp emptySignatureHelp = SignatureHelp.create(Collections.emptyList(), null, null);
private ServerCapabilities serverCapabilities;
private LanguageServerImpl(TruffleAdapter adapter, PrintWriter info, PrintWriter err) {
this.truffleAdapter = adapter;
this.info = info;
this.err = err;
}
public static LanguageServerImpl create(TruffleAdapter adapter, PrintWriter info, PrintWriter err) {
return new LanguageServerImpl(adapter, info, err);
}
@Override
public CompletableFuture initialize(InitializeParams initializeParams) {
// TODO: Read params.getCapabilities();
ServerCapabilities capabilities = ServerCapabilities.create();
capabilities.setTextDocumentSync(TEXT_DOCUMENT_SYNC_KIND);
capabilities.setDocumentSymbolProvider(false);
capabilities.setWorkspaceSymbolProvider(false);
capabilities.setDefinitionProvider(false);
capabilities.setDocumentHighlightProvider(true);
capabilities.setCodeLensProvider(CodeLensOptions.create().setResolveProvider(false));
capabilities.setCompletionProvider(CompletionOptions.create().setResolveProvider(false));
capabilities.setCodeActionProvider(true);
capabilities.setSignatureHelpProvider(SignatureHelpOptions.create());
capabilities.setHoverProvider(true);
capabilities.setReferencesProvider(false);
List commands = new ArrayList<>(truffleAdapter.getExtensionCommandNames());
commands.add(DRY_RUN);
commands.add(GET_COVERAGE);
capabilities.setExecuteCommandProvider(ExecuteCommandOptions.create(commands));
truffleAdapter.initializeLSPServer(new LSPServerAccessor() {
public void sendCustomNotification(String method, Object params) {
client.sendCustomNotification(method, params);
}
public Map getOpenFileURI2LangId() {
return openedFileUri2LangId;
}
public Source getSource(URI uri) {
return truffleAdapter.getSource(uri);
}
});
this.serverCapabilities = capabilities;
// The workspace folder can be scanned here to gather static code information.
// Not used currently.
return CompletableFuture.completedFuture(InitializeResult.create(capabilities));
}
@Override
protected boolean supportsMethod(String method, JSONObject params) {
return DelegateServers.supportsMethod(method, params, serverCapabilities);
}
@Override
public CompletableFuture