Search in sources :

Example 6 with DiagnosticCode

use of com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode in project bsl-language-server by 1c-syntax.

the class DiagnosticIgnoranceComputer method checkTrailingComment.

private boolean checkTrailingComment(Set<Integer> codeLines, Token comment) {
    int commentLine = comment.getLine();
    if (!codeLines.contains(commentLine)) {
        return false;
    }
    DiagnosticCode key = checkIgnoreOff(IGNORE_ALL_OFF, comment);
    if (key == null) {
        key = checkIgnoreOff(IGNORE_DIAGNOSTIC_OFF, comment);
    }
    if (key == null) {
        return false;
    }
    Deque<Integer> stack = ignoranceStack.get(key);
    stack.pop();
    addIgnoredRange(key, commentLine, commentLine);
    return true;
}
Also used : DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode)

Example 7 with DiagnosticCode

use of com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode in project bsl-language-server by 1c-syntax.

the class DiagnosticIgnoranceComputer method compute.

@Override
public Data compute() {
    diagnosticIgnorance.clear();
    ignoranceStack.clear();
    List<Token> codeTokens = documentContext.getTokensFromDefaultChannel();
    if (codeTokens.isEmpty()) {
        return new Data(diagnosticIgnorance);
    }
    Set<Integer> codeLines = codeTokens.stream().map(Token::getLine).collect(Collectors.toSet());
    List<Token> comments = documentContext.getComments();
    for (Token comment : comments) {
        // Variable is used for short circuit evaluation.
        // noinspection unused
        boolean ignored = checkTrailingComment(codeLines, comment) || checkIgnoreOff(IGNORE_ALL_OFF, comment) != null || checkIgnoreOn(IGNORE_ALL_ON, comment) || checkIgnoreOff(IGNORE_DIAGNOSTIC_OFF, comment) != null || checkIgnoreOn(IGNORE_DIAGNOSTIC_ON, comment);
    }
    int lastTokenLine = codeTokens.get(codeTokens.size() - 1).getLine();
    ignoranceStack.forEach((DiagnosticCode diagnosticKey, Deque<Integer> ignoreRangeStarts) -> ignoreRangeStarts.forEach(ignoreRangeStart -> addIgnoredRange(diagnosticKey, ignoreRangeStart, lastTokenLine)));
    return new Data(diagnosticIgnorance);
}
Also used : Predicate(java.util.function.Predicate) Collection(java.util.Collection) Token(org.antlr.v4.runtime.Token) Set(java.util.Set) Diagnostic(org.eclipse.lsp4j.Diagnostic) HashMap(java.util.HashMap) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Deque(java.util.Deque) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Matcher(java.util.regex.Matcher) Map(java.util.Map) Pattern(java.util.regex.Pattern) AllArgsConstructor(lombok.AllArgsConstructor) ArrayDeque(java.util.ArrayDeque) CaseInsensitivePattern(com.github._1c_syntax.utils.CaseInsensitivePattern) CheckForNull(javax.annotation.CheckForNull) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Range(org.apache.commons.lang3.Range) Token(org.antlr.v4.runtime.Token) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque)

Example 8 with DiagnosticCode

use of com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode 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 9 with DiagnosticCode

use of com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode in project bsl-language-server by 1c-syntax.

the class CodeActionProviderTest method testGetCodeActions.

@Test
void testGetCodeActions() {
    // given
    DiagnosticInfo diagnosticInfo = new DiagnosticInfo(CanonicalSpellingKeywordsDiagnostic.class, configuration);
    DiagnosticCode diagnosticCode = diagnosticInfo.getCode();
    List<Diagnostic> diagnostics = documentContext.getDiagnostics().stream().filter(diagnostic -> diagnostic.getCode().equals(diagnosticCode)).peek(diagnostic -> diagnostic.setTags(null)).collect(Collectors.toList());
    CodeActionParams params = new CodeActionParams();
    TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(documentContext.getUri().toString());
    CodeActionContext codeActionContext = new CodeActionContext();
    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).hasSizeGreaterThanOrEqualTo(3).anyMatch(codeAction -> codeAction.getDiagnostics().contains(diagnostics.get(0))).anyMatch(codeAction -> codeAction.getDiagnostics().contains(diagnostics.get(1))).anyMatch(codeAction -> codeAction.getKind().equals(CodeActionKind.QuickFix)).allMatch(codeAction -> (codeAction.getDiagnostics().size() == 1) == toBoolean(codeAction.getIsPreferred()));
}
Also used : 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) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) 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 10 with DiagnosticCode

use of com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode in project bsl-language-server by 1c-syntax.

the class SmokyTest method testIAllDiagnostics.

@SneakyThrows
@Test
void testIAllDiagnostics() {
    // прочитаем все файлы ресурсов
    var srcDir = "./src/test/resources/";
    var fixtures = FileUtils.listFiles(new File(srcDir), new String[] { "bsl", "os" }, true);
    // получим все возможные коды диагностик и положим в мапу "включенным"
    Map<String, Either<Boolean, Map<String, Object>>> diagnostics = diagnosticInfos.stream().map(DiagnosticInfo::getCode).collect(Collectors.toMap(diagnosticCode -> diagnosticCode.getStringValue(), diagnosticCode -> Either.forLeft(true), (a, b) -> b));
    // создадим новый конфиг, в котором включим все диагностики
    configuration.getDiagnosticsOptions().setParameters(diagnostics);
    // для каждой фикстуры расчитаем диагностики
    // если упадет, запомним файл и текст ошибки
    Map<File, Exception> diagnosticErrors = new HashMap<>();
    fixtures.forEach(filePath -> {
        try {
            var documentContext = TestUtils.getDocumentContextFromFile(filePath.toString());
            documentContext.getDiagnostics();
        } catch (Exception e) {
            diagnosticErrors.put(filePath, e);
        }
    });
    assertThat(diagnosticErrors).isEmpty();
}
Also used : DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) BSLLSPLauncher(com.github._1c_syntax.bsl.languageserver.BSLLSPLauncher) SneakyThrows(lombok.SneakyThrows) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) FileUtils(org.apache.commons.io.FileUtils) Diagnostic(org.eclipse.lsp4j.Diagnostic) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) File(java.io.File) ExpectSystemExitWithStatus(com.ginsberg.junit.exit.ExpectSystemExitWithStatus) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Paths(java.nio.file.Paths) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) CleanupContextBeforeClassAndAfterEachTestMethod(com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterEachTestMethod) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) HashMap(java.util.HashMap) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) File(java.io.File) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Aggregations

DiagnosticCode (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)5 Diagnostic (org.eclipse.lsp4j.Diagnostic)5 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)5 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)4 DiagnosticInfo (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo)4 Collections (java.util.Collections)4 CheckForNull (javax.annotation.CheckForNull)4 Test (org.junit.jupiter.api.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 TestUtils (com.github._1c_syntax.bsl.languageserver.util.TestUtils)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 CodeAction (org.eclipse.lsp4j.CodeAction)3 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)3