22// The .NET Foundation licenses this file to you under the MIT license.
33// See the LICENSE file in the project root for more information.
44
5+ #nullable enable
6+
57using System ;
68using System . Diagnostics ;
79using System . Runtime . InteropServices ;
@@ -28,7 +30,7 @@ internal partial class InvisibleEditor : ForegroundThreadAffinitizedObject, IInv
2830 private ITextBuffer _buffer ;
2931 private IVsTextLines _vsTextLines ;
3032 private IVsInvisibleEditor _invisibleEditor ;
31- private OLE . Interop . IOleUndoManager _manager ;
33+ private OLE . Interop . IOleUndoManager ? _manager ;
3234 private readonly bool _needsUndoRestored ;
3335
3436 /// <remarks>
@@ -38,15 +40,15 @@ internal partial class InvisibleEditor : ForegroundThreadAffinitizedObject, IInv
3840 /// <see cref="IVsUIShellOpenDocument4.IsDocumentInAProject2"/>, which performs a much slower query of all
3941 /// projects in the solution.</para>
4042 /// </remarks>
41- public InvisibleEditor ( IServiceProvider serviceProvider , string filePath , IVsHierarchy hierarchyOpt , bool needsSave , bool needsUndoDisabled )
43+ public InvisibleEditor ( IServiceProvider serviceProvider , string filePath , IVsHierarchy ? hierarchy , bool needsSave , bool needsUndoDisabled )
4244 : base ( serviceProvider . GetMefService < IThreadingContext > ( ) , assertIsForeground : true )
4345 {
4446 _serviceProvider = serviceProvider ;
4547 _filePath = filePath ;
4648 _needsSave = needsSave ;
4749
4850 var invisibleEditorManager = ( IIntPtrReturningVsInvisibleEditorManager ) serviceProvider . GetService ( typeof ( SVsInvisibleEditorManager ) ) ;
49- var vsProject = TryGetProjectOfHierarchy ( hierarchyOpt ) ;
51+ var vsProject = TryGetProjectOfHierarchy ( hierarchy ) ;
5052 Marshal . ThrowExceptionForHR ( invisibleEditorManager . RegisterInvisibleEditor ( filePath , vsProject , 0 , null , out var invisibleEditorPtr ) ) ;
5153
5254 try
@@ -59,14 +61,13 @@ public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVsHie
5961 try
6062 {
6163 var docData = Marshal . GetObjectForIUnknown ( docDataPtr ) ;
62- _vsTextLines = docData as IVsTextLines ;
63- var vsTextBuffer = ( IVsTextBuffer ) docData ;
64+ _vsTextLines = ( IVsTextLines ) docData ;
6465 var editorAdapterFactoryService = serviceProvider . GetMefService < IVsEditorAdaptersFactoryService > ( ) ;
65- _buffer = editorAdapterFactoryService . GetDocumentBuffer ( vsTextBuffer ) ;
66+ _buffer = editorAdapterFactoryService . GetDocumentBuffer ( _vsTextLines ) ;
6667 if ( needsUndoDisabled )
6768 {
68- Marshal . ThrowExceptionForHR ( vsTextBuffer . GetUndoManager ( out _manager ) ) ;
69- Marshal . ThrowExceptionForHR ( ( _manager as IVsUndoState ) . IsEnabled ( out var isEnabled ) ) ;
69+ Marshal . ThrowExceptionForHR ( _vsTextLines . GetUndoManager ( out _manager ) ) ;
70+ Marshal . ThrowExceptionForHR ( ( ( IVsUndoState ) _manager ) . IsEnabled ( out var isEnabled ) ) ;
7071 _needsUndoRestored = isEnabled != 0 ;
7172 if ( _needsUndoRestored )
7273 {
@@ -87,18 +88,18 @@ public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVsHie
8788 }
8889 }
8990
90- private IVsProject TryGetProjectOfHierarchy ( IVsHierarchy hierarchyOpt )
91+ private IVsProject ? TryGetProjectOfHierarchy ( IVsHierarchy ? hierarchy )
9192 {
9293 // The invisible editor manager will fail in cases where the IVsProject passed to it is not consistent with
9394 // the IVsProject known to IVsSolution (e.g. if the object is a wrapper like AbstractHostObject created by
9495 // the CPS-based project system). This method returns an IVsProject instance known to the solution, or null
9596 // if the project could not be determined.
96- if ( hierarchyOpt == null )
97+ if ( hierarchy == null )
9798 {
9899 return null ;
99100 }
100101
101- if ( ! ErrorHandler . Succeeded ( hierarchyOpt . GetGuidProperty (
102+ if ( ! ErrorHandler . Succeeded ( hierarchy . GetGuidProperty (
102103 ( uint ) VSConstants . VSITEMID . Root ,
103104 ( int ) __VSHPROPID . VSHPROPID_ProjectIDGuid ,
104105 out var projectId ) ) )
@@ -143,8 +144,8 @@ public void Dispose()
143144 {
144145 AssertIsForeground ( ) ;
145146
146- _buffer = null ;
147- _vsTextLines = null ;
147+ _buffer = null ! ;
148+ _vsTextLines = null ! ;
148149
149150 try
150151 {
@@ -177,7 +178,7 @@ public void Dispose()
177178
178179 // Clean up our RCW. This RCW is a unique RCW, so this is actually safe to do!
179180 Marshal . ReleaseComObject ( _invisibleEditor ) ;
180- _invisibleEditor = null ;
181+ _invisibleEditor = null ! ;
181182
182183 GC . SuppressFinalize ( this ) ;
183184 }
0 commit comments