FindLocalReferences() 공개 메소드

Finds all references of a given variable.
public FindLocalReferences ( IVariable variable, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, ICompilation compilation, FoundReferenceCallback callback, CancellationToken cancellationToken ) : void
variable IVariable The variable for which to look.
unresolvedFile CSharpUnresolvedFile The type system representation of the file being searched.
syntaxTree SyntaxTree The syntax tree of the file being searched.
compilation ICompilation The compilation.
callback FoundReferenceCallback Callback used to report the references that were found.
cancellationToken System.Threading.CancellationToken Cancellation token that may be used to cancel the operation.
리턴 void
예제 #1
0
        public FindUsagesResponse FindUsages(FindUsagesRequest request)
        {
            var res = _parser.ParsedContent(request.Buffer, request.FileName);
            var loc = new TextLocation(request.Line, request.Column);
            var result = new ConcurrentBag<AstNode>();
            var findReferences = new FindReferences();
            ResolveResult resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);
            if (resolveResult is LocalResolveResult)
            {
                var variable = (resolveResult as LocalResolveResult).Variable;
                findReferences.FindLocalReferences(variable, res.UnresolvedFile, res.SyntaxTree, res.Compilation, (node, rr) => result.Add(node), CancellationToken.None);
            }
            else
            {
                IEntity entity = null;
                if (resolveResult is TypeResolveResult)
                {
                    entity = (resolveResult as TypeResolveResult).Type.GetDefinition();
                }

                if (resolveResult is MemberResolveResult)
                {
                    entity = (resolveResult as MemberResolveResult).Member;
                }

                if (entity == null)
                {
                    return new FindUsagesResponse {Usages = new List<Usage>()};
                }
                var searchScopes = findReferences.GetSearchScopes(entity);

                var interesting = new List<CSharpUnresolvedFile>();

                foreach (var scope in searchScopes)
                {
                    var scopeInteresting = findReferences.GetInterestingFiles(scope, res.Compilation);
                    interesting.AddRange(scopeInteresting);
                }

                Parallel.ForEach(interesting, file =>
                    {
                        ParsedResult parsedResult = _parser.ParsedContent(
                            _solution.GetFile(file.FileName).Content.Text, file.FileName);
                        findReferences.FindReferencesInFile(searchScopes, file, parsedResult.SyntaxTree,
                                                            parsedResult.Compilation,
                                                            (node, rr) => result.Add(node), CancellationToken.None);
                    });

            }

            var usages = result.Select(node => new Usage
            {
                FileName = node.GetRegion().FileName,
                Text = node.Preview(_solution.GetFile(node.GetRegion().FileName)).Replace("'", "''"),
                Line = node.StartLocation.Line,
                Column = node.StartLocation.Column,
            });

            return new FindUsagesResponse { Usages = usages };
        }
        public IEnumerable <MemberReference> FindInDocument(MonoDevelop.Ide.Gui.Document doc)
        {
            if (string.IsNullOrEmpty(memberName))
            {
                return(Enumerable.Empty <MemberReference> ());
            }
            var editor         = doc.Editor;
            var parsedDocument = doc.ParsedDocument;

            if (parsedDocument == null)
            {
                return(Enumerable.Empty <MemberReference> ());
            }
            var unit   = parsedDocument.GetAst <SyntaxTree> ();
            var file   = parsedDocument.ParsedFile as CSharpUnresolvedFile;
            var result = new List <MemberReference> ();

            foreach (var obj in searchedMembers)
            {
                if (obj is IEntity)
                {
                    var entity = (IEntity)obj;

                    // May happen for anonymous types since empty constructors are always generated.
                    // But there is no declaring type definition for them - we filter out this case.
                    if (entity.EntityType == EntityType.Constructor && entity.DeclaringTypeDefinition == null)
                    {
                        continue;
                    }

                    refFinder.FindReferencesInFile(refFinder.GetSearchScopes(entity), file, unit, doc.Compilation, (astNode, r) => {
                        if (IsNodeValid(obj, astNode))
                        {
                            result.Add(GetReference(r, astNode, editor.FileName, editor));
                        }
                    }, CancellationToken.None);
                }
                else if (obj is IVariable)
                {
                    refFinder.FindLocalReferences((IVariable)obj, file, unit, doc.Compilation, (astNode, r) => {
                        if (IsNodeValid(obj, astNode))
                        {
                            result.Add(GetReference(r, astNode, editor.FileName, editor));
                        }
                    }, CancellationToken.None);
                }
                else if (obj is ITypeParameter)
                {
                    refFinder.FindTypeParameterReferences((ITypeParameter)obj, file, unit, doc.Compilation, (astNode, r) => {
                        if (IsNodeValid(obj, astNode))
                        {
                            result.Add(GetReference(r, astNode, editor.FileName, editor));
                        }
                    }, CancellationToken.None);
                }
            }
            return(result);
        }
        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;
        }
        public IEnumerable <MemberReference> FindInDocument(MonoDevelop.Ide.Gui.Document doc)
        {
            if (string.IsNullOrEmpty(memberName))
            {
                return(Enumerable.Empty <MemberReference> ());
            }
            var editor         = doc.Editor;
            var parsedDocument = doc.ParsedDocument;

            if (parsedDocument == null)
            {
                return(Enumerable.Empty <MemberReference> ());
            }
            var unit   = parsedDocument.GetAst <CompilationUnit> ();
            var file   = parsedDocument.ParsedFile as CSharpParsedFile;
            var result = new List <MemberReference> ();

            foreach (var obj in searchedMembers)
            {
                if (obj is IEntity)
                {
                    refFinder.FindReferencesInFile(refFinder.GetSearchScopes((IEntity)obj), file, unit, doc.Compilation, (astNode, r) =>
                    {
                        if (IsNodeValid(obj, astNode))
                        {
                            result.Add(GetReference(r, astNode, editor.FileName, editor));
                        }
                    }, CancellationToken.None);
                }
                else if (obj is IVariable)
                {
                    refFinder.FindLocalReferences((IVariable)obj, file, unit, doc.Compilation, (astNode, r) =>
                    {
                        if (IsNodeValid(obj, astNode))
                        {
                            result.Add(GetReference(r, astNode, editor.FileName, editor));
                        }
                    }, CancellationToken.None);
                }
                else if (obj is ITypeParameter)
                {
                    refFinder.FindTypeParameterReferences((ITypeParameter)obj, file, unit, doc.Compilation, (astNode, r) =>
                    {
                        if (IsNodeValid(obj, astNode))
                        {
                            result.Add(GetReference(r, astNode, editor.FileName, editor));
                        }
                    }, CancellationToken.None);
                }
            }
            return(result);
        }
			void FindIssuesInNode(AstNode anchor, AstNode node, string accessorName = "setter")
			{
				if (node == null || node.IsNull)
					return;
				var localResolveResult = context.GetResolverStateBefore(node)
					.LookupSimpleNameOrTypeName("value", new List<IType>(), NameLookupMode.Expression) as LocalResolveResult; 
				if (localResolveResult == null)
					return;

				var variable = localResolveResult.Variable;
				bool referenceFound = false;
				var findRef = new FindReferences();
				var syntaxTree = (SyntaxTree)context.RootNode;
				findRef.FindLocalReferences(variable, context.UnresolvedFile, syntaxTree, context.Compilation, (n, entity) => {
					if (n.StartLocation >= node.StartLocation && n.EndLocation <= node.EndLocation) {
						referenceFound = true;
					}
				}, CancellationToken.None);

				if(!referenceFound)
					AddIssue(anchor, context.TranslateString("The " + accessorName + " does not use the 'value' parameter"));
			}
		bool HasDependencyCheck(FindReferences referenceFinder, LocalResolveResult localResolveResult)
		{
			bool result = false;
			referenceFinder.FindLocalReferences(localResolveResult.Variable, context.UnresolvedFile,
			                                    (SyntaxTree)context.RootNode, context.Compilation,
			                                    (node, resolveResult) => {
				result |= VariableHasBeenConverted(localResolveResult.Variable);
			}, CancellationToken.None);
			return result;
		}
예제 #7
0
		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;
		}
 public override void Rename(IVariable variable, string name)
 {
     FindReferences refFinder = new FindReferences ();
     refFinder.FindLocalReferences (variable,
                                    context.UnresolvedFile,
                                    context.RootNode as SyntaxTree,
                                    context.Compilation, (n, r) => Rename (n, name),
                                    context.CancellationToken);
 }
예제 #9
0
        private List<AstNode> GetResolvedNodes(ResolveResultType type, AstNode node)
        {
            var resolvedNodes = new List<AstNode>();
            ResolveResult resolveResult = _resolver.Resolve(node);
            if (resolveResult != null)
            {
                var findReferences = new FindReferences();
                FoundReferenceCallback callback = delegate (AstNode matchNode, ResolveResult result)
                {
                    resolvedNodes.Add(matchNode);
                };

                if (type == ResolveResultType.Local)
                {
                    var localResolveResult = resolveResult as LocalResolveResult;
                    if (localResolveResult != null)
                        findReferences.FindLocalReferences(localResolveResult.Variable, _unresolvedFile, SyntaxTree, _compilation, callback, CancellationToken.None);
                }
                else if (type == ResolveResultType.Member)
                {
                    var memberResolveResult = resolveResult as MemberResolveResult;
                    if (memberResolveResult != null)
                    {
                        var searchScopes = findReferences.GetSearchScopes(memberResolveResult.Member);
                        findReferences.FindReferencesInFile(searchScopes, _unresolvedFile, SyntaxTree, _compilation, callback, CancellationToken.None);
                    }
                }
                else if (type == ResolveResultType.Type)
                {
                    var typeResolveResult = resolveResult as TypeResolveResult;
                    if (typeResolveResult != null)
                    {
                        var searchScopes = findReferences.GetSearchScopes(typeResolveResult.Type.GetDefinition());
                        findReferences.FindReferencesInFile(searchScopes, _unresolvedFile, SyntaxTree, _compilation, callback, CancellationToken.None);
                    }
                }
            }
            else
            {
            }
            return resolvedNodes;
        }