Browse Source

Implement EnvDTE FileCodeModel2.AddImport method.

pull/28/head
Matt Ward 13 years ago
parent
commit
0c05940258
  1. 2
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. 27
      src/AddIns/Misc/PackageManagement/Project/Src/DocumentNamespaceCreator.cs
  3. 20
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/FileCodeModel2.cs
  4. 11
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/FileCodeModelCodeElements.cs
  5. 13
      src/AddIns/Misc/PackageManagement/Project/Src/IDocumentNamespaceCreator.cs
  6. 18
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/FileCodeModel2Tests.cs

2
src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj

@ -79,6 +79,7 @@ @@ -79,6 +79,7 @@
<Compile Include="Src\Design\FakePackageOperation.cs" />
<Compile Include="Src\Design\FakeSelectedProject.cs" />
<Compile Include="Src\DocumentLoader.cs" />
<Compile Include="Src\DocumentNamespaceCreator.cs" />
<Compile Include="Src\DomRegionExtensions.cs" />
<Compile Include="Src\EnvDTE\CodeAttribute.cs" />
<Compile Include="Src\EnvDTE\CodeAttribute2.cs" />
@ -133,6 +134,7 @@ @@ -133,6 +134,7 @@
<Compile Include="Src\EnvDTE\vsCMInfoLocation.cs" />
<Compile Include="Src\EnvDTE\vsCMPropertyKind.cs" />
<Compile Include="Src\EnvDTE\vsEPReplaceTextOptions.cs" />
<Compile Include="Src\IDocumentNamespaceCreator.cs" />
<Compile Include="Src\IProjectBrowserUpdater.cs" />
<Compile Include="Src\IRefactoringDocumentView.cs" />
<Compile Include="Src\IFieldExtensions.cs" />

27
src/AddIns/Misc/PackageManagement/Project/Src/DocumentNamespaceCreator.cs

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// 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 ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Refactoring;
namespace ICSharpCode.PackageManagement
{
public class DocumentNamespaceCreator : IDocumentNamespaceCreator
{
public void AddNamespace(ICompilationUnit compilationUnit, string newNamespace)
{
if (WorkbenchSingleton.InvokeRequired) {
WorkbenchSingleton.SafeThreadCall(() => AddNamespace(compilationUnit, newNamespace));
} else {
IViewContent view = FileService.OpenFile(compilationUnit.FileName);
var textEditor = view as ITextEditorProvider;
IDocument document = textEditor.TextEditor.Document;
NamespaceRefactoringService.AddUsingDeclaration(compilationUnit, document, newNamespace, false);
}
}
}
}

20
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/FileCodeModel2.cs

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.PackageManagement.EnvDTE
@ -10,20 +11,35 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -10,20 +11,35 @@ namespace ICSharpCode.PackageManagement.EnvDTE
{
Project project;
FileProjectItem projectItem;
IDocumentNamespaceCreator namespaceCreator;
public FileCodeModel2(Project project, FileProjectItem projectItem)
: this(project, projectItem, new DocumentNamespaceCreator())
{
}
public FileCodeModel2(
Project project,
FileProjectItem projectItem,
IDocumentNamespaceCreator namespaceCreator)
{
this.project = project;
this.projectItem = projectItem;
this.namespaceCreator = namespaceCreator;
}
public CodeElements CodeElements {
get { return new FileCodeModelCodeElements(project, projectItem); }
get { return new FileCodeModelCodeElements(GetCompilationUnit()); }
}
public void AddImport(string name, object position = null, string alias = null)
{
namespaceCreator.AddNamespace(GetCompilationUnit(), name);
}
ICompilationUnit GetCompilationUnit()
{
return project.GetCompilationUnit(projectItem.FileName);
}
}
}

11
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/FileCodeModelCodeElements.cs

@ -10,13 +10,11 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -10,13 +10,11 @@ namespace ICSharpCode.PackageManagement.EnvDTE
{
public class FileCodeModelCodeElements : CodeElementsList
{
Project project;
FileProjectItem projectItem;
ICompilationUnit compilationUnit;
public FileCodeModelCodeElements(Project project, FileProjectItem projectItem)
public FileCodeModelCodeElements(ICompilationUnit compilationUnit)
{
this.project = project;
this.projectItem = projectItem;
this.compilationUnit = compilationUnit;
GetCodeElements();
}
@ -29,8 +27,7 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -29,8 +27,7 @@ namespace ICSharpCode.PackageManagement.EnvDTE
IList<IUsing> GetNamespaceImports()
{
return project
.GetCompilationUnit(projectItem.FileName)
return compilationUnit
.UsingScope
.Usings;
}

13
src/AddIns/Misc/PackageManagement/Project/Src/IDocumentNamespaceCreator.cs

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
// 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 ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.PackageManagement
{
public interface IDocumentNamespaceCreator
{
void AddNamespace(ICompilationUnit compilationUnit, string newNamespace);
}
}

18
src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/FileCodeModel2Tests.cs

@ -3,10 +3,12 @@ @@ -3,10 +3,12 @@
using System;
using ICSharpCode.EasyCodeDom;
using ICSharpCode.PackageManagement;
using ICSharpCode.PackageManagement.EnvDTE;
using ICSharpCode.SharpDevelop.Project;
using NUnit.Framework;
using PackageManagement.Tests.Helpers;
using Rhino.Mocks;
namespace PackageManagement.Tests.EnvDTE
{
@ -19,6 +21,7 @@ namespace PackageManagement.Tests.EnvDTE @@ -19,6 +21,7 @@ namespace PackageManagement.Tests.EnvDTE
FakeFileService fakeFileService;
FileProjectItem fileProjectItem;
CompilationUnitHelper compilationUnitHelper;
IDocumentNamespaceCreator namespaceCreator;
void CreateProjectWithOneFile()
{
@ -44,7 +47,8 @@ namespace PackageManagement.Tests.EnvDTE @@ -44,7 +47,8 @@ namespace PackageManagement.Tests.EnvDTE
void CreateFileCodeModel()
{
fileCodeModel = new FileCodeModel2(project, fileProjectItem);
namespaceCreator = MockRepository.GenerateStub<IDocumentNamespaceCreator>();
fileCodeModel = new FileCodeModel2(project, fileProjectItem, namespaceCreator);
}
CodeImport GetFirstCodeImportFromCodeElements()
@ -100,5 +104,17 @@ namespace PackageManagement.Tests.EnvDTE @@ -100,5 +104,17 @@ namespace PackageManagement.Tests.EnvDTE
Assert.AreEqual("Test.CodeModel", import.Namespace);
}
[Test]
public void AddImport_AddSystemXmlNamespace_NamespaceAndcompilationUnitPassedToNamespaceCreator()
{
CreateProjectWithOneFile();
CreateCompilationUnitForFileProjectItem();
CreateFileCodeModel();
fileCodeModel.AddImport("System.Xml");
namespaceCreator.AssertWasCalled(creator => creator.AddNamespace(compilationUnitHelper.CompilationUnit, "System.Xml"));
}
}
}

Loading…
Cancel
Save