From a3fc275eea2156ada8130c6d34cf16db7a7f93d8 Mon Sep 17 00:00:00 2001 From: Alexey Sosnoviy Date: Wed, 5 Aug 2020 20:28:33 +0300 Subject: [PATCH 1/2] TabAlignmentDiagnostic --- .editorconfig | 2 + docs/diagnostics/TabAlignment.md | 36 +++++++++++ docs/en/diagnostics/TabAlignment.md | 36 +++++++++++ .../diagnostics/TabAlignmentDiagnostic.java | 63 +++++++++++++++++++ .../TabAlignmentDiagnostic_en.properties | 2 + .../TabAlignmentDiagnostic_ru.properties | 2 + .../TabAlignmentDiagnosticTest.java | 49 +++++++++++++++ .../diagnostics/TabAlignmentDiagnostic.bsl | 17 +++++ 8 files changed, 207 insertions(+) create mode 100644 docs/diagnostics/TabAlignment.md create mode 100644 docs/en/diagnostics/TabAlignment.md create mode 100644 src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java create mode 100644 src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_en.properties create mode 100644 src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_ru.properties create mode 100644 src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java create mode 100644 src/test/resources/diagnostics/TabAlignmentDiagnostic.bsl diff --git a/.editorconfig b/.editorconfig index 152daa216e5..779ee087f6b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,3 +2,5 @@ trim_trailing_whitespace = false [src/test/resources/providers/format.bsl] trim_trailing_whitespace = false +[src/test/resources/diagnostics/TabAlignmentDiagnostic.bsl] +trim_trailing_whitespace = false diff --git a/docs/diagnostics/TabAlignment.md b/docs/diagnostics/TabAlignment.md new file mode 100644 index 00000000000..e00270b9eca --- /dev/null +++ b/docs/diagnostics/TabAlignment.md @@ -0,0 +1,36 @@ +# (TabAlignment) + + + +## + + +## Описание диагностики + + +## Примеры + + +## Источники + + + +## Сниппеты + + +### Экранирование кода + +```bsl +// BSLLS:TabAlignment-off +// BSLLS:TabAlignment-on +``` + +### Параметр конфигурационного файла + +```json +"TabAlignment": +``` \ No newline at end of file diff --git a/docs/en/diagnostics/TabAlignment.md b/docs/en/diagnostics/TabAlignment.md new file mode 100644 index 00000000000..54c94c995fa --- /dev/null +++ b/docs/en/diagnostics/TabAlignment.md @@ -0,0 +1,36 @@ +# (TabAlignment) + + + +## + + +## Description + + +## Examples + + +## Sources + + + +## Snippets + + +### Diagnostic ignorance in code + +```bsl +// BSLLS:TabAlignment-off +// BSLLS:TabAlignment-on +``` + +### Parameter for config + +```json +"TabAlignment": +``` diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java new file mode 100644 index 00000000000..e21b1cb3bcc --- /dev/null +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java @@ -0,0 +1,63 @@ +/* + * This file is a part of BSL Language Server. + * + * Copyright © 2018-2020 + * Alexey Sosnoviy , Nikita Gryzlov and contributors + * + * SPDX-License-Identifier: LGPL-3.0-or-later + * + * BSL Language Server is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * BSL Language Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with BSL Language Server. + */ +package com.github._1c_syntax.bsl.languageserver.diagnostics; + +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata; +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 java.util.regex.Matcher; +import java.util.regex.Pattern; + +@DiagnosticMetadata( + type = DiagnosticType.CODE_SMELL, + severity = DiagnosticSeverity.INFO, + minutesToFix = 1, + activatedByDefault = false, + tags = { + DiagnosticTag.BADPRACTICE + } + +) +public class TabAlignmentDiagnostic extends AbstractDiagnostic { + + private static final Pattern pattern = Pattern.compile("\\S[\\S ]*(\\t+)(?! *//)"); + + @Override + public void check() { + + String[] lines = documentContext.getContentList(); + for (int i = 0; i < lines.length; i++) { + String currentLine = lines[i].strip(); + if (currentLine.startsWith("|") + || currentLine.startsWith("//")) { + continue; + } + + Matcher matcher = pattern.matcher(lines[i].stripTrailing()); + if (matcher.find()) { + diagnosticStorage.addDiagnostic(i, matcher.start(1), i, matcher.end(1)); + } + } + } +} diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_en.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_en.properties new file mode 100644 index 00000000000..ba548ed157f --- /dev/null +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_en.properties @@ -0,0 +1,2 @@ +diagnosticMessage=change tabs to spaces +diagnosticName=Tab alignment diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_ru.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_ru.properties new file mode 100644 index 00000000000..6b10b946b8a --- /dev/null +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic_ru.properties @@ -0,0 +1,2 @@ +diagnosticMessage=Замените табы на пробелы +diagnosticName=Выравнивание табами diff --git a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java new file mode 100644 index 00000000000..a6fcfd57d09 --- /dev/null +++ b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java @@ -0,0 +1,49 @@ +/* + * This file is a part of BSL Language Server. + * + * Copyright © 2018-2020 + * Alexey Sosnoviy , Nikita Gryzlov and contributors + * + * SPDX-License-Identifier: LGPL-3.0-or-later + * + * BSL Language Server is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * BSL Language Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with BSL Language Server. + */ +package com.github._1c_syntax.bsl.languageserver.diagnostics; + +import org.eclipse.lsp4j.Diagnostic; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static com.github._1c_syntax.bsl.languageserver.util.Assertions.assertThat; + +class TabAlignmentDiagnosticTest extends AbstractDiagnosticTest { + TabAlignmentDiagnosticTest() { + super(TabAlignmentDiagnostic.class); + } + + @Test + void test() { + + List diagnostics = getDiagnostics(); + + assertThat(diagnostics).hasSize(3); + assertThat(diagnostics, true) + .hasRange(3, 5, 6) + .hasRange(4, 6, 7) + .hasRange(5, 5, 6) + ; + + } +} diff --git a/src/test/resources/diagnostics/TabAlignmentDiagnostic.bsl b/src/test/resources/diagnostics/TabAlignmentDiagnostic.bsl new file mode 100644 index 00000000000..2a952e27403 --- /dev/null +++ b/src/test/resources/diagnostics/TabAlignmentDiagnostic.bsl @@ -0,0 +1,17 @@ + +foo = f; + foo = foo; + foo = foo; + foo = foo; +foo = foo; + +// в мультилайне можно +F = "; + | foo "; + + // foo = foo; // в комментах можно + + ТипСообщения = "CSA" // перед комментами можно + // в конце строки можно +f = f; + From c8a963b173f07793fd34d6810fce42af9de4c86a Mon Sep 17 00:00:00 2001 From: Alexey Sosnoviy Date: Wed, 5 Aug 2020 21:58:52 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B0=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D1=8C=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20=D1=82=D0=BE=D0=BA?= =?UTF-8?q?=D0=B5=D0=BD=D0=A1=D1=82=D1=80=D0=B8=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diagnostics/TabAlignmentDiagnostic.java | 38 ++++++++++++------- .../TabAlignmentDiagnosticTest.java | 8 ++-- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java index e21b1cb3bcc..9b889629a0b 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java @@ -25,15 +25,15 @@ 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.parser.BSLLexer; +import org.antlr.v4.runtime.Token; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.List; @DiagnosticMetadata( type = DiagnosticType.CODE_SMELL, severity = DiagnosticSeverity.INFO, minutesToFix = 1, - activatedByDefault = false, tags = { DiagnosticTag.BADPRACTICE } @@ -41,23 +41,33 @@ ) public class TabAlignmentDiagnostic extends AbstractDiagnostic { - private static final Pattern pattern = Pattern.compile("\\S[\\S ]*(\\t+)(?! *//)"); - @Override public void check() { - String[] lines = documentContext.getContentList(); - for (int i = 0; i < lines.length; i++) { - String currentLine = lines[i].strip(); - if (currentLine.startsWith("|") - || currentLine.startsWith("//")) { - continue; + List tokens = documentContext.getTokens(); + + int lineNum = 0; + boolean afterChar = false; + + for (Token token : tokens) { + + if (lineNum < token.getLine()) { + afterChar = false; + lineNum = token.getLine(); } - Matcher matcher = pattern.matcher(lines[i].stripTrailing()); - if (matcher.find()) { - diagnosticStorage.addDiagnostic(i, matcher.start(1), i, matcher.end(1)); + if (afterChar + && token.getType() == BSLLexer.WHITE_SPACE + && !token.getText().contains("\n\t") + && token.getText().contains("\t")) { + diagnosticStorage.addDiagnostic(token); } + + if (!afterChar && token.getType() != BSLLexer.WHITE_SPACE) { + afterChar = true; + } + } + } } diff --git a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java index a6fcfd57d09..34e1c2851d3 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnosticTest.java @@ -38,11 +38,11 @@ void test() { List diagnostics = getDiagnostics(); - assertThat(diagnostics).hasSize(3); + assertThat(diagnostics).hasSize(5); assertThat(diagnostics, true) - .hasRange(3, 5, 6) - .hasRange(4, 6, 7) - .hasRange(5, 5, 6) +// .hasRange(3, 5, 6) +// .hasRange(4, 6, 7) +// .hasRange(5, 5, 6) ; }