Skip to content

Commit

Permalink
Merge pull request 1c-syntax#3190 from artbear/bad-words
Browse files Browse the repository at this point in the history
Доработка BadWords - опция поиска по комментариям + показ запрещенного слова в замечаниях
  • Loading branch information
nixel2007 authored Nov 27, 2023
2 parents 6e0e5a8 + 3020c65 commit 782ef5e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 17 deletions.
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 @@ 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) {

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'

Check notice

Code scanning / QDJVMC

Constant values Note

Value nclocData is always 'null'
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())
);
}
}
}
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);
}
}

0 comments on commit 782ef5e

Please sign in to comment.