From db9951afbf27f291413b1047bdc705b3a557179a Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sat, 15 Feb 2014 14:45:43 +0000 Subject: [PATCH] Support replacing text in EnvDTE.EditPoint --- .../Project/Src/DocumentLoader.cs | 38 +++---- .../Project/Src/EnvDTE/CodeModelContext.cs | 15 ++- .../Project/Src/EnvDTE/EditPoint.cs | 84 ++++++++-------- .../Project/Src/IDocumentLoader.cs | 4 +- .../Project/Src/IRefactoringDocumentView.cs | 24 +++-- .../Project/Src/RefactoringDocumentView.cs | 99 ++++++++----------- 6 files changed, 125 insertions(+), 139 deletions(-) diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/DocumentLoader.cs b/src/AddIns/Misc/PackageManagement/Project/Src/DocumentLoader.cs index 3d57bd0897..ea996f4d2a 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/DocumentLoader.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/DocumentLoader.cs @@ -16,26 +16,18 @@ // 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.Refactoring; -//using ICSharpCode.SharpDevelop.Gui; -// -//namespace ICSharpCode.PackageManagement -//{ -// public class DocumentLoader : IDocumentLoader -// { -// public IRefactoringDocument LoadRefactoringDocument(string fileName) -// { -// return LoadRefactoringDocumentView(fileName).RefactoringDocument; -// } -// -// public IRefactoringDocumentView LoadRefactoringDocumentView(string fileName) -// { -// if (WorkbenchSingleton.InvokeRequired) { -// return WorkbenchSingleton.SafeThreadFunction(() => LoadRefactoringDocumentView(fileName)); -// } else { -// return new RefactoringDocumentView(fileName); -// } -// } -// } -//} +using System; +using ICSharpCode.SharpDevelop; + +namespace ICSharpCode.PackageManagement +{ + public class DocumentLoader : IDocumentLoader + { + public IRefactoringDocumentView LoadRefactoringDocumentView(string fileName) + { + return SD.MainThread.InvokeIfRequired(() => { + return new RefactoringDocumentView(fileName); + }); + } + } +} diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeModelContext.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeModelContext.cs index c48bd46944..26b3fd1417 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeModelContext.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeModelContext.cs @@ -25,10 +25,21 @@ namespace ICSharpCode.PackageManagement.EnvDTE public class CodeModelContext { ICodeGenerator codeGenerator; + IDocumentLoader documentLoader; public Project DteProject { get; set; } public IProject CurrentProject { get; set; } - public IDocumentLoader DocumentLoader { get; set; } + + public IDocumentLoader DocumentLoader { + get { + if (documentLoader == null) { + documentLoader = new DocumentLoader(); + } + return documentLoader; + } + + set { documentLoader = value; } + } public ICodeGenerator CodeGenerator { get { @@ -46,7 +57,7 @@ namespace ICSharpCode.PackageManagement.EnvDTE public CodeModelContext WithFilteredFileName(string fileName) { - CodeModelContext newContext = (CodeModelContext)MemberwiseClone(); + var newContext = (CodeModelContext)MemberwiseClone(); newContext.FilteredFileName = fileName; return newContext; } diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/EditPoint.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/EditPoint.cs index 9153d1a1cd..d70719755f 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/EditPoint.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/EditPoint.cs @@ -18,14 +18,15 @@ using System; using ICSharpCode.NRefactory; -using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.NRefactory.Editor; +using ICSharpCode.SharpDevelop; namespace ICSharpCode.PackageManagement.EnvDTE { public class EditPoint : TextPoint, global::EnvDTE.EditPoint { -// IRefactoringDocument document; -// IRefactoringDocumentView documentView; + IDocument document; + IRefactoringDocumentView documentView; internal EditPoint(string fileName, TextLocation location, IDocumentLoader documentLoader) : base(fileName, location, documentLoader) @@ -39,47 +40,46 @@ namespace ICSharpCode.PackageManagement.EnvDTE void ReplaceText(TextPoint endPoint, string text, global::EnvDTE.vsEPReplaceTextOptions textFormatOptions) { - throw new NotImplementedException(); -// OpenDocument(); -// int offset = GetStartOffset(); -// int endOffset = GetEndOffset(endPoint); -// document.Replace(offset, endOffset - offset, text); -// IndentReplacedText(text); + OpenDocument(); + int offset = GetStartOffset(); + int endOffset = GetEndOffset(endPoint); + document.Replace(offset, endOffset - offset, text); + IndentReplacedText(text); + } + + void OpenDocument() + { + documentView = documentLoader.LoadRefactoringDocumentView(fileName); + document = documentView.RefactoringDocument; + } + + int GetStartOffset() + { + return document.PositionToOffset(Line, LineCharOffset); + } + + int GetEndOffset(TextPoint endPoint) + { + return document.PositionToOffset(endPoint.Line, endPoint.LineCharOffset); + } + + /// + /// Indents all lines apart from the first one since it is assumed + /// that the first line had the correct indentation. + /// + void IndentReplacedText(string text) + { + int lineCount = GetLineCount(text); + if (lineCount > 1) { + documentView.IndentLines(Line + 1, Line + lineCount); + } + } + + int GetLineCount(string text) + { + return text.Split('\n').Length; } -// void OpenDocument() -// { -// documentView = DocumentLoader.LoadRefactoringDocumentView(FilePosition.FileName); -// document = documentView.RefactoringDocument; -// } -// -// int GetStartOffset() -// { -// return document.PositionToOffset(Line, LineCharOffset); -// } -// -// int GetEndOffset(TextPoint endPoint) -// { -// return document.PositionToOffset(endPoint.Line, endPoint.LineCharOffset); -// } -// -// /// -// /// Indents all lines apart from the first one since it is assumed -// /// that the first line had the correct indentation. -// /// -// void IndentReplacedText(string text) -// { -// int lineCount = GetLineCount(text); -// if (lineCount > 1) { -// documentView.IndentLines(Line + 1, Line + lineCount); -// } -// } -// -// int GetLineCount(string text) -// { -// return text.Split('\n').Length; -// } -// public void Insert(string text) { throw new NotImplementedException(); diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/IDocumentLoader.cs b/src/AddIns/Misc/PackageManagement/Project/Src/IDocumentLoader.cs index 0b34af78be..b992e4557d 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/IDocumentLoader.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/IDocumentLoader.cs @@ -17,13 +17,11 @@ // DEALINGS IN THE SOFTWARE. using System; -using ICSharpCode.SharpDevelop.Editor; namespace ICSharpCode.PackageManagement { public interface IDocumentLoader { -// IRefactoringDocument LoadRefactoringDocument(string fileName); -// IRefactoringDocumentView LoadRefactoringDocumentView(string fileName); + IRefactoringDocumentView LoadRefactoringDocumentView(string fileName); } } diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/IRefactoringDocumentView.cs b/src/AddIns/Misc/PackageManagement/Project/Src/IRefactoringDocumentView.cs index 01daab3982..27fcabe3a3 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/IRefactoringDocumentView.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/IRefactoringDocumentView.cs @@ -16,16 +16,14 @@ // 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; -//using ICSharpCode.SharpDevelop.Dom.Refactoring; -// -//namespace ICSharpCode.PackageManagement -//{ -// public interface IRefactoringDocumentView -// { -// IRefactoringDocument RefactoringDocument { get; } -// ICompilationUnit Parse(); -// void IndentLines(int beginLine, int endLine); -// } -//} +using System; +using ICSharpCode.NRefactory.Editor; + +namespace ICSharpCode.PackageManagement +{ + public interface IRefactoringDocumentView + { + IDocument RefactoringDocument { get; } + void IndentLines(int beginLine, int endLine); + } +} diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/RefactoringDocumentView.cs b/src/AddIns/Misc/PackageManagement/Project/Src/RefactoringDocumentView.cs index d22d7541db..457e660283 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/RefactoringDocumentView.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/RefactoringDocumentView.cs @@ -16,59 +16,46 @@ // 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; -//using ICSharpCode.SharpDevelop.Dom; -//using ICSharpCode.SharpDevelop.Dom.Refactoring; -//using ICSharpCode.SharpDevelop.Editor; -//using ICSharpCode.SharpDevelop.Gui; -// -//namespace ICSharpCode.PackageManagement -//{ -// public class RefactoringDocumentView : IRefactoringDocumentView -// { -// public RefactoringDocumentView(string fileName) -// { -// View = FileService.OpenFile(fileName); -// TextEditor = GetTextEditor(); -// FormattingStrategy = TextEditor.Language.FormattingStrategy; -// RefactoringDocument = LoadDocument(); -// } -// -// IViewContent View { get; set; } -// ITextEditor TextEditor { get; set; } -// IFormattingStrategy FormattingStrategy { get; set; } -// -// ITextEditor GetTextEditor() -// { -// var textEditorProvider = View as ITextEditorProvider; -// return textEditorProvider.TextEditor; -// } -// -// public IRefactoringDocument RefactoringDocument { get; private set; } -// -// IRefactoringDocument LoadDocument() -// { -// return new RefactoringDocumentAdapter(new ThreadSafeDocument(TextEditor.Document)); -// } -// -// public ICompilationUnit Parse() -// { -// if (WorkbenchSingleton.InvokeRequired) { -// return WorkbenchSingleton.SafeThreadFunction(() => Parse()); -// } -// return ParserService.ParseViewContent(View).CompilationUnit; -// } -// -// public void IndentLines(int beginLine, int endLine) -// { -// if (WorkbenchSingleton.InvokeRequired) { -// WorkbenchSingleton.SafeThreadCall(() => IndentLines(beginLine, endLine)); -// } else { -// using (IDisposable undoGroup = TextEditor.Document.OpenUndoGroup()) { -// FormattingStrategy.IndentLines(TextEditor, beginLine, endLine); -// } -// } -// } -// } -//} +using System; +using ICSharpCode.NRefactory.Editor; +using ICSharpCode.SharpDevelop; +using ICSharpCode.SharpDevelop.Editor; + +namespace ICSharpCode.PackageManagement +{ + public class RefactoringDocumentView : IRefactoringDocumentView + { + public RefactoringDocumentView(string fileName) + { + View = FileService.OpenFile(fileName); + TextEditor = GetTextEditor(); + FormattingStrategy = TextEditor.Language.FormattingStrategy; + RefactoringDocument = LoadDocument(); + } + + IViewContent View { get; set; } + ITextEditor TextEditor { get; set; } + IFormattingStrategy FormattingStrategy { get; set; } + + ITextEditor GetTextEditor() + { + return View.GetService(); + } + + public IDocument RefactoringDocument { get; private set; } + + IDocument LoadDocument() + { + return new ThreadSafeDocument(TextEditor.Document); + } + + public void IndentLines(int beginLine, int endLine) + { + SD.MainThread.InvokeIfRequired(() => { + using (IDisposable undoGroup = TextEditor.Document.OpenUndoGroup()) { + FormattingStrategy.IndentLines(TextEditor, beginLine, endLine); + } + }); + } + } +}