Browse Source

Merge NRefactory changes from SharpDevelop into NRefactory repository.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
6bc485fa1b
  1. 2
      ICSharpCode.Editor/TextChangeEventArgs.cs
  2. 6
      ICSharpCode.NRefactory.Demo/CSDemo.cs
  3. 4
      ICSharpCode.NRefactory/CSharp/Resolver/CompositeResolveVisitorNavigator.cs
  4. 51
      ICSharpCode.NRefactory/CSharp/Resolver/FindReferenceSearchScope.cs
  5. 112
      ICSharpCode.NRefactory/CSharp/Resolver/FindReferences.cs
  6. 5
      ICSharpCode.NRefactory/CSharp/Resolver/LocalResolveResult.cs
  7. 5
      ICSharpCode.NRefactory/CSharp/Resolver/MemberResolveResult.cs
  8. 2
      ICSharpCode.NRefactory/CSharp/Resolver/ResolveAtLocation.cs
  9. 5
      ICSharpCode.NRefactory/CSharp/Resolver/ResolveResult.cs
  10. 9
      ICSharpCode.NRefactory/CSharp/Resolver/TypeResolveResult.cs
  11. 1
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  12. 2
      ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs

2
ICSharpCode.Editor/TextChangeEventArgs.cs

@ -39,7 +39,7 @@ namespace ICSharpCode.Editor
} }
/// <summary> /// <summary>
/// The text that was inserted. /// The text that was removed.
/// </summary> /// </summary>
public string RemovedText { public string RemovedText {
get { return removedText; } get { return removedText; }

6
ICSharpCode.NRefactory.Demo/CSDemo.cs

@ -282,10 +282,12 @@ namespace ICSharpCode.NRefactory.Demo
FindReferences fr = new FindReferences(); FindReferences fr = new FindReferences();
int referenceCount = 0; int referenceCount = 0;
fr.ReferenceFound += delegate { referenceCount++; }; FoundReferenceCallback callback = delegate(AstNode matchNode, ResolveResult result) {
referenceCount++;
};
var searchScopes = fr.GetSearchScopes(entity); var searchScopes = fr.GetSearchScopes(entity);
navigator = new CompositeResolveVisitorNavigator(searchScopes.ToArray()); navigator = new CompositeResolveVisitorNavigator(searchScopes.Select(s => s.GetNavigator(callback)).ToArray());
visitor = new ResolveVisitor(resolver, parsedFile, navigator); visitor = new ResolveVisitor(resolver, parsedFile, navigator);
visitor.Scan(compilationUnit); visitor.Scan(compilationUnit);

4
ICSharpCode.NRefactory/CSharp/Resolver/CompositeResolveVisitorNavigator.cs

@ -25,8 +25,10 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
{ {
IResolveVisitorNavigator[] navigators; IResolveVisitorNavigator[] navigators;
public CompositeResolveVisitorNavigator(IResolveVisitorNavigator[] navigators) public CompositeResolveVisitorNavigator(params IResolveVisitorNavigator[] navigators)
{ {
if (navigators == null)
throw new ArgumentNullException("navigators");
this.navigators = navigators; this.navigators = navigators;
} }

51
ICSharpCode.NRefactory/CSharp/Resolver/FindReferenceSearchScope.cs

@ -0,0 +1,51 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
/// <summary>
/// Represents a scope in which references are searched.
/// </summary>
public interface IFindReferenceSearchScope
{
/// <summary>
/// Gets the search term. Only files that contain this identifier need to be parsed.
/// Can return null if all files need to be parsed.
/// </summary>
string SearchTerm { get; }
/// <summary>
/// Gets the accessibility that defines the search scope.
/// </summary>
Accessibility Accessibility { get; }
/// <summary>
/// Gets the top-level entity that defines the search scope.
/// </summary>
ITypeDefinition TopLevelTypeDefinition { get; }
/// <summary>
/// Creates a navigator that can find references to this entity and reports
/// them to the specified callback.
/// </summary>
IResolveVisitorNavigator GetNavigator(FoundReferenceCallback callback);
}
}

112
ICSharpCode.NRefactory/CSharp/Resolver/FindReferences.cs

@ -26,6 +26,8 @@ using ICSharpCode.NRefactory.Utils;
namespace ICSharpCode.NRefactory.CSharp.Resolver namespace ICSharpCode.NRefactory.CSharp.Resolver
{ {
public delegate void FoundReferenceCallback(AstNode astNode, ResolveResult result);
/// <summary> /// <summary>
/// 'Find references' implementation. /// 'Find references' implementation.
/// </summary> /// </summary>
@ -36,21 +38,12 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// </remarks> /// </remarks>
public sealed class FindReferences public sealed class FindReferences
{ {
CancellationToken cancellationToken; #region Properties
/// <summary> /// <summary>
/// Callback that is invoked whenever a reference is found. /// Gets/Sets the cancellation token.
/// </summary> /// </summary>
public event Action<AstNode, ResolveResult> ReferenceFound; public CancellationToken CancellationToken { get; set; }
#region Constructor
public FindReferences(CancellationToken cancellationToken = default(CancellationToken))
{
this.cancellationToken = cancellationToken;
}
#endregion
#region Properties
/// <summary> /// <summary>
/// Gets/Sets whether to find type references even if an alias is being used. /// Gets/Sets whether to find type references even if an alias is being used.
/// </summary> /// </summary>
@ -104,28 +97,32 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
#endregion #endregion
#region class SearchScope #region class SearchScope
public abstract class SearchScope : IResolveVisitorNavigator abstract class SearchScope : IResolveVisitorNavigator, IFindReferenceSearchScope
{ {
protected string searchTerm; protected string searchTerm;
internal Accessibility accessibility; internal Accessibility accessibility;
internal ITypeDefinition topLevelTypeDefinition; internal ITypeDefinition topLevelTypeDefinition;
internal FindReferences findReferences;
/// <summary> FoundReferenceCallback callback;
/// Gets the search term. Only files that contain this identifier need to be parsed.
/// Can return null if all files need to be parsed.
/// </summary>
public string SearchTerm { get { return searchTerm; } }
/// <summary> IResolveVisitorNavigator IFindReferenceSearchScope.GetNavigator(FoundReferenceCallback callback)
/// Gets the accessibility that defines the search scope. {
/// </summary> SearchScope n = (SearchScope)MemberwiseClone();
public Accessibility Accessibility { get { return accessibility; } } n.callback = callback;
return n;
}
string IFindReferenceSearchScope.SearchTerm {
get { return searchTerm; }
}
/// <summary> Accessibility IFindReferenceSearchScope.Accessibility {
/// Gets the top-level entity that defines the search scope. get { return accessibility; }
/// </summary> }
public ITypeDefinition TopLevelTypeDefinition { get { return topLevelTypeDefinition; } }
ITypeDefinition IFindReferenceSearchScope.TopLevelTypeDefinition {
get { return topLevelTypeDefinition; }
}
internal abstract bool CanMatch(AstNode node); internal abstract bool CanMatch(AstNode node);
internal abstract bool IsMatch(ResolveResult rr); internal abstract bool IsMatch(ResolveResult rr);
@ -156,15 +153,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
protected void ReportMatch(AstNode node, ResolveResult result) protected void ReportMatch(AstNode node, ResolveResult result)
{ {
var referenceFound = findReferences.ReferenceFound; if (callback != null)
if (referenceFound != null) callback(node, result);
referenceFound(node, result);
} }
} }
#endregion #endregion
#region GetSearchScopes #region GetSearchScopes
public IList<SearchScope> GetSearchScopes(IEntity entity) public IList<IFindReferenceSearchScope> GetSearchScopes(IEntity entity)
{ {
if (entity == null) if (entity == null)
throw new ArgumentNullException("entity"); throw new ArgumentNullException("entity");
@ -205,19 +201,17 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
additionalScope = new FindChainedConstructorReferences(ctor); additionalScope = new FindChainedConstructorReferences(ctor);
break; break;
case EntityType.Destructor: case EntityType.Destructor:
return EmptyList<SearchScope>.Instance; return EmptyList<IFindReferenceSearchScope>.Instance;
default: default:
throw new ArgumentException("Unknown entity type " + entity.EntityType); throw new ArgumentException("Unknown entity type " + entity.EntityType);
} }
if (scope.accessibility == Accessibility.None) if (scope.accessibility == Accessibility.None)
scope.accessibility = effectiveAccessibility; scope.accessibility = effectiveAccessibility;
scope.topLevelTypeDefinition = topLevelTypeDefinition; scope.topLevelTypeDefinition = topLevelTypeDefinition;
scope.findReferences = this;
if (additionalScope != null) { if (additionalScope != null) {
if (additionalScope.accessibility == Accessibility.None) if (additionalScope.accessibility == Accessibility.None)
additionalScope.accessibility = effectiveAccessibility; additionalScope.accessibility = effectiveAccessibility;
additionalScope.topLevelTypeDefinition = topLevelTypeDefinition; additionalScope.topLevelTypeDefinition = topLevelTypeDefinition;
additionalScope.findReferences = this;
return new[] { scope, additionalScope }; return new[] { scope, additionalScope };
} else { } else {
return new[] { scope }; return new[] { scope };
@ -229,7 +223,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <summary> /// <summary>
/// Gets the file names that possibly contain references to the element being searched for. /// Gets the file names that possibly contain references to the element being searched for.
/// </summary> /// </summary>
public IEnumerable<string> GetInterestingFileNames(SearchScope searchScope, ITypeResolveContext context) public IList<string> GetInterestingFileNames(IFindReferenceSearchScope searchScope, IEnumerable<ITypeDefinition> allTypes, ITypeResolveContext context)
{ {
IEnumerable<ITypeDefinition> interestingTypes; IEnumerable<ITypeDefinition> interestingTypes;
if (searchScope.TopLevelTypeDefinition != null) { if (searchScope.TopLevelTypeDefinition != null) {
@ -239,41 +233,41 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
interestingTypes = new [] { searchScope.TopLevelTypeDefinition.GetDefinition() }; interestingTypes = new [] { searchScope.TopLevelTypeDefinition.GetDefinition() };
break; break;
case Accessibility.Protected: case Accessibility.Protected:
interestingTypes = GetInterestingTypesProtected(context, searchScope.TopLevelTypeDefinition); interestingTypes = GetInterestingTypesProtected(allTypes, context, searchScope.TopLevelTypeDefinition);
break; break;
case Accessibility.Internal: case Accessibility.Internal:
interestingTypes = GetInterestingTypesInternal(context, searchScope.TopLevelTypeDefinition.ProjectContent); interestingTypes = GetInterestingTypesInternal(allTypes, context, searchScope.TopLevelTypeDefinition.ProjectContent);
break; break;
case Accessibility.ProtectedAndInternal: case Accessibility.ProtectedAndInternal:
interestingTypes = GetInterestingTypesProtected(context, searchScope.TopLevelTypeDefinition) interestingTypes = GetInterestingTypesProtected(allTypes, context, searchScope.TopLevelTypeDefinition)
.Intersect(GetInterestingTypesInternal(context, searchScope.TopLevelTypeDefinition.ProjectContent)); .Intersect(GetInterestingTypesInternal(allTypes, context, searchScope.TopLevelTypeDefinition.ProjectContent));
break; break;
case Accessibility.ProtectedOrInternal: case Accessibility.ProtectedOrInternal:
interestingTypes = GetInterestingTypesProtected(context, searchScope.TopLevelTypeDefinition) interestingTypes = GetInterestingTypesProtected(allTypes, context, searchScope.TopLevelTypeDefinition)
.Union(GetInterestingTypesInternal(context, searchScope.TopLevelTypeDefinition.ProjectContent)); .Union(GetInterestingTypesInternal(allTypes, context, searchScope.TopLevelTypeDefinition.ProjectContent));
break; break;
default: default:
interestingTypes = context.GetTypes(); interestingTypes = allTypes;
break; break;
} }
} else { } else {
interestingTypes = context.GetTypes(); interestingTypes = allTypes;
} }
return (from typeDef in interestingTypes return (from typeDef in interestingTypes
from part in typeDef.GetParts() from part in typeDef.GetParts()
where part.ParsedFile != null where part.ParsedFile != null
select part.ParsedFile.FileName select part.ParsedFile.FileName
).Distinct(Platform.FileNameComparer); ).Distinct(Platform.FileNameComparer).ToList();
} }
IEnumerable<ITypeDefinition> GetInterestingTypesProtected(ITypeResolveContext context, ITypeDefinition referencedTypeDefinition) IEnumerable<ITypeDefinition> GetInterestingTypesProtected(IEnumerable<ITypeDefinition> allTypes, ITypeResolveContext context, ITypeDefinition referencedTypeDefinition)
{ {
return referencedTypeDefinition.GetSubTypeDefinitions(context); return allTypes.Where(t => t.IsDerivedFrom(referencedTypeDefinition, context));
} }
IEnumerable<ITypeDefinition> GetInterestingTypesInternal(ITypeResolveContext context, IProjectContent referencedProjectContent) IEnumerable<ITypeDefinition> GetInterestingTypesInternal(IEnumerable<ITypeDefinition> allTypes, ITypeResolveContext context, IProjectContent referencedProjectContent)
{ {
return context.GetTypes().Where(t => referencedProjectContent.InternalsVisibleTo(t.ProjectContent, context)); return allTypes.Where(t => referencedProjectContent.InternalsVisibleTo(t.ProjectContent, context));
} }
#endregion #endregion
@ -285,11 +279,13 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <param name="parsedFile">The type system representation of the file being searched.</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="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="context">The type resolve context to use for resolving the file.</param>
public void FindReferencesInFile(SearchScope searchScope, CSharpParsedFile parsedFile, CompilationUnit compilationUnit, ITypeResolveContext context) /// <param name="callback">Callback used to report the references that were found.</param>
public void FindReferencesInFile(IFindReferenceSearchScope searchScope, CSharpParsedFile parsedFile, CompilationUnit compilationUnit,
ITypeResolveContext context, FoundReferenceCallback callback)
{ {
if (searchScope == null) if (searchScope == null)
throw new ArgumentNullException("searchScope"); throw new ArgumentNullException("searchScope");
FindReferencesInFile(new[] { searchScope }, parsedFile, compilationUnit, context); FindReferencesInFile(new[] { searchScope }, parsedFile, compilationUnit, context, callback);
} }
/// <summary> /// <summary>
@ -299,7 +295,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <param name="parsedFile">The type system representation of the file being searched.</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="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="context">The type resolve context to use for resolving the file.</param>
public void FindReferencesInFile(IList<SearchScope> searchScopes, CSharpParsedFile parsedFile, CompilationUnit compilationUnit, ITypeResolveContext context) /// <param name="callback">Callback used to report the references that were found.</param>
public void FindReferencesInFile(IList<IFindReferenceSearchScope> searchScopes, CSharpParsedFile parsedFile, CompilationUnit compilationUnit,
ITypeResolveContext context, FoundReferenceCallback callback)
{ {
if (searchScopes == null) if (searchScopes == null)
throw new ArgumentNullException("searchScopes"); throw new ArgumentNullException("searchScopes");
@ -309,21 +307,17 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
throw new ArgumentNullException("compilationUnit"); throw new ArgumentNullException("compilationUnit");
if (context == null) if (context == null)
throw new ArgumentNullException("context"); throw new ArgumentNullException("context");
cancellationToken.ThrowIfCancellationRequested(); this.CancellationToken.ThrowIfCancellationRequested();
if (searchScopes.Count == 0) if (searchScopes.Count == 0)
return; return;
foreach (SearchScope scope in searchScopes) {
if (scope.findReferences != this)
throw new ArgumentException("Cannot use a search scope that was created by another FindReferences instance");
}
using (var ctx = context.Synchronize()) { using (var ctx = context.Synchronize()) {
IResolveVisitorNavigator navigator; IResolveVisitorNavigator navigator;
if (searchScopes.Count == 1) if (searchScopes.Count == 1)
navigator = searchScopes[0]; navigator = searchScopes[0].GetNavigator(callback);
else else
navigator = new CompositeResolveVisitorNavigator(searchScopes.ToArray()); navigator = new CompositeResolveVisitorNavigator(searchScopes.Select(s => s.GetNavigator(callback)).ToArray());
navigator = new DetectSkippableNodesNavigator(navigator, compilationUnit); navigator = new DetectSkippableNodesNavigator(navigator, compilationUnit);
CSharpResolver resolver = new CSharpResolver(ctx, cancellationToken); CSharpResolver resolver = new CSharpResolver(ctx, this.CancellationToken);
ResolveVisitor v = new ResolveVisitor(resolver, parsedFile, navigator); ResolveVisitor v = new ResolveVisitor(resolver, parsedFile, navigator);
v.Scan(compilationUnit); v.Scan(compilationUnit);
} }

5
ICSharpCode.NRefactory/CSharp/Resolver/LocalResolveResult.cs

@ -68,5 +68,10 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
{ {
return string.Format("[LocalResolveResult {0}]", variable); return string.Format("[LocalResolveResult {0}]", variable);
} }
public override DomRegion GetDefinitionRegion()
{
return variable.Region;
}
} }
} }

5
ICSharpCode.NRefactory/CSharp/Resolver/MemberResolveResult.cs

@ -92,5 +92,10 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
{ {
return string.Format("[{0} {1}]", GetType().Name, member); return string.Format("[{0} {1}]", GetType().Name, member);
} }
public override DomRegion GetDefinitionRegion()
{
return member.Region;
}
} }
} }

2
ICSharpCode.NRefactory/CSharp/Resolver/ResolveAtLocation.cs

@ -32,6 +32,8 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
CancellationToken cancellationToken = default(CancellationToken)) CancellationToken cancellationToken = default(CancellationToken))
{ {
AstNode node = cu.GetNodeAt(location); AstNode node = cu.GetNodeAt(location);
if (node == null)
return null;
AstNode resolvableNode; AstNode resolvableNode;
if (node is Identifier) { if (node is Identifier) {
resolvableNode = node.Parent; resolvableNode = node.Parent;

5
ICSharpCode.NRefactory/CSharp/Resolver/ResolveResult.cs

@ -63,5 +63,10 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
{ {
return Enumerable.Empty<ResolveResult>(); return Enumerable.Empty<ResolveResult>();
} }
public virtual DomRegion GetDefinitionRegion()
{
return DomRegion.Empty;
}
} }
} }

9
ICSharpCode.NRefactory/CSharp/Resolver/TypeResolveResult.cs

@ -30,5 +30,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
: base(type) : base(type)
{ {
} }
public override DomRegion GetDefinitionRegion()
{
ITypeDefinition def = this.Type.GetDefinition();
if (def != null)
return def.Region;
else
return DomRegion.Empty;
}
} }
} }

1
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -104,6 +104,7 @@
<Compile Include="CSharp\Resolver\DetectSkippableNodesNavigator.cs" /> <Compile Include="CSharp\Resolver\DetectSkippableNodesNavigator.cs" />
<Compile Include="CSharp\Resolver\FindReferencedEntities.cs" /> <Compile Include="CSharp\Resolver\FindReferencedEntities.cs" />
<Compile Include="CSharp\Resolver\FindReferences.cs" /> <Compile Include="CSharp\Resolver\FindReferences.cs" />
<Compile Include="CSharp\Resolver\FindReferenceSearchScope.cs" />
<Compile Include="CSharp\Resolver\Log.cs" /> <Compile Include="CSharp\Resolver\Log.cs" />
<Compile Include="CSharp\Resolver\OperatorResolveResult.cs" /> <Compile Include="CSharp\Resolver\OperatorResolveResult.cs" />
<Compile Include="CSharp\Resolver\ConversionResolveResult.cs" /> <Compile Include="CSharp\Resolver\ConversionResolveResult.cs" />

2
ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs

@ -38,7 +38,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
public class SimpleProjectContent : AbstractAnnotatable, IProjectContent, ISerializable, IDeserializationCallback public class SimpleProjectContent : AbstractAnnotatable, IProjectContent, ISerializable, IDeserializationCallback
{ {
readonly TypeStorage types = new TypeStorage(); readonly TypeStorage types = new TypeStorage();
readonly ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim(); readonly ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
readonly Dictionary<string, IParsedFile> fileDict = new Dictionary<string, IParsedFile>(Platform.FileNameComparer); readonly Dictionary<string, IParsedFile> fileDict = new Dictionary<string, IParsedFile>(Platform.FileNameComparer);
#region Constructor #region Constructor

Loading…
Cancel
Save