-
Notifications
You must be signed in to change notification settings - Fork 121
Add semantic highlighting for NStr and StrTemplate functions #3697
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
Draft
Copilot
wants to merge
3
commits into
develop
Choose a base branch
from
copilot/resolve-coloring-nstr-strshablon
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
...1c_syntax/bsl/languageserver/semantictokens/NStrAndStrTemplateSemanticTokensSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * 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.semantictokens; | ||
|
|
||
| import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; | ||
| import com.github._1c_syntax.bsl.languageserver.utils.MultilingualStringAnalyser; | ||
| import com.github._1c_syntax.bsl.languageserver.utils.Trees; | ||
| import com.github._1c_syntax.bsl.parser.BSLParser; | ||
| import com.github._1c_syntax.bsl.parser.BSLParserBaseVisitor; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.antlr.v4.runtime.Token; | ||
| import org.eclipse.lsp4j.SemanticTokenTypes; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Сапплаер семантических токенов для функций НСтр (NStr) и СтрШаблон (StrTemplate). | ||
| * <p> | ||
| * Для НСтр: подсвечивает языковые ключи (ru=, en=) в строковых параметрах. | ||
| * <p> | ||
| * Для СтрШаблон: подсвечивает плейсхолдеры (%1, %2, %(1)) в строковых параметрах. | ||
| */ | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class NStrAndStrTemplateSemanticTokensSupplier implements SemanticTokensSupplier { | ||
|
|
||
| private static final Set<Integer> STRING_TOKEN_TYPES = Set.of( | ||
| BSLParser.STRING, | ||
| BSLParser.STRINGPART, | ||
| BSLParser.STRINGSTART, | ||
| BSLParser.STRINGTAIL | ||
| ); | ||
|
|
||
| private final SemanticTokensHelper helper; | ||
|
|
||
| @Override | ||
| public List<SemanticTokenEntry> getSemanticTokens(DocumentContext documentContext) { | ||
| List<SemanticTokenEntry> entries = new ArrayList<>(); | ||
|
|
||
| var visitor = new NStrAndStrTemplateVisitor(entries, helper); | ||
| visitor.visit(documentContext.getAst()); | ||
|
|
||
| return entries; | ||
| } | ||
|
|
||
| /** | ||
| * Visitor for finding NStr and StrTemplate method calls. | ||
| */ | ||
| private static class NStrAndStrTemplateVisitor extends BSLParserBaseVisitor<Void> { | ||
| private final List<SemanticTokenEntry> entries; | ||
| private final SemanticTokensHelper helper; | ||
|
|
||
| public NStrAndStrTemplateVisitor(List<SemanticTokenEntry> entries, SemanticTokensHelper helper) { | ||
| this.entries = entries; | ||
| this.helper = helper; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitGlobalMethodCall(BSLParser.GlobalMethodCallContext ctx) { | ||
| if (MultilingualStringAnalyser.isNStrCall(ctx)) { | ||
| processNStrCall(ctx); | ||
| } else if (MultilingualStringAnalyser.isStrTemplateCall(ctx)) { | ||
| processStrTemplateCall(ctx); | ||
| } | ||
|
|
||
| return super.visitGlobalMethodCall(ctx); | ||
| } | ||
|
|
||
| private void processNStrCall(BSLParser.GlobalMethodCallContext ctx) { | ||
| var callParams = ctx.doCall().callParamList().callParam(); | ||
| if (callParams.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the first parameter (the multilingual string) | ||
| var firstParam = callParams.get(0); | ||
| var stringTokens = getStringTokens(firstParam); | ||
|
|
||
| for (Token token : stringTokens) { | ||
| String tokenText = token.getText(); | ||
| int tokenLine = token.getLine() - 1; // Convert to 0-indexed | ||
| int tokenStart = token.getCharPositionInLine(); | ||
|
|
||
| // Find language keys in the string using MultilingualStringAnalyser | ||
| var positions = MultilingualStringAnalyser.findLanguageKeyPositions(tokenText); | ||
| for (var position : positions) { | ||
| helper.addEntry( | ||
| entries, | ||
| tokenLine, | ||
| tokenStart + position.start(), | ||
| position.length(), | ||
| SemanticTokenTypes.Property | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void processStrTemplateCall(BSLParser.GlobalMethodCallContext ctx) { | ||
| var callParams = ctx.doCall().callParamList().callParam(); | ||
| if (callParams.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the first parameter (the template string) | ||
| var firstParam = callParams.get(0); | ||
| var stringTokens = getStringTokens(firstParam); | ||
|
|
||
| for (Token token : stringTokens) { | ||
| String tokenText = token.getText(); | ||
| int tokenLine = token.getLine() - 1; // Convert to 0-indexed | ||
| int tokenStart = token.getCharPositionInLine(); | ||
|
|
||
| // Find placeholders in the string using MultilingualStringAnalyser | ||
| var positions = MultilingualStringAnalyser.findPlaceholderPositions(tokenText); | ||
| for (var position : positions) { | ||
| helper.addEntry( | ||
| entries, | ||
| tokenLine, | ||
| tokenStart + position.start(), | ||
| position.length(), | ||
| SemanticTokenTypes.Parameter | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private List<Token> getStringTokens(BSLParser.CallParamContext callParam) { | ||
| List<Token> stringTokens = new ArrayList<>(); | ||
| var tokens = Trees.getTokens(callParam); | ||
|
|
||
| for (Token token : tokens) { | ||
| if (STRING_TOKEN_TYPES.contains(token.getType())) { | ||
| stringTokens.add(token); | ||
| } | ||
| } | ||
|
|
||
| return stringTokens; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@copilot используй MultilingualStringAnalyser. Расширь его при необходимости
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.
Refactored to use
MultilingualStringAnalyser. Extended it with:isNStrCall()andisStrTemplateCall()static methodsfindLanguageKeyPositions()andfindPlaceholderPositions()static methodsMatchPositionrecord for position infoSTR_TEMPLATE_PLACEHOLDER_PATTERN(same pattern as inIncorrectUseOfStrTemplateDiagnostic)Commit: 5c618ee