Skip to content

Commit 2bde9dc

Browse files
Merge pull request #45978 from jasonmalinowski/nullable-annotate-invisibleeditor
Nullable annotate InvisibleEditor.cs
2 parents 39ed7eb + fb34e59 commit 2bde9dc

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

src/VisualStudio/Core/Def/Implementation/FindReferences/VisualStudioDefinitionsAndReferencesFactory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ private ImmutableArray<TaggedText> GetDisplayParts(
6464
private string GetSourceLine(string filePath, int lineNumber)
6565
{
6666
using var invisibleEditor = new InvisibleEditor(
67-
_serviceProvider, filePath, hierarchyOpt: null, needsSave: false, needsUndoDisabled: false);
67+
_serviceProvider, filePath, hierarchy: null, needsSave: false, needsUndoDisabled: false);
6868
var vsTextLines = invisibleEditor.VsTextLines;
69-
if (vsTextLines != null &&
70-
vsTextLines.GetLengthOfLine(lineNumber, out var lineLength) == VSConstants.S_OK &&
69+
if (vsTextLines.GetLengthOfLine(lineNumber, out var lineLength) == VSConstants.S_OK &&
7170
vsTextLines.GetLineText(lineNumber, 0, lineNumber, lineLength, out var lineText) == VSConstants.S_OK)
7271
{
7372
return lineText;

src/VisualStudio/Core/Def/Implementation/ProjectSystem/InvisibleEditor.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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+
57
using System;
68
using System.Diagnostics;
79
using 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
}

src/VisualStudio/Core/Impl/RoslynVisualStudioWorkspace.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ internal override IInvisibleEditor OpenInvisibleEditor(DocumentId documentId)
8282
}
8383
}
8484

85+
// Documents in the VisualStudioWorkspace always have file paths since that's how we get files given
86+
// to us from the project system.
87+
Contract.ThrowIfNull(textDocument.FilePath);
88+
8589
return new InvisibleEditor(ServiceProvider.GlobalProvider, textDocument.FilePath, GetHierarchy(documentId.ProjectId), needsSave, needsUndoDisabled);
8690
}
8791

src/VisualStudio/LiveShare/Impl/Client/RemoteLanguageServiceWorkspace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ protected override void ApplyDocumentTextChanged(DocumentId documentId, SourceTe
491491
{
492492
// The edits would get sent by the co-authoring service to the owner.
493493
// The invisible editor saves the file on being disposed, which should get reflected on the owner's side.
494-
using (var invisibleEditor = new InvisibleEditor(_serviceProvider, document.FilePath, hierarchyOpt: null,
494+
using (var invisibleEditor = new InvisibleEditor(_serviceProvider, document.FilePath!, hierarchy: null,
495495
needsSave: true, needsUndoDisabled: false))
496496
{
497497
UpdateText(invisibleEditor.TextBuffer, text);

0 commit comments

Comments
 (0)