com.github._1c_syntax.bsl.languageserver.BSLLanguageServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bsl-language-server Show documentation
Show all versions of bsl-language-server Show documentation
Language Server Protocol implementation for 1C (BSL) - 1C:Enterprise 8 and OneScript languages.
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy , Nikita Fedkin and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* BSL Language Server is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* BSL Language Server 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BSL Language Server.
*/
package com.github._1c_syntax.bsl.languageserver;
import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.languageserver.jsonrpc.DiagnosticParams;
import com.github._1c_syntax.bsl.languageserver.jsonrpc.Diagnostics;
import com.github._1c_syntax.bsl.languageserver.jsonrpc.ProtocolExtension;
import com.github._1c_syntax.bsl.languageserver.providers.DocumentSymbolProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp4j.CallHierarchyRegistrationOptions;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.CodeActionOptions;
import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.ColorProviderOptions;
import org.eclipse.lsp4j.DefinitionOptions;
import org.eclipse.lsp4j.DocumentFormattingOptions;
import org.eclipse.lsp4j.DocumentLinkOptions;
import org.eclipse.lsp4j.DocumentRangeFormattingOptions;
import org.eclipse.lsp4j.DocumentSymbolOptions;
import org.eclipse.lsp4j.FoldingRangeProviderOptions;
import org.eclipse.lsp4j.HoverOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.ReferenceOptions;
import org.eclipse.lsp4j.RenameCapabilities;
import org.eclipse.lsp4j.RenameOptions;
import org.eclipse.lsp4j.SaveOptions;
import org.eclipse.lsp4j.SelectionRangeRegistrationOptions;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.ServerInfo;
import org.eclipse.lsp4j.TextDocumentClientCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.TextDocumentSyncOptions;
import org.eclipse.lsp4j.WorkspaceSymbolOptions;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.eclipse.lsp4j.services.WorkspaceService;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@Slf4j
@Component
@RequiredArgsConstructor
public class BSLLanguageServer implements LanguageServer, ProtocolExtension {
private final LanguageServerConfiguration configuration;
private final BSLTextDocumentService textDocumentService;
private final BSLWorkspaceService workspaceService;
private final ClientCapabilitiesHolder clientCapabilitiesHolder;
private final ServerContext context;
private final ServerInfo serverInfo;
private boolean shutdownWasCalled;
@Override
public CompletableFuture initialize(InitializeParams params) {
clientCapabilitiesHolder.setCapabilities(params.getCapabilities());
setConfigurationRoot(params);
CompletableFuture.runAsync(context::populateContext);
var capabilities = new ServerCapabilities();
capabilities.setTextDocumentSync(getTextDocumentSyncOptions());
capabilities.setDocumentRangeFormattingProvider(getDocumentRangeFormattingProvider());
capabilities.setDocumentFormattingProvider(getDocumentFormattingProvider());
capabilities.setFoldingRangeProvider(getFoldingRangeProvider());
capabilities.setDocumentSymbolProvider(getDocumentSymbolProvider());
capabilities.setCodeActionProvider(getCodeActionProvider());
capabilities.setCodeLensProvider(getCodeLensProvider());
capabilities.setDocumentLinkProvider(getDocumentLinkProvider());
capabilities.setWorkspaceSymbolProvider(getWorkspaceProvider());
capabilities.setHoverProvider(getHoverProvider());
capabilities.setReferencesProvider(getReferencesProvider());
capabilities.setDefinitionProvider(getDefinitionProvider());
capabilities.setCallHierarchyProvider(getCallHierarchyProvider());
capabilities.setSelectionRangeProvider(getSelectionRangeProvider());
capabilities.setColorProvider(getColorProvider());
capabilities.setRenameProvider(getRenameProvider(params));
var result = new InitializeResult(capabilities, serverInfo);
return CompletableFuture.completedFuture(result);
}
private void setConfigurationRoot(InitializeParams params) {
var workspaceFolders = params.getWorkspaceFolders();
if (workspaceFolders == null || workspaceFolders.isEmpty()) {
return;
}
String rootUri = workspaceFolders.get(0).getUri();
Path rootPath;
try {
rootPath = new File(new URI(rootUri).getPath()).getCanonicalFile().toPath();
} catch (URISyntaxException | IOException e) {
LOGGER.error("Can't read root URI from initialization params.", e);
return;
}
Path configurationRoot = LanguageServerConfiguration.getCustomConfigurationRoot(
configuration,
rootPath);
context.setConfigurationRoot(configurationRoot);
}
@Override
public CompletableFuture