Browse Source

Refactor EnvDTE.EditPoint and add tests.

pull/375/head
Matt Ward 12 years ago
parent
commit
23cd5137af
  1. 6
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. 4
      src/AddIns/Misc/PackageManagement/Project/Src/DocumentLoader.cs
  3. 8
      src/AddIns/Misc/PackageManagement/Project/Src/DocumentView.cs
  4. 37
      src/AddIns/Misc/PackageManagement/Project/Src/DomRegionExtensions.cs
  5. 6
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/EditPoint.cs
  6. 1
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/TextPoint.cs
  7. 2
      src/AddIns/Misc/PackageManagement/Project/Src/IDocumentLoader.cs
  8. 4
      src/AddIns/Misc/PackageManagement/Project/Src/IDocumentView.cs
  9. 62
      src/AddIns/Misc/PackageManagement/Project/Src/IMethodOrPropertyExtensions.cs
  10. 329
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/EditPointTests.cs

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

@ -89,7 +89,7 @@ @@ -89,7 +89,7 @@
<Compile Include="Src\Design\FakeProjectBuilder.cs" />
<Compile Include="Src\Design\FakeSelectedProject.cs" />
<Compile Include="Src\DocumentLoader.cs" />
<Compile Include="Src\DomRegionExtensions.cs" />
<Compile Include="Src\DocumentView.cs" />
<Compile Include="Src\EnvDTE\CodeAttribute.cs" />
<Compile Include="Src\EnvDTE\CodeAttribute2.cs" />
<Compile Include="Src\EnvDTE\CodeAttributeArgument.cs" />
@ -151,6 +151,7 @@ @@ -151,6 +151,7 @@
</Compile>
<Compile Include="Src\FileConflictViewModel.cs" />
<Compile Include="Src\ICodeGenerator.cs" />
<Compile Include="Src\IDocumentView.cs" />
<Compile Include="Src\IPackageRepositoryFactoryEvents.cs" />
<Compile Include="Src\IPackageViewModelParent.cs" />
<Compile Include="Src\IUpdatePackagesAction.cs" />
@ -161,10 +162,8 @@ @@ -161,10 +162,8 @@
<Compile Include="Src\IUpdatePackageSettings.cs" />
<Compile Include="Src\IVirtualMethodUpdater.cs" />
<Compile Include="Src\IProjectBrowserUpdater.cs" />
<Compile Include="Src\IRefactoringDocumentView.cs" />
<Compile Include="Src\IFieldExtensions.cs" />
<Compile Include="Src\IDocumentLoader.cs" />
<Compile Include="Src\IMethodOrPropertyExtensions.cs" />
<Compile Include="Src\InstalledPackageViewModel.cs" />
<Compile Include="Src\InstalledPackageViewModelFactory.cs" />
<Compile Include="Src\IPackageManagementSelectedProject.cs" />
@ -292,7 +291,6 @@ @@ -292,7 +291,6 @@
<Compile Include="Src\ProjectBrowserUpdater.cs" />
<Compile Include="Src\ProjectTemplatePackageRepositoryCache.cs" />
<Compile Include="Src\ProjectTemplatePackagesSettingsFileName.cs" />
<Compile Include="Src\RefactoringDocumentView.cs" />
<Compile Include="Src\RegisteredPackageRepositories.cs" />
<Compile Include="Src\ManagePackagesView.cs">
<DependentUpon>ManagePackagesView.xaml</DependentUpon>

4
src/AddIns/Misc/PackageManagement/Project/Src/DocumentLoader.cs

@ -23,10 +23,10 @@ namespace ICSharpCode.PackageManagement @@ -23,10 +23,10 @@ namespace ICSharpCode.PackageManagement
{
public class DocumentLoader : IDocumentLoader
{
public IRefactoringDocumentView LoadRefactoringDocumentView(string fileName)
public IDocumentView LoadDocumentView(string fileName)
{
return SD.MainThread.InvokeIfRequired(() => {
return new RefactoringDocumentView(fileName);
return new DocumentView(fileName);
});
}
}

8
src/AddIns/Misc/PackageManagement/Project/Src/RefactoringDocumentView.cs → src/AddIns/Misc/PackageManagement/Project/Src/DocumentView.cs

@ -23,14 +23,14 @@ using ICSharpCode.SharpDevelop.Editor; @@ -23,14 +23,14 @@ using ICSharpCode.SharpDevelop.Editor;
namespace ICSharpCode.PackageManagement
{
public class RefactoringDocumentView : IRefactoringDocumentView
public class DocumentView : IDocumentView
{
public RefactoringDocumentView(string fileName)
public DocumentView(string fileName)
{
View = FileService.OpenFile(fileName);
TextEditor = GetTextEditor();
FormattingStrategy = TextEditor.Language.FormattingStrategy;
RefactoringDocument = LoadDocument();
Document = LoadDocument();
}
IViewContent View { get; set; }
@ -42,7 +42,7 @@ namespace ICSharpCode.PackageManagement @@ -42,7 +42,7 @@ namespace ICSharpCode.PackageManagement
return View.GetService<ITextEditor>();
}
public IDocument RefactoringDocument { get; private set; }
public IDocument Document { get; private set; }
IDocument LoadDocument()
{

37
src/AddIns/Misc/PackageManagement/Project/Src/DomRegionExtensions.cs

@ -1,37 +0,0 @@ @@ -1,37 +0,0 @@
// Copyright (c) 2014 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;
//using ICSharpCode.SharpDevelop.Dom;
//
//namespace ICSharpCode.PackageManagement
//{
// public static class DomRegionExtensions
// {
// public static FilePosition ToStartPosition(this DomRegion region, ICompilationUnit compilationUnit)
// {
// return new FilePosition(compilationUnit, region.BeginLine, region.BeginColumn);
// }
//
// public static FilePosition ToEndPosition(this DomRegion region, ICompilationUnit compilationUnit)
// {
// return new FilePosition(compilationUnit, region.EndLine, region.EndColumn);
// }
// }
//}

6
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/EditPoint.cs

@ -26,7 +26,7 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -26,7 +26,7 @@ namespace ICSharpCode.PackageManagement.EnvDTE
public class EditPoint : TextPoint, global::EnvDTE.EditPoint
{
IDocument document;
IRefactoringDocumentView documentView;
IDocumentView documentView;
internal EditPoint(string fileName, TextLocation location, IDocumentLoader documentLoader)
: base(fileName, location, documentLoader)
@ -49,8 +49,8 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -49,8 +49,8 @@ namespace ICSharpCode.PackageManagement.EnvDTE
void OpenDocument()
{
documentView = documentLoader.LoadRefactoringDocumentView(fileName);
document = documentView.RefactoringDocument;
documentView = documentLoader.LoadDocumentView(fileName);
document = documentView.Document;
}
int GetStartOffset()

1
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/TextPoint.cs

@ -19,7 +19,6 @@ @@ -19,7 +19,6 @@
using System;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.PackageManagement.EnvDTE
{

2
src/AddIns/Misc/PackageManagement/Project/Src/IDocumentLoader.cs

@ -22,6 +22,6 @@ namespace ICSharpCode.PackageManagement @@ -22,6 +22,6 @@ namespace ICSharpCode.PackageManagement
{
public interface IDocumentLoader
{
IRefactoringDocumentView LoadRefactoringDocumentView(string fileName);
IDocumentView LoadDocumentView(string fileName);
}
}

4
src/AddIns/Misc/PackageManagement/Project/Src/IRefactoringDocumentView.cs → src/AddIns/Misc/PackageManagement/Project/Src/IDocumentView.cs

@ -21,9 +21,9 @@ using ICSharpCode.NRefactory.Editor; @@ -21,9 +21,9 @@ using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.PackageManagement
{
public interface IRefactoringDocumentView
public interface IDocumentView
{
IDocument RefactoringDocument { get; }
IDocument Document { get; }
void IndentLines(int beginLine, int endLine);
}
}

62
src/AddIns/Misc/PackageManagement/Project/Src/IMethodOrPropertyExtensions.cs

@ -1,62 +0,0 @@ @@ -1,62 +0,0 @@
// Copyright (c) 2014 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.SharpDevelop.Dom;
//
//namespace ICSharpCode.PackageManagement
//{
// public static class IMethodOrPropertyExtensions
// {
// public static FilePosition GetStartPosition(this IMethodOrProperty method)
// {
// return method.Region.ToStartPosition(method.CompilationUnit);
// }
//
// public static FilePosition GetEndPosition(this IMethodOrProperty method)
// {
// if (method.DeclaringTypeIsInterface()) {
// return method.Region.ToEndPosition(method.CompilationUnit);
// }
// return method.BodyRegion.ToEndPosition(method.CompilationUnit);
// }
//
// public static bool DeclaringTypeIsInterface(this IMethodOrProperty method)
// {
// return method.DeclaringType.ClassType == ClassType.Interface;
// }
//
// public static bool IsConstructor(this IMethodOrProperty methodOrProperty)
// {
// var method = methodOrProperty as IMethod;
// if (method != null) {
// return method.IsConstructor;
// }
// return false;
// }
//
// public static bool HasTypeParameters(this IMethodOrProperty methodOrProperty)
// {
// var method = methodOrProperty as IMethod;
// if ((method != null) && (method.TypeParameters != null)) {
// return method.TypeParameters.Count > 0;
// }
// return false;
// }
// }
//}

329
src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/EditPointTests.cs

@ -16,166 +16,169 @@ @@ -16,166 +16,169 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//using System;
//using ICSharpCode.PackageManagement;
//using ICSharpCode.PackageManagement.EnvDTE;
//using ICSharpCode.SharpDevelop.Dom;
//using ICSharpCode.SharpDevelop.Dom.Refactoring;
//using NUnit.Framework;
//using PackageManagement.Tests.Helpers;
//using Rhino.Mocks;
//
//namespace PackageManagement.Tests.EnvDTE
//{
// [TestFixture]
// public class EditPointTests
// {
// FieldHelper fieldHelper;
// MethodHelper methodHelper;
// TextPoint endPoint;
// EditPoint editPoint;
// IRefactoringDocument document;
// IRefactoringDocumentView documentView;
// IDocumentLoader documentLoader;
//
// [SetUp]
// public void Init()
// {
// fieldHelper = new FieldHelper();
// methodHelper = new MethodHelper();
// CreateDocumentLoader();
// }
//
// void CreateDocumentLoader()
// {
// document = MockRepository.GenerateStub<IRefactoringDocument>();
// documentView = MockRepository.GenerateStub<IRefactoringDocumentView>();
// documentView.Stub(view => view.RefactoringDocument).Return(document);
// documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
// }
//
// void CreateField(string fileName, DomRegion region)
// {
// fieldHelper.CreateField("Class1.MyField");
// fieldHelper.SetRegion(region);
// fieldHelper.SetCompilationUnitFileName(fileName);
// }
//
// void CreateMethod(string fileName, DomRegion region, DomRegion bodyRegion)
// {
// methodHelper.CreateMethod("Class1.MyMethod");
// methodHelper.SetRegion(region);
// methodHelper.SetBodyRegion(bodyRegion);
// methodHelper.SetCompilationUnitFileName(fileName);
// }
//
// void DocumentOffsetToReturn(int line, int column, int offset)
// {
// document.Stub(d => d.PositionToOffset(line, column)).Return(offset);
// }
//
// void CreateFieldEditPoint()
// {
// var codeVariable = new CodeVariable(fieldHelper.Field, documentLoader);
// TextPoint startPoint = (TextPoint)codeVariable.GetStartPoint();
// endPoint = (TextPoint)codeVariable.GetEndPoint();
// editPoint = (EditPoint)startPoint.CreateEditPoint();
// }
//
// void CreateMethodEditPoint()
// {
// var codeFunction = new CodeFunction(methodHelper.Method, documentLoader, null);
// TextPoint startPoint = (TextPoint)codeFunction.GetStartPoint();
// endPoint = (TextPoint)codeFunction.GetEndPoint();
// editPoint = (EditPoint)startPoint.CreateEditPoint();
// }
//
// void ReplaceText(string text)
// {
// editPoint.ReplaceText(endPoint, text, (int)global::EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
// }
//
// void DocumentFileName(string fileName)
// {
// documentLoader.Stub(loader => loader.LoadRefactoringDocumentView(fileName)).Return(documentView);
// }
//
// void AssertDocumentViewIndentLinesWasNotCalled()
// {
// documentView.AssertWasNotCalled(view => view.IndentLines(Arg<int>.Is.Anything, Arg<int>.Is.Anything));
// }
//
// [Test]
// public void ReplaceText_EditPointCreatedFromFieldStartPoint_ReplacesTextBetweenStartAndEndPoint()
// {
// string fileName = @"d:\projects\test.cs";
// var fieldRegion = new DomRegion(1, 5, 3, 12);
// CreateField(fileName, fieldRegion);
// DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
// DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
// DocumentFileName(fileName);
// CreateFieldEditPoint();
//
// ReplaceText("Test");
//
// document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
// }
//
// [Test]
// public void ReplaceText_EditPointCreatedFromMethodStartPoint_ReplacesTextBetweenStartAndEndPoint()
// {
// string fileName = @"d:\projects\test.cs";
// var methodRegion = new DomRegion(1, 5, 1, 10);
// var methodBodyRegion = new DomRegion(1, 10, 3, 12);
// CreateMethod(fileName, methodRegion, methodBodyRegion);
// methodHelper.AddDeclaringType("MyClass");
// DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
// DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
// DocumentFileName(fileName);
// CreateMethodEditPoint();
//
// ReplaceText("Test");
//
// document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
// }
//
// [Test]
// public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsFourLines_IndentsLinesTwoThreeFourFiveAndSix()
// {
// string fileName = @"d:\projects\test.cs";
// var fieldRegion = new DomRegion(1, 5, 1, 10);
// CreateField(fileName, fieldRegion);
// DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
// DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
// DocumentFileName(fileName);
// CreateFieldEditPoint();
//
// string replacementText =
// "First\r\n" +
// "Second\r\n" +
// "Third\r\n" +
// "Fourth\r\n" +
// "Five";
//
// ReplaceText(replacementText);
//
// documentView.AssertWasCalled(view => view.IndentLines(2, 6));
// }
//
// [Test]
// public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsSingleLine_TextIsNotIndented()
// {
// string fileName = @"d:\projects\test.cs";
// var fieldRegion = new DomRegion(1, 5, 1, 10);
// CreateField(fileName, fieldRegion);
// DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
// DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
// DocumentFileName(fileName);
// CreateFieldEditPoint();
//
// ReplaceText("Test");
//
// AssertDocumentViewIndentLinesWasNotCalled();
// }
// }
//}
using System;
using System.Linq;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.PackageManagement;
using ICSharpCode.PackageManagement.EnvDTE;
using ICSharpCode.SharpDevelop;
using NUnit.Framework;
using Rhino.Mocks;
namespace PackageManagement.Tests.EnvDTE
{
[TestFixture]
public class EditPointTests : CodeModelTestBase
{
CodeVariable codeVariable;
IField field;
CodeFunction2 codeFunction;
TextPoint endPoint;
EditPoint editPoint;
IDocument document;
IDocumentView documentView;
IDocumentLoader documentLoader;
[SetUp]
public void Init()
{
CreateDocumentLoader();
}
void CreateDocumentLoader()
{
document = MockRepository.GenerateStub<IDocument>();
documentView = MockRepository.GenerateStub<IDocumentView>();
documentView.Stub(view => view.Document).Return(document);
documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
}
void CreateField(DomRegion region)
{
AddCodeFile("class.cs", "class c {}");
codeModelContext.DocumentLoader = documentLoader;
field = MockRepository.GenerateStub<IField>();
field.Stub(f => f.Region).Return(region);
codeVariable = new CodeVariable(codeModelContext, field);
}
void CreateMethod(DomRegion region)
{
AddCodeFile("class.cs", "class c {}");
codeModelContext.DocumentLoader = documentLoader;
IMethod method = MockRepository.GenerateStub<IMethod>();
method.Stub(m => m.Region).Return(region);
codeFunction = new CodeFunction2(codeModelContext, method);
}
void DocumentOffsetToReturn(int line, int column, int offset)
{
document.Stub(d => d.PositionToOffset(line, column)).Return(offset);
}
void CreateFieldEditPoint()
{
var startPoint = (TextPoint)codeVariable.GetStartPoint();
endPoint = (TextPoint)codeVariable.GetEndPoint();
editPoint = (EditPoint)startPoint.CreateEditPoint();
}
void CreateMethodEditPoint()
{
var startPoint = (TextPoint)codeFunction.GetStartPoint();
endPoint = (TextPoint)codeFunction.GetEndPoint();
editPoint = (EditPoint)startPoint.CreateEditPoint();
}
void ReplaceText(string text)
{
editPoint.ReplaceText(endPoint, text, (int)global::EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
}
void DocumentFileName(string fileName)
{
documentLoader.Stub(loader => loader.LoadDocumentView(fileName)).Return(documentView);
}
void AssertDocumentViewIndentLinesWasNotCalled()
{
documentView.AssertWasNotCalled(view => view.IndentLines(Arg<int>.Is.Anything, Arg<int>.Is.Anything));
}
[Test]
public void ReplaceText_EditPointCreatedFromFieldStartPoint_ReplacesTextBetweenStartAndEndPoint()
{
string fileName = @"d:\projects\test.cs";
var fieldRegion = new DomRegion(fileName, 1, 5, 3, 12);
CreateField(fieldRegion);
DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
DocumentFileName(fileName);
CreateFieldEditPoint();
ReplaceText("Test");
document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
}
[Test]
public void ReplaceText_EditPointCreatedFromMethodStartPoint_ReplacesTextBetweenStartAndEndPoint()
{
string fileName = @"d:\projects\test.cs";
var methodRegion = new DomRegion(fileName, 1, 5, 3, 12);
CreateMethod(methodRegion);
DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
DocumentFileName(fileName);
CreateMethodEditPoint();
ReplaceText("Test");
document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
}
[Test]
public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsFourLines_IndentsLinesTwoThreeFourFiveAndSix()
{
string fileName = @"d:\projects\test.cs";
var fieldRegion = new DomRegion(fileName, 1, 5, 1, 10);
CreateField(fieldRegion);
DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
DocumentFileName(fileName);
CreateFieldEditPoint();
string replacementText =
"First\r\n" +
"Second\r\n" +
"Third\r\n" +
"Fourth\r\n" +
"Five";
ReplaceText(replacementText);
documentView.AssertWasCalled(view => view.IndentLines(2, 6));
}
[Test]
public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsSingleLine_TextIsNotIndented()
{
string fileName = @"d:\projects\test.cs";
var fieldRegion = new DomRegion(fileName, 1, 5, 1, 10);
CreateField(fieldRegion);
DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
DocumentFileName(fileName);
CreateFieldEditPoint();
ReplaceText("Test");
AssertDocumentViewIndentLinesWasNotCalled();
}
}
}

Loading…
Cancel
Save