diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2017-08-03 14:34:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-03 14:34:39 +0300 |
commit | 093037fbc016e6dc653d74df5c3583d7c99aff6a (patch) | |
tree | c700ccb70391d250920b2085e0725a880f5a32fd /main/src | |
parent | 12b8c473e811339452e0683160116c859f96b516 (diff) | |
parent | 5b9ec3a93c55e0ad6dcb13b9f946e58b3250d0df (diff) |
Merge pull request #2775 from mono/master-fix58139
Fixed 'Bug 58139 - Paste corrupts inserted text'
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextPasteHandler.cs | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextPasteHandler.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextPasteHandler.cs index 010d1a1322..e26c1a9208 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextPasteHandler.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextPasteHandler.cs @@ -38,6 +38,7 @@ using Microsoft.CodeAnalysis.Formatting.Rules; using Roslyn.Utilities; using System.Threading; using Microsoft.CodeAnalysis; +using System.Linq; namespace MonoDevelop.CSharp.Formatting { @@ -52,41 +53,49 @@ namespace MonoDevelop.CSharp.Formatting this.indent = indent; } - public override string FormatPlainText (int offset, string text, byte[] copyData) + public override string FormatPlainText (int insertionOffset, string text, byte [] copyData) { - return engine.FormatPlainText (indent.Editor, offset, text, copyData); - } + var result = engine.FormatPlainText (indent.Editor, insertionOffset, text, copyData); - public override byte[] GetCopyData (int offset, int length) - { - return engine.GetCopyData (indent.Editor, new TextSpan (offset, length)); - } - - public override async Task PostFomatPastedText (int insertionOffset, int insertedChars) - { - if (indent.Editor.Options.IndentStyle == IndentStyle.None || - indent.Editor.Options.IndentStyle == IndentStyle.Auto) - return; if (DefaultSourceEditorOptions.Instance.OnTheFlyFormatting) { - var tree = await indent.DocumentContext.AnalysisDocument.GetSyntaxTreeAsync (); + var tree = indent.DocumentContext.AnalysisDocument.GetSyntaxTreeAsync ().WaitAndGetResult (default (CancellationToken)); + tree = tree.WithChangedText (tree.GetText ().WithChanges (new TextChange (new TextSpan (insertionOffset, 0), text))); + + var insertedChars = text.Length; var startLine = indent.Editor.GetLineByOffset (insertionOffset); - var endLine = indent.Editor.GetLineByOffset (insertionOffset + insertedChars); - int lineStartOffset = startLine.Offset != endLine.Offset ? startLine.Offset : insertionOffset; - int formatCharsCount = insertedChars + (insertionOffset - lineStartOffset); + var policy = indent.DocumentContext.GetFormattingPolicy (); var textPolicy = indent.DocumentContext.Project.Policies.Get<Ide.Gui.Content.TextStylePolicy> (indent.Editor.MimeType); var optionSet = policy.CreateOptions (textPolicy); - var span = new TextSpan (lineStartOffset, formatCharsCount); + var span = new TextSpan (insertionOffset, insertedChars); - var rules = new List<IFormattingRule> () { new PasteFormattingRule () }; + var rules = new List<IFormattingRule> { new PasteFormattingRule () }; rules.AddRange (Formatter.GetDefaultFormattingRules (indent.DocumentContext.AnalysisDocument)); var root = tree.GetRoot (); - var changes = Formatter.GetFormattedTextChanges (root, SpecializedCollections.SingletonEnumerable (span), indent.DocumentContext.RoslynWorkspace, optionSet, rules, default(CancellationToken)); - indent.Editor.ApplyTextChanges (changes); - return; + var changes = Formatter.GetFormattedTextChanges (root, SpecializedCollections.SingletonEnumerable (span), indent.DocumentContext.RoslynWorkspace, optionSet, rules, default (CancellationToken)); + var doc = TextEditorFactory.CreateNewDocument (); + doc.Text = text; + doc.ApplyTextChanges (changes.Where (c => c.Span.Start - insertionOffset < text.Length && c.Span.Start - insertionOffset >= 0).Select (delegate (TextChange c) { + return new TextChange (new TextSpan (c.Span.Start - insertionOffset, c.Span.Length), c.NewText); + })); + return doc.Text; } - // Just correct the start line of the paste operation - the text is already indented. + + return result; + } + + public override byte [] GetCopyData (int offset, int length) + { + return engine.GetCopyData (indent.Editor, new TextSpan (offset, length)); + } + + public override async Task PostFomatPastedText (int insertionOffset, int insertedChars) + { + if (indent.Editor.Options.IndentStyle == IndentStyle.None || + indent.Editor.Options.IndentStyle == IndentStyle.Auto) + return; + // Just correct the start line of the paste operation - the text is already Formatted. var curLine = indent.Editor.GetLineByOffset (insertionOffset); var curLineOffset = curLine.Offset; indent.SafeUpdateIndentEngine (curLineOffset); |