Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit db9951a

Browse files
committed
Support replacing text in EnvDTE.EditPoint
1 parent 25fa355 commit db9951a

6 files changed

Lines changed: 125 additions & 139 deletions

File tree

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,18 @@
1616
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1717
// DEALINGS IN THE SOFTWARE.
1818

19-
//using System;
20-
//using ICSharpCode.SharpDevelop.Dom.Refactoring;
21-
//using ICSharpCode.SharpDevelop.Gui;
22-
//
23-
//namespace ICSharpCode.PackageManagement
24-
//{
25-
// public class DocumentLoader : IDocumentLoader
26-
// {
27-
// public IRefactoringDocument LoadRefactoringDocument(string fileName)
28-
// {
29-
// return LoadRefactoringDocumentView(fileName).RefactoringDocument;
30-
// }
31-
//
32-
// public IRefactoringDocumentView LoadRefactoringDocumentView(string fileName)
33-
// {
34-
// if (WorkbenchSingleton.InvokeRequired) {
35-
// return WorkbenchSingleton.SafeThreadFunction(() => LoadRefactoringDocumentView(fileName));
36-
// } else {
37-
// return new RefactoringDocumentView(fileName);
38-
// }
39-
// }
40-
// }
41-
//}
19+
using System;
20+
using ICSharpCode.SharpDevelop;
21+
22+
namespace ICSharpCode.PackageManagement
23+
{
24+
public class DocumentLoader : IDocumentLoader
25+
{
26+
public IRefactoringDocumentView LoadRefactoringDocumentView(string fileName)
27+
{
28+
return SD.MainThread.InvokeIfRequired(() => {
29+
return new RefactoringDocumentView(fileName);
30+
});
31+
}
32+
}
33+
}

src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeModelContext.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,21 @@ namespace ICSharpCode.PackageManagement.EnvDTE
2525
public class CodeModelContext
2626
{
2727
ICodeGenerator codeGenerator;
28+
IDocumentLoader documentLoader;
2829

2930
public Project DteProject { get; set; }
3031
public IProject CurrentProject { get; set; }
31-
public IDocumentLoader DocumentLoader { get; set; }
32+
33+
public IDocumentLoader DocumentLoader {
34+
get {
35+
if (documentLoader == null) {
36+
documentLoader = new DocumentLoader();
37+
}
38+
return documentLoader;
39+
}
40+
41+
set { documentLoader = value; }
42+
}
3243

3344
public ICodeGenerator CodeGenerator {
3445
get {
@@ -46,7 +57,7 @@ public ICodeGenerator CodeGenerator {
4657

4758
public CodeModelContext WithFilteredFileName(string fileName)
4859
{
49-
CodeModelContext newContext = (CodeModelContext)MemberwiseClone();
60+
var newContext = (CodeModelContext)MemberwiseClone();
5061
newContext.FilteredFileName = fileName;
5162
return newContext;
5263
}

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

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
using System;
2020
using ICSharpCode.NRefactory;
21-
using ICSharpCode.SharpDevelop.Dom;
21+
using ICSharpCode.NRefactory.Editor;
22+
using ICSharpCode.SharpDevelop;
2223

2324
namespace ICSharpCode.PackageManagement.EnvDTE
2425
{
2526
public class EditPoint : TextPoint, global::EnvDTE.EditPoint
2627
{
27-
// IRefactoringDocument document;
28-
// IRefactoringDocumentView documentView;
28+
IDocument document;
29+
IRefactoringDocumentView documentView;
2930

3031
internal EditPoint(string fileName, TextLocation location, IDocumentLoader documentLoader)
3132
: base(fileName, location, documentLoader)
@@ -39,47 +40,46 @@ public void ReplaceText(object pointOrCount, string text, int flags)
3940

4041
void ReplaceText(TextPoint endPoint, string text, global::EnvDTE.vsEPReplaceTextOptions textFormatOptions)
4142
{
42-
throw new NotImplementedException();
43-
// OpenDocument();
44-
// int offset = GetStartOffset();
45-
// int endOffset = GetEndOffset(endPoint);
46-
// document.Replace(offset, endOffset - offset, text);
47-
// IndentReplacedText(text);
43+
OpenDocument();
44+
int offset = GetStartOffset();
45+
int endOffset = GetEndOffset(endPoint);
46+
document.Replace(offset, endOffset - offset, text);
47+
IndentReplacedText(text);
48+
}
49+
50+
void OpenDocument()
51+
{
52+
documentView = documentLoader.LoadRefactoringDocumentView(fileName);
53+
document = documentView.RefactoringDocument;
54+
}
55+
56+
int GetStartOffset()
57+
{
58+
return document.PositionToOffset(Line, LineCharOffset);
59+
}
60+
61+
int GetEndOffset(TextPoint endPoint)
62+
{
63+
return document.PositionToOffset(endPoint.Line, endPoint.LineCharOffset);
64+
}
65+
66+
/// <summary>
67+
/// Indents all lines apart from the first one since it is assumed
68+
/// that the first line had the correct indentation.
69+
/// </summary>
70+
void IndentReplacedText(string text)
71+
{
72+
int lineCount = GetLineCount(text);
73+
if (lineCount > 1) {
74+
documentView.IndentLines(Line + 1, Line + lineCount);
75+
}
76+
}
77+
78+
int GetLineCount(string text)
79+
{
80+
return text.Split('\n').Length;
4881
}
4982

50-
// void OpenDocument()
51-
// {
52-
// documentView = DocumentLoader.LoadRefactoringDocumentView(FilePosition.FileName);
53-
// document = documentView.RefactoringDocument;
54-
// }
55-
//
56-
// int GetStartOffset()
57-
// {
58-
// return document.PositionToOffset(Line, LineCharOffset);
59-
// }
60-
//
61-
// int GetEndOffset(TextPoint endPoint)
62-
// {
63-
// return document.PositionToOffset(endPoint.Line, endPoint.LineCharOffset);
64-
// }
65-
//
66-
// /// <summary>
67-
// /// Indents all lines apart from the first one since it is assumed
68-
// /// that the first line had the correct indentation.
69-
// /// </summary>
70-
// void IndentReplacedText(string text)
71-
// {
72-
// int lineCount = GetLineCount(text);
73-
// if (lineCount > 1) {
74-
// documentView.IndentLines(Line + 1, Line + lineCount);
75-
// }
76-
// }
77-
//
78-
// int GetLineCount(string text)
79-
// {
80-
// return text.Split('\n').Length;
81-
// }
82-
//
8383
public void Insert(string text)
8484
{
8585
throw new NotImplementedException();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
// DEALINGS IN THE SOFTWARE.
1818

1919
using System;
20-
using ICSharpCode.SharpDevelop.Editor;
2120

2221
namespace ICSharpCode.PackageManagement
2322
{
2423
public interface IDocumentLoader
2524
{
26-
// IRefactoringDocument LoadRefactoringDocument(string fileName);
27-
// IRefactoringDocumentView LoadRefactoringDocumentView(string fileName);
25+
IRefactoringDocumentView LoadRefactoringDocumentView(string fileName);
2826
}
2927
}

src/AddIns/Misc/PackageManagement/Project/Src/IRefactoringDocumentView.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1717
// DEALINGS IN THE SOFTWARE.
1818

19-
//using System;
20-
//using ICSharpCode.SharpDevelop.Dom;
21-
//using ICSharpCode.SharpDevelop.Dom.Refactoring;
22-
//
23-
//namespace ICSharpCode.PackageManagement
24-
//{
25-
// public interface IRefactoringDocumentView
26-
// {
27-
// IRefactoringDocument RefactoringDocument { get; }
28-
// ICompilationUnit Parse();
29-
// void IndentLines(int beginLine, int endLine);
30-
// }
31-
//}
19+
using System;
20+
using ICSharpCode.NRefactory.Editor;
21+
22+
namespace ICSharpCode.PackageManagement
23+
{
24+
public interface IRefactoringDocumentView
25+
{
26+
IDocument RefactoringDocument { get; }
27+
void IndentLines(int beginLine, int endLine);
28+
}
29+
}

src/AddIns/Misc/PackageManagement/Project/Src/RefactoringDocumentView.cs

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,46 @@
1616
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1717
// DEALINGS IN THE SOFTWARE.
1818

19-
//using System;
20-
//using ICSharpCode.SharpDevelop;
21-
//using ICSharpCode.SharpDevelop.Dom;
22-
//using ICSharpCode.SharpDevelop.Dom.Refactoring;
23-
//using ICSharpCode.SharpDevelop.Editor;
24-
//using ICSharpCode.SharpDevelop.Gui;
25-
//
26-
//namespace ICSharpCode.PackageManagement
27-
//{
28-
// public class RefactoringDocumentView : IRefactoringDocumentView
29-
// {
30-
// public RefactoringDocumentView(string fileName)
31-
// {
32-
// View = FileService.OpenFile(fileName);
33-
// TextEditor = GetTextEditor();
34-
// FormattingStrategy = TextEditor.Language.FormattingStrategy;
35-
// RefactoringDocument = LoadDocument();
36-
// }
37-
//
38-
// IViewContent View { get; set; }
39-
// ITextEditor TextEditor { get; set; }
40-
// IFormattingStrategy FormattingStrategy { get; set; }
41-
//
42-
// ITextEditor GetTextEditor()
43-
// {
44-
// var textEditorProvider = View as ITextEditorProvider;
45-
// return textEditorProvider.TextEditor;
46-
// }
47-
//
48-
// public IRefactoringDocument RefactoringDocument { get; private set; }
49-
//
50-
// IRefactoringDocument LoadDocument()
51-
// {
52-
// return new RefactoringDocumentAdapter(new ThreadSafeDocument(TextEditor.Document));
53-
// }
54-
//
55-
// public ICompilationUnit Parse()
56-
// {
57-
// if (WorkbenchSingleton.InvokeRequired) {
58-
// return WorkbenchSingleton.SafeThreadFunction(() => Parse());
59-
// }
60-
// return ParserService.ParseViewContent(View).CompilationUnit;
61-
// }
62-
//
63-
// public void IndentLines(int beginLine, int endLine)
64-
// {
65-
// if (WorkbenchSingleton.InvokeRequired) {
66-
// WorkbenchSingleton.SafeThreadCall(() => IndentLines(beginLine, endLine));
67-
// } else {
68-
// using (IDisposable undoGroup = TextEditor.Document.OpenUndoGroup()) {
69-
// FormattingStrategy.IndentLines(TextEditor, beginLine, endLine);
70-
// }
71-
// }
72-
// }
73-
// }
74-
//}
19+
using System;
20+
using ICSharpCode.NRefactory.Editor;
21+
using ICSharpCode.SharpDevelop;
22+
using ICSharpCode.SharpDevelop.Editor;
23+
24+
namespace ICSharpCode.PackageManagement
25+
{
26+
public class RefactoringDocumentView : IRefactoringDocumentView
27+
{
28+
public RefactoringDocumentView(string fileName)
29+
{
30+
View = FileService.OpenFile(fileName);
31+
TextEditor = GetTextEditor();
32+
FormattingStrategy = TextEditor.Language.FormattingStrategy;
33+
RefactoringDocument = LoadDocument();
34+
}
35+
36+
IViewContent View { get; set; }
37+
ITextEditor TextEditor { get; set; }
38+
IFormattingStrategy FormattingStrategy { get; set; }
39+
40+
ITextEditor GetTextEditor()
41+
{
42+
return View.GetService<ITextEditor>();
43+
}
44+
45+
public IDocument RefactoringDocument { get; private set; }
46+
47+
IDocument LoadDocument()
48+
{
49+
return new ThreadSafeDocument(TextEditor.Document);
50+
}
51+
52+
public void IndentLines(int beginLine, int endLine)
53+
{
54+
SD.MainThread.InvokeIfRequired(() => {
55+
using (IDisposable undoGroup = TextEditor.Document.OpenUndoGroup()) {
56+
FormattingStrategy.IndentLines(TextEditor, beginLine, endLine);
57+
}
58+
});
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)