Search in sources :

Example 1 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 checkIgnoreOn.

private boolean checkIgnoreOn(Pattern ignoreOn, Token comment) {
    Matcher matcher = ignoreOn.matcher(comment.getText());
    if (!matcher.find()) {
        return false;
    }
    DiagnosticCode key = getKey(matcher);
    Deque<Integer> stack = ignoranceStack.computeIfAbsent(key, s -> new ArrayDeque<>());
    if (stack.isEmpty()) {
        return false;
    }
    int ignoreRangeStart = stack.pop();
    int ignoreRangeEnd = comment.getLine();
    addIgnoredRange(key, ignoreRangeStart, ignoreRangeEnd);
    return true;
}
Also used : Matcher(java.util.regex.Matcher) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode)

Example 2 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 checkIgnoreOff.

@CheckForNull
private DiagnosticCode checkIgnoreOff(Pattern ignoreOff, Token comment) {
    Matcher matcher = ignoreOff.matcher(comment.getText());
    if (!matcher.find()) {
        return null;
    }
    DiagnosticCode key = getKey(matcher);
    Deque<Integer> stack = ignoranceStack.computeIfAbsent(key, s -> new ArrayDeque<>());
    stack.push(comment.getLine());
    return key;
}
Also used : Matcher(java.util.regex.Matcher) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) CheckForNull(javax.annotation.CheckForNull)

Example 3 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 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 4 with DiagnosticCode

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

the class QuickFixSupplierTest method testGetQuickFixClass.

@Test
void testGetQuickFixClass() {
    Optional<Class<? extends QuickFixProvider>> quickFixClass = quickFixSupplier.getQuickFixClass(new DiagnosticCode("NON_EXISTING"));
    assertThat(quickFixClass).isEmpty();
    quickFixClass = quickFixSupplier.getQuickFixClass(new DiagnosticCode("CommitTransactionOutsideTryCatch"));
    assertThat(quickFixClass).isEmpty();
    quickFixClass = quickFixSupplier.getQuickFixClass(new DiagnosticCode("CommentedCode"));
    assertThat(quickFixClass).hasValue(CommentedCodeDiagnostic.class);
}
Also used : QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with DiagnosticCode

use of com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode in project sonar-bsl-plugin-community by 1c-syntax.

the class BSLCoreSensor method getLanguageServerConfiguration.

private LanguageServerConfiguration getLanguageServerConfiguration() {
    boolean overrideConfiguration = context.config().get(BSLCommunityProperties.LANG_SERVER_OVERRIDE_CONFIGURATION_KEY).map(Boolean::parseBoolean).orElse(BSLCommunityProperties.LANG_SERVER_OVERRIDE_CONFIGURATION_DEFAULT_VALUE);
    var configuration = BSLLSBinding.getLanguageServerConfiguration();
    if (overrideConfiguration) {
        String configurationPath = context.config().get(BSLCommunityProperties.LANG_SERVER_CONFIGURATION_PATH_KEY).orElse(BSLCommunityProperties.LANG_SERVER_CONFIGURATION_PATH_DEFAULT_VALUE);
        File configurationFile = new File(configurationPath);
        if (configurationFile.exists()) {
            LOGGER.info("BSL LS configuration file exists. Overriding SonarQube rules' settings...");
            configuration.update(configurationFile);
            return configuration;
        } else {
            LOGGER.error("Can't find bsl configuration file {}. Using SonarQube config instead.", configurationPath);
        }
    }
    String diagnosticLanguageCode = context.config().get(BSLCommunityProperties.LANG_SERVER_DIAGNOSTIC_LANGUAGE_KEY).orElse(BSLCommunityProperties.LANG_SERVER_DIAGNOSTIC_LANGUAGE_DEFAULT_VALUE);
    configuration.setLanguage(Language.valueOf(diagnosticLanguageCode.toUpperCase(Locale.ENGLISH)));
    SkipSupport skipSupport = context.config().get(BSLCommunityProperties.LANG_SERVER_COMPUTE_DIAGNOSTICS_SKIP_SUPPORT_KEY).map(value -> value.toUpperCase(Locale.ENGLISH).replace(" ", "_")).map(SkipSupport::valueOf).orElse(SkipSupport.valueOf(BSLCommunityProperties.LANG_SERVER_COMPUTE_DIAGNOSTICS_SKIP_SUPPORT_DEFAULT_VALUE.toUpperCase(Locale.ENGLISH)));
    configuration.getDiagnosticsOptions().setSkipSupport(skipSupport);
    ActiveRules activeRules = context.activeRules();
    Map<String, Either<Boolean, Map<String, Object>>> diagnostics = new HashMap<>();
    Collection<DiagnosticInfo> diagnosticInfos = BSLLSBinding.getDiagnosticInfos();
    for (DiagnosticInfo diagnosticInfo : diagnosticInfos) {
        String diagnosticCode = diagnosticInfo.getCode().getStringValue();
        ActiveRule activeRule = activeRules.find(RuleKey.of(BSLLanguageServerRuleDefinition.REPOSITORY_KEY, diagnosticCode));
        if (activeRule == null) {
            diagnostics.put(diagnosticCode, Either.forLeft(false));
        } else {
            Map<String, String> params = activeRule.params();
            List<DiagnosticParameterInfo> diagnosticParameters = diagnosticInfo.getParameters();
            Map<String, Object> diagnosticConfiguration = new HashMap<>(diagnosticParameters.size());
            params.forEach((String key, String value) -> diagnosticInfo.getParameter(key).ifPresent(diagnosticParameterInfo -> diagnosticConfiguration.put(key, castDiagnosticParameterValue(value, diagnosticParameterInfo.getType()))));
            diagnostics.put(diagnosticCode, Either.forRight(diagnosticConfiguration));
        }
    }
    configuration.getDiagnosticsOptions().setParameters(diagnostics);
    return configuration;
}
Also used : ActiveRule(org.sonar.api.batch.rule.ActiveRule) Arrays(java.util.Arrays) Language(com.github._1c_syntax.bsl.languageserver.configuration.Language) Token(org.antlr.v4.runtime.Token) ActiveRules(org.sonar.api.batch.rule.ActiveRules) StringUtils(org.apache.commons.lang3.StringUtils) FilePredicates(org.sonar.api.batch.fs.FilePredicates) Absolute(com.github._1c_syntax.utils.Absolute) FileLinesContext(org.sonar.api.measures.FileLinesContext) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) Loggers(org.sonar.api.utils.log.Loggers) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Locale(java.util.Locale) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) URI(java.net.URI) MetricStorage(com.github._1c_syntax.bsl.languageserver.context.MetricStorage) Path(java.nio.file.Path) ServerContext(com.github._1c_syntax.bsl.languageserver.context.ServerContext) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) Collection(java.util.Collection) FileSystem(org.sonar.api.batch.fs.FileSystem) SensorContext(org.sonar.api.batch.sensor.SensorContext) Collectors(java.util.stream.Collectors) CoreMetrics(org.sonar.api.measures.CoreMetrics) StandardCharsets(java.nio.charset.StandardCharsets) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) RuleKey(org.sonar.api.rule.RuleKey) SkipSupport(com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.SkipSupport) ProgressBarBuilder(me.tongfei.progressbar.ProgressBarBuilder) InputFile(org.sonar.api.batch.fs.InputFile) Sensor(org.sonar.api.batch.sensor.Sensor) HashMap(java.util.HashMap) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) BSLLSBinding(com.github._1c_syntax.bsl.languageserver.BSLLSBinding) BSLLanguage(com.github._1c_syntax.bsl.sonar.language.BSLLanguage) ArrayList(java.util.ArrayList) StreamSupport(java.util.stream.StreamSupport) Logger(org.sonar.api.utils.log.Logger) ProgressBar(me.tongfei.progressbar.ProgressBar) BSLLexer(com.github._1c_syntax.bsl.parser.BSLLexer) IOException(java.io.IOException) File(java.io.File) ProgressBarStyle(me.tongfei.progressbar.ProgressBarStyle) DiagnosticParameterInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticParameterInfo) BSLLanguageServerRuleDefinition(com.github._1c_syntax.bsl.sonar.language.BSLLanguageServerRuleDefinition) Collections(java.util.Collections) HashMap(java.util.HashMap) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) ActiveRule(org.sonar.api.batch.rule.ActiveRule) DiagnosticParameterInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticParameterInfo) SkipSupport(com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.SkipSupport) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File)

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