Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Использование сонаровского DI для инстанцирования компонентов #108

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,48 +72,43 @@
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<String> 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);

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<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
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;
import org.sonar.api.batch.fs.InputFile;
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;
Expand All @@ -48,7 +51,8 @@
import java.util.Set;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@ScannerSide
@SonarLintSide
public class BSLHighlighter {

private static final Set<Integer> BSL_KEYWORDS = createBslKeywords();
Expand All @@ -69,7 +73,8 @@ public class BSLHighlighter {
private static final Set<Integer> SDBL_COMMENTS = createSdblComments();
private static final Set<Integer> SDBL_PARAMETERS = createSdblParameters();

private final SensorContext context;
@Setter
private SensorContext context;

public void saveHighlighting(InputFile inputFile, DocumentContext documentContext) {
Set<HighlightingData> highlightingData = new HashSet<>(documentContext.getTokens().size());
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/github/_1c_syntax/bsl/sonar/IssuesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DiagnosticSeverity, Severity> severityMap;
private final Map<DiagnosticSeverity, RuleType> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -82,25 +83,28 @@ 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();

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();

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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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"))
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down Expand Up @@ -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<Token> tokens = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class LanguageServerDiagnosticsLoaderSensorTest {

Expand All @@ -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);

Expand All @@ -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));
Expand All @@ -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();
Expand Down