Skip to content

Commit

Permalink
Реализовано размещение замечайни по диагностикам со свойством canLoca…
Browse files Browse the repository at this point in the history
…teOnProject на уровне проекта, а не файла

+ фиксы для новой версии bslls
  • Loading branch information
theshadowco committed Jan 12, 2024
1 parent 3cb89ce commit 74beb14
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
implementation("org.sonarsource.api.plugin", "sonar-plugin-api", "9.14.0.375")

// в jitpack лежат в группе com.github.1c-syntax, в централе - io.github.1c-syntax
implementation("io.github.1c-syntax", "bsl-language-server", "0.22.0") {
implementation("io.github.1c-syntax", "bsl-language-server", "0.23.0-rc.3") {
exclude("com.github.1c-syntax", "utils")
}
implementation("com.github.1c-syntax", "utils", "0.5.1")
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.SkipSupport;
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo;
import com.github._1c_syntax.bsl.parser.BSLLexer;
import com.github._1c_syntax.bsl.sonar.language.BSLLanguage;
Expand All @@ -36,6 +37,7 @@
import me.tongfei.progressbar.ProgressBarStyle;
import org.antlr.v4.runtime.Token;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.jetbrains.annotations.NotNull;
import org.sonar.api.batch.fs.InputFile;
Expand All @@ -55,9 +57,11 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand All @@ -72,6 +76,8 @@ public class BSLCoreSensor implements Sensor {
private final IssuesLoader issuesLoader;
private final BSLHighlighter highlighter;

private final Set<String> diagnosticsOnProject;

public BSLCoreSensor(SensorContext context, FileLinesContextFactory fileLinesContextFactory) {
this.context = context;
this.fileLinesContextFactory = fileLinesContextFactory;
Expand All @@ -95,6 +101,8 @@ public BSLCoreSensor(SensorContext context, FileLinesContextFactory fileLinesCon

issuesLoader = new IssuesLoader(context);
highlighter = new BSLHighlighter(context);

diagnosticsOnProject = new HashSet<>();
}

@Override
Expand Down Expand Up @@ -170,15 +178,20 @@ public void execute(SensorContext context) {
BSLLSBinding.getApplicationContext().close();
}


private void processFile(InputFile inputFile, ServerContext bslServerContext) {
var uri = inputFile.uri();
var documentContext = bslServerContext.addDocument(uri);
bslServerContext.rebuildDocument(documentContext);

if (langServerEnabled) {
documentContext.getDiagnostics()
.forEach(diagnostic -> issuesLoader.createIssue(inputFile, diagnostic));
.forEach((Diagnostic diagnostic) -> {
if (diagnosticsOnProject.contains(DiagnosticCode.getStringValue(diagnostic.getCode()))) {
issuesLoader.createIssue(Either.forRight(context.project()), diagnostic);
} else {
issuesLoader.createIssue(Either.forLeft(inputFile), diagnostic);
}
});
}

saveCpd(inputFile, documentContext);
Expand Down Expand Up @@ -221,7 +234,6 @@ private void saveCpd(InputFile inputFile, DocumentContext documentContext) {
synchronized (this) {
cpdTokens.save();
}

}

private void saveMeasures(InputFile inputFile, DocumentContext documentContext) {
Expand Down Expand Up @@ -332,6 +344,10 @@ private LanguageServerConfiguration getLanguageServerConfiguration() {
diagnosticCode,
Either.forRight(diagnosticConfiguration)
);

if (diagnosticInfo.canLocateOnProject()) {
diagnosticsOnProject.add(diagnosticCode);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class BSLHighlighter {
private static final Set<Integer> SDBL_STRINGS = createSdblStrings();
private static final Set<Integer> SDBL_COMMENTS = createSdblComments();
private static final Set<Integer> SDBL_PARAMETERS = createSdblParameters();
private static final Set<Integer> SDBL_EDS = createSdblEDS();

private final SensorContext context;

Expand Down Expand Up @@ -258,6 +259,8 @@ private static TypeOfText getTypeOfTextSDBL(int tokenType) {
typeOfText = TypeOfText.COMMENT;
} else if (SDBL_PARAMETERS.contains(tokenType)) {
typeOfText = TypeOfText.ANNOTATION;
} else if (SDBL_EDS.contains(tokenType)) {
typeOfText = TypeOfText.KEYWORD_LIGHT;
} else {
typeOfText = null;
}
Expand Down Expand Up @@ -525,7 +528,30 @@ private static Set<Integer> createSdblFunctions() {
SDBLLexer.VALUETYPE,
SDBLLexer.WEEK,
SDBLLexer.WEEKDAY,
SDBLLexer.YEAR
SDBLLexer.YEAR,
SDBLLexer.INT,
SDBLLexer.ACOS,
SDBLLexer.ASIN,
SDBLLexer.ATAN,
SDBLLexer.COS,
SDBLLexer.SIN,
SDBLLexer.TAN,
SDBLLexer.LOG,
SDBLLexer.LOG10,
SDBLLexer.EXP,
SDBLLexer.POW,
SDBLLexer.SQRT,
SDBLLexer.LOWER,
SDBLLexer.STRINGLENGTH,
SDBLLexer.TRIMALL,
SDBLLexer.TRIML,
SDBLLexer.TRIMR,
SDBLLexer.UPPER,
SDBLLexer.ROUND,
SDBLLexer.STOREDDATASIZE,
SDBLLexer.UUID,
SDBLLexer.STRFIND,
SDBLLexer.STRREPLACE
);
}

Expand Down Expand Up @@ -610,6 +636,14 @@ private static Set<Integer> createSdblParameters() {
);
}

private static Set<Integer> createSdblEDS() {
return Set.of(
SDBLLexer.EDS_CUBE,
SDBLLexer.EDS_TABLE,
SDBLLexer.EDS_CUBE_DIMTABLE
);
}

@Data
@RequiredArgsConstructor
@EqualsAndHashCode(exclude = "active")
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/com/github/_1c_syntax/bsl/sonar/IssuesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.lsp4j.DiagnosticRelatedInformation;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.jetbrains.annotations.NotNull;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
Expand All @@ -42,6 +43,7 @@
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.fs.InputProject;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

Expand Down Expand Up @@ -152,7 +154,7 @@ private Map<String, LoaderSettings> computeLoaderSettings(SensorContext context)
return settings;
}

public void createIssue(InputFile inputFile, Diagnostic diagnostic) {
public void createIssue(Either<InputFile, InputProject> fileOrProject, Diagnostic diagnostic) {

var ruleId = DiagnosticCode.getStringValue(diagnostic.getCode());

Expand All @@ -166,14 +168,14 @@ public void createIssue(InputFile inputFile, Diagnostic diagnostic) {
var activeRule = context.activeRules().find(ruleKey);

if (settings.needCreateExternalIssues && activeRule == null) {
createExternalIssue(settings, inputFile, diagnostic);
createExternalIssue(settings, fileOrProject, diagnostic);
return;
}

var issue = context.newIssue();
issue.forRule(ruleKey);

processDiagnostic(inputFile,
processDiagnostic(fileOrProject,
diagnostic,
ruleId,
issue::newLocation,
Expand All @@ -183,20 +185,23 @@ public void createIssue(InputFile inputFile, Diagnostic diagnostic) {
issue.save();
}

private void processDiagnostic(InputFile inputFile,
private void processDiagnostic(Either<InputFile, InputProject> fileOrProject,
Diagnostic diagnostic,
String ruleId,
Supplier<NewIssueLocation> newIssueLocationSupplier,
Consumer<NewIssueLocation> newIssueAddLocationConsumer,
Consumer<NewIssueLocation> newIssueAtConsumer) {

var textRange = getTextRange(inputFile, diagnostic.getRange(), ruleId);

var location = newIssueLocationSupplier.get();
location.on(inputFile);
location.at(textRange);
location.message(diagnostic.getMessage());
if (fileOrProject.isLeft()) {
var textRange = getTextRange(fileOrProject.getLeft(), diagnostic.getRange(), ruleId);
location.on(fileOrProject.getLeft());
location.at(textRange);
} else {
location.on(fileOrProject.getRight());
}

location.message(diagnostic.getMessage());
newIssueAtConsumer.accept(location);

var relatedInformation = diagnostic.getRelatedInformation();
Expand All @@ -221,7 +226,9 @@ private void processDiagnostic(InputFile inputFile,
}
}

private void createExternalIssue(LoaderSettings settings, InputFile inputFile, Diagnostic diagnostic) {
private void createExternalIssue(LoaderSettings settings,
Either<InputFile, InputProject> fileOrProject,
Diagnostic diagnostic) {
var issue = context.newExternalIssue();

issue.engineId(settings.engineId);
Expand All @@ -232,7 +239,7 @@ private void createExternalIssue(LoaderSettings settings, InputFile inputFile, D
issue.type(ruleTypeMap.get(diagnostic.getSeverity()));
issue.severity(severityMap.get(diagnostic.getSeverity()));

processDiagnostic(inputFile,
processDiagnostic(fileOrProject,
diagnostic,
ruleId,
issue::newLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.github._1c_syntax.bsl.sonar.language.BSLLanguage;
import org.apache.commons.io.FileUtils;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
Expand Down Expand Up @@ -103,7 +104,7 @@ private void processFileInfo(FileInfo fileInfo) {
}

private void processDiagnostic(InputFile inputFile, Diagnostic diagnostic) {
issueLoader.createIssue(inputFile, diagnostic);
issueLoader.createIssue(Either.forLeft(inputFile), diagnostic);
}

@CheckForNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,6 @@ void testComplexity() {

@Test
void testCPD() {
var diagnosticName = "OneStatementPerLine";
var ruleKey = RuleKey.of(BSLLanguageServerRuleDefinition.REPOSITORY_KEY, diagnosticName);

// Mock visitor for metrics.
var fileLinesContext = mock(FileLinesContext.class);
var fileLinesContextFactory = mock(FileLinesContextFactory.class);
Expand All @@ -235,7 +232,6 @@ void testCPD() {
assertThat(context.cpdTokens(componentKey))
.filteredOn(tok -> tok.getValue().startsWith("ПропущенныйТокен"))
.isEmpty();

}

private void setActiveRules(SensorContextTester context, String diagnosticName, RuleKey ruleKey) {
Expand Down Expand Up @@ -276,5 +272,4 @@ private SensorContextTester createSensorContext() {

return context;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void initContext(Vocabulary vocabulary) {
documentContext = mock(DocumentContext.class);
List<Token> tokens = new ArrayList<>();

int maxTokenType = vocabulary.getMaxTokenType();
var maxTokenType = vocabulary.getMaxTokenType();
for (var tokenType = 1; tokenType <= maxTokenType; tokenType++) {
var token = new CommonToken(tokenType, "a");
token.setLine(1);
Expand Down Expand Up @@ -382,7 +382,30 @@ private Map<String, TypeOfText> getHighlightingMapSDBL(Vocabulary vocabulary) {
"VALUETYPE",
"WEEK",
"WEEKDAY",
"YEAR"
"YEAR",
"INT",
"ACOS",
"ASIN",
"ATAN",
"COS",
"SIN",
"TAN",
"LOG",
"LOG10",
"EXP",
"POW",
"SQRT",
"LOWER",
"STRINGLENGTH",
"TRIMALL",
"TRIML",
"TRIMR",
"UPPER",
"ROUND",
"STOREDDATASIZE",
"UUID",
"STRFIND",
"STRREPLACE"
);

Set<String> metadataTypes = Set.of(
Expand Down Expand Up @@ -460,6 +483,12 @@ private Map<String, TypeOfText> getHighlightingMapSDBL(Vocabulary vocabulary) {
"BAR" // TODO: Убрать из лексера
);

Set<String> eds = Set.of(
"EDS_CUBE",
"EDS_TABLE",
"EDS_CUBE_DIMTABLE"
);

var maxTokenType = vocabulary.getMaxTokenType();

Map<String, TypeOfText> highlightingMap = new HashMap<>();
Expand All @@ -485,6 +514,8 @@ private Map<String, TypeOfText> getHighlightingMapSDBL(Vocabulary vocabulary) {
typeOfText = TypeOfText.KEYWORD_LIGHT;
} else if (virtualTables.contains(ruleName)) {
typeOfText = TypeOfText.KEYWORD_LIGHT;
} else if (eds.contains(ruleName)) {
typeOfText = TypeOfText.KEYWORD_LIGHT;
} else if (ruleName.equals("STR")) {
typeOfText = TypeOfText.STRING;
} else if (ruleName.contains("LINE_COMMENT")) {
Expand All @@ -501,5 +532,4 @@ private Map<String, TypeOfText> getHighlightingMapSDBL(Vocabulary vocabulary) {
}
return highlightingMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ void testQualityProfileAll() {
profile.define(contextProfile);
assertThat(contextProfile.profilesByLanguageAndName().get(BSLLanguage.KEY)).hasSize(5);
}

}
Loading

0 comments on commit 74beb14

Please sign in to comment.