bool IsReferenced(IVariable variable, AstNode node, SyntaxTree syntaxTree, RefactoringContext context) { int referencesFound = 0; var findRef = new FindReferences(); findRef.FindLocalReferences(variable, context.UnresolvedFile, syntaxTree, context.Compilation, (n, entity) => { referencesFound++; }, CancellationToken.None); // One reference is the declaration, and that does not count return referencesFound > 1; }
bool HasDependency(RefactoringContext context, AstNode firstSearchNode, AstNode targetNode) { var referenceFinder = new FindReferences(); var identifiers = targetNode.Descendants .Where(n => n is IdentifierExpression) .Select<AstNode, IdentifierExpression>(node => (IdentifierExpression)node); foreach (var identifier in identifiers) { var resolveResult = context.Resolve(identifier); var localResolveResult = resolveResult as LocalResolveResult; if (localResolveResult == null) continue; bool referenceFound = false; // var variable = localResolveResult.Variable; var syntaxTree = context.RootNode as SyntaxTree; referenceFinder.FindLocalReferences(localResolveResult.Variable, context.UnresolvedFile, syntaxTree, context.Compilation, (node, nodeResolveResult) => { if (node.StartLocation > firstSearchNode.StartLocation && node.EndLocation < targetNode.StartLocation) referenceFound = true; }, CancellationToken.None); if (referenceFound) return true; } return false; }