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 @@ -26,10 +26,10 @@
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType;
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import com.github._1c_syntax.utils.CaseInsensitivePattern;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@DiagnosticMetadata(
Expand All @@ -44,17 +44,25 @@
public class BadWordsDiagnostic extends AbstractDiagnostic {

private static final String BAD_WORDS_DEFAULT = "";
private static final boolean FIND_IN_COMMENTS_DEFAULT = true;

@DiagnosticParameter(
type = String.class,
defaultValue = BAD_WORDS_DEFAULT
)
private Pattern badWords = CaseInsensitivePattern.compile(BAD_WORDS_DEFAULT);

@Override
@DiagnosticParameter(
type = Boolean.class,
defaultValue = "" + FIND_IN_COMMENTS_DEFAULT
)
private boolean findInComments = FIND_IN_COMMENTS_DEFAULT;

@Override
public void configure(Map<String, Object> configuration) {
this.badWords = CaseInsensitivePattern.compile(
(String) configuration.getOrDefault("badWords", BAD_WORDS_DEFAULT));
this.findInComments = (boolean) configuration.getOrDefault("findInComments", FIND_IN_COMMENTS_DEFAULT);
}

@Override
Expand All @@ -63,13 +71,39 @@
if (badWords.pattern().isBlank()) {
return;
}
var moduleLines = documentContext.getContentList();
if (findInComments) {
checkAllLines(moduleLines);
return;
}
checkLinesWithoutComments(moduleLines);
}

String[] moduleLines = documentContext.getContentList();
for (int i = 0; i < moduleLines.length; i++) {
Matcher matcher = badWords.matcher(moduleLines[i]);
while (matcher.find()) {
diagnosticStorage.addDiagnostic(i, matcher.start(), i, matcher.end());
}
private void checkAllLines(String[] moduleLines) {
for (var i = 0; i < moduleLines.length; i++) {
checkLine(moduleLines, i);
}
}

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
}
}

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())
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
],
"title": "Prohibited words",
"properties": {
"findInComments": {
"description": "Find in comments",
"default": true,
"type": "boolean",
"title": "Find in comments"
},
"badWords": {
"description": "Regular expression for prohibited words.",
"default": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
diagnosticMessage=Prohibited word found.
diagnosticMessage=Prohibited word found <%s>.
diagnosticName=Prohibited words
badWords=Regular expression for prohibited words.
findInComments=Find in comments
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
diagnosticMessage=В тексте модуля найдено запрещенное слово.
diagnosticMessage=В тексте модуля найдено запрещенное слово <%s>.
diagnosticName=Запрещенные слова
badWords=Регулярное выражение для слов-исключений.
findInComments=Искать в комментариях
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BadWordsDiagnosticTest extends AbstractDiagnosticTest<BadWordsDiagnostic>{
void test() {

List<Diagnostic> diagnostics = getDiagnostics();
assertThat(diagnostics).hasSize(0); // Проверка количества
assertThat(diagnostics).isEmpty(); // Проверка количества
}

@Test
Expand All @@ -52,11 +52,29 @@ void testConfigure() {

assertThat(diagnostics).hasSize(6);
assertThat(diagnostics, true)
.hasRange(0, 42, 0, 47)
.hasRange(0, 48, 0, 54)
.hasRange(4, 4, 4, 9)
.hasRange(6, 24, 6, 29)
.hasRange(6, 34, 6, 39)
.hasRange(8, 4, 8, 10);
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <лотус>.", 0, 42, 0, 47)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <шмотус>.", 0, 48, 0, 54)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Лотус>.", 4, 4, 4, 9)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Лотус>.", 6, 24, 6, 29)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Лотус>.", 6, 34, 6, 39)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Шмотус>.", 8, 4, 8, 10);
}

@Test
void testFindWithoutComments() {

Map<String, Object> configuration = diagnosticInstance.info.getDefaultConfiguration();
configuration.put("badWords", "лотус|шмотус");
configuration.put("findInComments", false);
diagnosticInstance.configure(configuration);

List<Diagnostic> diagnostics = getDiagnostics();

assertThat(diagnostics).hasSize(4);
assertThat(diagnostics, true)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Лотус>.", 4, 4, 4, 9)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Лотус>.", 6, 24, 6, 29)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Лотус>.", 6, 34, 6, 39)
.hasMessageOnRange("В тексте модуля найдено запрещенное слово <Шмотус>.", 8, 4, 8, 10);
}
}
Loading