-
Notifications
You must be signed in to change notification settings - Fork 121
Add configurable template function highlighting for semantic tokens #3714
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
Changes from 13 commits
7154dc6
9e98aa5
0c6881a
dfe9a0e
b564116
2d52ed8
e2dac8b
359b714
799e037
d81c817
f20814e
c003a24
e8e067d
bc8faac
8aec16e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2025 | ||
| * 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.configuration.semantictokens; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Предварительно разобранные паттерны функций-шаблонизаторов. | ||
| * <p> | ||
| * Структура: | ||
| * <ul> | ||
| * <li>localMethods: Set методов для локального вызова (без модуля)</li> | ||
| * <li>moduleMethodPairs: Map из имени модуля -> Set методов этого модуля</li> | ||
| * </ul> | ||
| * | ||
| * @param localMethods Методы для локального вызова (без указания модуля) | ||
| * @param moduleMethodPairs Методы с указанием модуля (модуль -> набор методов) | ||
| */ | ||
| public record ParsedStrTemplateMethods( | ||
| Set<String> localMethods, | ||
| Map<String, Set<String>> moduleMethodPairs | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2025 | ||
| * 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.configuration.semantictokens; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Настройки для семантических токенов. | ||
| * <p> | ||
| * Позволяет указать дополнительные функции-шаблонизаторы строк, | ||
| * аналогичные СтрШаблон/StrTemplate, для подсветки плейсхолдеров (%1, %2 и т.д.). | ||
| */ | ||
| @Data | ||
| @AllArgsConstructor(onConstructor = @__({@JsonCreator(mode = JsonCreator.Mode.DISABLED)})) | ||
| @NoArgsConstructor | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| public class SemanticTokensOptions { | ||
|
|
||
| /** | ||
| * Список паттернов "Модуль.Метод" для функций-шаблонизаторов строк. | ||
| * <p> | ||
| * Строки внутри вызовов этих функций будут подсвечиваться так же, | ||
| * как строки в СтрШаблон/StrTemplate (с выделением плейсхолдеров %1, %2 и т.д.). | ||
| * <p> | ||
| * Формат: "ИмяМодуля.ИмяМетода", например: | ||
nixel2007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * <ul> | ||
| * <li>"СтроковыеФункцииКлиентСервер.ПодставитьПараметрыВСтроку"</li> | ||
| * <li>"StringFunctionsClientServer.SubstituteParametersToString"</li> | ||
| * <li>"ПодставитьПараметрыВСтроку" - для локального вызова без указания модуля</li> | ||
| * </ul> | ||
| * <p> | ||
| * По умолчанию включает стандартные варианты из БСП. | ||
| */ | ||
| private List<String> strTemplateMethods = new ArrayList<>(List.of( | ||
| // Локальный вызов | ||
| "ПодставитьПараметрыВСтроку", | ||
| "SubstituteParametersToString", | ||
| // Стандартный модуль БСП | ||
| "СтроковыеФункцииКлиентСервер.ПодставитьПараметрыВСтроку", | ||
| // Английский вариант | ||
| "StringFunctionsClientServer.SubstituteParametersToString" | ||
| )); | ||
|
|
||
| /** | ||
| * Возвращает предварительно разобранные паттерны функций-шаблонизаторов. | ||
| * | ||
| * @return Разобранные паттерны для быстрого поиска | ||
| */ | ||
| @JsonIgnore | ||
| public ParsedStrTemplateMethods getParsedStrTemplateMethods() { | ||
| var localMethods = new HashSet<String>(); | ||
| var moduleMethodPairs = new HashMap<String, Set<String>>(); | ||
|
|
||
| for (var pattern : strTemplateMethods) { | ||
| if (pattern.isBlank()) { | ||
| continue; | ||
| } | ||
| var patternLower = pattern.toLowerCase(Locale.ENGLISH); | ||
|
|
||
| if (patternLower.contains(".")) { | ||
| var parts = patternLower.split("\\.", 2); | ||
| if (parts.length == 2 && !parts[0].isEmpty() && !parts[1].isEmpty()) { | ||
| moduleMethodPairs | ||
| .computeIfAbsent(parts[0], k -> new HashSet<>()) | ||
| .add(parts[1]); | ||
| } | ||
| } else { | ||
| localMethods.add(patternLower); | ||
| } | ||
| } | ||
|
|
||
| return new ParsedStrTemplateMethods(localMethods, moduleMethodPairs); | ||
| } | ||
nixel2007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
nixel2007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2025 | ||
| * 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. | ||
| */ | ||
| /** | ||
| * Пакет содержит настройки для семантических токенов. | ||
| */ | ||
| @NullMarked | ||
| package com.github._1c_syntax.bsl.languageserver.configuration.semantictokens; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,9 @@ | |||||
| */ | ||||||
| package com.github._1c_syntax.bsl.languageserver.semantictokens; | ||||||
|
|
||||||
| import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration; | ||||||
| import com.github._1c_syntax.bsl.languageserver.configuration.events.LanguageServerConfigurationChangedEvent; | ||||||
| import com.github._1c_syntax.bsl.languageserver.configuration.semantictokens.ParsedStrTemplateMethods; | ||||||
| import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; | ||||||
| import com.github._1c_syntax.bsl.languageserver.semantictokens.strings.AstTokenInfo; | ||||||
| import com.github._1c_syntax.bsl.languageserver.semantictokens.strings.QueryContext; | ||||||
|
|
@@ -33,11 +36,13 @@ | |||||
| import com.github._1c_syntax.bsl.languageserver.utils.MultilingualStringAnalyser; | ||||||
| import com.github._1c_syntax.bsl.languageserver.utils.Ranges; | ||||||
| import com.github._1c_syntax.bsl.parser.BSLLexer; | ||||||
| import jakarta.annotation.PostConstruct; | ||||||
| import lombok.RequiredArgsConstructor; | ||||||
| import org.antlr.v4.runtime.Token; | ||||||
| import org.eclipse.lsp4j.Position; | ||||||
| import org.eclipse.lsp4j.Range; | ||||||
| import org.eclipse.lsp4j.SemanticTokenTypes; | ||||||
| import org.springframework.context.event.EventListener; | ||||||
| import org.springframework.stereotype.Component; | ||||||
|
|
||||||
| import java.util.ArrayList; | ||||||
|
|
@@ -57,6 +62,7 @@ | |||||
| * <li>Запросы SDBL: разбивает строки на части вокруг токенов запроса и добавляет токены SDBL</li> | ||||||
| * <li>НСтр/NStr: подсвечивает языковые ключи (ru=, en=)</li> | ||||||
| * <li>СтрШаблон/StrTemplate: подсвечивает плейсхолдеры (%1, %2)</li> | ||||||
| * <li>Конфигурируемые функции-шаблонизаторы: подсвечивает плейсхолдеры (%1, %2)</li> | ||||||
| * <li>Обычные строки: выдаёт токен для всей строки</li> | ||||||
| * </ul> | ||||||
| */ | ||||||
|
|
@@ -72,6 +78,30 @@ public class StringSemanticTokensSupplier implements SemanticTokensSupplier { | |||||
| ); | ||||||
|
|
||||||
| private final SemanticTokensHelper helper; | ||||||
| private final LanguageServerConfiguration configuration; | ||||||
|
|
||||||
| private ParsedStrTemplateMethods parsedStrTemplateMethods; | ||||||
|
||||||
| private ParsedStrTemplateMethods parsedStrTemplateMethods; | |
| private volatile ParsedStrTemplateMethods parsedStrTemplateMethods; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
LanguageServerConfigurationTestshould be updated to include verification thatSemanticTokensOptionsis properly initialized and configured. Similar to howInlayHintOptionsandCodeLensOptionsare tested intestPartialInitialization(), add assertions to verify that the newsemanticTokensOptionsfield is correctly loaded from configuration files and has appropriate defaults.