未验证 提交 e2b583f1 编写于 作者: J Jason Malinowski 提交者: GitHub

Merge pull request #39607 from jasonmalinowski/fix-null-textsnapshot-crashes

Fix crash where we are not always finding text snapshots
......@@ -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."));
}
}
}
......
......@@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
......@@ -357,7 +356,7 @@ public override IReadOnlyList<TextChangeRange> GetChangeRanges(SourceText oldTex
return GetChangeRanges(oldSnapshot, oldText.Length, newSnapshot);
}
private IReadOnlyList<TextChangeRange> GetChangeRanges(ITextImage oldImage, int oldTextLength, ITextImage newImage)
private IReadOnlyList<TextChangeRange> GetChangeRanges(ITextImage? oldImage, int oldTextLength, ITextImage? newImage)
{
if (oldImage == null ||
newImage == null ||
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Text;
namespace Microsoft.CodeAnalysis.Text
......@@ -17,20 +18,20 @@ public static SourceTextContainer AsTextContainer(this ITextBuffer buffer)
public static ITextBuffer GetTextBuffer(this SourceTextContainer textContainer)
=> TryGetTextBuffer(textContainer) ?? throw new ArgumentException(TextEditorResources.textContainer_is_not_a_SourceTextContainer_that_was_created_from_an_ITextBuffer, nameof(textContainer));
public static ITextBuffer TryGetTextBuffer(this SourceTextContainer textContainer)
public static ITextBuffer? TryGetTextBuffer(this SourceTextContainer? textContainer)
=> (textContainer as TextBufferContainer)?.TryFindEditorTextBuffer();
/// <summary>
/// Returns the ITextSnapshot behind this SourceText, or null if it wasn't created from one.
/// Returns the <see cref="ITextSnapshot"/> behind this <see cref="SourceText"/>, or null if it wasn't created from one.
///
/// Note that multiple ITextSnapshots may map to the same SourceText instance if
/// ITextSnapshot.Version.ReiteratedVersionNumber doesn't change.
/// Note that multiple <see cref="ITextSnapshot"/>s may map to the same <see cref="SourceText"/> instance if it's
/// <see cref="ITextVersion.ReiteratedVersionNumber" /> doesn't change.
/// </summary>
/// <returns>The underlying ITextSnapshot.</returns>
public static ITextSnapshot FindCorrespondingEditorTextSnapshot(this SourceText text)
public static ITextSnapshot? FindCorrespondingEditorTextSnapshot(this SourceText? text)
=> (text as SnapshotSourceText)?.TryFindEditorSnapshot();
internal static ITextImage TryFindCorrespondingEditorTextImage(this SourceText text)
internal static ITextImage? TryFindCorrespondingEditorTextImage(this SourceText? text)
=> (text as SnapshotSourceText)?.TextImage;
internal static TextLine AsTextLine(this ITextSnapshotLine line)
......@@ -48,7 +49,7 @@ internal static SourceText AsRoslynText(this ITextSnapshot textSnapshot, ITextBu
/// <summary>
/// Gets the workspace corresponding to the text buffer.
/// </summary>
public static Workspace GetWorkspace(this ITextBuffer buffer)
public static Workspace? GetWorkspace(this ITextBuffer buffer)
{
var container = buffer.AsTextContainer();
if (Workspace.TryGetWorkspace(container, out var workspace))
......@@ -73,7 +74,7 @@ public static IEnumerable<Document> GetRelatedDocumentsWithChanges(this ITextSna
/// associated with the buffer if it is linked into multiple projects or is part of a Shared Project. In this case, the <see cref="Workspace"/>
/// is responsible for keeping track of which of these <see cref="Document"/>s is in the current project context.
/// </summary>
public static Document GetOpenDocumentInCurrentContextWithChanges(this ITextSnapshot text)
public static Document? GetOpenDocumentInCurrentContextWithChanges(this ITextSnapshot text)
=> text.AsText().GetOpenDocumentInCurrentContextWithChanges();
/// <summary>
......@@ -90,7 +91,7 @@ public static IEnumerable<Document> GetRelatedDocuments(this ITextBuffer buffer)
/// with the specified text's container, or the text's container isn't associated with a workspace,
/// then the method returns false.
/// </summary>
internal static Document GetDocumentWithFrozenPartialSemantics(this SourceText text, CancellationToken cancellationToken)
internal static Document? GetDocumentWithFrozenPartialSemantics(this SourceText text, CancellationToken cancellationToken)
{
var document = text.GetOpenDocumentInCurrentContextWithChanges();
return document?.WithFrozenPartialSemantics(cancellationToken);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System.Collections.Generic;
using System.Linq;
using Roslyn.Utilities;
......@@ -19,7 +21,7 @@ public static IEnumerable<Document> GetRelatedDocumentsWithChanges(this SourceTe
{
var ids = workspace.GetRelatedDocumentIds(text.Container);
var sol = workspace.CurrentSolution.WithDocumentText(ids, text, PreservationMode.PreserveIdentity);
return ids.Select(id => sol.GetDocument(id)).Where(d => d != null);
return ids.Select(id => sol.GetDocument(id)).WhereNotNull();
}
return SpecializedCollections.EmptyEnumerable<Document>();
......@@ -29,7 +31,7 @@ public static IEnumerable<Document> GetRelatedDocumentsWithChanges(this SourceTe
/// Gets the document from the corresponding workspace's current solution that is associated with the source text's container
/// in its current project context, updated to contain the same text as the source if necessary.
/// </summary>
public static Document GetOpenDocumentInCurrentContextWithChanges(this SourceText text)
public static Document? GetOpenDocumentInCurrentContextWithChanges(this SourceText text)
{
if (Workspace.TryGetWorkspace(text.Container, out var workspace))
{
......@@ -59,7 +61,7 @@ public static IEnumerable<Document> GetRelatedDocuments(this SourceTextContainer
{
var sol = workspace.CurrentSolution;
var ids = workspace.GetRelatedDocumentIds(container);
return ids.Select(id => sol.GetDocument(id)).Where(d => d != null);
return ids.Select(id => sol.GetDocument(id)).WhereNotNull();
}
return SpecializedCollections.EmptyEnumerable<Document>();
......@@ -69,7 +71,7 @@ public static IEnumerable<Document> GetRelatedDocuments(this SourceTextContainer
/// Gets the document from the corresponding workspace's current solution that is associated with the text container
/// in its current project context.
/// </summary>
public static Document GetOpenDocumentInCurrentContext(this SourceTextContainer container)
public static Document? GetOpenDocumentInCurrentContext(this SourceTextContainer container)
{
if (Workspace.TryGetWorkspace(container, out var workspace))
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册