use of com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol in project bsl-language-server by 1c-syntax.
the class SymbolTree method getVariableSymbol.
/**
* Попытка поиска символа переменной по узлу дерева разбора.
* <p>
* Implementation note Поиск осуществляется по месту определения переменной (declaration).
*
* @param ctx узел дерева разбора документа.
* @return найденный символ переменной.
*/
public Optional<VariableSymbol> getVariableSymbol(BSLParserRuleContext ctx) {
BSLParserRuleContext varNameNode;
if (Trees.nodeContainsErrors(ctx)) {
varNameNode = ctx;
} else if (ctx instanceof BSLParser.ModuleVarDeclarationContext) {
varNameNode = ((BSLParser.ModuleVarDeclarationContext) ctx).var_name();
} else if (ctx instanceof BSLParser.SubVarDeclarationContext) {
varNameNode = ((BSLParser.SubVarDeclarationContext) ctx).var_name();
} else {
varNameNode = ctx;
}
Range variableNameRange = Ranges.create(varNameNode);
return getVariables().stream().filter(variableSymbol -> variableSymbol.getVariableNameRange().equals(variableNameRange)).findAny();
}
use of com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol in project bsl-language-server by 1c-syntax.
the class SymbolTreeComputer method compute.
@Override
public SymbolTree compute() {
ModuleSymbol moduleSymbol = new ModuleSymbolComputer(documentContext).compute();
List<MethodSymbol> methods = new MethodSymbolComputer(documentContext).compute();
List<RegionSymbol> regions = new RegionSymbolComputer(documentContext).compute();
List<VariableSymbol> variables = new VariableSymbolComputer(documentContext).compute();
List<SourceDefinedSymbol> allOfThem = new ArrayList<>(methods);
allOfThem.addAll(regions);
allOfThem.addAll(variables);
allOfThem.sort(Comparator.comparingInt(symbol -> symbol.getRange().getStart().getLine()));
List<SourceDefinedSymbol> topLevelSymbols = new ArrayList<>();
SourceDefinedSymbol currentParent = moduleSymbol;
for (SourceDefinedSymbol symbol : allOfThem) {
currentParent = placeSymbol(topLevelSymbols, currentParent, symbol);
}
return new SymbolTree(moduleSymbol);
}
use of com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol in project bsl-language-server by 1c-syntax.
the class UseLessForEachDiagnostic method visitForEachStatement.
@Override
public ParseTree visitForEachStatement(BSLParser.ForEachStatementContext ctx) {
TerminalNode iterator = ctx.IDENTIFIER();
String iteratorIdName = iterator.getText();
boolean isVariable = documentContext.getSymbolTree().getVariables().stream().filter(variableSymbol -> variableSymbol.getKind() == VariableKind.GLOBAL || variableSymbol.getKind() == VariableKind.MODULE).anyMatch(variableSymbol -> variableSymbol.getName().equalsIgnoreCase(iteratorIdName));
if (isVariable) {
return super.visitForEachStatement(ctx);
}
boolean hasUsage = Trees.findAllTokenNodes(ctx.codeBlock(), BSLParser.IDENTIFIER).stream().filter(node -> iteratorIdName.equalsIgnoreCase(node.getText())).anyMatch(parentClassMatchTo(ComplexIdentifierContext.class).or(parentClassMatchTo(LValueContext.class)).or(parentClassMatchTo(CallStatementContext.class)));
if (!hasUsage) {
diagnosticStorage.addDiagnostic(iterator.getSymbol());
}
return super.visitForEachStatement(ctx);
}
Aggregations