Skip to content

Commit a6c9470

Browse files
authored
Merge pull request #2763 from 1c-syntax/feature/ignoreExtenshionDiagnostic
Игнорирование диагностик в методах &ИзменениеИКонтроль
2 parents 288df9e + 31b6bc9 commit a6c9470

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

src/main/java/com/github/_1c_syntax/bsl/languageserver/context/computer/DiagnosticIgnoranceComputer.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
package com.github._1c_syntax.bsl.languageserver.context.computer;
2323

2424
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
25+
import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol;
26+
import com.github._1c_syntax.bsl.languageserver.context.symbol.annotations.Annotation;
27+
import com.github._1c_syntax.bsl.languageserver.context.symbol.annotations.AnnotationKind;
2528
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode;
29+
import com.github._1c_syntax.bsl.parser.BSLLexer;
2630
import com.github._1c_syntax.utils.CaseInsensitivePattern;
2731
import lombok.AllArgsConstructor;
2832
import org.antlr.v4.runtime.Token;
@@ -38,11 +42,14 @@
3842
import java.util.List;
3943
import java.util.Map;
4044
import java.util.Set;
45+
import java.util.TreeMap;
4146
import java.util.function.Predicate;
4247
import java.util.regex.Matcher;
4348
import java.util.regex.Pattern;
4449
import java.util.stream.Collectors;
4550

51+
import static org.antlr.v4.runtime.Token.HIDDEN_CHANNEL;
52+
4653
public class DiagnosticIgnoranceComputer implements Computer<DiagnosticIgnoranceComputer.Data> {
4754

4855
private static final DiagnosticCode ALL_DIAGNOSTICS_KEY = new DiagnosticCode("all");
@@ -82,6 +89,54 @@ public Data compute() {
8289
if (codeTokens.isEmpty()) {
8390
return new Data(diagnosticIgnorance);
8491
}
92+
93+
computeCommentsIgnorance(codeTokens);
94+
computeExtensionIgnorance();
95+
96+
return new Data(diagnosticIgnorance);
97+
}
98+
99+
private void computeExtensionIgnorance() {
100+
var lines = new TreeMap<Integer, Boolean>();
101+
102+
documentContext.getSymbolTree()
103+
.getMethods().stream()
104+
.filter(
105+
method -> method.getAnnotations().stream()
106+
.map(Annotation::getKind)
107+
.anyMatch(kind -> kind == AnnotationKind.CHANGEANDVALIDATE)
108+
).forEach((MethodSymbol methodSymbol) -> {
109+
lines.put(methodSymbol.getRange().getStart().getLine(), true);
110+
lines.put(methodSymbol.getRange().getEnd().getLine(), false);
111+
});
112+
113+
// not extended method
114+
if (lines.isEmpty()) {
115+
return;
116+
}
117+
118+
documentContext.getTokens().stream()
119+
.filter(token -> token.getChannel() == HIDDEN_CHANNEL)
120+
.filter(token -> token.getType() == BSLLexer.PREPROC_INSERT || token.getType() == BSLLexer.PREPROC_ENDINSERT)
121+
.forEach(token -> lines.put(token.getLine(), token.getType() == BSLLexer.PREPROC_ENDINSERT));
122+
123+
var lastTokenLine = -1;
124+
var tokenLine = -1;
125+
126+
for (Map.Entry<Integer, Boolean> entry : lines.entrySet()) {
127+
128+
if (Boolean.TRUE.equals(entry.getValue())) {
129+
tokenLine = entry.getKey();
130+
} else {
131+
lastTokenLine = entry.getKey();
132+
addIgnoredRange(ALL_DIAGNOSTICS_KEY, tokenLine, lastTokenLine);
133+
}
134+
135+
}
136+
137+
}
138+
139+
private void computeCommentsIgnorance(List<Token> codeTokens) {
85140
Set<Integer> codeLines = codeTokens.stream().map(Token::getLine).collect(Collectors.toSet());
86141

87142
List<Token> comments = documentContext.getComments();
@@ -102,8 +157,6 @@ public Data compute() {
102157
ignoranceStack.forEach((DiagnosticCode diagnosticKey, Deque<Integer> ignoreRangeStarts) ->
103158
ignoreRangeStarts.forEach(ignoreRangeStart -> addIgnoredRange(diagnosticKey, ignoreRangeStart, lastTokenLine))
104159
);
105-
106-
return new Data(diagnosticIgnorance);
107160
}
108161

109162
private boolean checkTrailingComment(Set<Integer> codeLines, Token comment) {

src/test/java/com/github/_1c_syntax/bsl/languageserver/context/computer/DiagnosticIgnoranceComputerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ void testDiagnosticIgnorance() {
6868
assertThat(notIgnoredDiagnostics).noneMatch(data::diagnosticShouldBeIgnored);
6969
}
7070

71+
@Test
72+
void testDiagnosticIgnoranceExtension() {
73+
74+
// given
75+
String filePath = "./src/test/resources/context/computer/DiagnosticIgnoranceComputerExtensionTest.bsl";
76+
final var documentContext = getDocumentContextFromFile(filePath);
77+
78+
List<Diagnostic> ignoredDiagnostics = new ArrayList<>();
79+
ignoredDiagnostics.add(createDiagnostic("MissingSpace", 6));
80+
81+
List<Diagnostic> notIgnoredDiagnostics = new ArrayList<>();
82+
notIgnoredDiagnostics.add(createDiagnostic("MissingSpace", 16));
83+
notIgnoredDiagnostics.add(createDiagnostic("UnusedLocalVariable", 16));
84+
notIgnoredDiagnostics.add(createDiagnostic("SemicolonPresence", 10));
85+
86+
// when
87+
Computer<DiagnosticIgnoranceComputer.Data> diagnosticIgnoranceComputer =
88+
new DiagnosticIgnoranceComputer(documentContext);
89+
DiagnosticIgnoranceComputer.Data data = diagnosticIgnoranceComputer.compute();
90+
91+
// then
92+
assertThat(ignoredDiagnostics).allMatch(data::diagnosticShouldBeIgnored);
93+
assertThat(notIgnoredDiagnostics).noneMatch(data::diagnosticShouldBeIgnored);
94+
95+
}
96+
7197
private static Diagnostic createDiagnostic(String code, int line) {
7298
Diagnostic diagnostic = new Diagnostic();
7399
diagnostic.setCode(code);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
&ИзменениеИКонтроль("ТекстОшибки")
3+
Функция Расш1_ТекстОшибки(ТекстОшибки)
4+
5+
#Удаление
6+
а=Метод(Параметр,Параметр)
7+
#КонецУдаления
8+
#Вставка
9+
10+
Возврат ТекстОшибки + Символы.ПС + НСтр("ru = 'ТРУ ТРУ ТРУ.'")
11+
12+
#КонецВставки
13+
Возврат ТекстОшибки + Символы.ПС + НСтр("ru = 'Обратитесь к администратору.'")
14+
КонецФункции
15+
16+
Процедура Просто()
17+
а=Метод(Параметр,Параметр);
18+
КонецПроцедуры

0 commit comments

Comments
 (0)