Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/websocket Поддержка работы через websocket #2567

Merged
merged 12 commits into from
Oct 28, 2022
Merged
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ dependencies {

// spring
api("org.springframework.boot:spring-boot-starter")
api("org.springframework.boot:spring-boot-starter-websocket")
api("info.picocli:picocli-spring-boot-starter:4.6.3")

// lsp4j core
api("org.eclipse.lsp4j", "org.eclipse.lsp4j", "0.14.0")
api("org.eclipse.lsp4j", "org.eclipse.lsp4j", "0.17.0")
api("org.eclipse.lsp4j", "org.eclipse.lsp4j.websocket", "0.17.0")

// 1c-syntax
api("com.github.1c-syntax", "bsl-parser", "167aaad827322e09ccde4658a71152dad234de4b") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
import com.github._1c_syntax.bsl.languageserver.cli.FormatCommand;
import com.github._1c_syntax.bsl.languageserver.cli.LanguageServerStartCommand;
import com.github._1c_syntax.bsl.languageserver.cli.VersionCommand;
import com.github._1c_syntax.bsl.languageserver.cli.WebsocketCommand;
import com.github._1c_syntax.utils.CaseInsensitivePattern;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.stereotype.Component;
import picocli.CommandLine;
import picocli.CommandLine.Option;
Expand All @@ -54,7 +57,8 @@
AnalyzeCommand.class,
FormatCommand.class,
VersionCommand.class,
LanguageServerStartCommand.class
LanguageServerStartCommand.class,
WebsocketCommand.class
},
usageHelpAutoWidth = true,
synopsisSubcommandLabel = "[COMMAND [ARGS]]",
Expand Down Expand Up @@ -100,7 +104,9 @@ public class BSLLSPLauncher implements Callable<Integer>, CommandLineRunner, Exi
private int exitCode;

public static void main(String[] args) {
var applicationContext = SpringApplication.run(BSLLSPLauncher.class, args);
var applicationContext = new SpringApplicationBuilder(BSLLSPLauncher.class)
.web(getWebApplicationType(args))
.run(args);
var launcher = applicationContext.getBean(BSLLSPLauncher.class);
if (launcher.getExitCode() >= 0) {
System.exit(
Expand Down Expand Up @@ -158,4 +164,15 @@ public Integer call() {
// заглушка, командой как таковой не пользуемся
return 0;
}

private static WebApplicationType getWebApplicationType(String[] args) {
WebApplicationType webApplicationType;
var argsList = Arrays.asList(args);
if (argsList.contains("-w") || argsList.contains("--websocket")) {
webApplicationType = WebApplicationType.SERVLET;
} else {
webApplicationType = WebApplicationType.NONE;
}
return webApplicationType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
import org.eclipse.lsp4j.PrepareRenameParams;
import org.eclipse.lsp4j.PrepareRenameResult;
import org.eclipse.lsp4j.Range;
Expand All @@ -87,6 +88,7 @@
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -380,14 +382,14 @@ public CompletableFuture<Diagnostics> diagnostics(DiagnosticParams params) {
}

@Override
public CompletableFuture<Either<Range, PrepareRenameResult>> prepareRename(PrepareRenameParams params) {
public CompletableFuture<Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior>> prepareRename(PrepareRenameParams params) {
var documentContext = context.getDocument(params.getTextDocument().getUri());
if (documentContext == null) {
return CompletableFuture.completedFuture(null);
}

return CompletableFuture.supplyAsync(() ->
Either.forLeft(renameProvider.getPrepareRename(documentContext, params)));
Either3.forFirst(renameProvider.getPrepareRename(documentContext, params)));
otymko marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> 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.cli;

import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.Callable;

import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;

/**
* Запускает приложение в режиме Websocket Language Server
* Ключ команды:
* -w, (--websocket)
* Параметры:
* -c, (--configuration) &lt;arg&gt; - Путь к конфигурационному файлу BSL Language Server (.bsl-language-server.json).
* Возможно указывать как в абсолютном, так и относительном виде.
* Если параметр опущен, то будут использованы настройки по умолчанию.
* --server.port - Порт, на котором открывается соединение. Если параметр опущен,
otymko marked this conversation as resolved.
Show resolved Hide resolved
* то будет использован порт по умолчанию, а именно 8025.
* Выводимая информация:
* Данный режим используется для взаимодействия с клиентом по протоколу LSP через websocket
*/
@Slf4j
@Command(
name = "websocket",
aliases = {"-w", "--websocket"},
description = "Websocket server mode",
usageHelpAutoWidth = true,
footer = "@|green Copyright(c) 2018-2022|@")
@Component
@RequiredArgsConstructor
public class WebsocketCommand implements Callable<Integer> {
@Option(
names = {"-h", "--help"},
usageHelp = true,
description = "Show this help message and exit")
private boolean usageHelpRequested;

@Option(
names = {"-c", "--configuration"},
description = "Path to language server configuration file",
paramLabel = "<path>",
defaultValue = "")
private String configurationOption;

private final LanguageServerConfiguration configuration;

public Integer call() {
var configurationFile = new File(configurationOption);
if (configurationFile.exists()) {
configuration.update(configurationFile);
}

return -1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> 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.websocket;

import com.github._1c_syntax.bsl.languageserver.BSLLanguageServer;
import com.github._1c_syntax.bsl.languageserver.LanguageClientHolder;
import lombok.RequiredArgsConstructor;
import org.eclipse.lsp4j.jsonrpc.Launcher.Builder;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.websocket.WebSocketEndpoint;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
@RequiredArgsConstructor
public class BSLLSWebSocketEndpoint extends WebSocketEndpoint<LanguageClient> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Жабадок спасет мир=)


private final BSLLanguageServer languageServer;
private final LanguageClientHolder languageClientHolder;

@Override
protected void configure(Builder<LanguageClient> builder) {
builder.setLocalService(languageServer);
builder.setRemoteInterface(LanguageClient.class);
}

@Override
protected void connect(Collection<Object> localServices, LanguageClient remoteProxy) {
languageClientHolder.connect(remoteProxy);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> 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.websocket;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServerEndpointRegistration;

import javax.websocket.server.ServerEndpointConfig;

@Configuration
@ConditionalOnWebApplication
@EnableWebSocket
public class WebSocketConfiguration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Жабадок спасет мир


@Bean
public ServerEndpointConfig serverEndpointConfig() {
return new ServerEndpointRegistration("/bsl-language-server", BSLLSWebSocketEndpoint.class);
otymko marked this conversation as resolved.
Show resolved Hide resolved
}

@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> 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.
*/
/**
* Пакет предназначен для реализации работы BSL Language Server через websocket.
*/
package com.github._1c_syntax.bsl.languageserver.websocket;
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.main.web-application-type=none
server.port=8025
Copy link
Member

@otymko otymko Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это бы сразу указать в доке.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В джавадоке указано, в маркдаун еще перенести надо

spring.main.banner-mode=off
spring.main.log-startup-info=false
logging.level.org.springframework.boot.autoconfigure.logging=INFO
Expand Down
Loading