Search in sources :

Example 61 with DocumentContext

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

the class BSLTextDocumentService method didChange.

@Override
public void didChange(DidChangeTextDocumentParams params) {
    // TODO: Place to optimize -> migrate to #TextDocumentSyncKind.INCREMENTAL and build changed parse tree
    DocumentContext documentContext = context.getDocument(params.getTextDocument().getUri());
    if (documentContext == null) {
        return;
    }
    documentContext.rebuild(params.getContentChanges().get(0).getText(), params.getTextDocument().getVersion());
    if (configuration.getDiagnosticsOptions().getComputeTrigger() == ComputeTrigger.ONTYPE) {
        validate(documentContext);
    }
}
Also used : DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext)

Example 62 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext 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 63 with DocumentContext

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

the class FixAllCodeActionSupplier method getFixAllCodeAction.

private List<CodeAction> getFixAllCodeAction(Either<String, Integer> diagnosticCode, CodeActionParams params, DocumentContext documentContext) {
    Optional<Class<? extends QuickFixProvider>> quickFixClass = quickFixSupplier.getQuickFixClass(diagnosticCode);
    if (quickFixClass.isEmpty()) {
        return Collections.emptyList();
    }
    List<Diagnostic> suitableDiagnostics = documentContext.getComputedDiagnostics().stream().filter(diagnostic -> diagnosticCode.equals(diagnostic.getCode())).collect(Collectors.toList());
    // if incomingDiagnostics list has size = 1 - it will be displayed as regular quick fix
    if (suitableDiagnostics.size() < ADD_FIX_ALL_DIAGNOSTICS_THRESHOLD) {
        return Collections.emptyList();
    }
    CodeActionContext fixAllContext = new CodeActionContext();
    fixAllContext.setDiagnostics(suitableDiagnostics);
    fixAllContext.setOnly(Collections.singletonList(CodeActionKind.QuickFix));
    CodeActionParams fixAllParams = new CodeActionParams();
    fixAllParams.setTextDocument(params.getTextDocument());
    fixAllParams.setRange(params.getRange());
    fixAllParams.setContext(fixAllContext);
    Class<? extends QuickFixProvider> quickFixProviderClass = quickFixClass.get();
    QuickFixProvider quickFixInstance = quickFixSupplier.getQuickFixInstance(quickFixProviderClass);
    return quickFixInstance.getQuickFixes(suitableDiagnostics, fixAllParams, documentContext);
}
Also used : CodeAction(org.eclipse.lsp4j.CodeAction) QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider) Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Collectors(java.util.stream.Collectors) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Component(org.springframework.stereotype.Component) List(java.util.List) Stream(java.util.stream.Stream) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Optional(java.util.Optional) Collections(java.util.Collections) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider) Diagnostic(org.eclipse.lsp4j.Diagnostic)

Example 64 with DocumentContext

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

Example 65 with DocumentContext

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

the class CognitiveComplexityComputerTest method compute.

@Test
void compute() {
    // given
    DocumentContext documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/context/computer/CognitiveComplexityComputerTest.bsl");
    // when
    Computer<ComplexityData> cognitiveComplexityComputer = new CognitiveComplexityComputer(documentContext);
    ComplexityData data = cognitiveComplexityComputer.compute();
    final Map<MethodSymbol, Integer> methodsComplexity = data.getMethodsComplexity();
    // then
    MethodSymbol example1 = documentContext.getSymbolTree().getMethods().get(0);
    Integer example1Complexity = methodsComplexity.get(example1);
    assertThat(example1Complexity).isEqualTo(19);
    MethodSymbol example2 = documentContext.getSymbolTree().getMethods().get(1);
    Integer example2Complexity = methodsComplexity.get(example2);
    assertThat(example2Complexity).isEqualTo(33);
    MethodSymbol example3 = documentContext.getSymbolTree().getMethods().get(2);
    Integer example3Complexity = methodsComplexity.get(example3);
    assertThat(example3Complexity).isEqualTo(20);
    MethodSymbol example4 = documentContext.getSymbolTree().getMethods().get(3);
    Integer example4Complexity = methodsComplexity.get(example4);
    assertThat(example4Complexity).isEqualTo(15);
}
Also used : MethodSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)86 Test (org.junit.jupiter.api.Test)69 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)57 Diagnostic (org.eclipse.lsp4j.Diagnostic)36 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)21 SneakyThrows (lombok.SneakyThrows)17 Path (java.nio.file.Path)16 CodeAction (org.eclipse.lsp4j.CodeAction)16 TestUtils (com.github._1c_syntax.bsl.languageserver.util.TestUtils)15 Autowired (org.springframework.beans.factory.annotation.Autowired)15 Collectors (java.util.stream.Collectors)14 Range (org.eclipse.lsp4j.Range)13 ArrayList (java.util.ArrayList)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)12 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)12 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)11 MethodSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol)10 MDCommonModule (com.github._1c_syntax.mdclasses.mdo.MDCommonModule)10