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..9b889629a0b --- /dev/null +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/TabAlignmentDiagnostic.java @@ -0,0 +1,73 @@ +/* + * 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 com.github._1c_syntax.bsl.parser.BSLLexer; +import org.antlr.v4.runtime.Token; + +import java.util.List; + +@DiagnosticMetadata( + type = DiagnosticType.CODE_SMELL, + severity = DiagnosticSeverity.INFO, + minutesToFix = 1, + tags = { + DiagnosticTag.BADPRACTICE + } + +) +public class TabAlignmentDiagnostic extends AbstractDiagnostic { + + @Override + public void check() { + + List tokens = documentContext.getTokens(); + + int lineNum = 0; + boolean afterChar = false; + + for (Token token : tokens) { + + if (lineNum < token.getLine()) { + afterChar = false; + lineNum = token.getLine(); + } + + 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/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..34e1c2851d3 --- /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(5); + 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; +