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

Доработка BadWords - опция поиска по комментариям + показ запрещенного слова в замечаниях #3190

Merged
merged 7 commits into from
Nov 27, 2023
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 @@
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) {

Check notice on line 90 in src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic.java

View workflow job for this annotation

GitHub Actions / qodana

Constant values

Value `nclocData` is always 'null'
nixel2007 marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
final var moduleNumber = i - 1; // т.к. в токенах нумерация строк с 1, а в moduleLines с 0
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
checkLine(moduleLines, moduleNumber);
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
}
Loading