Browse Source

Add "IUsingScope" to allow representing the nesting of usings in namespaces in SharpDevelop.Dom.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3675 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 17 years ago
parent
commit
ba290b6bf9
  1. 8
      src/AddIns/BackendBindings/Boo/BooBinding/Project/Src/CodeCompletion/ConvertVisitor.cs
  2. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonAstWalker.cs
  3. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonResolver.cs
  4. 4
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/ParseImportTestFixture.cs
  5. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Resolver/NamespaceContentsAddedToCtrlSpaceTestFixture.cs
  6. 11
      src/AddIns/Misc/CodeAnalysis/Src/SuppressMessageCommand.cs
  7. 6
      src/AddIns/Misc/UnitTesting/Test/Utils/MockClass.cs
  8. 8
      src/Main/Base/Project/Src/Services/RefactoringService/NamespaceRefactoringService.cs
  9. 8
      src/Main/Base/Test/MemberLookupHelperTests.cs
  10. 6
      src/Main/Base/Test/SearchClassTests.cs
  11. 8
      src/Main/Base/Test/SearchGenericClassTests.cs
  12. 6
      src/Main/Base/Test/Utils/MockClass.cs
  13. 2
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj
  14. 2
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/CtrlSpaceResolveHelper.cs
  15. 29
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ExtensionMethods.cs
  16. 11
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/AbstractEntity.cs
  17. 25
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/DefaultClass.cs
  18. 14
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/DefaultCompilationUnit.cs
  19. 70
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/DefaultUsingScope.cs
  20. 7
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/IClass.cs
  21. 6
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/ICompilationUnit.cs
  22. 46
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/IUsingScope.cs
  23. 53
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/NRefactoryASTConvertVisitor.cs
  24. 25
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/NRefactoryResolver.cs
  25. 6
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ProjectContent/DefaultProjectContent.cs
  26. 6
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/NRefactoryRefactoringProvider.cs

8
src/AddIns/BackendBindings/Boo/BooBinding/Project/Src/CodeCompletion/ConvertVisitor.cs

@ -137,7 +137,7 @@ namespace Grunwald.BooBinding.CodeCompletion
u.Usings.Add(p.Namespace); u.Usings.Add(p.Namespace);
else else
u.AddAlias(p.Alias.Name, new GetClassReturnType(_cu.ProjectContent, p.Namespace, 0)); u.AddAlias(p.Alias.Name, new GetClassReturnType(_cu.ProjectContent, p.Namespace, 0));
_cu.Usings.Add(u); _cu.UsingScope.Usings.Add(u);
} }
private IClass OuterClass { private IClass OuterClass {
@ -504,6 +504,12 @@ namespace Grunwald.BooBinding.CodeCompletion
OuterClass.Properties.Add(property); OuterClass.Properties.Add(property);
property.UserData = node; property.UserData = node;
} }
public override void OnNamespaceDeclaration(Boo.Lang.Compiler.Ast.NamespaceDeclaration node)
{
_cu.UsingScope = new DefaultUsingScope { NamespaceName = node.Name };
base.OnNamespaceDeclaration(node);
}
} }
} }

2
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonAstWalker.cs

@ -108,7 +108,7 @@ namespace ICSharpCode.PythonBinding
Console.WriteLine("Name: " + name.MakeString()); Console.WriteLine("Name: " + name.MakeString());
newUsing.Usings.Add(name.MakeString()); newUsing.Usings.Add(name.MakeString());
} }
compilationUnit.Usings.Add(newUsing); compilationUnit.UsingScope.Usings.Add(newUsing);
return false; return false;
} }

2
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonResolver.cs

@ -287,7 +287,7 @@ namespace ICSharpCode.PythonBinding
if (typeName != null) { if (typeName != null) {
IClass resolvedClass = GetClass(typeName); IClass resolvedClass = GetClass(typeName);
if (resolvedClass != null) { if (resolvedClass != null) {
DefaultClass dummyClass = new DefaultClass(null, "Global"); DefaultClass dummyClass = new DefaultClass(DefaultCompilationUnit.DummyCompilationUnit, "Global");
DefaultMethod dummyMethod = new DefaultMethod(dummyClass, String.Empty); DefaultMethod dummyMethod = new DefaultMethod(dummyClass, String.Empty);
DefaultField.LocalVariableField field = new DefaultField.LocalVariableField(resolvedClass.DefaultReturnType, expression, DomRegion.Empty, dummyClass); DefaultField.LocalVariableField field = new DefaultField.LocalVariableField(resolvedClass.DefaultReturnType, expression, DomRegion.Empty, dummyClass);
return new LocalResolveResult(dummyMethod, field); return new LocalResolveResult(dummyMethod, field);

4
src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/ParseImportTestFixture.cs

@ -38,13 +38,13 @@ namespace PythonBinding.Tests.Parsing
[Test] [Test]
public void OneUsing() public void OneUsing()
{ {
Assert.AreEqual(1, compilationUnit.Usings.Count); Assert.AreEqual(1, compilationUnit.UsingScope.Usings.Count);
} }
[Test] [Test]
public void UsingSystem() public void UsingSystem()
{ {
Assert.AreEqual("System", compilationUnit.Usings[0].Usings[0]); Assert.AreEqual("System", compilationUnit.UsingScope.Usings[0].Usings[0]);
} }
} }
} }

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Resolver/NamespaceContentsAddedToCtrlSpaceTestFixture.cs

@ -44,7 +44,7 @@ namespace PythonBinding.Tests.Resolver
// Add usings. // Add usings.
DefaultUsing newUsing = new DefaultUsing(cu.ProjectContent); DefaultUsing newUsing = new DefaultUsing(cu.ProjectContent);
newUsing.Usings.Add("MyNamespace"); newUsing.Usings.Add("MyNamespace");
cu.Usings.Add(newUsing); cu.UsingScope.Usings.Add(newUsing);
results = resolver.CtrlSpace(0, "".Length, parseInfo, "", ExpressionContext.Default); results = resolver.CtrlSpace(0, "".Length, parseInfo, "", ExpressionContext.Default);
} }

11
src/AddIns/Misc/CodeAnalysis/Src/SuppressMessageCommand.cs

@ -141,11 +141,20 @@ namespace ICSharpCode.CodeAnalysis
{ {
if (CheckImports(cu.ProjectContent.DefaultImports)) if (CheckImports(cu.ProjectContent.DefaultImports))
return true; return true;
foreach (IUsing u in cu.Usings) { return CheckImports(cu.UsingScope);
}
static bool CheckImports(IUsingScope scope)
{
foreach (IUsing u in scope.Usings) {
if (CheckImports(u)) { if (CheckImports(u)) {
return true; return true;
} }
} }
foreach (IUsingScope childscope in scope.ChildScopes) {
if (CheckImports(childscope))
return true;
}
return false; return false;
} }

6
src/AddIns/Misc/UnitTesting/Test/Utils/MockClass.cs

@ -127,6 +127,12 @@ namespace UnitTesting.Tests.Utils
} }
} }
public IUsingScope UsingScope {
get {
throw new NotImplementedException();
}
}
public DomRegion BodyRegion { public DomRegion BodyRegion {
get { get {
throw new NotImplementedException(); throw new NotImplementedException();

8
src/Main/Base/Project/Src/Services/RefactoringService/NamespaceRefactoringService.cs

@ -46,7 +46,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring
if (info == null) return; if (info == null) return;
ICompilationUnit cu = info.MostRecentCompilationUnit; ICompilationUnit cu = info.MostRecentCompilationUnit;
List<IUsing> newUsings = new List<IUsing>(cu.Usings); List<IUsing> newUsings = new List<IUsing>(cu.UsingScope.Usings);
if (sort) { if (sort) {
newUsings.Sort(CompareUsings); newUsings.Sort(CompareUsings);
} }
@ -72,7 +72,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring
PutEmptyLineAfterLastSystemNamespace(newUsings); PutEmptyLineAfterLastSystemNamespace(newUsings);
} }
cu.ProjectContent.Language.CodeGenerator.ReplaceUsings(new TextEditorDocument(document), cu.Usings, newUsings); cu.ProjectContent.Language.CodeGenerator.ReplaceUsings(new TextEditorDocument(document), cu.UsingScope.Usings, newUsings);
} }
static void PutEmptyLineAfterLastSystemNamespace(List<IUsing> newUsings) static void PutEmptyLineAfterLastSystemNamespace(List<IUsing> newUsings)
@ -98,7 +98,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring
IUsing newUsingDecl = new DefaultUsing(cu.ProjectContent); IUsing newUsingDecl = new DefaultUsing(cu.ProjectContent);
newUsingDecl.Usings.Add(newNamespace); newUsingDecl.Usings.Add(newNamespace);
List<IUsing> newUsings = new List<IUsing>(cu.Usings); List<IUsing> newUsings = new List<IUsing>(cu.UsingScope.Usings);
if (sortExistingUsings) { if (sortExistingUsings) {
newUsings.Sort(CompareUsings); newUsings.Sort(CompareUsings);
} }
@ -118,7 +118,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring
if (sortExistingUsings) { if (sortExistingUsings) {
PutEmptyLineAfterLastSystemNamespace(newUsings); PutEmptyLineAfterLastSystemNamespace(newUsings);
} }
cu.ProjectContent.Language.CodeGenerator.ReplaceUsings(new TextEditorDocument(document), cu.Usings, newUsings); cu.ProjectContent.Language.CodeGenerator.ReplaceUsings(new TextEditorDocument(document), cu.UsingScope.Usings, newUsings);
} }
} }
} }

8
src/Main/Base/Test/MemberLookupHelperTests.cs

@ -89,8 +89,8 @@ namespace ICSharpCode.SharpDevelop.Tests
DefaultProjectContent dpc = new DefaultProjectContent(); DefaultProjectContent dpc = new DefaultProjectContent();
dpc.ReferencedContents.Add(msc); dpc.ReferencedContents.Add(msc);
DefaultCompilationUnit cu = new DefaultCompilationUnit(dpc); DefaultCompilationUnit cu = new DefaultCompilationUnit(dpc);
cu.Usings.Add(new DefaultUsing(dpc, new DomRegion(1,1, 5,5))); cu.UsingScope.Usings.Add(new DefaultUsing(dpc, new DomRegion(1,1, 5,5)));
cu.Usings[0].Usings.Add("System.Collections.Generic"); cu.UsingScope.Usings[0].Usings.Add("System.Collections.Generic");
DefaultClass listDerivingClass = new DefaultClass(cu, "DerivesFromList"); DefaultClass listDerivingClass = new DefaultClass(cu, "DerivesFromList");
cu.Classes.Add(listDerivingClass); cu.Classes.Add(listDerivingClass);
@ -108,8 +108,8 @@ namespace ICSharpCode.SharpDevelop.Tests
DefaultProjectContent dpc = new DefaultProjectContent(); DefaultProjectContent dpc = new DefaultProjectContent();
dpc.ReferencedContents.Add(msc); dpc.ReferencedContents.Add(msc);
DefaultCompilationUnit cu = new DefaultCompilationUnit(dpc); DefaultCompilationUnit cu = new DefaultCompilationUnit(dpc);
cu.Usings.Add(new DefaultUsing(dpc, new DomRegion(1,1, 5,5))); cu.UsingScope.Usings.Add(new DefaultUsing(dpc, new DomRegion(1,1, 5,5)));
cu.Usings[0].Usings.Add("System.Collections.Generic"); cu.UsingScope.Usings[0].Usings.Add("System.Collections.Generic");
DefaultClass listDerivingClass = new DefaultClass(cu, "DerivesFromList"); DefaultClass listDerivingClass = new DefaultClass(cu, "DerivesFromList");
cu.Classes.Add(listDerivingClass); cu.Classes.Add(listDerivingClass);

6
src/Main/Base/Test/SearchClassTests.cs

@ -25,9 +25,9 @@ namespace ICSharpCode.SharpDevelop.Tests
pc.Language = language; pc.Language = language;
DefaultCompilationUnit cu = new DefaultCompilationUnit(pc); DefaultCompilationUnit cu = new DefaultCompilationUnit(pc);
if (language == LanguageProperties.VBNet) if (language == LanguageProperties.VBNet)
cu.Usings.Add(CreateUsing(pc, "syStEm")); cu.UsingScope.Usings.Add(CreateUsing(pc, "syStEm"));
else else
cu.Usings.Add(CreateUsing(pc, "System")); cu.UsingScope.Usings.Add(CreateUsing(pc, "System"));
return cu; return cu;
} }
@ -168,7 +168,7 @@ namespace ICSharpCode.SharpDevelop.Tests
ICompilationUnit cu = Prepare(LanguageProperties.CSharp); ICompilationUnit cu = Prepare(LanguageProperties.CSharp);
cu.ProjectContent.ReferencedContents.Add(ref1.ProjectContent); cu.ProjectContent.ReferencedContents.Add(ref1.ProjectContent);
cu.ProjectContent.ReferencedContents.Add(ref2.ProjectContent); cu.ProjectContent.ReferencedContents.Add(ref2.ProjectContent);
cu.Usings.Add(new DefaultUsing(cu.ProjectContent) { Usings = { "NS1", "NS2" } }); cu.UsingScope.Usings.Add(new DefaultUsing(cu.ProjectContent) { Usings = { "NS1", "NS2" } });
SearchTypeResult r = cu.ProjectContent.SearchType(new SearchTypeRequest("ClassName", 0, null, cu, 1, 1)); SearchTypeResult r = cu.ProjectContent.SearchType(new SearchTypeRequest("ClassName", 0, null, cu, 1, 1));
Assert.AreEqual(ModifierEnum.Public, r.Result.GetUnderlyingClass().Modifiers); Assert.AreEqual(ModifierEnum.Public, r.Result.GetUnderlyingClass().Modifiers);

8
src/Main/Base/Test/SearchGenericClassTests.cs

@ -29,7 +29,7 @@ namespace ICSharpCode.SharpDevelop.Tests
pc.Language = language; pc.Language = language;
DefaultCompilationUnit cu = new DefaultCompilationUnit(pc); DefaultCompilationUnit cu = new DefaultCompilationUnit(pc);
if (usingMode == 1) { if (usingMode == 1) {
cu.Usings.Add(CreateUsing(pc, "syStEm.coLLectIons")); cu.UsingScope.Usings.Add(CreateUsing(pc, "syStEm.coLLectIons"));
pc.DefaultImports = new DefaultUsing(pc); pc.DefaultImports = new DefaultUsing(pc);
pc.DefaultImports.Usings.Add("syStEm"); pc.DefaultImports.Usings.Add("syStEm");
pc.DefaultImports.Usings.Add("syStEm.coLLEctionS.GeNeRic"); pc.DefaultImports.Usings.Add("syStEm.coLLEctionS.GeNeRic");
@ -39,9 +39,9 @@ namespace ICSharpCode.SharpDevelop.Tests
pc.DefaultImports.Usings.Add("syStEm.coLLEctioNs"); pc.DefaultImports.Usings.Add("syStEm.coLLEctioNs");
pc.DefaultImports.Usings.Add("syStEm.coLLEctionS.GeNeRic"); pc.DefaultImports.Usings.Add("syStEm.coLLEctionS.GeNeRic");
} else { // usingMode == 0 } else { // usingMode == 0
cu.Usings.Add(CreateUsing(pc, "System")); cu.UsingScope.Usings.Add(CreateUsing(pc, "System"));
cu.Usings.Add(CreateUsing(pc, "System.Collections")); cu.UsingScope.Usings.Add(CreateUsing(pc, "System.Collections"));
cu.Usings.Add(CreateUsing(pc, "System.Collections.Generic")); cu.UsingScope.Usings.Add(CreateUsing(pc, "System.Collections.Generic"));
} }
return cu; return cu;
} }

6
src/Main/Base/Test/Utils/MockClass.cs

@ -61,6 +61,12 @@ namespace ICSharpCode.SharpDevelop.Tests.Utils
} }
} }
public IUsingScope UsingScope {
get {
throw new NotImplementedException();
}
}
public DomRegion Region { public DomRegion Region {
get { get {
throw new NotImplementedException(); throw new NotImplementedException();

2
src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj

@ -88,6 +88,7 @@
<Compile Include="Src\Implementations\DefaultReturnType.cs" /> <Compile Include="Src\Implementations\DefaultReturnType.cs" />
<Compile Include="Src\Implementations\DefaultTypeParameter.cs" /> <Compile Include="Src\Implementations\DefaultTypeParameter.cs" />
<Compile Include="Src\Implementations\DefaultUsing.cs" /> <Compile Include="Src\Implementations\DefaultUsing.cs" />
<Compile Include="Src\Implementations\DefaultUsingScope.cs" />
<Compile Include="Src\Implementations\ElementReturnType.cs" /> <Compile Include="Src\Implementations\ElementReturnType.cs" />
<Compile Include="Src\Implementations\GenericReturnType.cs" /> <Compile Include="Src\Implementations\GenericReturnType.cs" />
<Compile Include="Src\Implementations\GetClassReturnType.cs" /> <Compile Include="Src\Implementations\GetClassReturnType.cs" />
@ -102,6 +103,7 @@
<Compile Include="Src\Interfaces\ExplicitInterfaceImplementation.cs" /> <Compile Include="Src\Interfaces\ExplicitInterfaceImplementation.cs" />
<Compile Include="Src\Interfaces\IDomProgressMonitor.cs" /> <Compile Include="Src\Interfaces\IDomProgressMonitor.cs" />
<Compile Include="Src\Interfaces\IFreezable.cs" /> <Compile Include="Src\Interfaces\IFreezable.cs" />
<Compile Include="Src\Interfaces\IUsingScope.cs" />
<Compile Include="Src\LazyList.cs" /> <Compile Include="Src\LazyList.cs" />
<Compile Include="Src\NRefactoryResolver\CodeSnippetConverter.cs" /> <Compile Include="Src\NRefactoryResolver\CodeSnippetConverter.cs" />
<Compile Include="Src\NRefactoryResolver\CSharpToVBNetConvertVisitor.cs" /> <Compile Include="Src\NRefactoryResolver\CSharpToVBNetConvertVisitor.cs" />

2
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/CtrlSpaceResolveHelper.cs

@ -84,7 +84,7 @@ namespace ICSharpCode.SharpDevelop.Dom
{ {
IProjectContent projectContent = cu.ProjectContent; IProjectContent projectContent = cu.ProjectContent;
projectContent.AddNamespaceContents(result, "", projectContent.Language, true); projectContent.AddNamespaceContents(result, "", projectContent.Language, true);
foreach (IUsing u in cu.Usings) { foreach (IUsing u in cu.GetAllUsings()) {
AddUsing(result, u, projectContent); AddUsing(result, u, projectContent);
} }
AddUsing(result, projectContent.DefaultImports, projectContent); AddUsing(result, projectContent.DefaultImports, projectContent);

29
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ExtensionMethods.cs

@ -8,6 +8,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.SharpDevelop.Dom namespace ICSharpCode.SharpDevelop.Dom
{ {
@ -27,5 +28,33 @@ namespace ICSharpCode.SharpDevelop.Dom
foreach (T o in elements) foreach (T o in elements)
list.Add(o); list.Add(o);
} }
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> input, Func<T, IEnumerable<T>> recursion)
{
Stack<IEnumerator<T>> stack = new Stack<IEnumerator<T>>();
try {
stack.Push(input.GetEnumerator());
while (stack.Count > 0) {
while (stack.Peek().MoveNext()) {
T element = stack.Peek().Current;
yield return element;
IEnumerable<T> children = recursion(element);
if (children != null) {
stack.Push(children.GetEnumerator());
}
}
stack.Pop();
}
} finally {
while (stack.Count > 0) {
stack.Pop().Dispose();
}
}
}
public static IEnumerable<IUsing> GetAllUsings(this ICompilationUnit cu)
{
return (new[]{cu.UsingScope}).Flatten(s=>s.ChildScopes).SelectMany(s=>s.Usings);
}
} }
} }

11
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/AbstractEntity.cs

@ -18,9 +18,9 @@ namespace ICSharpCode.SharpDevelop.Dom
IClass declaringType; IClass declaringType;
string fullyQualifiedName = null; string fullyQualifiedName;
string name = null; string name;
string nspace = null; string nspace;
public AbstractEntity(IClass declaringType) public AbstractEntity(IClass declaringType)
{ {
@ -29,11 +29,10 @@ namespace ICSharpCode.SharpDevelop.Dom
public AbstractEntity(IClass declaringType, string name) public AbstractEntity(IClass declaringType, string name)
{ {
if (declaringType == null)
throw new ArgumentNullException("declaringType");
this.declaringType = declaringType; this.declaringType = declaringType;
this.name = name; this.name = name;
nspace = declaringType.FullyQualifiedName; if (declaringType != null)
nspace = declaringType.FullyQualifiedName;
// lazy-computing the fully qualified name for class members saves ~7 MB RAM (when loading the SharpDevelop solution). // lazy-computing the fully qualified name for class members saves ~7 MB RAM (when loading the SharpDevelop solution).
//fullyQualifiedName = nspace + '.' + name; //fullyQualifiedName = nspace + '.' + name;

25
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/DefaultClass.cs

@ -26,6 +26,7 @@ namespace ICSharpCode.SharpDevelop.Dom
IList<IMethod> methods; IList<IMethod> methods;
IList<IEvent> events; IList<IEvent> events;
IList<ITypeParameter> typeParameters; IList<ITypeParameter> typeParameters;
IUsingScope usingScope;
protected override void FreezeInternal() protected override void FreezeInternal()
{ {
@ -119,23 +120,47 @@ namespace ICSharpCode.SharpDevelop.Dom
} }
} }
/// <summary>
/// Gets the using scope of contains this class.
/// </summary>
public IUsingScope UsingScope {
get { return usingScope; }
set {
if (value == null)
throw new ArgumentNullException("UsingScope");
CheckBeforeMutation();
usingScope = value;
}
}
public DefaultClass(ICompilationUnit compilationUnit, string fullyQualifiedName) : base(null) public DefaultClass(ICompilationUnit compilationUnit, string fullyQualifiedName) : base(null)
{ {
if (compilationUnit == null)
throw new ArgumentNullException("compilationUnit");
if (fullyQualifiedName == null)
throw new ArgumentNullException("fullyQualifiedName");
this.compilationUnit = compilationUnit; this.compilationUnit = compilationUnit;
this.FullyQualifiedName = fullyQualifiedName; this.FullyQualifiedName = fullyQualifiedName;
this.UsingScope = compilationUnit.UsingScope;
} }
public DefaultClass(ICompilationUnit compilationUnit, IClass declaringType) : base(declaringType) public DefaultClass(ICompilationUnit compilationUnit, IClass declaringType) : base(declaringType)
{ {
if (compilationUnit == null)
throw new ArgumentNullException("compilationUnit");
this.compilationUnit = compilationUnit; this.compilationUnit = compilationUnit;
this.UsingScope = compilationUnit.UsingScope;
} }
public DefaultClass(ICompilationUnit compilationUnit, ClassType classType, ModifierEnum modifiers, DomRegion region, IClass declaringType) : base(declaringType) public DefaultClass(ICompilationUnit compilationUnit, ClassType classType, ModifierEnum modifiers, DomRegion region, IClass declaringType) : base(declaringType)
{ {
if (compilationUnit == null)
throw new ArgumentNullException("compilationUnit");
this.compilationUnit = compilationUnit; this.compilationUnit = compilationUnit;
this.region = region; this.region = region;
this.classType = classType; this.classType = classType;
Modifiers = modifiers; Modifiers = modifiers;
this.UsingScope = compilationUnit.UsingScope;
} }
// fields must be volatile to ensure that the optimizer doesn't reorder accesses to it // fields must be volatile to ensure that the optimizer doesn't reorder accesses to it

14
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/DefaultCompilationUnit.cs

@ -21,7 +21,7 @@ namespace ICSharpCode.SharpDevelop.Dom
return this; return this;
} }
IList<IUsing> usings = new List<IUsing>(); IUsingScope usingScope = new DefaultUsingScope();
IList<IClass> classes = new List<IClass>(); IList<IClass> classes = new List<IClass>();
IList<IAttribute> attributes = new List<IAttribute>(); IList<IAttribute> attributes = new List<IAttribute>();
IList<FoldingRegion> foldingRegions = new List<FoldingRegion>(); IList<FoldingRegion> foldingRegions = new List<FoldingRegion>();
@ -30,11 +30,11 @@ namespace ICSharpCode.SharpDevelop.Dom
protected override void FreezeInternal() protected override void FreezeInternal()
{ {
// Deep Freeze: freeze lists and their contents // Deep Freeze: freeze lists and their contents
usings = FreezeList(usings);
classes = FreezeList(classes); classes = FreezeList(classes);
attributes = FreezeList(attributes); attributes = FreezeList(attributes);
foldingRegions = FreezeList(foldingRegions); foldingRegions = FreezeList(foldingRegions);
tagComments = FreezeList(tagComments); tagComments = FreezeList(tagComments);
usingScope.Freeze();
base.FreezeInternal(); base.FreezeInternal();
} }
@ -85,9 +85,13 @@ namespace ICSharpCode.SharpDevelop.Dom
} }
} }
public virtual IList<IUsing> Usings { public virtual IUsingScope UsingScope {
get { get { return usingScope; }
return usings; set {
if (value == null)
throw new ArgumentNullException("UsingScope");
CheckBeforeMutation();
usingScope = value;
} }
} }

70
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Implementations/DefaultUsingScope.cs

@ -0,0 +1,70 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
namespace ICSharpCode.SharpDevelop.Dom
{
public class DefaultUsingScope : AbstractFreezable, IUsingScope
{
DomRegion region;
IUsingScope parent;
IList<IUsing> usings;
IList<IUsingScope> childScopes;
string namespaceName = "";
protected override void FreezeInternal()
{
base.FreezeInternal();
usings = FreezeList(usings);
childScopes = FreezeList(childScopes);
}
public DomRegion Region {
get { return region; }
set {
CheckBeforeMutation();
region = value;
}
}
public IUsingScope Parent {
get { return parent; }
set {
CheckBeforeMutation();
parent = value;
}
}
public virtual IList<IUsing> Usings {
get {
if (usings == null)
usings = new List<IUsing>();
return usings;
}
}
public virtual IList<IUsingScope> ChildScopes {
get {
if (childScopes == null)
childScopes = new List<IUsingScope>();
return childScopes;
}
}
public string NamespaceName {
get { return namespaceName; }
set {
if (value == null)
throw new ArgumentNullException("NamespaceName");
CheckBeforeMutation();
namespaceName = value;
}
}
}
}

7
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/IClass.cs

@ -43,6 +43,13 @@ namespace ICSharpCode.SharpDevelop.Dom
get; get;
} }
/// <summary>
/// Gets the using scope of contains this class.
/// </summary>
IUsingScope UsingScope {
get;
}
IList<IReturnType> BaseTypes { IList<IReturnType> BaseTypes {
get; get;
} }

6
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/ICompilationUnit.cs

@ -30,7 +30,11 @@ namespace ICSharpCode.SharpDevelop.Dom
get; get;
} }
IList<IUsing> Usings { /// <summary>
/// Gets the main using scope of the compilation unit.
/// That scope usually represents the root namespace.
/// </summary>
IUsingScope UsingScope {
get; get;
} }

46
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/IUsingScope.cs

@ -0,0 +1,46 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
namespace ICSharpCode.SharpDevelop.Dom
{
/// <summary>
/// A scope that can contain using declarations.
/// In C#, every file is a using scope, and every "namespace" declaration inside
/// the file is a nested using scope.
/// </summary>
public interface IUsingScope : IFreezable
{
/// <summary>
/// Gets the region of the using scope.
/// </summary>
DomRegion Region { get; }
/// <summary>
/// Gets the parent scope.
/// Returns null if this is a root scope.
/// </summary>
IUsingScope Parent { get; }
/// <summary>
/// Gets the usings in this using scope.
/// </summary>
IList<IUsing> Usings { get; }
/// <summary>
/// Gets the list of child scopes. Child scopes usually represent "namespace" declarations.
/// </summary>
IList<IUsingScope> ChildScopes { get; }
/// <summary>
/// Gets the name of the namespace represented by the using scope.
/// </summary>
string NamespaceName { get; }
}
}

53
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/NRefactoryASTConvertVisitor.cs

@ -19,8 +19,8 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
{ {
public class NRefactoryASTConvertVisitor : AbstractAstVisitor public class NRefactoryASTConvertVisitor : AbstractAstVisitor
{ {
ICompilationUnit cu; DefaultCompilationUnit cu;
Stack<string> currentNamespace = new Stack<string>(); DefaultUsingScope currentNamespace;
Stack<DefaultClass> currentClass = new Stack<DefaultClass>(); Stack<DefaultClass> currentClass = new Stack<DefaultClass>();
public string VBRootNamespace { get; set; } public string VBRootNamespace { get; set; }
@ -181,13 +181,12 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
if (compilationUnit == null) { if (compilationUnit == null) {
return null; return null;
} }
currentNamespace = new DefaultUsingScope();
if (!string.IsNullOrEmpty(VBRootNamespace)) { if (!string.IsNullOrEmpty(VBRootNamespace)) {
currentNamespace.Push(VBRootNamespace); currentNamespace.NamespaceName = VBRootNamespace;
compilationUnit.AcceptChildren(this, data);
currentNamespace.Pop();
} else {
compilationUnit.AcceptChildren(this, data);
} }
cu.UsingScope = currentNamespace;
compilationUnit.AcceptChildren(this, data);
return cu; return cu;
} }
@ -197,7 +196,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
foreach (AST.Using u in usingDeclaration.Usings) { foreach (AST.Using u in usingDeclaration.Usings) {
u.AcceptVisitor(this, us); u.AcceptVisitor(this, us);
} }
cu.Usings.Add(us); currentNamespace.Usings.Add(us);
return data; return data;
} }
@ -313,18 +312,24 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
return null; return null;
} }
string PrependCurrentNamespace(string name)
{
if (string.IsNullOrEmpty(currentNamespace.NamespaceName))
return name;
else
return currentNamespace.NamespaceName + "." + name;
}
public override object VisitNamespaceDeclaration(AST.NamespaceDeclaration namespaceDeclaration, object data) public override object VisitNamespaceDeclaration(AST.NamespaceDeclaration namespaceDeclaration, object data)
{ {
string name; DefaultUsingScope oldNamespace = currentNamespace;
if (currentNamespace.Count == 0) { currentNamespace = new DefaultUsingScope {
name = namespaceDeclaration.Name; Parent = oldNamespace,
} else { NamespaceName = PrependCurrentNamespace(namespaceDeclaration.Name),
name = currentNamespace.Peek() + '.' + namespaceDeclaration.Name; };
} oldNamespace.ChildScopes.Add(currentNamespace);
currentNamespace.Push(name);
object ret = namespaceDeclaration.AcceptChildren(this, data); object ret = namespaceDeclaration.AcceptChildren(this, data);
currentNamespace.Pop(); currentNamespace = oldNamespace;
return ret; return ret;
} }
@ -364,13 +369,10 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
cur.InnerClasses.Add(c); cur.InnerClasses.Add(c);
c.FullyQualifiedName = cur.FullyQualifiedName + '.' + typeDeclaration.Name; c.FullyQualifiedName = cur.FullyQualifiedName + '.' + typeDeclaration.Name;
} else { } else {
if (currentNamespace.Count == 0) { c.FullyQualifiedName = PrependCurrentNamespace(typeDeclaration.Name);
c.FullyQualifiedName = typeDeclaration.Name;
} else {
c.FullyQualifiedName = currentNamespace.Peek() + '.' + typeDeclaration.Name;
}
cu.Classes.Add(c); cu.Classes.Add(c);
} }
c.UsingScope = currentNamespace;
currentClass.Push(c); currentClass.Push(c);
ConvertTemplates(typeDeclaration.Templates, c); // resolve constrains in context of the class ConvertTemplates(typeDeclaration.Templates, c); // resolve constrains in context of the class
@ -468,13 +470,10 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
cur.InnerClasses.Add(c); cur.InnerClasses.Add(c);
c.FullyQualifiedName = cur.FullyQualifiedName + '.' + name; c.FullyQualifiedName = cur.FullyQualifiedName + '.' + name;
} else { } else {
if (currentNamespace.Count == 0) { c.FullyQualifiedName = PrependCurrentNamespace(name);
c.FullyQualifiedName = name;
} else {
c.FullyQualifiedName = currentNamespace.Peek() + '.' + name;
}
cu.Classes.Add(c); cu.Classes.Add(c);
} }
c.UsingScope = currentNamespace;
currentClass.Push(c); // necessary for CreateReturnType currentClass.Push(c); // necessary for CreateReturnType
ConvertTemplates(templates, c); ConvertTemplates(templates, c);

25
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/NRefactoryResolver.cs

@ -205,7 +205,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
} else if ("global".Equals(expression, StringComparison.InvariantCultureIgnoreCase)) { } else if ("global".Equals(expression, StringComparison.InvariantCultureIgnoreCase)) {
return new NamespaceResolveResult(null, null, ""); return new NamespaceResolveResult(null, null, "");
} }
// array // array
} else if (language == NR.SupportedLanguage.CSharp && expressionResult.Context.IsTypeContext && !expressionResult.Context.IsObjectCreation) { } else if (language == NR.SupportedLanguage.CSharp && expressionResult.Context.IsTypeContext && !expressionResult.Context.IsObjectCreation) {
expr = ParseTypeReference(expression); expr = ParseTypeReference(expression);
} }
@ -596,16 +596,19 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
} }
if (languageProperties.CanImportClasses) { if (languageProperties.CanImportClasses) {
foreach (IUsing @using in cu.Usings) { IUsingScope scope = callingClass != null ? callingClass.UsingScope : cu.UsingScope;
foreach (string import in @using.Usings) { for (; scope != null; scope = scope.Parent) {
IClass c = GetClass(import, 0); foreach (IUsing @using in scope.Usings) {
if (c != null) { foreach (string import in @using.Usings) {
ResolveResult rr = ResolveMember(c.DefaultReturnType, identifier, IClass c = GetClass(import, 0);
identifierExpression.TypeArguments, if (c != null) {
IsInvoked(identifierExpression), ResolveResult rr = ResolveMember(c.DefaultReturnType, identifier,
false, null); identifierExpression.TypeArguments,
if (rr != null && rr.IsValid) IsInvoked(identifierExpression),
return rr; false, null);
if (rr != null && rr.IsValid)
return rr;
}
} }
} }
} }

6
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ProjectContent/DefaultProjectContent.cs

@ -238,7 +238,7 @@ namespace ICSharpCode.SharpDevelop.Dom
/// </summary> /// </summary>
private class GenericClassContainer : DefaultClass private class GenericClassContainer : DefaultClass
{ {
public GenericClassContainer(string fullyQualifiedName) : base(null, fullyQualifiedName) {} public GenericClassContainer(string fullyQualifiedName) : base(DefaultCompilationUnit.DummyCompilationUnit, fullyQualifiedName) {}
IClass[] realClasses = new IClass[4]; IClass[] realClasses = new IClass[4];
@ -744,7 +744,7 @@ namespace ICSharpCode.SharpDevelop.Dom
return null; return null;
} }
foreach (IUsing u in unit.Usings) { foreach (IUsing u in unit.GetAllUsings()) {
if (u != null) { if (u != null) {
string nameSpace = u.SearchNamespace(name); string nameSpace = u.SearchNamespace(name);
if (nameSpace != null) { if (nameSpace != null) {
@ -854,7 +854,7 @@ namespace ICSharpCode.SharpDevelop.Dom
} }
if (request.CurrentCompilationUnit != null) { if (request.CurrentCompilationUnit != null) {
// Combine name with usings // Combine name with usings
foreach (IUsing u in request.CurrentCompilationUnit.Usings) { foreach (IUsing u in request.CurrentCompilationUnit.GetAllUsings()) {
if (u != null) { if (u != null) {
foreach (IReturnType r in u.SearchType(name, request.TypeParameterCount)) { foreach (IReturnType r in u.SearchType(name, request.TypeParameterCount)) {
if (MatchesRequest(ref request, r)) { if (MatchesRequest(ref request, r)) {

6
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/NRefactoryRefactoringProvider.cs

@ -8,6 +8,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using ICSharpCode.NRefactory.Ast; using ICSharpCode.NRefactory.Ast;
@ -510,7 +511,8 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring
if (tr.ExtensionMethod != null) { if (tr.ExtensionMethod != null) {
// the invocation of an extension method can implicitly use a using // the invocation of an extension method can implicitly use a using
StringComparer nameComparer = cu.ProjectContent.Language.NameComparer; StringComparer nameComparer = cu.ProjectContent.Language.NameComparer;
foreach (IUsing import in cu.Usings) { // go through all usings in all nested child scopes
foreach (IUsing import in cu.GetAllUsings()) {
foreach (string i in import.Usings) { foreach (string i in import.Usings) {
if (nameComparer.Equals(tr.ExtensionMethod.DeclaringType.Namespace, i)) { if (nameComparer.Equals(tr.ExtensionMethod.DeclaringType.Namespace, i)) {
usedUsings.Add(import); usedUsings.Add(import);
@ -528,7 +530,7 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring
} }
List<IUsing> unusedUsings = new List<IUsing>(); List<IUsing> unusedUsings = new List<IUsing>();
foreach (IUsing import in cu.Usings) { foreach (IUsing import in cu.GetAllUsings()) {
if (!usedUsings.Contains(import)) { if (!usedUsings.Contains(import)) {
if (import.HasAliases) { if (import.HasAliases) {
foreach (string key in import.Aliases.Keys) { foreach (string key in import.Aliases.Keys) {

Loading…
Cancel
Save