Skip to content

Commit

Permalink
CommandProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 committed Dec 5, 2022
1 parent 540e8f7 commit 12499ed
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Perfomance measurement - [SSL 3.1](../bench/index.html)
| [didChangeConfiguration](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeConfiguration) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | with restrictions, see [#1431](https://github.com/1c-syntax/bsl-language-server/issues/1431) |
| [didChangeWatchedFiles](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeWatchedFiles) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
| [symbol](https://microsoft.github.io/language-server-protocol/specification#workspace_symbol) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
| [executeCommand](https://microsoft.github.io/language-server-protocol/specification#workspace_executeCommand) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
| [executeCommand](https://microsoft.github.io/language-server-protocol/specification#workspace_executeCommand) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
| [applyEdit](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
| [willCreateFiles](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_willCreateFiles) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
| [didChangeConfiguration](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeConfiguration) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | с ограничениями, см. [#1431](https://github.com/1c-syntax/bsl-language-server/issues/1431) |
| [didChangeWatchedFiles](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeWatchedFiles) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
| [symbol](https://microsoft.github.io/language-server-protocol/specification#workspace_symbol) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
| [executeCommand](https://microsoft.github.io/language-server-protocol/specification#workspace_executeCommand) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
| [executeCommand](https://microsoft.github.io/language-server-protocol/specification#workspace_executeCommand) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
| [applyEdit](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
| [willCreateFiles](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_willCreateFiles) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.CommandProvider;
import com.github._1c_syntax.bsl.languageserver.providers.DocumentSymbolProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -40,6 +41,7 @@
import org.eclipse.lsp4j.DocumentLinkOptions;
import org.eclipse.lsp4j.DocumentRangeFormattingOptions;
import org.eclipse.lsp4j.DocumentSymbolOptions;
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.FoldingRangeProviderOptions;
import org.eclipse.lsp4j.HoverOptions;
import org.eclipse.lsp4j.InitializeParams;
Expand Down Expand Up @@ -78,6 +80,7 @@ public class BSLLanguageServer implements LanguageServer, ProtocolExtension {
private final LanguageServerConfiguration configuration;
private final BSLTextDocumentService textDocumentService;
private final BSLWorkspaceService workspaceService;
private final CommandProvider commandProvider;
private final ClientCapabilitiesHolder clientCapabilitiesHolder;
private final ServerContext context;
private final ServerInfo serverInfo;
Expand Down Expand Up @@ -108,6 +111,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
capabilities.setSelectionRangeProvider(getSelectionRangeProvider());
capabilities.setColorProvider(getColorProvider());
capabilities.setRenameProvider(getRenameProvider(params));
capabilities.setExecuteCommandProvider(getExecuteCommandProvider());

var result = new InitializeResult(capabilities, serverInfo);

Expand Down Expand Up @@ -308,4 +312,10 @@ private static Boolean getRenamePrepareSupport(InitializeParams params) {
.orElse(false);
}

private ExecuteCommandOptions getExecuteCommandProvider() {
var executeCommandOptions = new ExecuteCommandOptions();
executeCommandOptions.setCommands(commandProvider.getCommandIds());
executeCommandOptions.setWorkDoneProgress(Boolean.FALSE);
return executeCommandOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
package com.github._1c_syntax.bsl.languageserver;

import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.providers.CommandProvider;
import com.github._1c_syntax.bsl.languageserver.providers.SymbolProvider;
import lombok.RequiredArgsConstructor;
import org.apache.commons.beanutils.PropertyUtils;
import org.eclipse.lsp4j.DidChangeConfigurationParams;
import org.eclipse.lsp4j.DidChangeWatchedFilesParams;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.WorkspaceSymbolParams;
Expand All @@ -43,6 +45,7 @@
public class BSLWorkspaceService implements WorkspaceService {

private final LanguageServerConfiguration configuration;
private final CommandProvider commandProvider;
private final SymbolProvider symbolProvider;

@Override
Expand All @@ -64,4 +67,11 @@ public void didChangeConfiguration(DidChangeConfigurationParams params) {
public void didChangeWatchedFiles(DidChangeWatchedFilesParams params) {
// no-op
}

@Override
public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
var arguments = commandProvider.extractArguments(params);

return CompletableFuture.supplyAsync(() -> commandProvider.executeCommand(arguments));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
package com.github._1c_syntax.bsl.languageserver.codelenses;

import com.github._1c_syntax.bsl.languageserver.codelenses.databind.URITypeAdapter;
import com.github._1c_syntax.bsl.languageserver.databind.URITypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import lombok.Value;
import lombok.experimental.NonFinal;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> 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.commands;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import java.net.URI;

import static com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY;
import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = NAME, include = EXISTING_PROPERTY, property = "id", visible = true)
public interface CommandArguments {
URI getUri();
String getId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> 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.commands;

import org.eclipse.lsp4j.Command;

import java.beans.Introspector;
import java.util.Optional;

public interface CommandSupplier<T extends CommandArguments> {

default String getId() {
String simpleName = getClass().getSimpleName();
if (simpleName.endsWith("CommandSupplier")) {
simpleName = simpleName.substring(0, simpleName.length() - "CommandSupplier".length());
simpleName = Introspector.decapitalize(simpleName);
}

return simpleName;
}

default Command createCommand(String title) {
return new Command(title, getId());
}

Class<T> getCommandArgumentsClass();

Optional<Object> execute(T arguments);

default boolean refreshCodeLensesAfterExecuteCommand() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@
* You should have received a copy of the GNU Lesser General Public
* License along with BSL Language Server.
*/
/**
* Сериализация и десериализация классов пакета
* {@link com.github._1c_syntax.bsl.languageserver.codelenses}.
*/
@ParametersAreNonnullByDefault
package com.github._1c_syntax.bsl.languageserver.codelenses.databind;
package com.github._1c_syntax.bsl.languageserver.commands;

import com.github._1c_syntax.bsl.languageserver.databind.URITypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import lombok.Value;
import lombok.experimental.NonFinal;

import java.net.URI;

@Value
@NonFinal
public class DefaultCommandArguments implements CommandArguments {
@JsonAdapter(URITypeAdapter.class)
URI uri;
String id;

import javax.annotation.ParametersAreNonnullByDefault;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> 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.commands.infrastructure;

import com.github._1c_syntax.bsl.languageserver.commands.CommandArguments;
import com.github._1c_syntax.bsl.languageserver.commands.CommandSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Spring-конфигурация для определения бинов
* пакета {@link com.github._1c_syntax.bsl.languageserver.commands}.
*/
@Configuration
public class CommandsConfiguration {

@Bean
@SuppressWarnings("unchecked")
public Map<String, CommandSupplier<CommandArguments>> commandSuppliersById(
Collection<CommandSupplier<? extends CommandArguments>> codeLensSuppliers
) {
return codeLensSuppliers.stream()
.map(commandSupplier -> (CommandSupplier<CommandArguments>) commandSupplier)
.collect(Collectors.toMap(CommandSupplier::getId, Function.identity()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,51 @@
* 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.codelenses.databind;
package com.github._1c_syntax.bsl.languageserver.databind;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.github._1c_syntax.bsl.languageserver.codelenses.CodeLensData;
import com.github._1c_syntax.bsl.languageserver.codelenses.CodeLensSupplier;
import org.springframework.stereotype.Component;
import com.github._1c_syntax.bsl.languageserver.commands.CommandArguments;
import com.github._1c_syntax.bsl.languageserver.commands.CommandSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Преднастроенный экземпляр {@link ObjectMapper} для десериализации {@link CodeLensData}.
*/
@Component
public class CodeLensDataObjectMapper extends ObjectMapper {

private static final long serialVersionUID = 8904131809077953315L;
@Configuration
public class ObjectMapperConfiguration {

public CodeLensDataObjectMapper(List<CodeLensSupplier<? extends CodeLensData>> codeLensResolvers) {
super();
@Bean
public ObjectMapper objectMapper(
List<CodeLensSupplier<? extends CodeLensData>> codeLensResolvers,
List<CommandSupplier<? extends CommandArguments>> commandSuppliers
) {

var namedTypes = new ArrayList<NamedType>();
codeLensResolvers.stream()
.map(CodeLensDataObjectMapper::toNamedType)
.forEach(this::registerSubtypes);
.map(ObjectMapperConfiguration::toNamedType)
.collect(Collectors.toCollection(() -> namedTypes));
commandSuppliers.stream()
.map(ObjectMapperConfiguration::toNamedType)
.collect(Collectors.toCollection(() -> namedTypes));

var objectMapperBuilder = JsonMapper.builder();

namedTypes.forEach(objectMapperBuilder::registerSubtypes);

return objectMapperBuilder.build();
}

private static NamedType toNamedType(CodeLensSupplier<? extends CodeLensData> codeLensSupplier) {
return new NamedType(codeLensSupplier.getCodeLensDataClass(), codeLensSupplier.getId());
}

private static NamedType toNamedType(CommandSupplier<? extends CommandArguments> commandSupplier) {
return new NamedType(commandSupplier.getCommandArgumentsClass(), commandSupplier.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* 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.codelenses.databind;
package com.github._1c_syntax.bsl.languageserver.databind;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
Expand Down
Loading

0 comments on commit 12499ed

Please sign in to comment.