|
| 1 | +/* |
| 2 | + * This file is a part of BSL Language Server. |
| 3 | + * |
| 4 | + * Copyright (c) 2018-2025 |
| 5 | + * Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 8 | + * |
| 9 | + * BSL Language Server is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 3.0 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * BSL Language Server is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with BSL Language Server. |
| 21 | + */ |
| 22 | +package com.github._1c_syntax.bsl.languageserver.semantictokens; |
| 23 | + |
| 24 | +import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; |
| 25 | +import com.github._1c_syntax.bsl.languageserver.utils.Ranges; |
| 26 | +import com.github._1c_syntax.bsl.languageserver.utils.Trees; |
| 27 | +import com.github._1c_syntax.bsl.parser.BSLParser; |
| 28 | +import lombok.RequiredArgsConstructor; |
| 29 | +import org.antlr.v4.runtime.ParserRuleContext; |
| 30 | +import org.antlr.v4.runtime.tree.TerminalNode; |
| 31 | +import org.eclipse.lsp4j.SemanticTokenTypes; |
| 32 | +import org.jspecify.annotations.Nullable; |
| 33 | +import org.springframework.stereotype.Component; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +/** |
| 39 | + * Сапплаер семантических токенов для аннотаций и директив компиляции. |
| 40 | + */ |
| 41 | +@Component |
| 42 | +@RequiredArgsConstructor |
| 43 | +public class AnnotationSemanticTokensSupplier implements SemanticTokensSupplier { |
| 44 | + |
| 45 | + private final SemanticTokensHelper helper; |
| 46 | + |
| 47 | + @Override |
| 48 | + public List<SemanticTokenEntry> getSemanticTokens(DocumentContext documentContext) { |
| 49 | + List<SemanticTokenEntry> entries = new ArrayList<>(); |
| 50 | + var ast = documentContext.getAst(); |
| 51 | + |
| 52 | + // Compiler directives: single Decorator from '&' through directive symbol |
| 53 | + for (var compilerDirective : Trees.<BSLParser.CompilerDirectiveContext>findAllRuleNodes(ast, BSLParser.RULE_compilerDirective)) { |
| 54 | + addAmpersandRange(entries, compilerDirective.AMPERSAND(), compilerDirective.compilerDirectiveSymbol()); |
| 55 | + } |
| 56 | + |
| 57 | + // Annotations: single Decorator from '&' through annotation name; params identifiers as Parameter |
| 58 | + for (var annotation : Trees.<BSLParser.AnnotationContext>findAllRuleNodes(ast, BSLParser.RULE_annotation)) { |
| 59 | + addAmpersandRange(entries, annotation.AMPERSAND(), annotation.annotationName()); |
| 60 | + |
| 61 | + var annotationParams = annotation.annotationParams(); |
| 62 | + if (annotationParams == null) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + for (var annotationParam : annotationParams.annotationParam()) { |
| 67 | + var annotationParamName = annotationParam.annotationParamName(); |
| 68 | + if (annotationParamName != null) { |
| 69 | + helper.addRange(entries, Ranges.create(annotationParamName.IDENTIFIER()), SemanticTokenTypes.Parameter); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return entries; |
| 75 | + } |
| 76 | + |
| 77 | + private void addAmpersandRange(List<SemanticTokenEntry> entries, TerminalNode node, @Nullable ParserRuleContext name) { |
| 78 | + var ampersand = node.getSymbol(); // '&' |
| 79 | + if (name != null) { |
| 80 | + var symbolToken = name.getStart(); |
| 81 | + helper.addRange(entries, Ranges.create(ampersand, symbolToken), SemanticTokenTypes.Decorator); |
| 82 | + } else { |
| 83 | + helper.addRange(entries, Ranges.create(ampersand), SemanticTokenTypes.Decorator); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
0 commit comments