Browse Source

implement find references on local variables

newNRvisualizers
Siegfried Pammer 14 years ago
parent
commit
a79c7dad3f
  1. 19
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/Parser.cs
  2. 51
      src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs
  3. 4
      src/Main/Base/Project/Src/Editor/Commands/FindReferencesCommand.cs
  4. 5
      src/Main/Base/Project/Src/Services/ParserService/IParser.cs
  5. 2
      src/Main/Base/Project/Src/Services/ParserService/ParserService.cs
  6. 16
      src/Main/Base/Project/Src/Services/RefactoringService/FindReferenceService.cs
  7. 10
      src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs

19
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/Parser.cs

@ -14,6 +14,7 @@ using ICSharpCode.NRefactory.TypeSystem; @@ -14,6 +14,7 @@ using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Parser;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.SharpDevelop.Refactoring;
namespace CSharpBinding.Parser
{
@ -111,5 +112,23 @@ namespace CSharpBinding.Parser @@ -111,5 +112,23 @@ namespace CSharpBinding.Parser
return ResolveAtLocation.Resolve(context, parsedFile, cu, location, cancellationToken);
}
public void FindLocalReferences(ParseInformation parseInfo, IVariable variable, ITypeResolveContext context, Action<Reference> callback)
{
CompilationUnit cu = parseInfo.Annotation<CompilationUnit>();
if (cu == null)
throw new ArgumentException("Parse info does not have CompilationUnit");
CSharpParsedFile parsedFile = parseInfo.ParsedFile as CSharpParsedFile;
if (parsedFile == null)
throw new ArgumentException("Parse info does not have a C# ParsedFile");
new FindReferences().FindLocalReferences(
variable, parsedFile, cu, context,
delegate (AstNode node, ResolveResult result) {
var region = new DomRegion(parseInfo.FileName, node.StartLocation, node.EndLocation);
callback(new Reference(region, result));
});
}
}
}

51
src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs

@ -783,5 +783,56 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -783,5 +783,56 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
}
}
#endregion
#region Find Local Variable References
/// <summary>
/// Finds all references of a given variable.
/// </summary>
/// <param name="variable">The variable for which to look.</param>
/// <param name="parsedFile">The type system representation of the file being searched.</param>
/// <param name="compilationUnit">The compilation unit of the file being searched.</param>
/// <param name="context">The type resolve context to use for resolving the file.</param>
/// <param name="callback">Callback used to report the references that were found.</param>
public void FindLocalReferences(IVariable variable, CSharpParsedFile parsedFile, CompilationUnit compilationUnit,
ITypeResolveContext context, FoundReferenceCallback callback)
{
if (variable == null)
throw new ArgumentNullException("variable");
FindReferencesInFile(new FindLocalReferencesNavigator(variable), parsedFile, compilationUnit, context, callback);
}
class FindLocalReferencesNavigator : SearchScope
{
IVariable variable;
public FindLocalReferencesNavigator(IVariable variable)
{
this.variable = variable;
}
internal override bool CanMatch(AstNode node)
{
var expr = node as IdentifierExpression;
if (expr != null)
return expr.TypeArguments.Count == 0 && variable.Name == expr.Identifier;
var vi = node as VariableInitializer;
if (vi != null)
return vi.Name == variable.Name;
var pd = node as ParameterDeclaration;
if (pd != null)
return pd.Name == variable.Name;
var id = node as Identifier;
if (id != null)
return id.Name == variable.Name;
return false;
}
internal override bool IsMatch(ResolveResult rr)
{
var lrr = rr as LocalResolveResult;
return lrr != null && lrr.Variable.Name == variable.Name && lrr.Variable.Region == variable.Region;
}
}
#endregion
}
}

4
src/Main/Base/Project/Src/Editor/Commands/FindReferencesCommand.cs

@ -20,9 +20,7 @@ namespace ICSharpCode.SharpDevelop.Editor.Commands @@ -20,9 +20,7 @@ namespace ICSharpCode.SharpDevelop.Editor.Commands
return;
}
if (symbol is LocalResolveResult) {
#warning Find References not implemented for locals
throw new NotImplementedException();
//FindReferencesAndRenameHelper.RunFindReferences((LocalResolveResult)symbol);
FindReferencesAndRenameHelper.RunFindReferences((LocalResolveResult)symbol);
}
}
}

5
src/Main/Base/Project/Src/Services/ParserService/IParser.cs

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Threading;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
@ -8,8 +9,8 @@ using ICSharpCode.NRefactory.Editor; @@ -8,8 +9,8 @@ using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop.Editor;
using System;
using ICSharpCode.SharpDevelop.Parser;
using ICSharpCode.SharpDevelop.Refactoring;
namespace ICSharpCode.SharpDevelop.Project
{
@ -53,5 +54,7 @@ namespace ICSharpCode.SharpDevelop.Project @@ -53,5 +54,7 @@ namespace ICSharpCode.SharpDevelop.Project
bool fullParseInformationRequested);
ResolveResult Resolve(ParseInformation parseInfo, TextLocation location, ITypeResolveContext context, CancellationToken cancellationToken);
void FindLocalReferences(ParseInformation parseInfo, IVariable variable, ITypeResolveContext context, Action<Reference> callback);
}
}

2
src/Main/Base/Project/Src/Services/ParserService/ParserService.cs

@ -216,7 +216,7 @@ namespace ICSharpCode.SharpDevelop.Parser @@ -216,7 +216,7 @@ namespace ICSharpCode.SharpDevelop.Parser
/// <summary>
/// Gets the type resolve context for the default project content.
/// </summary>
static ITypeResolveContext GetDefaultTypeResolveContext()
public static ITypeResolveContext GetDefaultTypeResolveContext()
{
List<ITypeResolveContext> references = new List<ITypeResolveContext>();
references.Add(defaultProjectContent);

16
src/Main/Base/Project/Src/Services/RefactoringService/FindReferenceService.cs

@ -84,6 +84,22 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -84,6 +84,22 @@ namespace ICSharpCode.SharpDevelop.Refactoring
progressMonitor.Progress = workDone / totalWorkAmount;
}
}
public static void FindReferences(IVariable variable, Action<Reference> callback)
{
if (variable == null)
throw new ArgumentNullException("variable");
if (callback == null)
throw new ArgumentNullException("callback");
var fileName = FileName.Create(variable.Region.FileName);
IParser parser = ParserService.GetParser(fileName);
ParseInformation pi = ParserService.Parse(fileName);
if (pi == null || parser == null)
return;
IProject project = ParserService.GetProject(pi.ProjectContent);
var context = project != null ? project.TypeResolveContext : ParserService.GetDefaultTypeResolveContext();
parser.FindLocalReferences(pi, variable, context, callback);
}
#endregion
#region FindDerivedTypes

10
src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs

@ -5,8 +5,10 @@ using System; @@ -5,8 +5,10 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ICSharpCode.Core;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.Search;
@ -520,16 +522,16 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -520,16 +522,16 @@ namespace ICSharpCode.SharpDevelop.Refactoring
}
}
/*
public static void RunFindReferences(LocalResolveResult local)
{
var references = new List<Reference>();
FindReferenceService.FindReferences(local.Variable, r => { lock (references) references.Add(r); });
FindReferencesAndRenameHelper.ShowAsSearchResults(
StringParser.Parse("${res:SharpDevelop.Refactoring.ReferencesTo}",
new StringTagPair("Name", local.VariableName)),
RefactoringService.FindReferences(local, null)
new StringTagPair("Name", local.Variable.Name)),
references
);
}
*/
// public static ICSharpCode.Core.WinForms.MenuCommand MakeFindReferencesMenuCommand(EventHandler handler)
// {

Loading…
Cancel
Save