diff --git a/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java b/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java index 64dc0abf..ecee3e47 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java +++ b/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java @@ -33,6 +33,7 @@ import com.github._1c_syntax.bsl.sonar.language.BSLLanguage; import com.github._1c_syntax.bsl.sonar.language.BSLLanguageServerRuleDefinition; import com.github._1c_syntax.utils.Absolute; +import lombok.RequiredArgsConstructor; import me.tongfei.progressbar.ProgressBar; import me.tongfei.progressbar.ProgressBarStyle; import org.antlr.v4.runtime.Token; @@ -71,22 +72,29 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; +@RequiredArgsConstructor public class BSLCoreSensor implements Sensor { private static final Logger LOGGER = Loggers.get(BSLCoreSensor.class); - private final SensorContext context; private final FileLinesContextFactory fileLinesContextFactory; - - private final boolean langServerEnabled; - private final List sourcesList; - private final IssuesLoader issuesLoader; private final BSLHighlighter highlighter; + private final IssuesLoader issuesLoader; - private final boolean calculateCoverLoc; + private SensorContext context; + private boolean langServerEnabled; + private boolean calculateCoverLoc; - public BSLCoreSensor(SensorContext context, FileLinesContextFactory fileLinesContextFactory) { + @Override + public void describe(SensorDescriptor descriptor) { + descriptor.name("BSL Core Sensor"); + descriptor.onlyOnLanguage(BSLLanguage.KEY); + } + + @Override + public void execute(SensorContext context) { this.context = context; - this.fileLinesContextFactory = fileLinesContextFactory; + highlighter.setContext(context); + issuesLoader.setContext(context); langServerEnabled = context.config().getBoolean(BSLCommunityProperties.LANG_SERVER_ENABLED_KEY) .orElse(BSLCommunityProperties.LANG_SERVER_ENABLED_DEFAULT_VALUE); @@ -94,25 +102,13 @@ public BSLCoreSensor(SensorContext context, FileLinesContextFactory fileLinesCon calculateCoverLoc = context.config().getBoolean(BSLCommunityProperties.BSL_CALCULATE_LINE_TO_COVER_KEY) .orElse(BSLCommunityProperties.BSL_CALCULATE_LINE_TO_COVER_VALUE); - sourcesList = context.config().get("sonar.sources") + List sourcesList = context.config().get("sonar.sources") .map(sources -> Arrays.stream(StringUtils.split(sources, ",")) .map(String::strip) .collect(Collectors.toList())) .orElse(Collections.singletonList(".")); - issuesLoader = new IssuesLoader(context); - highlighter = new BSLHighlighter(context); - } - - @Override - public void describe(SensorDescriptor descriptor) { - descriptor.name("BSL Core Sensor"); - descriptor.onlyOnLanguage(BSLLanguage.KEY); - } - - @Override - public void execute(SensorContext context) { LOGGER.info("Parsing files..."); FileSystem fileSystem = context.fileSystem(); diff --git a/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighter.java b/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighter.java index 893d1c84..e7cffca8 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighter.java +++ b/src/main/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighter.java @@ -29,6 +29,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.antlr.v4.runtime.Token; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; @@ -36,6 +37,8 @@ import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.highlighting.NewHighlighting; import org.sonar.api.batch.sensor.highlighting.TypeOfText; +import org.sonar.api.scanner.ScannerSide; +import org.sonarsource.api.sonarlint.SonarLintSide; import javax.annotation.Nullable; import java.util.Collection; @@ -48,7 +51,8 @@ import java.util.Set; import java.util.stream.Collectors; -@RequiredArgsConstructor +@ScannerSide +@SonarLintSide public class BSLHighlighter { private static final Set BSL_KEYWORDS = createBslKeywords(); @@ -69,7 +73,8 @@ public class BSLHighlighter { private static final Set SDBL_COMMENTS = createSdblComments(); private static final Set SDBL_PARAMETERS = createSdblParameters(); - private final SensorContext context; + @Setter + private SensorContext context; public void saveHighlighting(InputFile inputFile, DocumentContext documentContext) { Set highlightingData = new HashSet<>(documentContext.getTokens().size()); diff --git a/src/main/java/com/github/_1c_syntax/bsl/sonar/IssuesLoader.java b/src/main/java/com/github/_1c_syntax/bsl/sonar/IssuesLoader.java index 65ce401c..e9ca7a6d 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/sonar/IssuesLoader.java +++ b/src/main/java/com/github/_1c_syntax/bsl/sonar/IssuesLoader.java @@ -43,8 +43,10 @@ import org.sonar.api.batch.sensor.issue.NewIssueLocation; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleType; +import org.sonar.api.scanner.ScannerSide; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonarsource.api.sonarlint.SonarLintSide; import javax.annotation.CheckForNull; import java.net.URI; @@ -54,23 +56,28 @@ import java.util.List; import java.util.Map; +@ScannerSide +@SonarLintSide public class IssuesLoader { private static final Logger LOGGER = Loggers.get(IssuesLoader.class); - private final SensorContext context; private final Map severityMap; private final Map ruleTypeMap; - private final FileSystem fileSystem; - private final FilePredicates predicates; - private final boolean createExternalIssuesWithACCSources; + private SensorContext context; + private FileSystem fileSystem; + private FilePredicates predicates; + private boolean createExternalIssuesWithACCSources; - public IssuesLoader(SensorContext context) { + public IssuesLoader() { + this.severityMap = createDiagnosticSeverityMap(); + this.ruleTypeMap = createRuleTypeMap(); + } + + public void setContext(SensorContext context) { this.context = context; this.fileSystem = context.fileSystem(); this.predicates = fileSystem.predicates(); - this.severityMap = createDiagnosticSeverityMap(); - this.ruleTypeMap = createRuleTypeMap(); this.createExternalIssuesWithACCSources = context.config().getBoolean(ACCProperties.CREATE_EXTERNAL_ISSUES) .orElse(ACCProperties.CREATE_EXTERNAL_ISSUES_DEFAULT_VALUE); } diff --git a/src/main/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensor.java b/src/main/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensor.java index 77ed1670..c980fba7 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensor.java +++ b/src/main/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensor.java @@ -57,10 +57,9 @@ public class LanguageServerDiagnosticsLoaderSensor implements Sensor { private static final Logger LOGGER = Loggers.get(LanguageServerDiagnosticsLoaderSensor.class); - public LanguageServerDiagnosticsLoaderSensor(final SensorContext context) { + public LanguageServerDiagnosticsLoaderSensor(final SensorContext context, IssuesLoader issueLoader) { this.context = context; - this.issueLoader = new IssuesLoader(context); - + this.issueLoader = issueLoader; } @Override diff --git a/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensorTest.java b/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensorTest.java index e564dcb3..8db2609a 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensorTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensorTest.java @@ -54,13 +54,14 @@ class BSLCoreSensorTest { private final File BASE_DIR = new File(BASE_PATH).getAbsoluteFile(); private final String FILE_NAME = "test.bsl"; private final Version SONAR_VERSION = Version.create(7, 9); - private final SensorContextTester context = SensorContextTester.create(BASE_DIR); @Test void testDescriptor() { FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class); + BSLHighlighter highlighter = mock(BSLHighlighter.class); + IssuesLoader issuesLoader = mock(IssuesLoader.class); - BSLCoreSensor sensor = new BSLCoreSensor(context, fileLinesContextFactory); + BSLCoreSensor sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor(); sensor.describe(sensorDescriptor); @@ -82,9 +83,12 @@ void testExecute() { FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class); when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext); + BSLHighlighter highlighter = mock(BSLHighlighter.class); + IssuesLoader issuesLoader = mock(IssuesLoader.class); + context = createSensorContext(); setActiveRules(context, diagnosticName, ruleKey); - sensor = new BSLCoreSensor(context, fileLinesContextFactory); + sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); assertThat(context.isCancelled()).isFalse(); @@ -92,7 +96,7 @@ void testExecute() { context = createSensorContext(); setActiveRules(context, diagnosticName, ruleKey); context.settings().setProperty(BSLCommunityProperties.LANG_SERVER_ENABLED_KEY, false); - sensor = new BSLCoreSensor(context, fileLinesContextFactory); + sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); assertThat(context.isCancelled()).isFalse(); @@ -100,7 +104,7 @@ void testExecute() { context = createSensorContext(); setActiveRules(context, diagnosticName, ruleKey); context.settings().setProperty(BSLCommunityProperties.LANG_SERVER_DIAGNOSTIC_LANGUAGE_KEY, Language.EN.getLanguageCode()); - sensor = new BSLCoreSensor(context, fileLinesContextFactory); + sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); assertThat(context.isCancelled()).isFalse(); @@ -109,7 +113,7 @@ void testExecute() { setActiveRules(context, diagnosticName, ruleKey); context.settings().setProperty(BSLCommunityProperties.LANG_SERVER_OVERRIDE_CONFIGURATION_KEY, Boolean.TRUE.toString()); context.settings().setProperty(BSLCommunityProperties.LANG_SERVER_CONFIGURATION_PATH_KEY, Path.of(BASE_PATH, ".bsl-language-server.json").toString()); - sensor = new BSLCoreSensor(context, fileLinesContextFactory); + sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); assertThat(context.isCancelled()).isFalse(); @@ -118,7 +122,7 @@ void testExecute() { setActiveRules(context, diagnosticName, ruleKey); context.settings().setProperty(BSLCommunityProperties.LANG_SERVER_OVERRIDE_CONFIGURATION_KEY, Boolean.TRUE.toString()); context.settings().setProperty(BSLCommunityProperties.LANG_SERVER_CONFIGURATION_PATH_KEY, "fake.file"); - sensor = new BSLCoreSensor(context, fileLinesContextFactory); + sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); assertThat(context.isCancelled()).isFalse(); @@ -137,6 +141,9 @@ void testExecuteCastDiagnosticParameterValue() { FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class); when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext); + BSLHighlighter highlighter = mock(BSLHighlighter.class); + IssuesLoader issuesLoader = mock(IssuesLoader.class); + SensorContextTester context = createSensorContext(); ActiveRules activeRules = new ActiveRulesBuilder() .addRule(newActiveRule(diagnosticEmptyCodeBlock, "commentAsCode", "true")) @@ -150,7 +157,7 @@ void testExecuteCastDiagnosticParameterValue() { .build(); context.setActiveRules(activeRules); - BSLCoreSensor sensor = new BSLCoreSensor(context, fileLinesContextFactory); + BSLCoreSensor sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); assertThat(context.isCancelled()).isFalse(); @@ -169,11 +176,14 @@ void testExecuteCoverage() { FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class); when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext); + BSLHighlighter highlighter = mock(BSLHighlighter.class); + IssuesLoader issuesLoader = mock(IssuesLoader.class); + context = createSensorContext(); context.settings().setProperty(BSLCommunityProperties.LANG_SERVER_ENABLED_KEY, false); context.settings().setProperty(BSLCommunityProperties.BSL_CALCULATE_LINE_TO_COVER_KEY, true); setActiveRules(context, diagnosticName, ruleKey); - sensor = new BSLCoreSensor(context, fileLinesContextFactory); + sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); assertThat(context.isCancelled()).isFalse(); @@ -195,7 +205,10 @@ void testComplexity() { FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class); when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext); - BSLCoreSensor sensor = new BSLCoreSensor(context, fileLinesContextFactory); + BSLHighlighter highlighter = mock(BSLHighlighter.class); + IssuesLoader issuesLoader = mock(IssuesLoader.class); + + BSLCoreSensor sensor = new BSLCoreSensor(fileLinesContextFactory, highlighter, issuesLoader); sensor.execute(context); diff --git a/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighterTest.java b/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighterTest.java index fc34fe72..908aa647 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighterTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighterTest.java @@ -84,7 +84,8 @@ void testHighlightingSDBL() { void testMergeHighlightingTokens() { // given context = SensorContextTester.create(Path.of(".")); - highlighter = new BSLHighlighter(context); + highlighter = new BSLHighlighter(); + highlighter.setContext(context); String content = "А = \"ВЫБРАТЬ РАЗРЕШЕННЫЕ Поле.Один \n" + "|КАК \n" + "| Один, 2 \n" + @@ -153,7 +154,8 @@ private void checkTokenTypeAtPosition(String componentKey, int line, int charact private void initContext(Vocabulary vocabulary) { context = SensorContextTester.create(Path.of(".")); - highlighter = new BSLHighlighter(context); + highlighter = new BSLHighlighter(); + highlighter.setContext(context); documentContext = mock(DocumentContext.class); List tokens = new ArrayList<>(); diff --git a/src/test/java/com/github/_1c_syntax/bsl/sonar/IssuesLoaderTest.java b/src/test/java/com/github/_1c_syntax/bsl/sonar/IssuesLoaderTest.java index fb30d10f..54408298 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/sonar/IssuesLoaderTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/sonar/IssuesLoaderTest.java @@ -54,7 +54,8 @@ void test_createExtIssue() { SensorContextTester context = SensorContextTester.create(BASE_DIR); InputFile inputFile = Tools.inputFileBSL(FILE_NAME, BASE_DIR); - IssuesLoader issuesLoader = new IssuesLoader(context); + IssuesLoader issuesLoader = new IssuesLoader(); + issuesLoader.setContext(context); Diagnostic diagnostic = new Diagnostic(); diagnostic.setCode(issueCode); @@ -91,7 +92,8 @@ void test_createIssue() { InputFile inputFile = Tools.inputFileBSL(FILE_NAME, BASE_DIR); context.fileSystem().add(inputFile); - IssuesLoader issuesLoader = new IssuesLoader(context); + IssuesLoader issuesLoader = new IssuesLoader(); + issuesLoader.setContext(context); Diagnostic diagnostic = new Diagnostic(); diagnostic.setCode(diagnosticName); diff --git a/src/test/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensorTest.java b/src/test/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensorTest.java index 653e5ac1..fafca2fd 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensorTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/sonar/LanguageServerDiagnosticsLoaderSensorTest.java @@ -37,6 +37,7 @@ import java.io.File; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; class LanguageServerDiagnosticsLoaderSensorTest { @@ -46,9 +47,9 @@ class LanguageServerDiagnosticsLoaderSensorTest { @Test void test_describe() { - SensorContextTester context = SensorContextTester.create(BASE_DIR); - LanguageServerDiagnosticsLoaderSensor diagnosticsLoaderSensor = new LanguageServerDiagnosticsLoaderSensor(context); + IssuesLoader issuesLoader = mock(IssuesLoader.class); + var diagnosticsLoaderSensor = new LanguageServerDiagnosticsLoaderSensor(context, issuesLoader); DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor(); diagnosticsLoaderSensor.describe(sensorDescriptor); @@ -58,6 +59,8 @@ void test_describe() { @Test void test_execute() { + IssuesLoader issuesLoader = mock(IssuesLoader.class); + InputFile inputFile = Tools.inputFileBSL(FILE_NAME, BASE_DIR); SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarLint(Version.create(7, 9)); @@ -76,7 +79,7 @@ void test_execute() { .build(); context.setActiveRules(activeRules); - LanguageServerDiagnosticsLoaderSensor diagnosticsLoaderSensor = new LanguageServerDiagnosticsLoaderSensor(context); + var diagnosticsLoaderSensor = new LanguageServerDiagnosticsLoaderSensor(context, issuesLoader); diagnosticsLoaderSensor.execute(context); assertThat(context.isCancelled()).isFalse();