Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Handle if caret is after the end offset of the buffer #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/Extension/SnippetSearch/Preview/CodePreviewSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down