Search in sources :

Example 51 with DocumentContext

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

the class GenerateStandardRegionsSupplier method getRegionsLanguage.

private ScriptVariant getRegionsLanguage(DocumentContext documentContext, FileType fileType) {
    ScriptVariant regionsLanguage;
    Configuration configuration = documentContext.getServerContext().getConfiguration();
    if (configuration.getConfigurationSource() == ConfigurationSource.EMPTY || fileType == FileType.OS) {
        regionsLanguage = getScriptVariantFromConfigLanguage();
    } else {
        regionsLanguage = documentContext.getServerContext().getConfiguration().getScriptVariant();
    }
    return regionsLanguage;
}
Also used : Configuration(com.github._1c_syntax.mdclasses.Configuration) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) ScriptVariant(com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant)

Example 52 with DocumentContext

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

the class QuickFixCodeActionSupplier method getCodeActionsByDiagnostic.

private List<CodeAction> getCodeActionsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, DocumentContext documentContext) {
    Optional<Class<? extends QuickFixProvider>> quickFixClass = quickFixSupplier.getQuickFixClass(diagnostic.getCode());
    if (quickFixClass.isEmpty()) {
        return Collections.emptyList();
    }
    Class<? extends QuickFixProvider> quickFixProviderClass = quickFixClass.get();
    QuickFixProvider quickFixInstance = quickFixSupplier.getQuickFixInstance(quickFixProviderClass);
    return quickFixInstance.getQuickFixes(Collections.singletonList(diagnostic), params, documentContext);
}
Also used : QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider)

Example 53 with DocumentContext

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

the class DocumentContextLazyDataMeasurer method handleEvent.

/**
 * Обработчик события {@link DocumentContextContentChangedEvent}. Вызывает основную логику выполнения замеров.
 *
 * @param event Событие
 */
@EventListener
@Order
@SneakyThrows
public void handleEvent(DocumentContextContentChangedEvent event) {
    var documentContext = event.getSource();
    LOGGER.debug("Take measurements for {}", documentContext.getUri());
    measureCollector.measureIt(documentContext::getAst, "context: ast");
    measureCollector.measureIt(documentContext::getQueries, "context: queries");
    for (SDBLTokenizer sdblTokenizer : documentContext.getQueries()) {
        measureCollector.measureIt(sdblTokenizer::getAst, "context: queryAst");
    }
    measureCollector.measureIt(documentContext::getSymbolTree, "context: symbolTree");
    measureCollector.measureIt(documentContext::getDiagnosticIgnorance, "context: diagnosticIgnorance");
    measureCollector.measureIt(documentContext::getCognitiveComplexityData, "context: cognitiveComplexity");
    measureCollector.measureIt(documentContext::getCyclomaticComplexityData, "context: cyclomaticComplexity");
    measureCollector.measureIt(documentContext::getMetrics, "context: metrics");
}
Also used : SDBLTokenizer(com.github._1c_syntax.bsl.parser.SDBLTokenizer) Order(org.springframework.core.annotation.Order) SneakyThrows(lombok.SneakyThrows) EventListener(org.springframework.context.event.EventListener)

Example 54 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project sonar-bsl-plugin-community by 1c-syntax.

the class BSLCoreSensor method processFile.

private void processFile(InputFile inputFile, ServerContext bslServerContext) {
    URI uri = inputFile.uri();
    String content;
    try {
        content = IOUtils.toString(inputFile.inputStream(), StandardCharsets.UTF_8);
    } catch (IOException e) {
        LOGGER.warn("Can't read content of file " + uri, e);
        content = "";
    }
    DocumentContext documentContext = bslServerContext.addDocument(uri, content, 1);
    if (langServerEnabled) {
        documentContext.getDiagnostics().forEach(diagnostic -> issuesLoader.createIssue(inputFile, diagnostic));
    }
    saveCpd(inputFile, documentContext);
    highlighter.saveHighlighting(inputFile, documentContext);
    saveMeasures(inputFile, documentContext);
    documentContext.clearSecondaryData();
}
Also used : IOException(java.io.IOException) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) URI(java.net.URI)

Example 55 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project sonar-bsl-plugin-community by 1c-syntax.

the class BSLHighlighter method saveHighlighting.

public void saveHighlighting(InputFile inputFile, DocumentContext documentContext) {
    Set<HighlightingData> highlightingData = new HashSet<>(documentContext.getTokens().size());
    // populate bsl highlight data
    documentContext.getTokens().forEach(token -> highlightToken(token, highlightingData, getTypeOfTextBSL(token.getType())));
    // compute and populate sdbl highlight data
    Map<Integer, List<Token>> queryTokens = documentContext.getQueries().stream().map(Tokenizer::getTokens).flatMap(Collection::stream).collect(Collectors.groupingBy(Token::getLine));
    Map<Integer, Set<HighlightingData>> highlightingDataSDBL = new HashMap<>(queryTokens.size());
    queryTokens.values().stream().flatMap(Collection::stream).forEach(token -> highlightToken(token, highlightingDataSDBL.computeIfAbsent(token.getLine(), BSLHighlighter::newHashSet), getTypeOfTextSDBL(token.getType())));
    // find bsl strings to check overlap with sdbl tokens
    Set<HighlightingData> strings = highlightingData.stream().filter(data -> data.getType() == TypeOfText.STRING).collect(Collectors.toSet());
    strings.forEach((HighlightingData string) -> {
        Range stringRange = string.getRange();
        // find overlapping tokens
        Set<HighlightingData> dataOfCurrentLine = highlightingDataSDBL.get(stringRange.getStart().getLine());
        if (Objects.isNull(dataOfCurrentLine)) {
            return;
        }
        List<HighlightingData> currentTokens = dataOfCurrentLine.stream().filter(sdblData -> Ranges.containsRange(stringRange, sdblData.getRange())).sorted(Comparator.comparing(data -> data.getRange().getStart().getCharacter())).collect(Collectors.toList());
        if (currentTokens.isEmpty()) {
            return;
        }
        // disable current bsl token
        string.setActive(false);
        // split current bsl token to parts excluding sdbl tokens
        Position start = stringRange.getStart();
        int line = start.getLine();
        int startChar;
        int endChar = start.getCharacter();
        for (HighlightingData currentToken : currentTokens) {
            startChar = endChar;
            endChar = currentToken.getRange().getStart().getCharacter();
            TypeOfText typeOfText = string.getType();
            if (startChar < endChar) {
                // add string part
                highlightingData.add(new HighlightingData(line, startChar, endChar, typeOfText));
            }
            endChar = currentToken.getRange().getEnd().getCharacter();
        }
        // add final string part
        startChar = endChar;
        endChar = string.getRange().getEnd().getCharacter();
        TypeOfText typeOfText = string.getType();
        if (startChar < endChar) {
            highlightingData.add(new HighlightingData(line, startChar, endChar, typeOfText));
        }
    });
    // merge collected bsl tokens with sdbl tokens
    highlightingDataSDBL.values().forEach(highlightingData::addAll);
    if (highlightingData.stream().filter(HighlightingData::isActive).findAny().isEmpty()) {
        return;
    }
    // save only active tokens
    NewHighlighting highlighting = context.newHighlighting().onFile(inputFile);
    highlightingData.stream().filter(HighlightingData::isActive).forEach(data -> highlighting.highlight(data.getRange().getStart().getLine(), data.getRange().getStart().getCharacter(), data.getRange().getEnd().getLine(), data.getRange().getEnd().getCharacter(), data.getType()));
    highlighting.save();
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Token(org.antlr.v4.runtime.Token) Range(org.eclipse.lsp4j.Range) HashMap(java.util.HashMap) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) HashSet(java.util.HashSet) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) Map(java.util.Map) Position(org.eclipse.lsp4j.Position) Nullable(javax.annotation.Nullable) SDBLLexer(com.github._1c_syntax.bsl.parser.SDBLLexer) BSLLexer(com.github._1c_syntax.bsl.parser.BSLLexer) Collection(java.util.Collection) Tokenizer(com.github._1c_syntax.bsl.parser.Tokenizer) Set(java.util.Set) EqualsAndHashCode(lombok.EqualsAndHashCode) SensorContext(org.sonar.api.batch.sensor.SensorContext) Collectors(java.util.stream.Collectors) TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) Objects(java.util.Objects) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) List(java.util.List) Data(lombok.Data) Comparator(java.util.Comparator) TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) List(java.util.List) Tokenizer(com.github._1c_syntax.bsl.parser.Tokenizer) HashSet(java.util.HashSet)

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