Search in sources :

Example 1 with Regions

use of com.github._1c_syntax.bsl.languageserver.utils.Regions 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 Regions

use of com.github._1c_syntax.bsl.languageserver.utils.Regions in project bsl-language-server by 1c-syntax.

the class DocumentSymbolProviderTest method testDocumentSymbol.

@Test
void testDocumentSymbol() {
    DocumentContext documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/providers/documentSymbol.bsl");
    List<Either<SymbolInformation, DocumentSymbol>> documentSymbols = documentSymbolProvider.getDocumentSymbols(documentContext);
    assertThat(documentSymbols).hasSize(9);
    // global variables
    assertThat(documentSymbols).filteredOn(documentSymbol -> documentSymbol.getRight().getKind().equals(SymbolKind.Variable)).hasSize(3).extracting(Either::getRight).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(0, 6, 0, 7))).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(2, 6, 2, 7))).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(2, 9, 2, 10)));
    // methods
    assertThat(documentSymbols).filteredOn(documentSymbol -> documentSymbol.getRight().getKind().equals(SymbolKind.Method)).hasSize(4).extracting(Either::getRight).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(4, 0, 5, 14))).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(7, 0, 8, 12))).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(10, 0, 13, 14))).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(47, 0, 48, 12))).filteredOn(documentSymbol1 -> documentSymbol1.getTags().contains(SymbolTag.Deprecated)).anyMatch(documentSymbol -> documentSymbol.getRange().equals(Ranges.create(47, 0, 48, 12)));
    // sub vars
    assertThat(documentSymbols).filteredOn(documentSymbol -> documentSymbol.getRight().getKind().equals(SymbolKind.Method)).extracting(Either::getRight).flatExtracting(DocumentSymbol::getChildren).filteredOn(documentSymbol -> documentSymbol.getKind() == SymbolKind.Variable).hasSize(3).anyMatch(subVar -> subVar.getRange().equals(Ranges.create(11, 10, 11, 11))).anyMatch(subVar -> subVar.getRange().equals(Ranges.create(12, 10, 12, 11))).anyMatch(subVar -> subVar.getRange().equals(Ranges.create(12, 12, 12, 13)));
    // regions
    assertThat(documentSymbols).filteredOn(documentSymbol -> documentSymbol.getRight().getKind() == SymbolKind.Namespace).extracting(Either::getRight).hasSize(2).flatExtracting(DocumentSymbol::getChildren).hasSize(3).anyMatch(documentSymbol -> documentSymbol.getKind() == SymbolKind.Namespace).anyMatch(documentSymbol -> documentSymbol.getKind() == SymbolKind.Method).filteredOn(documentSymbol -> documentSymbol.getKind() == SymbolKind.Method).hasSize(2).anyMatch(subVar -> subVar.getRange().equals(Ranges.create(17, 0, 19, 14))).anyMatch(subVar -> subVar.getRange().equals(Ranges.create(36, 0, 42, 14)));
    DocumentSymbol externalRegion = documentSymbols.stream().map(Either::getRight).filter(documentSymbol -> documentSymbol.getKind() == SymbolKind.Namespace).filter(documentSymbol -> documentSymbol.getName().equals("ВнешняяОбласть")).findFirst().get();
    assertThat(externalRegion.getChildren()).hasSize(1).allMatch(documentSymbol -> documentSymbol.getKind() == SymbolKind.Method);
    DocumentSymbol method = externalRegion.getChildren().get(0);
    assertThat(method.getChildren()).hasSize(1).allMatch(documentSymbol -> documentSymbol.getKind() == SymbolKind.Namespace).allMatch(documentSymbol -> documentSymbol.getName().equals("ВнутренняяОбласть1"));
    DocumentSymbol internalRegion1 = method.getChildren().get(0);
    assertThat(internalRegion1.getChildren()).hasSize(1).allMatch(documentSymbol -> documentSymbol.getKind() == SymbolKind.Namespace).allMatch(documentSymbol -> documentSymbol.getName().equals("ВнутренняяОбласть2"));
}
Also used : SymbolKind(org.eclipse.lsp4j.SymbolKind) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Test(org.junit.jupiter.api.Test) SymbolTag(org.eclipse.lsp4j.SymbolTag) List(java.util.List) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with Regions

use of com.github._1c_syntax.bsl.languageserver.utils.Regions in project bsl-language-server by 1c-syntax.

the class FoldingRangeProviderTest method testFoldingRange.

@Test
void testFoldingRange() {
    DocumentContext documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/providers/foldingRange.bsl");
    List<FoldingRange> foldingRanges = foldingRangeProvider.getFoldingRange(documentContext);
    assertThat(foldingRanges).hasSize(11);
    // regions
    assertThat(foldingRanges).filteredOn(foldingRange -> foldingRange.getKind().equals(FoldingRangeKind.Region)).anyMatch(foldingRange -> foldingRange.getStartLine() == 3 && foldingRange.getEndLine() == 26).anyMatch(foldingRange -> foldingRange.getStartLine() == 5 && foldingRange.getEndLine() == 19).anyMatch(foldingRange -> foldingRange.getStartLine() == 7 && foldingRange.getEndLine() == 17).anyMatch(foldingRange -> foldingRange.getStartLine() == 11 && foldingRange.getEndLine() == 15).anyMatch(foldingRange -> foldingRange.getStartLine() == 12 && foldingRange.getEndLine() == 14).anyMatch(foldingRange -> foldingRange.getStartLine() == 23 && foldingRange.getEndLine() == 24).anyMatch(foldingRange -> foldingRange.getStartLine() == 28 && foldingRange.getEndLine() == 29);
    // comments
    assertThat(foldingRanges).filteredOn(foldingRange -> foldingRange.getKind().equals(FoldingRangeKind.Comment)).hasSize(2).anyMatch(foldingRange -> foldingRange.getStartLine() == 9 && foldingRange.getEndLine() == 10).anyMatch(foldingRange -> foldingRange.getStartLine() == 21 && foldingRange.getEndLine() == 22);
    // import
    assertThat(foldingRanges).filteredOn(foldingRange -> foldingRange.getKind().equals(FoldingRangeKind.Imports)).hasSize(1).anyMatch(foldingRange -> foldingRange.getStartLine() == 0 && foldingRange.getEndLine() == 1);
}
Also used : Test(org.junit.jupiter.api.Test) List(java.util.List) FoldingRangeKind(org.eclipse.lsp4j.FoldingRangeKind) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) FoldingRange(org.eclipse.lsp4j.FoldingRange) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) FoldingRange(org.eclipse.lsp4j.FoldingRange) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with Regions

use of com.github._1c_syntax.bsl.languageserver.utils.Regions in project bsl-language-server by 1c-syntax.

the class GenerateStandardRegionsSupplierTest method testGetCodeActions.

@Test
void testGetCodeActions() {
    // given
    String filePath = "./src/test/resources/suppliers/generateRegion.bsl";
    DocumentContext documentContext = TestUtils.getDocumentContextFromFile(filePath);
    List<Diagnostic> diagnostics = new ArrayList<>();
    TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(documentContext.getUri().toString());
    CodeActionContext codeActionContext = new CodeActionContext();
    codeActionContext.setDiagnostics(diagnostics);
    CodeActionParams params = new CodeActionParams();
    params.setRange(new Range());
    params.setTextDocument(textDocumentIdentifier);
    params.setContext(codeActionContext);
    // when
    List<CodeAction> codeActions = codeActionSupplier.getCodeActions(params, documentContext);
    assertThat(codeActions).hasSize(1).anyMatch(codeAction -> codeAction.getTitle().equals("Generate missing regions"));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeAction(org.eclipse.lsp4j.CodeAction) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Range(org.eclipse.lsp4j.Range) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with Regions

use of com.github._1c_syntax.bsl.languageserver.utils.Regions 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)

Aggregations

DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 RegionSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol)3 Range (org.eclipse.lsp4j.Range)3 Test (org.junit.jupiter.api.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 TestUtils (com.github._1c_syntax.bsl.languageserver.util.TestUtils)2 Ranges (com.github._1c_syntax.bsl.languageserver.utils.Ranges)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 CodeAction (org.eclipse.lsp4j.CodeAction)2 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)2 Autowired (org.springframework.beans.factory.annotation.Autowired)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