Skip to content

Commit 97d8b04

Browse files
fix(documentview): handle text view initialization errors
Wraps GetTextView in a try-catch to handle InvalidOperationException when the text view is still initializing, such as during document restoration. This prevents crashes by allowing the view to remain null if initialization is incomplete.
1 parent 7fd5247 commit 97d8b04

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/toolkit/Community.VisualStudio.Toolkit.Shared/Documents/DocumentView.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.VisualStudio.Shell;
1+
using Microsoft.VisualStudio.Shell;
22
using Microsoft.VisualStudio.Shell.Interop;
33
using Microsoft.VisualStudio.Text;
44
using Microsoft.VisualStudio.Text.Editor;
@@ -23,7 +23,17 @@ internal DocumentView(IVsWindowFrame nativeFrame)
2323
{
2424
ThreadHelper.ThrowIfNotOnUIThread();
2525
WindowFrame frame = new(nativeFrame);
26-
IWpfTextView? view = VsShellUtilities.GetTextView(nativeFrame)?.ToIWpfTextView();
26+
IWpfTextView? view = null;
27+
28+
try
29+
{
30+
view = VsShellUtilities.GetTextView(nativeFrame)?.ToIWpfTextView();
31+
}
32+
catch (System.InvalidOperationException)
33+
{
34+
// Text view is still initializing - leave view as null.
35+
// This can happen during document restoration when opening a solution.
36+
}
2737

2838
WindowFrame = frame;
2939
TextView = view;

0 commit comments

Comments
 (0)