Skip to content

Commit e4fd883

Browse files
authored
Merge pull request #317 from 1c-syntax/feature/supportEdtStorage
Замечания на уровне проекта + прогрессивный расчет
2 parents 3cb89ce + 641777d commit e4fd883

File tree

13 files changed

+180
-52
lines changed

13 files changed

+180
-52
lines changed

build.gradle.kts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,19 @@ val sonarQubeVersion = "9.9.0.65466"
3232
dependencies {
3333
implementation("org.sonarsource.api.plugin", "sonar-plugin-api", "9.14.0.375")
3434

35-
// в jitpack лежат в группе com.github.1c-syntax, в централе - io.github.1c-syntax
36-
implementation("io.github.1c-syntax", "bsl-language-server", "0.22.0") {
37-
exclude("com.github.1c-syntax", "utils")
35+
implementation("io.github.1c-syntax", "bsl-language-server", "0.23.0-rc.5") {
36+
exclude("com.contrastsecurity", "java-sarif")
37+
exclude("io.sentry", "sentry-logback")
38+
exclude("org.springframework.boot", "spring-boot-starter-websocket")
3839
}
39-
implementation("com.github.1c-syntax", "utils", "0.5.1")
4040

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

4543
// MD to HTML converter of BSL LS rule descriptions
4644
implementation("org.commonmark", "commonmark", "0.21.0")
4745
implementation("org.commonmark", "commonmark-ext-gfm-tables", "0.21.0")
4846
implementation("org.commonmark", "commonmark-ext-autolink", "0.21.0")
4947
implementation("org.commonmark", "commonmark-ext-heading-anchor", "0.21.0")
50-
implementation("me.tongfei", "progressbar", "0.10.0")
51-
52-
// CONSTRAINTS
53-
implementation("com.google.guava", "guava") {
54-
version {
55-
strictly("32.0.1-jre")
56-
}
57-
}
58-
59-
compileOnly("com.google.code.findbugs", "jsr305", "3.0.2")
6048

6149
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.8.0")
6250
testImplementation("org.assertj", "assertj-core", "3.25.1")

src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.SkipSupport;
2828
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
2929
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
30+
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode;
3031
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo;
3132
import com.github._1c_syntax.bsl.parser.BSLLexer;
3233
import com.github._1c_syntax.bsl.sonar.language.BSLLanguage;
@@ -36,6 +37,7 @@
3637
import me.tongfei.progressbar.ProgressBarStyle;
3738
import org.antlr.v4.runtime.Token;
3839
import org.apache.commons.lang3.StringUtils;
40+
import org.eclipse.lsp4j.Diagnostic;
3941
import org.eclipse.lsp4j.jsonrpc.messages.Either;
4042
import org.jetbrains.annotations.NotNull;
4143
import org.sonar.api.batch.fs.InputFile;
@@ -55,9 +57,11 @@
5557
import java.util.Arrays;
5658
import java.util.Collections;
5759
import java.util.HashMap;
60+
import java.util.HashSet;
5861
import java.util.List;
5962
import java.util.Locale;
6063
import java.util.Map;
64+
import java.util.Set;
6165
import java.util.stream.Collectors;
6266
import java.util.stream.StreamSupport;
6367

@@ -72,6 +76,9 @@ public class BSLCoreSensor implements Sensor {
7276
private final IssuesLoader issuesLoader;
7377
private final BSLHighlighter highlighter;
7478

79+
private final Set<String> diagnosticsOnProject;
80+
private final Set<String> diagnosticsWithExtraMins;
81+
7582
public BSLCoreSensor(SensorContext context, FileLinesContextFactory fileLinesContextFactory) {
7683
this.context = context;
7784
this.fileLinesContextFactory = fileLinesContextFactory;
@@ -95,6 +102,9 @@ public BSLCoreSensor(SensorContext context, FileLinesContextFactory fileLinesCon
95102

96103
issuesLoader = new IssuesLoader(context);
97104
highlighter = new BSLHighlighter(context);
105+
106+
diagnosticsOnProject = new HashSet<>();
107+
diagnosticsWithExtraMins = new HashSet<>();
98108
}
99109

100110
@Override
@@ -170,15 +180,23 @@ public void execute(SensorContext context) {
170180
BSLLSBinding.getApplicationContext().close();
171181
}
172182

173-
174183
private void processFile(InputFile inputFile, ServerContext bslServerContext) {
175184
var uri = inputFile.uri();
176185
var documentContext = bslServerContext.addDocument(uri);
177186
bslServerContext.rebuildDocument(documentContext);
178187

179188
if (langServerEnabled) {
180189
documentContext.getDiagnostics()
181-
.forEach(diagnostic -> issuesLoader.createIssue(inputFile, diagnostic));
190+
.forEach((Diagnostic diagnostic) -> {
191+
var code = DiagnosticCode.getStringValue(diagnostic.getCode());
192+
var hasExtraMins = diagnosticsWithExtraMins.contains(code);
193+
194+
if (diagnosticsOnProject.contains(code)) {
195+
issuesLoader.createIssue(Either.forRight(context.project()), diagnostic, hasExtraMins);
196+
} else {
197+
issuesLoader.createIssue(Either.forLeft(inputFile), diagnostic, hasExtraMins);
198+
}
199+
});
182200
}
183201

184202
saveCpd(inputFile, documentContext);
@@ -221,7 +239,6 @@ private void saveCpd(InputFile inputFile, DocumentContext documentContext) {
221239
synchronized (this) {
222240
cpdTokens.save();
223241
}
224-
225242
}
226243

227244
private void saveMeasures(InputFile inputFile, DocumentContext documentContext) {
@@ -332,6 +349,14 @@ private LanguageServerConfiguration getLanguageServerConfiguration() {
332349
diagnosticCode,
333350
Either.forRight(diagnosticConfiguration)
334351
);
352+
353+
if (diagnosticInfo.canLocateOnProject()) {
354+
diagnosticsOnProject.add(diagnosticCode);
355+
}
356+
357+
if (diagnosticInfo.getExtraMinForComplexity() > 0) {
358+
diagnosticsWithExtraMins.add(diagnosticCode);
359+
}
335360
}
336361
}
337362

src/main/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighter.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class BSLHighlighter {
6565
private static final Set<Integer> SDBL_STRINGS = createSdblStrings();
6666
private static final Set<Integer> SDBL_COMMENTS = createSdblComments();
6767
private static final Set<Integer> SDBL_PARAMETERS = createSdblParameters();
68+
private static final Set<Integer> SDBL_EDS = createSdblEDS();
6869

6970
private final SensorContext context;
7071

@@ -258,6 +259,8 @@ private static TypeOfText getTypeOfTextSDBL(int tokenType) {
258259
typeOfText = TypeOfText.COMMENT;
259260
} else if (SDBL_PARAMETERS.contains(tokenType)) {
260261
typeOfText = TypeOfText.ANNOTATION;
262+
} else if (SDBL_EDS.contains(tokenType)) {
263+
typeOfText = TypeOfText.KEYWORD_LIGHT;
261264
} else {
262265
typeOfText = null;
263266
}
@@ -525,7 +528,30 @@ private static Set<Integer> createSdblFunctions() {
525528
SDBLLexer.VALUETYPE,
526529
SDBLLexer.WEEK,
527530
SDBLLexer.WEEKDAY,
528-
SDBLLexer.YEAR
531+
SDBLLexer.YEAR,
532+
SDBLLexer.INT,
533+
SDBLLexer.ACOS,
534+
SDBLLexer.ASIN,
535+
SDBLLexer.ATAN,
536+
SDBLLexer.COS,
537+
SDBLLexer.SIN,
538+
SDBLLexer.TAN,
539+
SDBLLexer.LOG,
540+
SDBLLexer.LOG10,
541+
SDBLLexer.EXP,
542+
SDBLLexer.POW,
543+
SDBLLexer.SQRT,
544+
SDBLLexer.LOWER,
545+
SDBLLexer.STRINGLENGTH,
546+
SDBLLexer.TRIMALL,
547+
SDBLLexer.TRIML,
548+
SDBLLexer.TRIMR,
549+
SDBLLexer.UPPER,
550+
SDBLLexer.ROUND,
551+
SDBLLexer.STOREDDATASIZE,
552+
SDBLLexer.UUID,
553+
SDBLLexer.STRFIND,
554+
SDBLLexer.STRREPLACE
529555
);
530556
}
531557

@@ -610,6 +636,14 @@ private static Set<Integer> createSdblParameters() {
610636
);
611637
}
612638

639+
private static Set<Integer> createSdblEDS() {
640+
return Set.of(
641+
SDBLLexer.EDS_CUBE,
642+
SDBLLexer.EDS_TABLE,
643+
SDBLLexer.EDS_CUBE_DIMTABLE
644+
);
645+
}
646+
613647
@Data
614648
@RequiredArgsConstructor
615649
@EqualsAndHashCode(exclude = "active")

src/main/java/com/github/_1c_syntax/bsl/sonar/IssuesLoader.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.eclipse.lsp4j.DiagnosticRelatedInformation;
3333
import org.eclipse.lsp4j.DiagnosticSeverity;
3434
import org.eclipse.lsp4j.Range;
35+
import org.eclipse.lsp4j.jsonrpc.messages.Either;
3536
import org.jetbrains.annotations.NotNull;
3637
import org.sonar.api.batch.fs.FilePredicates;
3738
import org.sonar.api.batch.fs.FileSystem;
@@ -42,6 +43,7 @@
4243
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
4344
import org.sonar.api.rule.RuleKey;
4445
import org.sonar.api.rules.RuleType;
46+
import org.sonar.api.scanner.fs.InputProject;
4547
import org.sonar.api.utils.log.Logger;
4648
import org.sonar.api.utils.log.Loggers;
4749

@@ -152,7 +154,15 @@ private Map<String, LoaderSettings> computeLoaderSettings(SensorContext context)
152154
return settings;
153155
}
154156

155-
public void createIssue(InputFile inputFile, Diagnostic diagnostic) {
157+
public void createIssue(InputFile file, Diagnostic diagnostic) {
158+
createIssue(Either.forLeft(file), diagnostic, false);
159+
}
160+
161+
public void createIssue(InputProject project, Diagnostic diagnostic) {
162+
createIssue(Either.forRight(project), diagnostic, false);
163+
}
164+
165+
public void createIssue(Either<InputFile, InputProject> fileOrProject, Diagnostic diagnostic, boolean hasExtraMins) {
156166

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

@@ -166,37 +176,43 @@ public void createIssue(InputFile inputFile, Diagnostic diagnostic) {
166176
var activeRule = context.activeRules().find(ruleKey);
167177

168178
if (settings.needCreateExternalIssues && activeRule == null) {
169-
createExternalIssue(settings, inputFile, diagnostic);
179+
createExternalIssue(settings, fileOrProject, diagnostic);
170180
return;
171181
}
172182

173183
var issue = context.newIssue();
174184
issue.forRule(ruleKey);
175-
176-
processDiagnostic(inputFile,
185+
processDiagnostic(fileOrProject,
177186
diagnostic,
178187
ruleId,
179188
issue::newLocation,
180189
issue::addLocation,
181190
issue::at);
182191

192+
if (hasExtraMins && diagnostic.getRelatedInformation() != null) {
193+
issue.gap((double) diagnostic.getRelatedInformation().size() - 1); // первый - это основной
194+
}
195+
183196
issue.save();
184197
}
185198

186-
private void processDiagnostic(InputFile inputFile,
199+
private void processDiagnostic(Either<InputFile, InputProject> fileOrProject,
187200
Diagnostic diagnostic,
188201
String ruleId,
189202
Supplier<NewIssueLocation> newIssueLocationSupplier,
190203
Consumer<NewIssueLocation> newIssueAddLocationConsumer,
191204
Consumer<NewIssueLocation> newIssueAtConsumer) {
192205

193-
var textRange = getTextRange(inputFile, diagnostic.getRange(), ruleId);
194-
195206
var location = newIssueLocationSupplier.get();
196-
location.on(inputFile);
197-
location.at(textRange);
198-
location.message(diagnostic.getMessage());
207+
if (fileOrProject.isLeft()) {
208+
var textRange = getTextRange(fileOrProject.getLeft(), diagnostic.getRange(), ruleId);
209+
location.on(fileOrProject.getLeft());
210+
location.at(textRange);
211+
} else {
212+
location.on(fileOrProject.getRight());
213+
}
199214

215+
location.message(diagnostic.getMessage());
200216
newIssueAtConsumer.accept(location);
201217

202218
var relatedInformation = diagnostic.getRelatedInformation();
@@ -221,7 +237,9 @@ private void processDiagnostic(InputFile inputFile,
221237
}
222238
}
223239

224-
private void createExternalIssue(LoaderSettings settings, InputFile inputFile, Diagnostic diagnostic) {
240+
private void createExternalIssue(LoaderSettings settings,
241+
Either<InputFile, InputProject> fileOrProject,
242+
Diagnostic diagnostic) {
225243
var issue = context.newExternalIssue();
226244

227245
issue.engineId(settings.engineId);
@@ -232,7 +250,7 @@ private void createExternalIssue(LoaderSettings settings, InputFile inputFile, D
232250
issue.type(ruleTypeMap.get(diagnostic.getSeverity()));
233251
issue.severity(severityMap.get(diagnostic.getSeverity()));
234252

235-
processDiagnostic(inputFile,
253+
processDiagnostic(fileOrProject,
236254
diagnostic,
237255
ruleId,
238256
issue::newLocation,

src/main/java/com/github/_1c_syntax/bsl/sonar/language/BSLLanguageServerRuleDefinition.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,20 @@ private void setUpNewRule(NewRule newRule) {
138138
newRule.addTags(tagsName);
139139
}
140140

141-
newRule.setDebtRemediationFunction(
142-
newRule.debtRemediationFunctions().linear(
143-
diagnosticInfo.getMinutesToFix() + "min"
144-
)
145-
);
141+
if (diagnosticInfo.getExtraMinForComplexity() > 0) {
142+
newRule.setDebtRemediationFunction(
143+
newRule.debtRemediationFunctions().linearWithOffset(
144+
(int) diagnosticInfo.getExtraMinForComplexity() + "min",
145+
diagnosticInfo.getMinutesToFix() + "min"
146+
)
147+
);
148+
} else {
149+
newRule.setDebtRemediationFunction(
150+
newRule.debtRemediationFunctions().constantPerIssue(
151+
diagnosticInfo.getMinutesToFix() + "min"
152+
)
153+
);
154+
}
146155
}
147156

148157
private String getHtmlDescription(String markdownDescription) {

src/test/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensorTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,6 @@ void testComplexity() {
212212

213213
@Test
214214
void testCPD() {
215-
var diagnosticName = "OneStatementPerLine";
216-
var ruleKey = RuleKey.of(BSLLanguageServerRuleDefinition.REPOSITORY_KEY, diagnosticName);
217-
218215
// Mock visitor for metrics.
219216
var fileLinesContext = mock(FileLinesContext.class);
220217
var fileLinesContextFactory = mock(FileLinesContextFactory.class);
@@ -235,7 +232,6 @@ void testCPD() {
235232
assertThat(context.cpdTokens(componentKey))
236233
.filteredOn(tok -> tok.getValue().startsWith("ПропущенныйТокен"))
237234
.isEmpty();
238-
239235
}
240236

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

277273
return context;
278274
}
279-
280275
}

0 commit comments

Comments
 (0)