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;
}
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"));
}
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);
}
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"));
}
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);
}
Aggregations