Search in sources :

Example 1 with Diagnostics

use of com.github._1c_syntax.bsl.languageserver.jsonrpc.Diagnostics 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 2 with Diagnostics

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

the class CodeActionAssert method fixes.

public CodeActionAssert fixes(Diagnostic diagnostic) {
    // check that actual value we want to make assertions on is not null.
    isNotNull();
    // saving original state
    String cachedContent = documentContext.getContent();
    // apply edits from quick fix
    final List<TextEdit> textEdits = getTextEdits();
    String[] contentList = documentContext.getContentList();
    textEdits.forEach(textEdit -> {
        final String newText = textEdit.getNewText();
        final Range range = textEdit.getRange();
        final Position start = range.getStart();
        int startLine = 0;
        int startChar = 0;
        int endLine = start.getLine();
        int endChar = 0;
        if (start.getCharacter() > 0) {
            endChar = start.getCharacter() - 1;
        }
        Range startRange = Ranges.create(startLine, startChar, endLine, endChar);
        final String startText = documentContext.getText(startRange);
        final Position end = range.getEnd();
        startLine = end.getLine();
        startChar = end.getCharacter();
        endLine = contentList.length - 1;
        endChar = max(contentList[endLine].length() - 1, 0);
        Range endRange = Ranges.create(startLine, startChar, endLine, endChar);
        final String endText = documentContext.getText(endRange);
        // TODO: does not work for several textedits changing content length (missed semicolon ie.)
        String content = startText + newText + endText;
        documentContext.rebuild(content, documentContext.getVersion() + 1);
    });
    // get diagnostics from fixed document
    final List<Diagnostic> diagnostics = bslDiagnostic.getDiagnostics(documentContext);
    // check if expected diagnostic is not present in new diagnostic list
    Assertions.assertThat(diagnostics).doesNotContain(diagnostic);
    // returning to original state
    documentContext.rebuild(cachedContent, documentContext.getVersion() + 1);
    return this;
}
Also used : Position(org.eclipse.lsp4j.Position) TextEdit(org.eclipse.lsp4j.TextEdit) Diagnostic(org.eclipse.lsp4j.Diagnostic) BSLDiagnostic(com.github._1c_syntax.bsl.languageserver.diagnostics.BSLDiagnostic) Range(org.eclipse.lsp4j.Range)

Example 3 with Diagnostics

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

the class InvalidCharacterInFileDiagnostic method getQuickFixes.

@Override
public List<CodeAction> getQuickFixes(List<Diagnostic> diagnostics, CodeActionParams params, DocumentContext documentContext) {
    List<TextEdit> textEdits = new ArrayList<>();
    diagnostics.stream().filter(diagnostic -> diagnostic.getMessage().equals(diagnosticMessageSpace)).forEach((Diagnostic diagnostic) -> {
        Range range = diagnostic.getRange();
        TextEdit textEdit = new TextEdit(range, ILLEGAL_SPACE_PATTERN.matcher(documentContext.getText(range)).replaceAll(" "));
        textEdits.add(textEdit);
    });
    diagnostics.stream().filter(diagnostic -> diagnostic.getMessage().equals(diagnosticMessageDash)).forEach((Diagnostic diagnostic) -> {
        Range range = diagnostic.getRange();
        TextEdit textEdit = new TextEdit(range, ILLEGAL_PATTERN.matcher(documentContext.getText(range)).replaceAll("-"));
        textEdits.add(textEdit);
    });
    return CodeActionProvider.createCodeActions(textEdits, info.getResourceString("quickFixMessage"), documentContext.getUri(), diagnostics);
}
Also used : CodeAction(org.eclipse.lsp4j.CodeAction) CodeActionProvider(com.github._1c_syntax.bsl.languageserver.providers.CodeActionProvider) BSLLexer(com.github._1c_syntax.bsl.parser.BSLLexer) Token(org.antlr.v4.runtime.Token) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) DiagnosticType(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) DiagnosticTag(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag) ArrayList(java.util.ArrayList) Lexer(org.antlr.v4.runtime.Lexer) DiagnosticSeverity(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) List(java.util.List) DiagnosticMetadata(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata) TextEdit(org.eclipse.lsp4j.TextEdit) PostConstruct(javax.annotation.PostConstruct) Pattern(java.util.regex.Pattern) TextEdit(org.eclipse.lsp4j.TextEdit) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range)

Example 4 with Diagnostics

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

the class CodeActionProviderTest method testOnly.

@Test
void testOnly() {
    // given
    CodeActionParams params = new CodeActionParams();
    TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(documentContext.getUri().toString());
    DiagnosticInfo diagnosticInfo = new DiagnosticInfo(CanonicalSpellingKeywordsDiagnostic.class, configuration);
    DiagnosticCode diagnosticCode = diagnosticInfo.getCode();
    List<Diagnostic> diagnostics = documentContext.getDiagnostics().stream().filter(diagnostic -> diagnostic.getCode().equals(diagnosticCode)).collect(Collectors.toList());
    CodeActionContext codeActionContext = new CodeActionContext();
    codeActionContext.setOnly(List.of(CodeActionKind.Refactor));
    codeActionContext.setDiagnostics(diagnostics);
    params.setRange(new Range());
    params.setTextDocument(textDocumentIdentifier);
    params.setContext(codeActionContext);
    // when
    List<Either<Command, CodeAction>> codeActions = codeActionProvider.getCodeActions(params, documentContext);
    // then
    assertThat(codeActions).extracting(Either::getRight).extracting(CodeAction::getKind).containsOnly(CodeActionKind.Refactor);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) CodeAction(org.eclipse.lsp4j.CodeAction) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Collectors(java.util.stream.Collectors) CanonicalSpellingKeywordsDiagnostic(com.github._1c_syntax.bsl.languageserver.diagnostics.CanonicalSpellingKeywordsDiagnostic) Test(org.junit.jupiter.api.Test) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) List(java.util.List) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Command(org.eclipse.lsp4j.Command) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) CheckForNull(javax.annotation.CheckForNull) Collections(java.util.Collections) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) Diagnostic(org.eclipse.lsp4j.Diagnostic) CanonicalSpellingKeywordsDiagnostic(com.github._1c_syntax.bsl.languageserver.diagnostics.CanonicalSpellingKeywordsDiagnostic) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Range(org.eclipse.lsp4j.Range) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with Diagnostics

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

the class TimeoutsInExternalResourcesDiagnosticTest method testCompatibilityMode836.

@SneakyThrows
@Test
void testCompatibilityMode836() {
    // when
    FileUtils.writeStringToFile(Paths.get(tempDir.toAbsolutePath().toString(), "Configuration.xml").toFile(), FileUtils.readFileToString(CONFIGURATION_FILE_PATH, StandardCharsets.UTF_8).replace("Version8_3_10", "Version8_3_6"), StandardCharsets.UTF_8);
    Path testFile = Paths.get("./src/test/resources/diagnostics/TimeoutsInExternalResourcesDiagnostic836.bsl").toAbsolutePath();
    initServerContext(tempDir.toAbsolutePath());
    DocumentContext newDocumentContext = TestUtils.getDocumentContext(testFile.toUri(), FileUtils.readFileToString(testFile.toFile(), StandardCharsets.UTF_8), context);
    List<Diagnostic> diagnostics = getDiagnostics(newDocumentContext);
    // then
    assertThat(newDocumentContext.getServerContext().getConfiguration().getCompatibilityMode()).isNotNull();
    assertThat(CompatibilityMode.compareTo(newDocumentContext.getServerContext().getConfiguration().getCompatibilityMode(), DiagnosticCompatibilityMode.COMPATIBILITY_MODE_8_3_6.getCompatibilityMode())).isZero();
    assertThat(diagnostics).hasSize(9);
    // check ranges
    assertThat(diagnostics, true).hasRange(3, 20, 3, 75).hasRange(5, 20, 5, 92).hasRange(9, 18, 9, 72).hasRange(13, 16, 13, 80).hasRange(21, 21, 21, 65).hasRange(34, 14, 34, 43).hasRange(71, 26, 71, 114).hasRange(78, 10, 78, 39).hasRange(80, 47, 80, 76);
}
Also used : Path(java.nio.file.Path) Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Aggregations

DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)25 Diagnostic (org.eclipse.lsp4j.Diagnostic)24 Test (org.junit.jupiter.api.Test)19 CodeAction (org.eclipse.lsp4j.CodeAction)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 Path (java.nio.file.Path)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)5 DiagnosticInfo (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo)5 Collectors (java.util.stream.Collectors)5 SneakyThrows (lombok.SneakyThrows)5 Collections (java.util.Collections)4 Range (org.eclipse.lsp4j.Range)4 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)4 TestUtils (com.github._1c_syntax.bsl.languageserver.util.TestUtils)3 Collection (java.util.Collection)3 Map (java.util.Map)3 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2