Skip to content

Commit

Permalink
ускорил реализацию правила
Browse files Browse the repository at this point in the history
использую предрассчитанное значение из метрик
  • Loading branch information
artbear committed Nov 26, 2023
1 parent 1009afa commit 3020c65
Showing 1 changed file with 25 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import com.github._1c_syntax.utils.CaseInsensitivePattern;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

@DiagnosticMetadata(
Expand Down Expand Up @@ -75,54 +71,39 @@ protected void check() {
if (badWords.pattern().isBlank()) {
return;
}
var moduleLines = documentContext.getContentList();
if (findInComments) {
checkAllLines(moduleLines);
return;
}
checkLinesWithoutComments(moduleLines);
}

var moduleLines = getContentList();
private void checkAllLines(String[] moduleLines) {
for (var i = 0; i < moduleLines.length; i++) {
var moduleLine = moduleLines[i];
if (moduleLine.isEmpty()) {
continue;
}
var matcher = badWords.matcher(moduleLine);
while (matcher.find()) {
diagnosticStorage.addDiagnostic(
Ranges.create(i, matcher.start(), i, matcher.end()),
info.getMessage(matcher.group())
);
}
checkLine(moduleLines, i);
}
}

private String[] getContentList() {
var moduleLines = documentContext.getContentList();
if (findInComments) {
return moduleLines;
}
var lineNumbersWithoutComments = getLineNumbersWithoutComments();
if (lineNumbersWithoutComments.isEmpty()) {
return moduleLines;
}
List<String> result = new ArrayList<>(lineNumbersWithoutComments.size());
for (var i = 0; i < moduleLines.length; i++) {
if (lineNumbersWithoutComments.contains(i)) {
result.add(moduleLines[i]);
} else {
result.add("");
}
private void checkLinesWithoutComments(String[] moduleLines) {
final var nclocData = documentContext.getMetrics().getNclocData();
for (int i : nclocData) {
final var moduleNumber = i - 1; // т.к. в токенах нумерация строк с 1, а в moduleLines с 0
checkLine(moduleLines, moduleNumber);
}
return result.toArray(new String[0]);
}

private Set<Integer> getLineNumbersWithoutComments() {
var result = new HashSet<Integer>();
var tokens = documentContext.getTokensFromDefaultChannel();
int lastLine = -1;
for (var token : tokens) {
var line = token.getLine();
if (line > lastLine) {
lastLine = line;
result.add(line - 1); // т.к. в токенах нумерация строк с 1
}
private void checkLine(String[] lines, int lineNumber) {
var moduleLine = lines[lineNumber];
if (moduleLine.isEmpty()) {
return;
}
var matcher = badWords.matcher(moduleLine);
while (matcher.find()) {
diagnosticStorage.addDiagnostic(
Ranges.create(lineNumber, matcher.start(), lineNumber, matcher.end()),
info.getMessage(matcher.group())
);
}
return result;
}
}

0 comments on commit 3020c65

Please sign in to comment.