Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.symbol.ParameterDefinition;
import com.github._1c_syntax.bsl.languageserver.context.symbol.SymbolTree;
import com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol;
import com.github._1c_syntax.bsl.languageserver.context.symbol.description.MethodDescription;
import com.github._1c_syntax.bsl.languageserver.context.symbol.description.SourceDefinedSymbolDescription;
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableDescription;
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind;
import com.github._1c_syntax.bsl.languageserver.events.LanguageServerInitializeRequestReceivedEvent;
import com.github._1c_syntax.bsl.languageserver.references.ReferenceIndex;
import com.github._1c_syntax.bsl.languageserver.references.ReferenceResolver;
Expand Down Expand Up @@ -236,36 +238,71 @@ private void addVariableSymbols(
List<Range> descriptionRanges,
BitSet documentationLines
) {
for (var variableSymbol : symbolTree.getVariables()) {
var nameRange = variableSymbol.getVariableNameRange();
if (!Ranges.isEmpty(nameRange)) {
Position pos = nameRange.getStart();
boolean isDefinition = referenceResolver.findReference(documentContext.getUri(), pos)
.map(ref -> ref.getOccurrenceType() == OccurrenceType.DEFINITION)
.orElse(false);
if (isDefinition) {
addRange(entries, nameRange, SemanticTokenTypes.Variable, SemanticTokenModifiers.Definition);
} else {
addRange(entries, nameRange, SemanticTokenTypes.Variable);
var references = referenceIndex.getReferencesFrom(documentContext.getUri(), SymbolKind.Variable);

if (references.isEmpty()) {
for (var variableSymbol : symbolTree.getVariables()) {
var nameRange = variableSymbol.getVariableNameRange();
if (!Ranges.isEmpty(nameRange)) {
Position pos = nameRange.getStart();
boolean isDefinition = referenceResolver.findReference(documentContext.getUri(), pos)
.map(ref -> ref.getOccurrenceType() == OccurrenceType.DEFINITION)
.orElse(false);

var tokenType = variableSymbol.getKind() == VariableKind.PARAMETER
? SemanticTokenTypes.Parameter
: SemanticTokenTypes.Variable;

if (isDefinition) {
addRange(entries, nameRange, tokenType, SemanticTokenModifiers.Definition);
} else {
addRange(entries, nameRange, tokenType);
}
}
}
variableSymbol.getDescription().ifPresent((VariableDescription description) -> {
processVariableDescription(descriptionRanges, documentationLines, description);
variableSymbol.getDescription().ifPresent((VariableDescription description) -> {
processVariableDescription(descriptionRanges, documentationLines, description);

description.getTrailingDescription().ifPresent((VariableDescription trailingDescription) ->
processVariableDescription(descriptionRanges, documentationLines, trailingDescription)
);
});
description.getTrailingDescription().ifPresent((VariableDescription trailingDescription) ->
processVariableDescription(descriptionRanges, documentationLines, trailingDescription)
);
});
}
} else {
references.stream()
.filter(com.github._1c_syntax.bsl.languageserver.references.model.Reference::isSourceDefinedSymbolReference)
.forEach(reference -> reference.getSourceDefinedSymbol()
.filter(symbol -> symbol instanceof VariableSymbol)
.map(symbol -> (VariableSymbol) symbol)
.ifPresent(variableSymbol -> {
var tokenType = variableSymbol.getKind() == VariableKind.PARAMETER
? SemanticTokenTypes.Parameter
: SemanticTokenTypes.Variable;

if (reference.getOccurrenceType() == OccurrenceType.DEFINITION) {
addRange(entries, reference.getSelectionRange(), tokenType, SemanticTokenModifiers.Definition);
} else {
addRange(entries, reference.getSelectionRange(), tokenType);
}
}));

symbolTree.getVariables().stream()
.filter(variableSymbol -> variableSymbol.getDescription().isPresent())
.forEach(variableSymbol ->
variableSymbol.getDescription().ifPresent((VariableDescription description) -> {
processVariableDescription(descriptionRanges, documentationLines, description);

description.getTrailingDescription().ifPresent((VariableDescription trailingDescription) ->
processVariableDescription(descriptionRanges, documentationLines, trailingDescription)
);
}));
}
}

private void addMethodSymbols(SymbolTree symbolTree, List<TokenEntry> entries, List<Range> descriptionRanges, BitSet documentationLines) {
for (var method : symbolTree.getMethods()) {
var semanticTokenType = method.isFunction() ? SemanticTokenTypes.Function : SemanticTokenTypes.Method;
addRange(entries, method.getSubNameRange(), semanticTokenType);
for (ParameterDefinition parameter : method.getParameters()) {
addRange(entries, parameter.getRange(), SemanticTokenTypes.Parameter);
}
// Parameters are handled in addVariableSymbols via VariableSymbol with proper modifiers
method.getDescription().ifPresent((MethodDescription description) ->
processVariableDescription(descriptionRanges, documentationLines, description)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,72 @@ void sameFileMethodCall_isHighlightedAsMethodTokenAtCallSite() {
assertThat(methodsOnCallLine).isGreaterThanOrEqualTo(1);
}

@Test
void parameterAndVariableTokenTypes() {
String bsl = String.join("\n",
"Процедура Тест(Парам1, Парам2)",
" Перем ЛокальнаяПеременная;",
" НеявнаяПеременная = 1;",
" ЛокальнаяПеременная2 = 2;",
" Результат = 3;",
" Для ПеременнаяЦикла = 1 По 10 Цикл",
" КонецЦикла;",
"КонецПроцедуры"
);

DocumentContext documentContext = TestUtils.getDocumentContext(bsl);
TextDocumentIdentifier textDocumentIdentifier = TestUtils.getTextDocumentIdentifier(documentContext.getUri());

SemanticTokens tokens = provider.getSemanticTokensFull(documentContext, new SemanticTokensParams(textDocumentIdentifier));

int paramIdx = legend.getTokenTypes().indexOf(SemanticTokenTypes.Parameter);
int varIdx = legend.getTokenTypes().indexOf(SemanticTokenTypes.Variable);
assertThat(paramIdx).isGreaterThanOrEqualTo(0);
assertThat(varIdx).isGreaterThanOrEqualTo(0);

List<DecodedToken> decoded = decode(tokens.getData());

long paramsInSignature = decoded.stream()
.filter(t -> t.line == 0 && t.type == paramIdx)
.count();
assertThat(paramsInSignature).as("Parameters in signature").isEqualTo(2);

long localVarDeclaration = decoded.stream()
.filter(t -> t.line == 1 && t.type == varIdx)
.count();
assertThat(localVarDeclaration).as("Explicit variable declaration").isEqualTo(1);

long implicitVarDeclaration1 = decoded.stream()
.filter(t -> t.line == 2 && t.type == varIdx)
.count();
assertThat(implicitVarDeclaration1).as("First implicit variable declaration").isEqualTo(1);

long implicitVarDeclaration2 = decoded.stream()
.filter(t -> t.line == 3 && t.type == varIdx)
.count();
assertThat(implicitVarDeclaration2).as("Second implicit variable declaration").isEqualTo(1);

long implicitVarDeclaration3 = decoded.stream()
.filter(t -> t.line == 4 && t.type == varIdx)
.count();
assertThat(implicitVarDeclaration3).as("Third implicit variable declaration").isEqualTo(1);

long forLoopVar = decoded.stream()
.filter(t -> t.line == 5 && t.type == varIdx)
.count();
assertThat(forLoopVar).as("For loop variable").isEqualTo(1);

long allParams = decoded.stream()
.filter(t -> t.type == paramIdx)
.count();
assertThat(allParams).as("Total parameters").isEqualTo(2);

long allVars = decoded.stream()
.filter(t -> t.type == varIdx)
.count();
assertThat(allVars).as("Total variables").isEqualTo(5);
}

// helpers
private record DecodedToken(int line, int start, int length, int type, int modifiers) {}

Expand Down