From d54e057655f9dca4a249ededebdf7054bcbfba43 Mon Sep 17 00:00:00 2001 From: Jason Malinowski Date: Thu, 31 Oct 2019 11:03:37 -0700 Subject: [PATCH] Fix crash where we are not always finding text snapshots Given this is not a reliable crash, my guess is the problem is we are sometimes processing an older Solution snapshot. Asking a specific SourceText for the specific ITextSnapshot it came from could fail if that snapshot had been GC'ed, whereas going to the container first and asking for the buffer directly should be more reliable. Either way we now also handle the null case. Fixes https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1010045 --- ...rredIndentationDocumentOptionsProviderFactory.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/EditorFeatures/Core/Implementation/Formatting/InferredIndentationDocumentOptionsProviderFactory.cs b/src/EditorFeatures/Core/Implementation/Formatting/InferredIndentationDocumentOptionsProviderFactory.cs index 6749ba5c688..622747ef4f9 100644 --- a/src/EditorFeatures/Core/Implementation/Formatting/InferredIndentationDocumentOptionsProviderFactory.cs +++ b/src/EditorFeatures/Core/Implementation/Formatting/InferredIndentationDocumentOptionsProviderFactory.cs @@ -5,6 +5,7 @@ using System.ComponentModel.Composition; using System.Threading; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.ErrorReporting; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Text; @@ -71,8 +72,16 @@ public bool TryGetDocumentOption(OptionKey option, out object? value) var currentDocument = _workspace.CurrentSolution.GetDocument(_documentId); if (currentDocument != null && currentDocument.TryGetText(out var text)) { - var snapshot = text.FindCorrespondingEditorTextSnapshot(); - return TryGetOptionForBuffer(snapshot.TextBuffer, option, out value); + var textBuffer = text.Container.TryGetTextBuffer(); + + if (textBuffer != null) + { + return TryGetOptionForBuffer(textBuffer, option, out value); + } + else + { + FatalError.ReportWithoutCrash(new System.Exception("We had an open document but it wasn't associated with a buffer. That meant we coudln't apply formatting settings.")); + } } } -- GitLab