From fc17d16f5893ba87eb857a932af89ecc7dbd75cb Mon Sep 17 00:00:00 2001 From: Tamas Balog Date: Tue, 7 Mar 2023 08:29:55 +0100 Subject: [PATCH] Handle if caret is after the end offset of the buffer --- .../Preview/CodePreviewSession.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Extension/SnippetSearch/Preview/CodePreviewSession.cs b/src/Extension/SnippetSearch/Preview/CodePreviewSession.cs index 3a4f7c0..4838c91 100644 --- a/src/Extension/SnippetSearch/Preview/CodePreviewSession.cs +++ b/src/Extension/SnippetSearch/Preview/CodePreviewSession.cs @@ -57,17 +57,20 @@ public void StartPreviewing(IWpfTextView textView, VisualStudioSnippet snippet) } var newSpan = new Span(caretBufferPosition, indentedCode.Length); - - // create read only region for the new snippet - IReadOnlyRegion newRegion; - using (var readEdit = textView.TextBuffer.CreateReadOnlyRegionEdit()) + //If the caret, for some reason, happens to be after the end of the current text buffer, + // then we don't show the preview. + if (caretBufferPosition <= textView.TextBuffer.CurrentSnapshot.Length) { - newRegion = readEdit.CreateReadOnlyRegion(newSpan); - - readEdit.Apply(); + // create read only region for the new snippet + IReadOnlyRegion newRegion; + using (var readEdit = textView.TextBuffer.CreateReadOnlyRegionEdit()) + { + newRegion = readEdit.CreateReadOnlyRegion(newSpan); + readEdit.Apply(); + } + + CurrentPreview = newRegion; } - - CurrentPreview = newRegion; } public void StopPreviewing(IWpfTextView textView)