Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 committed Jan 28, 2020
2 parents 8675610 + bf3f285 commit 3c80423
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@
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.languageserver.utils.Ranges;
import com.github._1c_syntax.bsl.languageserver.utils.RelatedInformation;
import com.github._1c_syntax.bsl.languageserver.utils.Trees;
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.bsl.parser.BSLParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.Tree;
import org.eclipse.lsp4j.DiagnosticRelatedInformation;
import org.eclipse.lsp4j.Range;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DiagnosticMetadata(
type = DiagnosticType.CODE_SMELL,
Expand All @@ -66,7 +69,16 @@ public ParseTree visitFile(BSLParser.FileContext ctx) {

// если областей нет, то и смысла дальше анализировть тоже нет
if (regions.isEmpty() && !ctx.getTokens().isEmpty()) {
diagnosticStorage.addDiagnostic(ctx);

List<DiagnosticRelatedInformation> relatedInformation = createRelatedInformations(ctx);

var ctxStart = ctx.getStart();
diagnosticStorage.addDiagnostic(
Ranges.create(ctxStart.getLine(),
ctxStart.getCharPositionInLine(),
ctxStart.getLine(),
ctxStart.getCharPositionInLine()),
relatedInformation);
return ctx;
}

Expand All @@ -76,6 +88,52 @@ public ParseTree visitFile(BSLParser.FileContext ctx) {

}

private List<DiagnosticRelatedInformation> createRelatedInformations(BSLParser.FileContext ctx) {
List<DiagnosticRelatedInformation> relatedInformation = new ArrayList<>();

// замечание будет на первой строке модуля, остальные блоки - в релейшенах

// 1. блок переменных
// 2. блок кода до методов
addChildrenToRelatedInformation(ctx, relatedInformation,
BSLParser.RULE_moduleVars, BSLParser.RULE_fileCodeBlockBeforeSub);

// 3. методы
documentContext.getMethods().stream()
.map(node ->
RelatedInformation.create(
documentContext.getUri(),
node.getSubNameRange(),
"+1"
)
)
.collect(Collectors.toCollection(() -> relatedInformation));

// 4. блок кода после методов
addChildrenToRelatedInformation(ctx, relatedInformation, BSLParser.RULE_fileCodeBlock);
return relatedInformation;
}

private void addChildrenToRelatedInformation(
BSLParser.FileContext ctx,
List<DiagnosticRelatedInformation> relatedInformation,
Integer... ruleIndex
) {
Trees.getChildren(ctx, ruleIndex).stream()
.filter(node ->
node.getStart() != null
&& node.getStop() != null
&& (node.getStart().getLine() <= node.getStop().getLine())) // todo исправить костыль
.map(node ->
RelatedInformation.create(
documentContext.getUri(),
Ranges.create(node),
"+1"
)
)
.collect(Collectors.toCollection(() -> relatedInformation));
}

@Override
public ParseTree visitModuleVar(BSLParser.ModuleVarContext ctx) {
Trees.getChildren(ctx).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ public void addDiagnostic(
);
}

public void addDiagnostic(
Range range,
List<DiagnosticRelatedInformation> relatedInformation
) {
addDiagnostic(
range,
diagnostic.getInfo().getMessage(),
relatedInformation
);
}

public void addDiagnostic(
Range range,
String diagnosticMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ void testNoRegions() {

assertThat(diagnostics).hasSize(1);
assertThat(diagnostics, true)
.hasRange(4, 0, 22, 0);
.hasRange(5, 0, 0)
;
assertThat(diagnostics.get(0).getRelatedInformation())
.isNotNull()
.isNotEmpty();
assertThat(diagnostics.get(0).getRelatedInformation().size()).isEqualTo(4);

}

Expand All @@ -78,7 +83,7 @@ void testCodeBlock() {

assertThat(diagnostics).hasSize(1);
assertThat(diagnostics, true)
.hasRange(0, 0, 0, 23);
.hasRange(1, 0, 0);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

Процедура Бб()
Сообщаить(42);
КонецПроцдуры
КонецПроцедуры

///////////////////////////////////////////
// инициализация
Expand Down

0 comments on commit 3c80423

Please sign in to comment.