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

Feature/tab alignment #1324

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions docs/diagnostics/TabAlignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# <Diagnostic name> (TabAlignment)

<Metadata>

## <Params>

<!-- Блоки выше заполняются автоматически, не трогать -->
## Описание диагностики
<!-- Описание диагностики заполняется вручную. Необходимо понятным языком описать смысл и схему работу -->

## Примеры
<!-- В данном разделе приводятся примеры, на которые диагностика срабатывает, а также можно привести пример, как можно исправить ситуацию -->

## Источники
<!-- Необходимо указывать ссылки на все источники, из которых почерпнута информация для создания диагностики -->
<!-- Примеры источников

* Источник: [Стандарт: Тексты модулей](https://its.1c.ru/db/v8std#content:456:hdoc)
* Полезная информация: [Отказ от использования модальных окон](https://its.1c.ru/db/metod8dev#content:5272:hdoc)
* Источник: [Cognitive complexity, ver. 1.4](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) -->

## Сниппеты
<!-- Блоки ниже заполняются автоматически, не трогать -->

### Экранирование кода

```bsl
// BSLLS:TabAlignment-off
// BSLLS:TabAlignment-on
```

### Параметр конфигурационного файла

```json
"TabAlignment": <DiagnosticConfig>
```
36 changes: 36 additions & 0 deletions docs/en/diagnostics/TabAlignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# <Diagnostic name> (TabAlignment)

<Metadata>

## <Params>

<!-- Блоки выше заполняются автоматически, не трогать -->
## Description
<!-- Описание диагностики заполняется вручную. Необходимо понятным языком описать смысл и схему работу -->

## Examples
<!-- В данном разделе приводятся примеры, на которые диагностика срабатывает, а также можно привести пример, как можно исправить ситуацию -->

## Sources
<!-- Необходимо указывать ссылки на все источники, из которых почерпнута информация для создания диагностики -->
<!-- Примеры источников

* Источник: [Стандарт: Тексты модулей](https://its.1c.ru/db/v8std#content:456:hdoc)
* Полезная информация: [Отказ от использования модальных окон](https://its.1c.ru/db/metod8dev#content:5272:hdoc)
* Источник: [Cognitive complexity, ver. 1.4](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) -->

## Snippets
<!-- Блоки ниже заполняются автоматически, не трогать -->

### Diagnostic ignorance in code

```bsl
// BSLLS:TabAlignment-off
// BSLLS:TabAlignment-on
```

### Parameter for config

```json
"TabAlignment": <DiagnosticConfig>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright © 2018-2020
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> 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<Token> 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;
}

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
diagnosticMessage=change tabs to spaces
diagnosticName=Tab alignment
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
diagnosticMessage=Замените табы на пробелы
diagnosticName=Выравнивание табами
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright © 2018-2020
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> 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<TabAlignmentDiagnostic> {
TabAlignmentDiagnosticTest() {
super(TabAlignmentDiagnostic.class);
}

@Test
void test() {

List<Diagnostic> diagnostics = getDiagnostics();

assertThat(diagnostics).hasSize(5);
assertThat(diagnostics, true)
// .hasRange(3, 5, 6)
// .hasRange(4, 6, 7)
// .hasRange(5, 5, 6)
;

}
}
17 changes: 17 additions & 0 deletions src/test/resources/diagnostics/TabAlignmentDiagnostic.bsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

foo = f;
foo = foo;
foo = foo;
foo = foo;
foo = foo;

// в мультилайне можно
F = ";
| foo ";

// foo = foo; // в комментах можно

ТипСообщения = "CSA" // перед комментами можно
// в конце строки можно
f = f;