From 8a4b2018a567d65882130b489e4eb10c7bb01028 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 8 Jun 2009 19:22:58 +0000 Subject: [PATCH] Avoid NullReferenceException when pasting text. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4267 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Document/NewLineFinder.cs | 2 ++ .../Editing/EditingCommandHandler.cs | 28 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/NewLineFinder.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/NewLineFinder.cs index af7b85e4cf..3207220015 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/NewLineFinder.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/NewLineFinder.cs @@ -51,6 +51,8 @@ namespace ICSharpCode.AvalonEdit.Document /// public static string NormalizeNewLines(string input, string newLine) { + if (input == null) + return null; Debug.Assert(IsNewLine(newLine)); SimpleSegment ds = NextNewLine(input, 0); if (ds == SimpleSegment.Invalid) // text does not contain any new lines diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs index 737c48034f..439003c7c0 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs @@ -208,14 +208,14 @@ namespace ICSharpCode.AvalonEdit.Editing { TransformSelectedLines( delegate (TextArea textArea, DocumentLine line) { - int offset = line.Offset; - ISegment s = TextUtilities.GetSingleIndentationSegment(line.Document, offset, textArea.Options.IndentationSize); - if (s.Length > 0) { - s = textArea.ReadOnlySectionProvider.GetDeletableSegments(s).FirstOrDefault(); - if (s != null && s.Length > 0) { - textArea.Document.Remove(s.Offset, s.Length); - } + int offset = line.Offset; + ISegment s = TextUtilities.GetSingleIndentationSegment(line.Document, offset, textArea.Options.IndentationSize); + if (s.Length > 0) { + s = textArea.ReadOnlySectionProvider.GetDeletableSegments(s).FirstOrDefault(); + if (s != null && s.Length > 0) { + textArea.Document.Remove(s.Offset, s.Length); } + } }, target, args, DefaultSegmentType.CurrentLine); } #endregion @@ -344,12 +344,14 @@ namespace ICSharpCode.AvalonEdit.Editing string newLine = NewLineFinder.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line); string text = NewLineFinder.NormalizeNewLines(Clipboard.GetText(), newLine); - bool fullLine = textArea.Options.CutCopyWholeLine && Clipboard.ContainsData(LineSelectedType); - if (fullLine) { - DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); - textArea.Document.Insert(currentLine.Offset, text); - } else { - textArea.ReplaceSelectionWithText(text); + if (!string.IsNullOrEmpty(text)) { + bool fullLine = textArea.Options.CutCopyWholeLine && Clipboard.ContainsData(LineSelectedType); + if (fullLine) { + DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); + textArea.Document.Insert(currentLine.Offset, text); + } else { + textArea.ReplaceSelectionWithText(text); + } } textArea.Caret.BringCaretToView(); args.Handled = true;