Skip to content

Commit

Permalink
Merge pull request #317 from 1c-syntax/feature/supportEdtStorage
Browse files Browse the repository at this point in the history
Замечания на уровне проекта + прогрессивный расчет
  • Loading branch information
theshadowco authored Jan 16, 2024
2 parents 3cb89ce + 641777d commit e4fd883
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 52 deletions.
20 changes: 4 additions & 16 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,19 @@ val sonarQubeVersion = "9.9.0.65466"
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") {
exclude("com.github.1c-syntax", "utils")
implementation("io.github.1c-syntax", "bsl-language-server", "0.23.0-rc.5") {
exclude("com.contrastsecurity", "java-sarif")
exclude("io.sentry", "sentry-logback")
exclude("org.springframework.boot", "spring-boot-starter-websocket")
}
implementation("com.github.1c-syntax", "utils", "0.5.1")

implementation("org.apache.commons", "commons-lang3", "3.14.0")
implementation("com.fasterxml.jackson.core", "jackson-databind", "2.16.1")
implementation("org.sonarsource.analyzer-commons", "sonar-analyzer-commons", "2.5.0.1358")

// MD to HTML converter of BSL LS rule descriptions
implementation("org.commonmark", "commonmark", "0.21.0")
implementation("org.commonmark", "commonmark-ext-gfm-tables", "0.21.0")
implementation("org.commonmark", "commonmark-ext-autolink", "0.21.0")
implementation("org.commonmark", "commonmark-ext-heading-anchor", "0.21.0")
implementation("me.tongfei", "progressbar", "0.10.0")

// CONSTRAINTS
implementation("com.google.guava", "guava") {
version {
strictly("32.0.1-jre")
}
}

compileOnly("com.google.code.findbugs", "jsr305", "3.0.2")

testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.8.0")
testImplementation("org.assertj", "assertj-core", "3.25.1")
Expand Down
31 changes: 28 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,9 @@ public class BSLCoreSensor implements Sensor {
private final IssuesLoader issuesLoader;
private final BSLHighlighter highlighter;

private final Set<String> diagnosticsOnProject;
private final Set<String> diagnosticsWithExtraMins;

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

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

diagnosticsOnProject = new HashSet<>();
diagnosticsWithExtraMins = new HashSet<>();
}

@Override
Expand Down Expand Up @@ -170,15 +180,23 @@ 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) -> {
var code = DiagnosticCode.getStringValue(diagnostic.getCode());
var hasExtraMins = diagnosticsWithExtraMins.contains(code);

if (diagnosticsOnProject.contains(code)) {
issuesLoader.createIssue(Either.forRight(context.project()), diagnostic, hasExtraMins);
} else {
issuesLoader.createIssue(Either.forLeft(inputFile), diagnostic, hasExtraMins);
}
});
}

saveCpd(inputFile, documentContext);
Expand Down Expand Up @@ -221,7 +239,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 +349,14 @@ private LanguageServerConfiguration getLanguageServerConfiguration() {
diagnosticCode,
Either.forRight(diagnosticConfiguration)
);

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

if (diagnosticInfo.getExtraMinForComplexity() > 0) {
diagnosticsWithExtraMins.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
42 changes: 30 additions & 12 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,15 @@ private Map<String, LoaderSettings> computeLoaderSettings(SensorContext context)
return settings;
}

public void createIssue(InputFile inputFile, Diagnostic diagnostic) {
public void createIssue(InputFile file, Diagnostic diagnostic) {
createIssue(Either.forLeft(file), diagnostic, false);
}

public void createIssue(InputProject project, Diagnostic diagnostic) {
createIssue(Either.forRight(project), diagnostic, false);
}

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

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

Expand All @@ -166,37 +176,43 @@ 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,
issue::addLocation,
issue::at);

if (hasExtraMins && diagnostic.getRelatedInformation() != null) {
issue.gap((double) diagnostic.getRelatedInformation().size() - 1); // первый - это основной
}

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 +237,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 +250,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 @@ -138,11 +138,20 @@ private void setUpNewRule(NewRule newRule) {
newRule.addTags(tagsName);
}

newRule.setDebtRemediationFunction(
newRule.debtRemediationFunctions().linear(
diagnosticInfo.getMinutesToFix() + "min"
)
);
if (diagnosticInfo.getExtraMinForComplexity() > 0) {
newRule.setDebtRemediationFunction(
newRule.debtRemediationFunctions().linearWithOffset(
(int) diagnosticInfo.getExtraMinForComplexity() + "min",
diagnosticInfo.getMinutesToFix() + "min"
)
);
} else {
newRule.setDebtRemediationFunction(
newRule.debtRemediationFunctions().constantPerIssue(
diagnosticInfo.getMinutesToFix() + "min"
)
);
}
}

private String getHtmlDescription(String markdownDescription) {
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;
}

}
Loading

0 comments on commit e4fd883

Please sign in to comment.