Search in sources :

Example 1 with DiagnosticInfo

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

use of com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo 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)

Example 3 with DiagnosticInfo

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

the class DiagnosticsConfiguration method diagnostics.

@Bean
@Scope("prototype")
public List<BSLDiagnostic> diagnostics(DocumentContext documentContext) {
    Collection<DiagnosticInfo> diagnosticInfos = diagnosticInfos();
    DiagnosticsOptions diagnosticsOptions = configuration.getDiagnosticsOptions();
    if (needToComputeDiagnostics(documentContext, diagnosticsOptions)) {
        FileType fileType = documentContext.getFileType();
        CompatibilityMode compatibilityMode = documentContext.getServerContext().getConfiguration().getCompatibilityMode();
        ModuleType moduleType = documentContext.getModuleType();
        return diagnosticInfos.stream().filter(diagnosticInfo -> isEnabled(diagnosticInfo, diagnosticsOptions)).filter(info -> inScope(info, fileType)).filter(info -> correctModuleType(info, moduleType, fileType)).filter(info -> passedCompatibilityMode(info, compatibilityMode)).map(DiagnosticInfo::getDiagnosticClass).map(diagnosticObjectProvider::get).collect(Collectors.toList());
    } else {
        return Collections.emptyList();
    }
}
Also used : SupportConfiguration(com.github._1c_syntax.mdclasses.supportconf.SupportConfiguration) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Scope(org.springframework.context.annotation.Scope) FileType(com.github._1c_syntax.bsl.languageserver.context.FileType) DiagnosticScope(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) DiagnosticCompatibilityMode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCompatibilityMode) Map(java.util.Map) DiagnosticsOptions(com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.DiagnosticsOptions) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) Mode(com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.Mode) SupportVariant(com.github._1c_syntax.mdclasses.supportconf.SupportVariant) Collection(java.util.Collection) ModuleType(com.github._1c_syntax.mdclasses.mdo.support.ModuleType) BSLDiagnostic(com.github._1c_syntax.bsl.languageserver.diagnostics.BSLDiagnostic) Collectors(java.util.stream.Collectors) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SkipSupport(com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.SkipSupport) Lookup(org.springframework.beans.factory.annotation.Lookup) Bean(org.springframework.context.annotation.Bean) CompatibilityMode(com.github._1c_syntax.mdclasses.common.CompatibilityMode) Comparator(java.util.Comparator) Collections(java.util.Collections) DiagnosticsOptions(com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.DiagnosticsOptions) ModuleType(com.github._1c_syntax.mdclasses.mdo.support.ModuleType) FileType(com.github._1c_syntax.bsl.languageserver.context.FileType) DiagnosticCompatibilityMode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCompatibilityMode) CompatibilityMode(com.github._1c_syntax.mdclasses.common.CompatibilityMode) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) Scope(org.springframework.context.annotation.Scope) DiagnosticScope(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope) Bean(org.springframework.context.annotation.Bean)

Example 4 with DiagnosticInfo

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

the class DiagnosticsConfiguration method inScope.

private static boolean inScope(DiagnosticInfo diagnosticInfo, FileType fileType) {
    DiagnosticScope scope = diagnosticInfo.getScope();
    DiagnosticScope fileScope;
    if (fileType == FileType.OS) {
        fileScope = DiagnosticScope.OS;
    } else {
        fileScope = DiagnosticScope.BSL;
    }
    return scope == DiagnosticScope.ALL || scope == fileScope;
}
Also used : DiagnosticScope(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope)

Example 5 with DiagnosticInfo

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

the class DiagnosticsConfiguration method correctModuleType.

private static boolean correctModuleType(DiagnosticInfo diagnosticInfo, ModuleType moduletype, FileType fileType) {
    if (fileType == FileType.OS) {
        return true;
    }
    ModuleType[] diagnosticModules = diagnosticInfo.getModules();
    if (diagnosticModules.length == 0) {
        return true;
    }
    boolean contain = false;
    for (ModuleType module : diagnosticModules) {
        if (module == moduletype) {
            contain = true;
            break;
        }
    }
    return contain;
}
Also used : ModuleType(com.github._1c_syntax.mdclasses.mdo.support.ModuleType)

Aggregations

LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)5 DiagnosticInfo (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)5 DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)4 Collections (java.util.Collections)4 TestUtils (com.github._1c_syntax.bsl.languageserver.util.TestUtils)3 Collection (java.util.Collection)3 Map (java.util.Map)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Diagnostic (org.eclipse.lsp4j.Diagnostic)3 Test (org.junit.jupiter.api.Test)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 SkipSupport (com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.SkipSupport)2 CanonicalSpellingKeywordsDiagnostic (com.github._1c_syntax.bsl.languageserver.diagnostics.CanonicalSpellingKeywordsDiagnostic)2 DiagnosticCode (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode)2 DiagnosticScope (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope)2 ModuleType (com.github._1c_syntax.mdclasses.mdo.support.ModuleType)2