diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic.java index 0a40ebb529a..68e3059be85 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic.java @@ -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( @@ -44,6 +44,7 @@ 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, @@ -51,10 +52,17 @@ public class BadWordsDiagnostic extends AbstractDiagnostic { ) 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 configuration) { this.badWords = CaseInsensitivePattern.compile( (String) configuration.getOrDefault("badWords", BAD_WORDS_DEFAULT)); + this.findInComments = (boolean) configuration.getOrDefault("findInComments", FIND_IN_COMMENTS_DEFAULT); } @Override @@ -63,13 +71,39 @@ protected void check() { 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) { + final var moduleNumber = i - 1; // т.к. в токенах нумерация строк с 1, а в moduleLines с 0 + checkLine(moduleLines, moduleNumber); + } + } + + 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()) + ); } } } diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json index 9310767b6eb..da1611a2973 100644 --- a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json @@ -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": "", diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_en.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_en.properties index d4f0db3b506..5d24e89b057 100644 --- a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_en.properties +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_en.properties @@ -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 diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_ru.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_ru.properties index de3556166c6..952f8aa5921 100644 --- a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_ru.properties +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnostic_ru.properties @@ -1,3 +1,4 @@ -diagnosticMessage=В тексте модуля найдено запрещенное слово. +diagnosticMessage=В тексте модуля найдено запрещенное слово <%s>. diagnosticName=Запрещенные слова badWords=Регулярное выражение для слов-исключений. +findInComments=Искать в комментариях diff --git a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnosticTest.java b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnosticTest.java index 22d5eff9d42..d68ba5ff425 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnosticTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/BadWordsDiagnosticTest.java @@ -38,7 +38,7 @@ class BadWordsDiagnosticTest extends AbstractDiagnosticTest{ void test() { List diagnostics = getDiagnostics(); - assertThat(diagnostics).hasSize(0); // Проверка количества + assertThat(diagnostics).isEmpty(); // Проверка количества } @Test @@ -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 configuration = diagnosticInstance.info.getDefaultConfiguration(); + configuration.put("badWords", "лотус|шмотус"); + configuration.put("findInComments", false); + diagnosticInstance.configure(configuration); + + List 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); } }