Skip to content

Commit 3dfb945

Browse files
committed
исправил ошибку в кейсах и реализации
1 parent bdf0a52 commit 3dfb945

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/RewriteMethodParameterDiagnostic.java

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.antlr.v4.runtime.tree.RuleNode;
3939
import org.antlr.v4.runtime.tree.TerminalNode;
4040
import org.apache.commons.lang3.tuple.Pair;
41+
import org.apache.commons.lang3.tuple.Triple;
4142
import org.eclipse.lsp4j.DiagnosticRelatedInformation;
4243
import org.eclipse.lsp4j.Position;
4344
import org.eclipse.lsp4j.Range;
@@ -60,6 +61,7 @@
6061

6162
@RequiredArgsConstructor
6263
public class RewriteMethodParameterDiagnostic extends AbstractDiagnostic {
64+
private static final int COUNT_OF_PAIR_FOR_SELF_ASSIGN = 2;
6365
private final ReferenceIndex referenceIndex;
6466

6567
@Override
@@ -70,7 +72,9 @@ public void check() {
7072
.map(this::isOverwrited)
7173
.filter(Optional::isPresent)
7274
.map(Optional::get)
73-
.forEach(variableSymbolListPair -> fireIssue(variableSymbolListPair.getLeft(), variableSymbolListPair.getRight()));
75+
.forEach(variableSymbolReferenceListTriple -> fireIssue(variableSymbolReferenceListTriple.getLeft(),
76+
variableSymbolReferenceListTriple.getMiddle(),
77+
variableSymbolReferenceListTriple.getRight()));
7478

7579
}
7680

@@ -89,25 +93,10 @@ private static Stream<VariableSymbol> getVariableByParameter(MethodSymbol method
8993
.findFirst().stream();
9094
}
9195

92-
private Optional<Pair<VariableSymbol, List<Reference>>> isOverwrited(VariableSymbol variable) {
96+
private Optional<Triple<VariableSymbol, Reference, List<Reference>>> isOverwrited(VariableSymbol variable) {
9397
final var references = getSortedReferencesByLocation(variable);
94-
if (isOverwrited(references)) {
95-
return Optional.of(Pair.of(variable, references));
96-
}
97-
return Optional.empty();
98-
}
99-
100-
private boolean isOverwrited(List<Reference> references) {
101-
if (!references.isEmpty()) {
102-
final var firstDefIntoAssign = references.get(0);
103-
if (firstDefIntoAssign.getOccurrenceType() == OccurrenceType.DEFINITION) {
104-
if (references.size() == 1) {
105-
return true;
106-
}
107-
return noneWritingToDefOrSelfAssign(firstDefIntoAssign, references.get(1));
108-
}
109-
}
110-
return false;
98+
return isOverwrited(references)
99+
.map(defReference -> Triple.of(variable, defReference, references));
111100
}
112101

113102
private List<Reference> getSortedReferencesByLocation(VariableSymbol variable) {
@@ -135,7 +124,29 @@ public static int compare(Position pos1, Position pos2) {
135124
// 1,9 1,4
136125
}
137126

138-
private boolean noneWritingToDefOrSelfAssign(Reference defRef, Reference nextRef) {
127+
private Optional<Reference> isOverwrited(List<Reference> references) {
128+
if (!references.isEmpty()) {
129+
final var firstDefIntoAssign = references.get(0);
130+
if (firstDefIntoAssign.getOccurrenceType() == OccurrenceType.DEFINITION) {
131+
if (references.size() == 1) {
132+
return Optional.of(firstDefIntoAssign);
133+
}
134+
var refContextInsideDefAssign = getRefContextInsideDefAssign(firstDefIntoAssign, references.get(1));
135+
if (refContextInsideDefAssign.isEmpty()){
136+
return Optional.of(firstDefIntoAssign);
137+
}
138+
var isSelfAssign = Boolean.TRUE.equals(refContextInsideDefAssign
139+
.map(RewriteMethodParameterDiagnostic::isVarNameOnlyIntoExpression)
140+
.orElseThrow());
141+
if (isSelfAssign && references.size() > COUNT_OF_PAIR_FOR_SELF_ASSIGN){
142+
return isOverwrited(references.subList(COUNT_OF_PAIR_FOR_SELF_ASSIGN, references.size()));
143+
}
144+
}
145+
}
146+
return Optional.empty();
147+
}
148+
149+
private Optional<RuleNode> getRefContextInsideDefAssign(Reference defRef, Reference nextRef) {
139150
final var defNode = Trees.findNodeContainsPosition(documentContext.getAst(),
140151
defRef.getSelectionRange().getStart());
141152
final var assignment = defNode
@@ -145,19 +156,15 @@ private boolean noneWritingToDefOrSelfAssign(Reference defRef, Reference nextRef
145156
.filter(BSLParser.AssignmentContext.class::isInstance)
146157
.map(BSLParser.AssignmentContext.class::cast);
147158
if (assignment.isEmpty()) {
148-
return true;
159+
return Optional.empty();
149160
}
150161

151-
final var refContext = Trees.findNodeContainsPosition(assignment.get(), nextRef.getSelectionRange().getStart())
162+
return Trees.findNodeContainsPosition(assignment.get(), nextRef.getSelectionRange().getStart())
152163
.map(TerminalNode::getParent);
153-
if (refContext.isEmpty()){
154-
return true;
155-
}
156-
return isVarNameOnlyIntoExpression(refContext);
157164
}
158165

159-
private static boolean isVarNameOnlyIntoExpression(Optional<RuleNode> refContext) {
160-
return refContext
166+
private static boolean isVarNameOnlyIntoExpression(RuleNode refContext) {
167+
return Optional.of(refContext)
161168
.filter(BSLParser.ComplexIdentifierContext.class::isInstance)
162169
.map(BSLParser.ComplexIdentifierContext.class::cast)
163170
.filter(node -> node.getChildCount() == 1)
@@ -170,7 +177,7 @@ private static boolean isVarNameOnlyIntoExpression(Optional<RuleNode> refContext
170177
.isPresent();
171178
}
172179

173-
private void fireIssue(VariableSymbol variable, List<Reference> references) {
180+
private void fireIssue(VariableSymbol variable, Reference nodeForIssue, List<Reference> references) {
174181
var refsForIssue = references.stream()
175182
.map(reference -> RelatedInformation.create(
176183
documentContext.getUri(),
@@ -184,6 +191,6 @@ private void fireIssue(VariableSymbol variable, List<Reference> references) {
184191
"0"));
185192
resultRefs.addAll(refsForIssue);
186193

187-
diagnosticStorage.addDiagnostic(references.get(0).getSelectionRange(), info.getMessage(variable.getName()), resultRefs);
194+
diagnosticStorage.addDiagnostic(nodeForIssue.getSelectionRange(), info.getMessage(variable.getName()), resultRefs);
188195
}
189196
}

src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/RewriteMethodParameterDiagnosticTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ void test() {
4141
assertThat(diagnostics, true)
4242
.hasRange(1, 4, 10)
4343
.hasRange(9, 4, 11)
44-
.hasRange(21, 4, 11)
45-
.hasRange(26, 4, 11)
44+
.hasRange(22, 4, 11)
45+
.hasRange(29, 4, 11)
4646
.hasRange(37, 4, 11)
47-
.hasSize(6);
47+
.hasSize(5);
4848

4949
}
5050
}

0 commit comments

Comments
 (0)