Skip to content

Commit a3fc275

Browse files
committed
TabAlignmentDiagnostic
1 parent 8ee9473 commit a3fc275

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
trim_trailing_whitespace = false
33
[src/test/resources/providers/format.bsl]
44
trim_trailing_whitespace = false
5+
[src/test/resources/diagnostics/TabAlignmentDiagnostic.bsl]
6+
trim_trailing_whitespace = false

docs/diagnostics/TabAlignment.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# <Diagnostic name> (TabAlignment)
2+
3+
<Metadata>
4+
5+
## <Params>
6+
7+
<!-- Блоки выше заполняются автоматически, не трогать -->
8+
## Описание диагностики
9+
<!-- Описание диагностики заполняется вручную. Необходимо понятным языком описать смысл и схему работу -->
10+
11+
## Примеры
12+
<!-- В данном разделе приводятся примеры, на которые диагностика срабатывает, а также можно привести пример, как можно исправить ситуацию -->
13+
14+
## Источники
15+
<!-- Необходимо указывать ссылки на все источники, из которых почерпнута информация для создания диагностики -->
16+
<!-- Примеры источников
17+
18+
* Источник: [Стандарт: Тексты модулей](https://its.1c.ru/db/v8std#content:456:hdoc)
19+
* Полезная информация: [Отказ от использования модальных окон](https://its.1c.ru/db/metod8dev#content:5272:hdoc)
20+
* Источник: [Cognitive complexity, ver. 1.4](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) -->
21+
22+
## Сниппеты
23+
<!-- Блоки ниже заполняются автоматически, не трогать -->
24+
25+
### Экранирование кода
26+
27+
```bsl
28+
// BSLLS:TabAlignment-off
29+
// BSLLS:TabAlignment-on
30+
```
31+
32+
### Параметр конфигурационного файла
33+
34+
```json
35+
"TabAlignment": <DiagnosticConfig>
36+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# <Diagnostic name> (TabAlignment)
2+
3+
<Metadata>
4+
5+
## <Params>
6+
7+
<!-- Блоки выше заполняются автоматически, не трогать -->
8+
## Description
9+
<!-- Описание диагностики заполняется вручную. Необходимо понятным языком описать смысл и схему работу -->
10+
11+
## Examples
12+
<!-- В данном разделе приводятся примеры, на которые диагностика срабатывает, а также можно привести пример, как можно исправить ситуацию -->
13+
14+
## Sources
15+
<!-- Необходимо указывать ссылки на все источники, из которых почерпнута информация для создания диагностики -->
16+
<!-- Примеры источников
17+
18+
* Источник: [Стандарт: Тексты модулей](https://its.1c.ru/db/v8std#content:456:hdoc)
19+
* Полезная информация: [Отказ от использования модальных окон](https://its.1c.ru/db/metod8dev#content:5272:hdoc)
20+
* Источник: [Cognitive complexity, ver. 1.4](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) -->
21+
22+
## Snippets
23+
<!-- Блоки ниже заполняются автоматически, не трогать -->
24+
25+
### Diagnostic ignorance in code
26+
27+
```bsl
28+
// BSLLS:TabAlignment-off
29+
// BSLLS:TabAlignment-on
30+
```
31+
32+
### Parameter for config
33+
34+
```json
35+
"TabAlignment": <DiagnosticConfig>
36+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is a part of BSL Language Server.
3+
*
4+
* Copyright © 2018-2020
5+
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Gryzlov <nixel2007@gmail.com> and contributors
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Language Server is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Language Server is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Language Server.
21+
*/
22+
package com.github._1c_syntax.bsl.languageserver.diagnostics;
23+
24+
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata;
25+
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity;
26+
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag;
27+
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType;
28+
29+
import java.util.regex.Matcher;
30+
import java.util.regex.Pattern;
31+
32+
@DiagnosticMetadata(
33+
type = DiagnosticType.CODE_SMELL,
34+
severity = DiagnosticSeverity.INFO,
35+
minutesToFix = 1,
36+
activatedByDefault = false,
37+
tags = {
38+
DiagnosticTag.BADPRACTICE
39+
}
40+
41+
)
42+
public class TabAlignmentDiagnostic extends AbstractDiagnostic {
43+
44+
private static final Pattern pattern = Pattern.compile("\\S[\\S ]*(\\t+)(?! *//)");
45+
46+
@Override
47+
public void check() {
48+
49+
String[] lines = documentContext.getContentList();
50+
for (int i = 0; i < lines.length; i++) {
51+
String currentLine = lines[i].strip();
52+
if (currentLine.startsWith("|")
53+
|| currentLine.startsWith("//")) {
54+
continue;
55+
}
56+
57+
Matcher matcher = pattern.matcher(lines[i].stripTrailing());
58+
if (matcher.find()) {
59+
diagnosticStorage.addDiagnostic(i, matcher.start(1), i, matcher.end(1));
60+
}
61+
}
62+
}
63+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
diagnosticMessage=change tabs to spaces
2+
diagnosticName=Tab alignment
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
diagnosticMessage=Замените табы на пробелы
2+
diagnosticName=Выравнивание табами
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is a part of BSL Language Server.
3+
*
4+
* Copyright © 2018-2020
5+
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Gryzlov <nixel2007@gmail.com> and contributors
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Language Server is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Language Server is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Language Server.
21+
*/
22+
package com.github._1c_syntax.bsl.languageserver.diagnostics;
23+
24+
import org.eclipse.lsp4j.Diagnostic;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.List;
28+
29+
import static com.github._1c_syntax.bsl.languageserver.util.Assertions.assertThat;
30+
31+
class TabAlignmentDiagnosticTest extends AbstractDiagnosticTest<TabAlignmentDiagnostic> {
32+
TabAlignmentDiagnosticTest() {
33+
super(TabAlignmentDiagnostic.class);
34+
}
35+
36+
@Test
37+
void test() {
38+
39+
List<Diagnostic> diagnostics = getDiagnostics();
40+
41+
assertThat(diagnostics).hasSize(3);
42+
assertThat(diagnostics, true)
43+
.hasRange(3, 5, 6)
44+
.hasRange(4, 6, 7)
45+
.hasRange(5, 5, 6)
46+
;
47+
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
foo = f;
3+
foo = foo;
4+
foo = foo;
5+
foo = foo;
6+
foo = foo;
7+
8+
// в мультилайне можно
9+
F = ";
10+
| foo ";
11+
12+
// foo = foo; // в комментах можно
13+
14+
ТипСообщения = "CSA" // перед комментами можно
15+
// в конце строки можно
16+
f = f;
17+

0 commit comments

Comments
 (0)