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)