Search in sources :

Example 1 with VariableSymbol

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();
}
Also used : List(java.util.List) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) Getter(lombok.Getter) BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) Optional(java.util.Optional) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range) Trees(com.github._1c_syntax.bsl.languageserver.utils.Trees) Collectors(java.util.stream.Collectors) Value(lombok.Value) ArrayList(java.util.ArrayList) BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range)

Example 2 with VariableSymbol

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);
}
Also used : RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) List(java.util.List) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) VariableSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol) SymbolTree(com.github._1c_syntax.bsl.languageserver.context.symbol.SymbolTree) Optional(java.util.Optional) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ModuleSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.ModuleSymbol) SourceDefinedSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.SourceDefinedSymbol) Comparator(java.util.Comparator) MethodSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol) ArrayList(java.util.ArrayList) RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) ModuleSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.ModuleSymbol) ArrayList(java.util.ArrayList) MethodSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol) SymbolTree(com.github._1c_syntax.bsl.languageserver.context.symbol.SymbolTree) SourceDefinedSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.SourceDefinedSymbol) VariableSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol)

Example 3 with VariableSymbol

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);
}
Also used : LValueContext(com.github._1c_syntax.bsl.parser.BSLParser.LValueContext) Predicate(java.util.function.Predicate) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) DiagnosticType(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType) DiagnosticTag(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag) ComplexIdentifierContext(com.github._1c_syntax.bsl.parser.BSLParser.ComplexIdentifierContext) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) DiagnosticSeverity(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity) VariableKind(com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind) DiagnosticMetadata(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata) CallStatementContext(com.github._1c_syntax.bsl.parser.BSLParser.CallStatementContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Trees(com.github._1c_syntax.bsl.languageserver.utils.Trees) ComplexIdentifierContext(com.github._1c_syntax.bsl.parser.BSLParser.ComplexIdentifierContext) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) LValueContext(com.github._1c_syntax.bsl.parser.BSLParser.LValueContext)

Aggregations

Ranges (com.github._1c_syntax.bsl.languageserver.utils.Ranges)2 Trees (com.github._1c_syntax.bsl.languageserver.utils.Trees)2 BSLParser (com.github._1c_syntax.bsl.parser.BSLParser)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)1 MethodSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol)1 ModuleSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.ModuleSymbol)1 RegionSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol)1 SourceDefinedSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.SourceDefinedSymbol)1 SymbolTree (com.github._1c_syntax.bsl.languageserver.context.symbol.SymbolTree)1 VariableSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol)1 VariableKind (com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind)1 DiagnosticMetadata (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata)1 DiagnosticSeverity (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity)1 DiagnosticTag (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag)1 DiagnosticType (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType)1 CallStatementContext (com.github._1c_syntax.bsl.parser.BSLParser.CallStatementContext)1 ComplexIdentifierContext (com.github._1c_syntax.bsl.parser.BSLParser.ComplexIdentifierContext)1