17 changed files with 567 additions and 15 deletions
@ -0,0 +1,23 @@ |
|||||||
|
// 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.Editor; |
||||||
|
using ICSharpCode.SharpDevelop.Gui; |
||||||
|
|
||||||
|
namespace ICSharpCode.PackageManagement |
||||||
|
{ |
||||||
|
public class DocumentLoader : IDocumentLoader |
||||||
|
{ |
||||||
|
public IDocument LoadDocument(string fileName) |
||||||
|
{ |
||||||
|
if (WorkbenchSingleton.InvokeRequired) { |
||||||
|
return WorkbenchSingleton.SafeThreadFunction(() => LoadDocument(fileName)); |
||||||
|
} else { |
||||||
|
var textEditorProvider = FileService.OpenFile(fileName) as ITextEditorProvider; |
||||||
|
return new ThreadSafeDocument(textEditorProvider.TextEditor.Document); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
// 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 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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -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.Editor; |
||||||
|
|
||||||
|
namespace ICSharpCode.PackageManagement |
||||||
|
{ |
||||||
|
public interface IDocumentLoader |
||||||
|
{ |
||||||
|
IDocument LoadDocument(string fileName); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
// 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 static class IFieldExtensions |
||||||
|
{ |
||||||
|
public static FilePosition GetStartPosition(this IField field) |
||||||
|
{ |
||||||
|
return field.Region.ToStartPosition(field.CompilationUnit); |
||||||
|
} |
||||||
|
|
||||||
|
public static FilePosition GetEndPosition(this IField field) |
||||||
|
{ |
||||||
|
return field.Region.ToEndPosition(field.CompilationUnit); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
// 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 static class IMethodOrPropertyExtensions |
||||||
|
{ |
||||||
|
public static FilePosition GetStartPosition(this IMethodOrProperty method) |
||||||
|
{ |
||||||
|
return method.Region.ToStartPosition(method.CompilationUnit); |
||||||
|
} |
||||||
|
|
||||||
|
public static FilePosition GetEndPosition(this IMethodOrProperty method) |
||||||
|
{ |
||||||
|
return method.BodyRegion.ToEndPosition(method.CompilationUnit); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,158 @@ |
|||||||
|
// 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.IO; |
||||||
|
using ICSharpCode.NRefactory; |
||||||
|
using ICSharpCode.SharpDevelop; |
||||||
|
using ICSharpCode.SharpDevelop.Editor; |
||||||
|
using ICSharpCode.SharpDevelop.Gui; |
||||||
|
|
||||||
|
namespace ICSharpCode.PackageManagement |
||||||
|
{ |
||||||
|
public class ThreadSafeDocument : IDocument |
||||||
|
{ |
||||||
|
IDocument document; |
||||||
|
|
||||||
|
public ThreadSafeDocument(IDocument document) |
||||||
|
{ |
||||||
|
this.document = document; |
||||||
|
} |
||||||
|
|
||||||
|
#pragma warning disable 0067
|
||||||
|
public event EventHandler<TextChangeEventArgs> Changing; |
||||||
|
public event EventHandler<TextChangeEventArgs> Changed; |
||||||
|
public event EventHandler TextChanged; |
||||||
|
#pragma warning restore 0067
|
||||||
|
|
||||||
|
public string Text { |
||||||
|
get { |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
set { |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public int TotalNumberOfLines { |
||||||
|
get { |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public ITextBufferVersion Version { |
||||||
|
get { |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public int TextLength { |
||||||
|
get { |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public IDocumentLine GetLine(int lineNumber) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public IDocumentLine GetLineForOffset(int offset) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public int PositionToOffset(int line, int column) |
||||||
|
{ |
||||||
|
if (WorkbenchSingleton.InvokeRequired) { |
||||||
|
return WorkbenchSingleton.SafeThreadFunction(() => PositionToOffset(line, column)); |
||||||
|
} else { |
||||||
|
return document.PositionToOffset(line, column); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public Location OffsetToPosition(int offset) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public void Insert(int offset, string text) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public void Insert(int offset, string text, AnchorMovementType defaultAnchorMovementType) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public void Remove(int offset, int length) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public void Replace(int offset, int length, string newText) |
||||||
|
{ |
||||||
|
if (WorkbenchSingleton.InvokeRequired) { |
||||||
|
WorkbenchSingleton.SafeThreadAsyncCall(() => Replace(offset, length, newText)); |
||||||
|
} else { |
||||||
|
document.Replace(offset, length, newText); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void StartUndoableAction() |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public void EndUndoableAction() |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public IDisposable OpenUndoGroup() |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public ITextAnchor CreateAnchor(int offset) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public ICSharpCode.SharpDevelop.ITextBuffer CreateSnapshot() |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public ICSharpCode.SharpDevelop.ITextBuffer CreateSnapshot(int offset, int length) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public System.IO.TextReader CreateReader() |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public TextReader CreateReader(int offset, int length) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public char GetCharAt(int offset) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public string GetText(int offset, int length) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public object GetService(Type serviceType) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,118 @@ |
|||||||
|
// 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.PackageManagement; |
||||||
|
using ICSharpCode.PackageManagement.EnvDTE; |
||||||
|
using ICSharpCode.SharpDevelop.Dom; |
||||||
|
using ICSharpCode.SharpDevelop.Editor; |
||||||
|
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; |
||||||
|
IDocument document; |
||||||
|
IDocumentLoader documentLoader; |
||||||
|
|
||||||
|
[SetUp] |
||||||
|
public void Init() |
||||||
|
{ |
||||||
|
fieldHelper = new FieldHelper(); |
||||||
|
methodHelper = new MethodHelper(); |
||||||
|
CreateDocumentLoader(); |
||||||
|
} |
||||||
|
|
||||||
|
void CreateDocumentLoader() |
||||||
|
{ |
||||||
|
document = MockRepository.GenerateStub<IDocument>(); |
||||||
|
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 = codeVariable.GetStartPoint(); |
||||||
|
endPoint = codeVariable.GetEndPoint(); |
||||||
|
editPoint = startPoint.CreateEditPoint(); |
||||||
|
} |
||||||
|
|
||||||
|
void CreateMethodEditPoint() |
||||||
|
{ |
||||||
|
var codeFunction = new CodeFunction(methodHelper.Method, documentLoader); |
||||||
|
TextPoint startPoint = codeFunction.GetStartPoint(); |
||||||
|
endPoint = codeFunction.GetEndPoint(); |
||||||
|
editPoint = startPoint.CreateEditPoint(); |
||||||
|
} |
||||||
|
|
||||||
|
void ReplaceText(string text) |
||||||
|
{ |
||||||
|
editPoint.ReplaceText(endPoint, text, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat); |
||||||
|
} |
||||||
|
|
||||||
|
void DocumentFileName(string fileName) |
||||||
|
{ |
||||||
|
documentLoader.Stub(loader => loader.LoadDocument(fileName)).Return(document); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ReplaceText_FieldEndPointCreatedFromStartPoint_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_MethodEndPointCreatedFromStartPoint_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); |
||||||
|
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")); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue