Search in sources :

Example 1 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 2 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)

Example 3 with DocumentContext

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

the class JUnitReporterTest method report.

@Test
void report() throws IOException {
    // given
    List<Diagnostic> diagnostics = new ArrayList<>();
    diagnostics.add(new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test"));
    diagnostics.add(new Diagnostic(Ranges.create(0, 1, 2, 4), "message4", DiagnosticSeverity.Error, "test-source2", "test3"));
    diagnostics.add(new Diagnostic(Ranges.create(3, 1, 4, 4), "message4", DiagnosticSeverity.Error, "test-source2", "test3"));
    DocumentContext documentContext = TestUtils.getDocumentContext(Paths.get("./src/test/java/diagnostics/CanonicalSpellingKeywordsDiagnostic.bsl").toUri(), "");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, diagnostics);
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    DiagnosticReporter reporter = new JUnitReporter();
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new XmlMapper();
    JUnitTestSuites report = mapper.readValue(file, JUnitTestSuites.class);
    assertThat(report).isNotNull();
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with DocumentContext

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

the class JsonReporterTest method report.

@Test
void report() throws IOException {
    // given
    Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    JsonReporter reporter = new JsonReporter();
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new AnalysisInfoObjectMapper();
    mapper.findAndRegisterModules();
    AnalysisInfo report = mapper.readValue(file, AnalysisInfo.class);
    Assertions.assertThat(report.getFileinfos()).hasSize(1);
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) AnalysisInfoObjectMapper(com.github._1c_syntax.bsl.languageserver.reporters.databind.AnalysisInfoObjectMapper) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AnalysisInfoObjectMapper(com.github._1c_syntax.bsl.languageserver.reporters.databind.AnalysisInfoObjectMapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with DocumentContext

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

the class TSLintReporterTest method report.

@Test
void report() throws IOException {
    // given
    Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    TSLintReporter reporter = new TSLintReporter();
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new ObjectMapper();
    List<TSLintReportEntry> report = mapper.readValue(file, new TypeReference<ArrayList<TSLintReportEntry>>() {
    });
    assertThat(report).hasSize(1);
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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