Skip to content

Commit

Permalink
Merge pull request #3035 from 1c-syntax/feature/inlayHintsApi
Browse files Browse the repository at this point in the history
inlayHints api
  • Loading branch information
nixel2007 authored Apr 14, 2023
2 parents 5030e73 + af8e0b1 commit bffba9c
Show file tree
Hide file tree
Showing 18 changed files with 674 additions and 24 deletions.
50 changes: 26 additions & 24 deletions docs/features/ConfigurationFile.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
| [semanticTokens](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
| [linkedEditingRange](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_linkedEditingRange) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
| [moniker](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_moniker) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
| [inlayHint](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | resolveProvider = false | да |
| [inlayHint/resolve](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#inlayHint_resolve) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
| [inlayHint/refresh](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_inlayHint_refresh) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | | |

<a id="cli"></a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.lsp4j.HoverOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.InlayHintRegistrationOptions;
import org.eclipse.lsp4j.ReferenceOptions;
import org.eclipse.lsp4j.RenameCapabilities;
import org.eclipse.lsp4j.RenameOptions;
Expand Down Expand Up @@ -111,6 +112,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
capabilities.setSelectionRangeProvider(getSelectionRangeProvider());
capabilities.setColorProvider(getColorProvider());
capabilities.setRenameProvider(getRenameProvider(params));
capabilities.setInlayHintProvider(getInlayHintProvider());
capabilities.setExecuteCommandProvider(getExecuteCommandProvider());

var result = new InitializeResult(capabilities, serverInfo);
Expand Down Expand Up @@ -312,6 +314,13 @@ private static Boolean getRenamePrepareSupport(InitializeParams params) {
.orElse(false);
}

private static InlayHintRegistrationOptions getInlayHintProvider() {
var inlayHintOptions = new InlayHintRegistrationOptions();
inlayHintOptions.setResolveProvider(Boolean.FALSE);
inlayHintOptions.setWorkDoneProgress(Boolean.FALSE);
return inlayHintOptions;
}

private ExecuteCommandOptions getExecuteCommandProvider() {
var executeCommandOptions = new ExecuteCommandOptions();
executeCommandOptions.setCommands(commandProvider.getCommandIds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.github._1c_syntax.bsl.languageserver.providers.FoldingRangeProvider;
import com.github._1c_syntax.bsl.languageserver.providers.FormatProvider;
import com.github._1c_syntax.bsl.languageserver.providers.HoverProvider;
import com.github._1c_syntax.bsl.languageserver.providers.InlayHintProvider;
import com.github._1c_syntax.bsl.languageserver.providers.ReferencesProvider;
import com.github._1c_syntax.bsl.languageserver.providers.RenameProvider;
import com.github._1c_syntax.bsl.languageserver.providers.SelectionRangeProvider;
Expand Down Expand Up @@ -74,6 +75,8 @@
import org.eclipse.lsp4j.FoldingRangeRequestParams;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.InlayHint;
import org.eclipse.lsp4j.InlayHintParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
Expand Down Expand Up @@ -118,6 +121,7 @@ public class BSLTextDocumentService implements TextDocumentService, ProtocolExte
private final SelectionRangeProvider selectionRangeProvider;
private final ColorProvider colorProvider;
private final RenameProvider renameProvider;
private final InlayHintProvider inlayHintProvider;

@Override
public CompletableFuture<Hover> hover(HoverParams params) {
Expand Down Expand Up @@ -304,6 +308,16 @@ public CompletableFuture<List<ColorPresentation>> colorPresentation(ColorPresent
return CompletableFuture.supplyAsync(() -> colorProvider.getColorPresentation(documentContext, params));
}

@Override
public CompletableFuture<List<InlayHint>> inlayHint(InlayHintParams params) {
var documentContext = context.getDocument(params.getTextDocument().getUri());
if (documentContext == null) {
return CompletableFuture.completedFuture(Collections.emptyList());
}

return CompletableFuture.supplyAsync(() -> inlayHintProvider.getInlayHint(documentContext, params));
}

@Override
public void didOpen(DidOpenTextDocumentParams params) {
var textDocumentItem = params.getTextDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ default Command createCommand(String title) {
*/
Optional<Object> execute(T arguments);

/**
* Флаг, показывающий необходимость обновить inlay hints после выполнения команды.
*
* @return Флаг, показывающий необходимость обновить inlay hints после выполнения команды.
*/
default boolean needRefreshInlayHintsAfterExecuteCommand() {
return false;
}

/**
* Флаг, показывающий необходимость обновить линзы после выполнения команды.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.DiagnosticsOptions;
import com.github._1c_syntax.bsl.languageserver.configuration.documentlink.DocumentLinkOptions;
import com.github._1c_syntax.bsl.languageserver.configuration.formating.FormattingOptions;
import com.github._1c_syntax.bsl.languageserver.configuration.inlayhints.InlayHintOptions;
import com.github._1c_syntax.utils.Absolute;
import edu.umd.cs.findbugs.annotations.Nullable;
import lombok.AccessLevel;
Expand Down Expand Up @@ -90,6 +91,10 @@ public class LanguageServerConfiguration {
@Setter(value = AccessLevel.NONE)
private DocumentLinkOptions documentLinkOptions = new DocumentLinkOptions();

@JsonProperty("inlayHint")
@Setter(value = AccessLevel.NONE)
private InlayHintOptions inlayHintOptions = new InlayHintOptions();

@JsonProperty("formatting")
@Setter(value = AccessLevel.NONE)
private FormattingOptions formattingOptions = new FormattingOptions();
Expand Down Expand Up @@ -217,6 +222,7 @@ private void loadConfigurationFile(File configurationFile) {
private void copyPropertiesFrom(LanguageServerConfiguration configuration) {
// todo: refactor
PropertyUtils.copyProperties(this, configuration);
PropertyUtils.copyProperties(this.inlayHintOptions, configuration.inlayHintOptions);
PropertyUtils.copyProperties(this.codeLensOptions, configuration.codeLensOptions);
PropertyUtils.copyProperties(this.diagnosticsOptions, configuration.diagnosticsOptions);
PropertyUtils.copyProperties(this.documentLinkOptions, configuration.documentLinkOptions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2023
* 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.configuration.inlayhints;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.github._1c_syntax.bsl.languageserver.configuration.databind.ParametersDeserializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import java.util.HashMap;
import java.util.Map;

/**
* Корневой класс для настройки {@link com.github._1c_syntax.bsl.languageserver.providers.InlayHintProvider}
*/
@Data
@AllArgsConstructor(onConstructor = @__({@JsonCreator(mode = JsonCreator.Mode.DISABLED)}))
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class InlayHintOptions {

/**
* Параметры сапплаеров inlay hints.
*/
@JsonDeserialize(using = ParametersDeserializer.class)
private Map<String, Either<Boolean, Map<String, Object>>> parameters = new HashMap<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2023
* 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.
*/
/**
* Пакет содержит настройки {@link com.github._1c_syntax.bsl.languageserver.providers.InlayHintProvider}
*/
@DefaultAnnotation(NonNull.class)
package com.github._1c_syntax.bsl.languageserver.configuration.inlayhints;

import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2023
* 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.inlayhints;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import org.eclipse.lsp4j.InlayHint;
import org.eclipse.lsp4j.InlayHintParams;

import java.beans.Introspector;
import java.util.List;

/**
* Базовый интерфейс для наполнения {@link com.github._1c_syntax.bsl.languageserver.providers.InlayHintProvider}
* данными о доступных в документе inlay hints.
*/
public interface InlayHintSupplier {

String INLAY_HINT_SUPPLIER = "InlayHintSupplier";

/**
* Идентификатор сапплаера.
*
* @return Идентификатор сапплаера.
*/
default String getId() {
String simpleName = getClass().getSimpleName();
if (simpleName.endsWith(INLAY_HINT_SUPPLIER)) {
simpleName = simpleName.substring(0, simpleName.length() - INLAY_HINT_SUPPLIER.length());
simpleName = Introspector.decapitalize(simpleName);
}

return simpleName;
}

/**
* Получить inlay hints, доступные в документе.
*
* @param documentContext Контекст документа, для которого надо рассчитать inlay hints.
* @param params Параметры запроса.
* @return Список inlay hints в документе.
*/
List<InlayHint> getInlayHints(DocumentContext documentContext, InlayHintParams params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2023
* 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.inlayhints.infrastructure;

import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.inlayhints.InlayHintSupplier;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

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

/**
* Получить список активированных в данный момент сапплаеров inlay hints.
*
* @param configuration Конфигурация сервера.
* @param inlayHintSuppliers Список сапплаеров inlay hints в разрезе из идентификаторов.
* @return Список активированных в данный момент сапплаеров inlay hints.
*/
@Bean
@Scope(SCOPE_PROTOTYPE)
public List<InlayHintSupplier> enabledInlayHintSuppliers(
LanguageServerConfiguration configuration,
Collection<InlayHintSupplier> inlayHintSuppliers
) {
var parameters = configuration.getInlayHintOptions().getParameters();
return inlayHintSuppliers.stream()
.filter(supplier -> supplierIsEnabled(supplier.getId(), parameters))
.collect(Collectors.toList());
}

private static boolean supplierIsEnabled(
String supplierId,
Map<String, Either<Boolean, Map<String, Object>>> parameters
) {
var supplierConfig = parameters.getOrDefault(supplierId, Either.forLeft(true));
return supplierConfig.isRight() || supplierConfig.getLeft();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2023
* 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.
*/
/**
* Spring-специфичные классы для настройки внутренней инфраструктуры
* пакета {@link com.github._1c_syntax.bsl.languageserver.inlayhints}.
*/
@DefaultAnnotation(NonNull.class)
package com.github._1c_syntax.bsl.languageserver.inlayhints.infrastructure;

import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
Loading

0 comments on commit bffba9c

Please sign in to comment.