|
| 1 | +/* |
| 2 | + * This file is a part of BSL Language Server. |
| 3 | + * |
| 4 | + * Copyright (c) 2018-2022 |
| 5 | + * Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <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.context.symbol.MethodSymbol; |
| 25 | +import com.github._1c_syntax.bsl.languageserver.context.symbol.ParameterDefinition; |
| 26 | +import com.github._1c_syntax.bsl.languageserver.context.symbol.VariableSymbol; |
| 27 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata; |
| 28 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity; |
| 29 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag; |
| 30 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType; |
| 31 | +import com.github._1c_syntax.bsl.languageserver.references.ReferenceIndex; |
| 32 | +import com.github._1c_syntax.bsl.languageserver.references.model.OccurrenceType; |
| 33 | +import com.github._1c_syntax.bsl.languageserver.references.model.Reference; |
| 34 | +import com.github._1c_syntax.bsl.languageserver.utils.RelatedInformation; |
| 35 | +import com.github._1c_syntax.bsl.languageserver.utils.Trees; |
| 36 | +import com.github._1c_syntax.bsl.parser.BSLParser; |
| 37 | +import lombok.RequiredArgsConstructor; |
| 38 | +import org.antlr.v4.runtime.tree.RuleNode; |
| 39 | +import org.antlr.v4.runtime.tree.TerminalNode; |
| 40 | +import org.apache.commons.lang3.tuple.Pair; |
| 41 | +import org.apache.commons.lang3.tuple.Triple; |
| 42 | +import org.eclipse.lsp4j.DiagnosticRelatedInformation; |
| 43 | +import org.eclipse.lsp4j.Position; |
| 44 | +import org.eclipse.lsp4j.Range; |
| 45 | +import org.eclipse.lsp4j.SymbolKind; |
| 46 | + |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.List; |
| 49 | +import java.util.Optional; |
| 50 | +import java.util.stream.Collectors; |
| 51 | +import java.util.stream.Stream; |
| 52 | + |
| 53 | +@DiagnosticMetadata( |
| 54 | + type = DiagnosticType.CODE_SMELL, |
| 55 | + severity = DiagnosticSeverity.MAJOR, |
| 56 | + minutesToFix = 2, |
| 57 | + tags = { |
| 58 | + DiagnosticTag.SUSPICIOUS |
| 59 | + } |
| 60 | +) |
| 61 | + |
| 62 | +@RequiredArgsConstructor |
| 63 | +public class RewriteMethodParameterDiagnostic extends AbstractDiagnostic { |
| 64 | + private static final int COUNT_OF_PAIR_FOR_SELF_ASSIGN = 2; |
| 65 | + private final ReferenceIndex referenceIndex; |
| 66 | + |
| 67 | + @Override |
| 68 | + public void check() { |
| 69 | + documentContext.getSymbolTree().getMethods().stream() |
| 70 | + .flatMap(RewriteMethodParameterDiagnostic::getParametersByValue) |
| 71 | + .flatMap(pair -> getVariableByParameter(pair.getLeft(), pair.getRight())) |
| 72 | + .map(this::isOverwrited) |
| 73 | + .filter(Optional::isPresent) |
| 74 | + .map(Optional::get) |
| 75 | + .forEach(variableSymbolReferenceListTriple -> fireIssue(variableSymbolReferenceListTriple.getLeft(), |
| 76 | + variableSymbolReferenceListTriple.getMiddle(), |
| 77 | + variableSymbolReferenceListTriple.getRight())); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + private static Stream<Pair<MethodSymbol, ParameterDefinition>> getParametersByValue(MethodSymbol methodSymbol) { |
| 82 | + return methodSymbol.getParameters().stream() |
| 83 | + .filter(ParameterDefinition::isByValue) |
| 84 | + .map(parameterDefinition -> Pair.of(methodSymbol, parameterDefinition)); |
| 85 | + } |
| 86 | + |
| 87 | + private static Stream<VariableSymbol> getVariableByParameter(MethodSymbol method, ParameterDefinition parameterDefinition) { |
| 88 | + return method.getChildren().stream() |
| 89 | + // в будущем могут появиться и другие символы, подчиненные методам |
| 90 | + .filter(sourceDefinedSymbol -> sourceDefinedSymbol.getSymbolKind() == SymbolKind.Variable) |
| 91 | + .filter(variable -> parameterDefinition.getRange().getStart().equals(variable.getSelectionRange().getStart())) |
| 92 | + .filter(VariableSymbol.class::isInstance) |
| 93 | + .map(VariableSymbol.class::cast) |
| 94 | + .findFirst().stream(); |
| 95 | + } |
| 96 | + |
| 97 | + private Optional<Triple<VariableSymbol, Reference, List<Reference>>> isOverwrited(VariableSymbol variable) { |
| 98 | + final var references = getSortedReferencesByLocation(variable); |
| 99 | + return isOverwrited(references) |
| 100 | + .map(defReference -> Triple.of(variable, defReference, references)); |
| 101 | + } |
| 102 | + |
| 103 | + private List<Reference> getSortedReferencesByLocation(VariableSymbol variable) { |
| 104 | + final var references = referenceIndex.getReferencesTo(variable); |
| 105 | + return references.stream() |
| 106 | + .sorted((o1, o2) -> compare(o1.getSelectionRange(), o2.getSelectionRange())) |
| 107 | + .collect(Collectors.toList()); |
| 108 | + } |
| 109 | + |
| 110 | + private static int compare(Range o1, Range o2) { |
| 111 | + return compare(o1.getStart(), o2.getStart()); |
| 112 | + } |
| 113 | + |
| 114 | + public static int compare(Position pos1, Position pos2) { |
| 115 | + // 1,1 10,10 |
| 116 | + if (pos1.getLine() < pos2.getLine()) { |
| 117 | + return -1; |
| 118 | + } |
| 119 | + // 10,10 1,1 |
| 120 | + if (pos1.getLine() > pos2.getLine()) { |
| 121 | + return 1; |
| 122 | + } |
| 123 | + // 1,4 1,9 |
| 124 | + return Integer.compare(pos1.getCharacter(), pos2.getCharacter()); |
| 125 | + // 1,9 1,4 |
| 126 | + } |
| 127 | + |
| 128 | + private Optional<Reference> isOverwrited(List<Reference> references) { |
| 129 | + if (!references.isEmpty()) { |
| 130 | + final var firstDefIntoAssign = references.get(0); |
| 131 | + if (firstDefIntoAssign.getOccurrenceType() == OccurrenceType.DEFINITION) { |
| 132 | + if (references.size() == 1) { |
| 133 | + return Optional.of(firstDefIntoAssign); |
| 134 | + } |
| 135 | + var refContextInsideDefAssign = getRefContextInsideDefAssign(firstDefIntoAssign, references.get(1)); |
| 136 | + if (refContextInsideDefAssign.isEmpty()) { |
| 137 | + return Optional.of(firstDefIntoAssign); |
| 138 | + } |
| 139 | + var isSelfAssign = Boolean.TRUE.equals(refContextInsideDefAssign |
| 140 | + .map(RewriteMethodParameterDiagnostic::isVarNameOnlyIntoExpression) |
| 141 | + .orElseThrow()); |
| 142 | + if (isSelfAssign && references.size() > COUNT_OF_PAIR_FOR_SELF_ASSIGN) { |
| 143 | + return isOverwrited(references.subList(COUNT_OF_PAIR_FOR_SELF_ASSIGN, references.size())); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return Optional.empty(); |
| 148 | + } |
| 149 | + |
| 150 | + private Optional<RuleNode> getRefContextInsideDefAssign(Reference defRef, Reference nextRef) { |
| 151 | + final var defNode = Trees.findTerminalNodeContainsPosition(documentContext.getAst(), |
| 152 | + defRef.getSelectionRange().getStart()); |
| 153 | + final var assignment = defNode |
| 154 | + .map(TerminalNode::getParent) |
| 155 | + .filter(BSLParser.LValueContext.class::isInstance) |
| 156 | + .map(RuleNode::getParent) |
| 157 | + .filter(BSLParser.AssignmentContext.class::isInstance) |
| 158 | + .map(BSLParser.AssignmentContext.class::cast); |
| 159 | + |
| 160 | + return assignment.flatMap(assignContext -> |
| 161 | + Trees.findTerminalNodeContainsPosition(assignContext, nextRef.getSelectionRange().getStart())) |
| 162 | + .map(TerminalNode::getParent); |
| 163 | + } |
| 164 | + |
| 165 | + private static boolean isVarNameOnlyIntoExpression(RuleNode refContext) { |
| 166 | + return Optional.of(refContext) |
| 167 | + .filter(BSLParser.ComplexIdentifierContext.class::isInstance) |
| 168 | + .map(BSLParser.ComplexIdentifierContext.class::cast) |
| 169 | + .filter(node -> node.getChildCount() == 1) |
| 170 | + .map(RuleNode::getParent) |
| 171 | + .filter(BSLParser.MemberContext.class::isInstance) |
| 172 | + .map(RuleNode::getParent) |
| 173 | + .filter(expression -> expression.getChildCount() == 1) |
| 174 | + .filter(BSLParser.ExpressionContext.class::isInstance) |
| 175 | + .isPresent(); |
| 176 | + } |
| 177 | + |
| 178 | + private void fireIssue(VariableSymbol variable, Reference nodeForIssue, List<Reference> references) { |
| 179 | + var refsForIssue = references.stream() |
| 180 | + .map(reference -> RelatedInformation.create( |
| 181 | + documentContext.getUri(), |
| 182 | + reference.getSelectionRange(), |
| 183 | + "+1" |
| 184 | + )).collect(Collectors.toList()); |
| 185 | + var resultRefs = new ArrayList<DiagnosticRelatedInformation>(); |
| 186 | + resultRefs.add(RelatedInformation.create( |
| 187 | + documentContext.getUri(), |
| 188 | + variable.getVariableNameRange(), |
| 189 | + "0")); |
| 190 | + resultRefs.addAll(refsForIssue); |
| 191 | + |
| 192 | + diagnosticStorage.addDiagnostic(nodeForIssue.getSelectionRange(), info.getMessage(variable.getName()), resultRefs); |
| 193 | + } |
| 194 | +} |
0 commit comments