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

Merge pull request #45978 from jasonmalinowski/nullable-annotate-invisibleeditor

Nullable annotate InvisibleEditor.cs
......@@ -64,10 +64,9 @@ public VisualStudioDefinitionsAndReferencesFactory(SVsServiceProvider servicePro
private string GetSourceLine(string filePath, int lineNumber)
{
using var invisibleEditor = new InvisibleEditor(
_serviceProvider, filePath, hierarchyOpt: null, needsSave: false, needsUndoDisabled: false);
_serviceProvider, filePath, hierarchy: null, needsSave: false, needsUndoDisabled: false);
var vsTextLines = invisibleEditor.VsTextLines;
if (vsTextLines != null &&
vsTextLines.GetLengthOfLine(lineNumber, out var lineLength) == VSConstants.S_OK &&
if (vsTextLines.GetLengthOfLine(lineNumber, out var lineLength) == VSConstants.S_OK &&
vsTextLines.GetLineText(lineNumber, 0, lineNumber, lineLength, out var lineText) == VSConstants.S_OK)
{
return lineText;
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
......@@ -28,7 +30,7 @@ internal partial class InvisibleEditor : ForegroundThreadAffinitizedObject, IInv
private ITextBuffer _buffer;
private IVsTextLines _vsTextLines;
private IVsInvisibleEditor _invisibleEditor;
private OLE.Interop.IOleUndoManager _manager;
private OLE.Interop.IOleUndoManager? _manager;
private readonly bool _needsUndoRestored;
/// <remarks>
......@@ -38,7 +40,7 @@ internal partial class InvisibleEditor : ForegroundThreadAffinitizedObject, IInv
/// <see cref="IVsUIShellOpenDocument4.IsDocumentInAProject2"/>, which performs a much slower query of all
/// projects in the solution.</para>
/// </remarks>
public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVsHierarchy hierarchyOpt, bool needsSave, bool needsUndoDisabled)
public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVsHierarchy? hierarchy, bool needsSave, bool needsUndoDisabled)
: base(serviceProvider.GetMefService<IThreadingContext>(), assertIsForeground: true)
{
_serviceProvider = serviceProvider;
......@@ -46,7 +48,7 @@ public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVsHie
_needsSave = needsSave;
var invisibleEditorManager = (IIntPtrReturningVsInvisibleEditorManager)serviceProvider.GetService(typeof(SVsInvisibleEditorManager));
var vsProject = TryGetProjectOfHierarchy(hierarchyOpt);
var vsProject = TryGetProjectOfHierarchy(hierarchy);
Marshal.ThrowExceptionForHR(invisibleEditorManager.RegisterInvisibleEditor(filePath, vsProject, 0, null, out var invisibleEditorPtr));
try
......@@ -59,14 +61,13 @@ public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVsHie
try
{
var docData = Marshal.GetObjectForIUnknown(docDataPtr);
_vsTextLines = docData as IVsTextLines;
var vsTextBuffer = (IVsTextBuffer)docData;
_vsTextLines = (IVsTextLines)docData;
var editorAdapterFactoryService = serviceProvider.GetMefService<IVsEditorAdaptersFactoryService>();
_buffer = editorAdapterFactoryService.GetDocumentBuffer(vsTextBuffer);
_buffer = editorAdapterFactoryService.GetDocumentBuffer(_vsTextLines);
if (needsUndoDisabled)
{
Marshal.ThrowExceptionForHR(vsTextBuffer.GetUndoManager(out _manager));
Marshal.ThrowExceptionForHR((_manager as IVsUndoState).IsEnabled(out var isEnabled));
Marshal.ThrowExceptionForHR(_vsTextLines.GetUndoManager(out _manager));
Marshal.ThrowExceptionForHR(((IVsUndoState)_manager).IsEnabled(out var isEnabled));
_needsUndoRestored = isEnabled != 0;
if (_needsUndoRestored)
{
......@@ -87,18 +88,18 @@ public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVsHie
}
}
private IVsProject TryGetProjectOfHierarchy(IVsHierarchy hierarchyOpt)
private IVsProject? TryGetProjectOfHierarchy(IVsHierarchy? hierarchy)
{
// The invisible editor manager will fail in cases where the IVsProject passed to it is not consistent with
// the IVsProject known to IVsSolution (e.g. if the object is a wrapper like AbstractHostObject created by
// the CPS-based project system). This method returns an IVsProject instance known to the solution, or null
// if the project could not be determined.
if (hierarchyOpt == null)
if (hierarchy == null)
{
return null;
}
if (!ErrorHandler.Succeeded(hierarchyOpt.GetGuidProperty(
if (!ErrorHandler.Succeeded(hierarchy.GetGuidProperty(
(uint)VSConstants.VSITEMID.Root,
(int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
out var projectId)))
......@@ -143,8 +144,8 @@ public void Dispose()
{
AssertIsForeground();
_buffer = null;
_vsTextLines = null;
_buffer = null!;
_vsTextLines = null!;
try
{
......@@ -177,7 +178,7 @@ public void Dispose()
// Clean up our RCW. This RCW is a unique RCW, so this is actually safe to do!
Marshal.ReleaseComObject(_invisibleEditor);
_invisibleEditor = null;
_invisibleEditor = null!;
GC.SuppressFinalize(this);
}
......
......@@ -82,6 +82,10 @@ internal override IInvisibleEditor OpenInvisibleEditor(DocumentId documentId)
}
}
// Documents in the VisualStudioWorkspace always have file paths since that's how we get files given
// to us from the project system.
Contract.ThrowIfNull(textDocument.FilePath);
return new InvisibleEditor(ServiceProvider.GlobalProvider, textDocument.FilePath, GetHierarchy(documentId.ProjectId), needsSave, needsUndoDisabled);
}
......
......@@ -491,7 +491,7 @@ protected override void ApplyDocumentTextChanged(DocumentId documentId, SourceTe
{
// The edits would get sent by the co-authoring service to the owner.
// The invisible editor saves the file on being disposed, which should get reflected on the owner's side.
using (var invisibleEditor = new InvisibleEditor(_serviceProvider, document.FilePath, hierarchyOpt: null,
using (var invisibleEditor = new InvisibleEditor(_serviceProvider, document.FilePath!, hierarchy: null,
needsSave: true, needsUndoDisabled: false))
{
UpdateText(invisibleEditor.TextBuffer, text);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册