Search in sources :

Example 1 with RegionSymbol

use of com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol in project bsl-language-server by 1c-syntax.

the class DuplicateRegionDiagnostic method visitFile.

@Override
public ParseTree visitFile(BSLParser.FileContext ctx) {
    List<RegionSymbol> regions = documentContext.getSymbolTree().getModuleLevelRegions();
    // анализировать модуль без областей не будем
    if (regions.isEmpty()) {
        return ctx;
    }
    // считаем дубли с учетом альтернативных имен для стандартных областей
    regions.stream().collect(Collectors.groupingBy(regionSymbol -> regionNames.getOrDefault(regionSymbol.getName(), regionSymbol.getName()))).forEach((String name, List<RegionSymbol> regionsList) -> {
        if (regionsList.size() > 1) {
            List<DiagnosticRelatedInformation> relatedInformation = new ArrayList<>();
            RegionSymbol currentRegion = regionsList.get(0);
            Range currentRange = currentRegion.getStartRange();
            regionsList.stream().map(region -> RelatedInformation.create(documentContext.getUri(), region.getStartRange(), "+1")).collect(Collectors.toCollection(() -> relatedInformation));
            diagnosticStorage.addDiagnostic(currentRange, info.getMessage(currentRegion.getName()), relatedInformation);
        }
    });
    return ctx;
}
Also used : RelatedInformation(com.github._1c_syntax.bsl.languageserver.utils.RelatedInformation) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range) DiagnosticType(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType) DiagnosticTag(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) DiagnosticSeverity(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity) Keywords(com.github._1c_syntax.bsl.languageserver.utils.Keywords) List(java.util.List) DiagnosticMetadata(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata) TreeMap(java.util.TreeMap) DiagnosticCompatibilityMode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCompatibilityMode) Map(java.util.Map) DiagnosticRelatedInformation(org.eclipse.lsp4j.DiagnosticRelatedInformation) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DiagnosticRelatedInformation(org.eclipse.lsp4j.DiagnosticRelatedInformation) Range(org.eclipse.lsp4j.Range)

Example 2 with RegionSymbol

use of com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol in project bsl-language-server by 1c-syntax.

the class RegionSymbolComputer method visitRegionEnd.

@Override
public ParseTree visitRegionEnd(BSLParser.RegionEndContext ctx) {
    if (regionStack.isEmpty()) {
        return super.visitRegionEnd(ctx);
    }
    var pair = regionStack.pop();
    RegionSymbol.RegionSymbolBuilder builder = pair.getLeft();
    BSLParser.RegionStartContext regionStartContext = pair.getRight();
    Range range = Ranges.create(regionStartContext, ctx);
    builder.range(range).endRange(Ranges.create(ctx));
    RegionSymbol region = builder.build();
    regions.add(region);
    return super.visitRegionEnd(ctx);
}
Also used : RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range)

Example 3 with RegionSymbol

use of com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol 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 4 with RegionSymbol

use of com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol in project bsl-language-server by 1c-syntax.

the class GenerateStandardRegionsSupplier method getCodeActions.

/**
 * При необходимости создает {@code CodeAction} для генерации отсутствующих
 * стандартных областей 1С
 *
 * @param params          параметры вызова генерации {@code codeAction}
 * @param documentContext представление программного модуля
 * @return {@code List<CodeAction>} если модуль не содержит всех стандартных областей,
 * пустой {@code List} если генерация областей не требуется
 */
@Override
public List<CodeAction> getCodeActions(CodeActionParams params, DocumentContext documentContext) {
    ModuleType moduleType = documentContext.getModuleType();
    FileType fileType = documentContext.getFileType();
    ScriptVariant regionsLanguage = getRegionsLanguage(documentContext, fileType);
    Set<String> neededStandardRegions;
    if (fileType == FileType.BSL) {
        neededStandardRegions = Regions.getStandardRegionsNamesByModuleType(moduleType, regionsLanguage);
    } else {
        neededStandardRegions = Regions.getOneScriptStandardRegions(regionsLanguage);
    }
    Set<String> documentRegionsNames = documentContext.getSymbolTree().getModuleLevelRegions().stream().map(RegionSymbol::getName).collect(Collectors.toSet());
    neededStandardRegions.removeAll(documentRegionsNames);
    if (neededStandardRegions.isEmpty()) {
        return Collections.emptyList();
    }
    String regionFormat = regionsLanguage == ScriptVariant.ENGLISH ? "#Region %s%n%n#EndRegion%n" : "#Область %s%n%n#КонецОбласти%n";
    String result = neededStandardRegions.stream().map(s -> String.format(regionFormat, s)).collect(Collectors.joining("\n"));
    TextEdit textEdit = new TextEdit(calculateFixRange(params.getRange()), result);
    WorkspaceEdit edit = new WorkspaceEdit();
    Map<String, List<TextEdit>> changes = Map.of(documentContext.getUri().toString(), Collections.singletonList(textEdit));
    edit.setChanges(changes);
    CodeAction codeAction = new CodeAction("Generate missing regions");
    codeAction.setDiagnostics(new ArrayList<>());
    codeAction.setKind(CodeActionKind.Refactor);
    codeAction.setEdit(edit);
    return List.of(codeAction);
}
Also used : ScriptVariant(com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant) Language(com.github._1c_syntax.bsl.languageserver.configuration.Language) Range(org.eclipse.lsp4j.Range) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Regions(com.github._1c_syntax.bsl.languageserver.utils.Regions) FileType(com.github._1c_syntax.bsl.languageserver.context.FileType) ArrayList(java.util.ArrayList) RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) Configuration(com.github._1c_syntax.mdclasses.Configuration) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) TextEdit(org.eclipse.lsp4j.TextEdit) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) ConfigurationSource(com.github._1c_syntax.mdclasses.common.ConfigurationSource) Map(java.util.Map) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Set(java.util.Set) ModuleType(com.github._1c_syntax.mdclasses.mdo.support.ModuleType) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) List(java.util.List) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ModuleType(com.github._1c_syntax.mdclasses.mdo.support.ModuleType) FileType(com.github._1c_syntax.bsl.languageserver.context.FileType) TextEdit(org.eclipse.lsp4j.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) ScriptVariant(com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

RegionSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Range (org.eclipse.lsp4j.Range)3 DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)2 BSLParser (com.github._1c_syntax.bsl.parser.BSLParser)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Language (com.github._1c_syntax.bsl.languageserver.configuration.Language)1 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)1 FileType (com.github._1c_syntax.bsl.languageserver.context.FileType)1 MethodSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol)1 ModuleSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.ModuleSymbol)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 DiagnosticCompatibilityMode (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCompatibilityMode)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