提交 bb044a32 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #18039 from CyrusNajmabadi/nullRefInUndo

Address null ref when we can't get an Undo manager.

Fixes https://github.com/dotnet/roslyn/issues/17898
......@@ -12,26 +12,13 @@ namespace Microsoft.CodeAnalysis.Text
public static partial class Extensions
{
public static SourceTextContainer AsTextContainer(this ITextBuffer buffer)
{
return TextBufferContainer.From(buffer);
}
=> TextBufferContainer.From(buffer);
public static ITextBuffer GetTextBuffer(this SourceTextContainer textContainer)
{
var textBuffer = TryGetTextBuffer(textContainer);
if (textBuffer == null)
{
throw new ArgumentException(TextEditorResources.textContainer_is_not_a_SourceTextContainer_that_was_created_from_an_ITextBuffer, nameof(textContainer));
}
return textBuffer;
}
=> 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)
{
var t = textContainer as TextBufferContainer;
return t == null ? null : t.EditorTextBuffer;
}
=> (textContainer as TextBufferContainer)?.EditorTextBuffer;
/// <summary>
/// Returns the ITextSnapshot behind this SourceText, or null if it wasn't created from one.
......@@ -41,25 +28,16 @@ public static ITextBuffer TryGetTextBuffer(this SourceTextContainer textContaine
/// </summary>
/// <returns>The underlying ITextSnapshot.</returns>
public static ITextSnapshot FindCorrespondingEditorTextSnapshot(this SourceText text)
{
var t = text as SnapshotSourceText;
return t == null ? null : t.EditorSnapshot;
}
=> (text as SnapshotSourceText)?.EditorSnapshot;
internal static TextLine AsTextLine(this ITextSnapshotLine line)
{
return line.Snapshot.AsText().Lines[line.LineNumber];
}
=> line.Snapshot.AsText().Lines[line.LineNumber];
public static SourceText AsText(this ITextSnapshot textSnapshot)
{
return SnapshotSourceText.From(textSnapshot);
}
=> SnapshotSourceText.From(textSnapshot);
internal static SourceText AsRoslynText(this ITextSnapshot textSnapshot, Encoding encoding)
{
return new SnapshotSourceText.ClosedSnapshotSourceText(textSnapshot, encoding);
}
=> new SnapshotSourceText.ClosedSnapshotSourceText(textSnapshot, encoding);
/// <summary>
/// Gets the workspace corresponding to the text buffer.
......@@ -81,9 +59,7 @@ public static Workspace GetWorkspace(this ITextBuffer buffer)
/// if the file is linked into multiple projects or is part of a Shared Project.
/// </summary>
public static IEnumerable<Document> GetRelatedDocumentsWithChanges(this ITextSnapshot text)
{
return text.AsText().GetRelatedDocumentsWithChanges();
}
=> text.AsText().GetRelatedDocumentsWithChanges();
/// <summary>
/// Gets the <see cref="Document"/> from the corresponding <see cref="Workspace.CurrentSolution"/> that is associated with the <see cref="ITextSnapshot"/>'s buffer
......@@ -92,18 +68,14 @@ public static IEnumerable<Document> GetRelatedDocumentsWithChanges(this ITextSna
/// 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)
{
return text.AsText().GetOpenDocumentInCurrentContextWithChanges();
}
=> text.AsText().GetOpenDocumentInCurrentContextWithChanges();
/// <summary>
/// Gets the <see cref="Document"/>s from the corresponding <see cref="Workspace.CurrentSolution"/> that are associated with the <see cref="ITextBuffer"/>.
/// There may be multiple <see cref="Document"/>s associated with the buffer if it is linked into multiple projects or is part of a Shared Project.
/// </summary>
public static IEnumerable<Document> GetRelatedDocuments(this ITextBuffer buffer)
{
return buffer.AsTextContainer().GetRelatedDocuments();
}
=> buffer.AsTextContainer().GetRelatedDocuments();
/// <summary>
/// Tries to get the document corresponding to the text from the current partial solution
......@@ -125,16 +97,8 @@ internal static async Task<Document> GetDocumentWithFrozenPartialSemanticsAsync(
}
internal static bool CanApplyChangeDocumentToWorkspace(this ITextBuffer buffer)
{
if (Workspace.TryGetWorkspace(buffer.AsTextContainer(), out var workspace))
{
return workspace.CanApplyChange(ApplyChangesKind.ChangeDocument);
}
else
{
return false;
}
}
=> Workspace.TryGetWorkspace(buffer.AsTextContainer(), out var workspace) &&
workspace.CanApplyChange(ApplyChangesKind.ChangeDocument);
/// <summary>
/// Get the encoding used to load this <see cref="ITextBuffer"/> if possible.
......@@ -145,13 +109,8 @@ internal static bool CanApplyChangeDocumentToWorkspace(this ITextBuffer buffer)
/// </para>
/// </summary>
internal static Encoding GetEncodingOrUTF8(this ITextBuffer textBuffer)
{
if (textBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDocument))
{
return textDocument.Encoding;
}
return Encoding.UTF8;
}
=> textBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDocument)
? textDocument.Encoding
: Encoding.UTF8;
}
}
}
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.OLE.Interop;
......@@ -18,7 +20,21 @@ internal static class IVsEditorAdaptersFactoryServiceExtensions
DocumentId contextDocumentId,
CancellationToken cancellationToken)
{
// https://github.com/dotnet/roslyn/issues/17898
// We have a report of a null ref occuring in this method. The only place we believe
// this could be would be if 'document' was null. Try to catch a reasonable
// non -fatal-watson dump to help track down what the root cause of this might be.
var document = workspace.CurrentSolution.GetDocument(contextDocumentId);
if (document == null)
{
var message = contextDocumentId == null
? $"{nameof(contextDocumentId)} was null."
: $"{nameof(contextDocumentId)} was not null.";
FatalError.ReportWithoutCrash(new InvalidOperationException(
"Could not retrieve document. " + message));
return null;
}
var text = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var textSnapshot = text.FindCorrespondingEditorTextSnapshot();
var textBuffer = textSnapshot?.TextBuffer;
......@@ -30,7 +46,7 @@ internal static class IVsEditorAdaptersFactoryServiceExtensions
{
if (subjectBuffer != null)
{
var adapter = editorAdaptersFactoryService.GetBufferAdapter(subjectBuffer);
var adapter = editorAdaptersFactoryService?.GetBufferAdapter(subjectBuffer);
if (adapter != null)
{
if (ErrorHandler.Succeeded(adapter.GetUndoManager(out var manager)))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册