Skip to content

Commit 1d0c2af

Browse files
committed
Avoid rooting document instances in NavigateToSearchResult
1 parent b7a4088 commit 1d0c2af

21 files changed

Lines changed: 132 additions & 59 deletions

File tree

src/EditorFeatures/CSharpTest/NavigateTo/NavigateToSearcherTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public TestNavigateToSearchResult(TestWorkspace workspace, TextSpan sourceSpan)
270270
_sourceSpan = sourceSpan;
271271
}
272272

273-
public Document Document => _workspace.CurrentSolution.Projects.Single().Documents.Single();
273+
public INavigableItem.NavigableDocument Document => INavigableItem.NavigableDocument.FromDocument(_workspace.CurrentSolution.Projects.Single().Documents.Single());
274274
public TextSpan SourceSpan => _sourceSpan;
275275

276276
public string AdditionalInformation => throw new NotImplementedException();

src/EditorFeatures/Core.Wpf/NavigateTo/DefaultNavigateToPreviewService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
#nullable disable
66

7+
using Microsoft.CodeAnalysis.Navigation;
78
using Microsoft.VisualStudio.Language.NavigateTo.Interfaces;
9+
using Microsoft.VisualStudio.Shell.Interop;
810

911
namespace Microsoft.CodeAnalysis.Editor.Implementation.NavigateTo
1012
{
1113
internal sealed class DefaultNavigateToPreviewService : INavigateToPreviewService
1214
{
13-
public int GetProvisionalViewingStatus(Document document)
14-
=> 0;
15+
public __VSPROVISIONALVIEWINGSTATUS GetProvisionalViewingStatus(INavigableItem.NavigableDocument document)
16+
=> __VSPROVISIONALVIEWINGSTATUS.PVS_Disabled;
1517

1618
public bool CanPreview(Document document)
1719
=> true;

src/EditorFeatures/Core.Wpf/NavigateTo/INavigateToPreviewService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
#nullable disable
66

77
using Microsoft.CodeAnalysis.Host;
8+
using Microsoft.CodeAnalysis.Navigation;
89
using Microsoft.VisualStudio.Language.NavigateTo.Interfaces;
10+
using Microsoft.VisualStudio.Shell.Interop;
911

1012
namespace Microsoft.CodeAnalysis.Editor.Implementation.NavigateTo
1113
{
1214
internal interface INavigateToPreviewService : IWorkspaceService
1315
{
14-
int GetProvisionalViewingStatus(Document document);
16+
__VSPROVISIONALVIEWINGSTATUS GetProvisionalViewingStatus(INavigableItem.NavigableDocument document);
1517
bool CanPreview(Document document);
1618
void PreviewItem(INavigateToItemDisplay itemDisplay);
1719
}

src/EditorFeatures/Core.Wpf/NavigateTo/NavigateToItemDisplay.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
1717
using Microsoft.VisualStudio.Imaging.Interop;
1818
using Microsoft.VisualStudio.Language.NavigateTo.Interfaces;
19+
using Microsoft.VisualStudio.Shell.Interop;
1920
using Microsoft.VisualStudio.Text;
2021
using Microsoft.VisualStudio.Utilities;
2122
using Roslyn.Utilities;
@@ -63,7 +64,7 @@ private ReadOnlyCollection<DescriptionItem> CreateDescriptionItems()
6364
return new List<DescriptionItem>().AsReadOnly();
6465
}
6566

66-
var sourceText = document.GetTextSynchronously(CancellationToken.None);
67+
var sourceText = document.GetTextSynchronously(document.Workspace.CurrentSolution, CancellationToken.None);
6768
var span = NavigateToUtilities.GetBoundedSpan(_searchResult.NavigableItem, sourceText);
6869

6970
var items = new List<DescriptionItem>
@@ -111,13 +112,13 @@ public int GetProvisionalViewingStatus()
111112
var document = _searchResult.NavigableItem.Document;
112113
if (document == null)
113114
{
114-
return 0;
115+
return (int)__VSPROVISIONALVIEWINGSTATUS.PVS_Disabled;
115116
}
116117

117-
var workspace = document.Project.Solution.Workspace;
118+
var workspace = document.Workspace;
118119
var previewService = workspace.Services.GetService<INavigateToPreviewService>();
119120

120-
return previewService.GetProvisionalViewingStatus(document);
121+
return (int)previewService.GetProvisionalViewingStatus(document);
121122
}
122123

123124
public void PreviewItem()
@@ -128,7 +129,7 @@ public void PreviewItem()
128129
return;
129130
}
130131

131-
var workspace = document.Project.Solution.Workspace;
132+
var workspace = document.Workspace;
132133
var previewService = workspace.Services.GetService<INavigateToPreviewService>();
133134

134135
previewService.PreviewItem(this);

src/EditorFeatures/Core.Wpf/Peek/PeekableItemSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static async IAsyncEnumerable<IPeekableItem> GetPeekableItemsForNavigabl
136136
if (await navigationService.CanNavigateToPositionAsync(
137137
workspace, document.Id, item.SourceSpan.Start, cancellationToken).ConfigureAwait(false))
138138
{
139-
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
139+
var text = await document.GetTextAsync(project.Solution, cancellationToken).ConfigureAwait(false);
140140
var linePositionSpan = text.Lines.GetLinePositionSpan(item.SourceSpan);
141141
if (document.FilePath != null)
142142
{

src/EditorFeatures/Core/CodeDefinitionWindow/DefinitionContextTracker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ internal async Task<ImmutableArray<CodeDefinitionWindowLocation>> GetContextFrom
171171
{
172172
if (await navigationService.CanNavigateToSpanAsync(workspace, item.Document.Id, item.SourceSpan, cancellationToken).ConfigureAwait(false))
173173
{
174-
var text = await item.Document.GetTextAsync(cancellationToken).ConfigureAwait(false);
174+
var text = await item.Document.GetTextAsync(document.Project.Solution, cancellationToken).ConfigureAwait(false);
175175
var linePositionSpan = text.Lines.GetLinePositionSpan(item.SourceSpan);
176176

177177
if (item.Document.FilePath != null)

src/EditorFeatures/Core/NavigateTo/NavigateToHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static async Task NavigateToAsync(
3535
if (document == null)
3636
return;
3737

38-
var workspace = document.Project.Solution.Workspace;
38+
var workspace = document.Workspace;
3939
var navigationService = workspace.Services.GetRequiredService<IDocumentNavigationService>();
4040

4141
// Document tabs opened by NavigateTo are carefully created as preview or regular tabs

src/EditorFeatures/Test2/CodeDefinitionWindow/CrossLanguageCodeDefinitionWindowTests.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ Namespace Microsoft.CodeAnalysis.Editor.CodeDefinitionWindow.UnitTests
2222
Private Class FakeNavigableItem
2323
Implements INavigableItem
2424

25-
Private ReadOnly _document As Document
25+
Private ReadOnly _document As INavigableItem.NavigableDocument
2626

2727
Public Sub New(document As Document)
28-
_document = document
28+
_document = INavigableItem.NavigableDocument.FromDocument(document)
2929
End Sub
3030

3131
Public ReadOnly Property ChildItems As ImmutableArray(Of INavigableItem) Implements INavigableItem.ChildItems
@@ -46,7 +46,7 @@ Namespace Microsoft.CodeAnalysis.Editor.CodeDefinitionWindow.UnitTests
4646
End Get
4747
End Property
4848

49-
Public ReadOnly Property Document As Document Implements INavigableItem.Document
49+
Public ReadOnly Property Document As INavigableItem.NavigableDocument Implements INavigableItem.Document
5050
Get
5151
Return _document
5252
End Get

src/Features/Core/Portable/ExternalAccess/VSTypeScript/VSTypeScriptNavigableItemWrapper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript
1212
internal sealed class VSTypeScriptNavigableItemWrapper : INavigableItem
1313
{
1414
private readonly IVSTypeScriptNavigableItem _navigableItem;
15+
private readonly INavigableItem.NavigableDocument _navigableDocument;
1516

1617
public VSTypeScriptNavigableItemWrapper(IVSTypeScriptNavigableItem navigableItem)
1718
{
1819
_navigableItem = navigableItem;
20+
_navigableDocument = INavigableItem.NavigableDocument.FromDocument(navigableItem.Document);
1921
}
2022

2123
public Glyph Glyph => _navigableItem.Glyph;
@@ -26,7 +28,7 @@ public VSTypeScriptNavigableItemWrapper(IVSTypeScriptNavigableItem navigableItem
2628

2729
public bool IsImplicitlyDeclared => _navigableItem.IsImplicitlyDeclared;
2830

29-
public Document Document => _navigableItem.Document;
31+
public INavigableItem.NavigableDocument Document => _navigableDocument;
3032

3133
public TextSpan SourceSpan => _navigableItem.SourceSpan;
3234

src/Features/Core/Portable/NavigateTo/RoslynNavigateToItem.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
8-
using System.Diagnostics.CodeAnalysis;
98
using System.IO;
109
using System.Runtime.Serialization;
1110
using System.Threading;
@@ -16,7 +15,6 @@
1615
using Microsoft.CodeAnalysis.PooledObjects;
1716
using Microsoft.CodeAnalysis.Shared.Extensions;
1817
using Microsoft.CodeAnalysis.Text;
19-
using Roslyn.Utilities;
2018

2119
namespace Microsoft.CodeAnalysis.NavigateTo
2220
{
@@ -108,7 +106,7 @@ private class NavigateToSearchResult : INavigateToSearchResult, INavigableItem
108106
/// <summary>
109107
/// The <see cref="Document"/> that <see cref="_item"/> is contained within.
110108
/// </summary>
111-
private readonly Document _itemDocument;
109+
private readonly INavigableItem.NavigableDocument _itemDocument;
112110

113111
/// <summary>
114112
/// The document the user was editing when they invoked the navigate-to operation.
@@ -124,35 +122,35 @@ public NavigateToSearchResult(
124122
Document? activeDocument)
125123
{
126124
_item = item;
127-
_itemDocument = itemDocument;
125+
_itemDocument = INavigableItem.NavigableDocument.FromDocument(itemDocument);
128126
if (activeDocument is not null)
129127
_activeDocument = (activeDocument.Id, activeDocument.Folders);
130128

131-
_additionalInformation = ComputeAdditionalInformation();
129+
_additionalInformation = ComputeAdditionalInformation(in item, itemDocument);
132130
_secondarySort = new Lazy<string>(ComputeSecondarySort);
133131
}
134132

135-
private string ComputeAdditionalInformation()
133+
private static string ComputeAdditionalInformation(in RoslynNavigateToItem item, Document itemDocument)
136134
{
137135
// For partial types, state what file they're in so the user can disambiguate the results.
138-
var combinedProjectName = ComputeCombinedProjectName();
139-
return (_item.DeclaredSymbolInfo.IsPartial, IsNonNestedNamedType()) switch
136+
var combinedProjectName = ComputeCombinedProjectName(in item, itemDocument);
137+
return (item.DeclaredSymbolInfo.IsPartial, IsNonNestedNamedType(in item)) switch
140138
{
141-
(true, true) => string.Format(FeaturesResources._0_dash_1, _itemDocument.Name, combinedProjectName),
142-
(true, false) => string.Format(FeaturesResources.in_0_1_2, _item.DeclaredSymbolInfo.ContainerDisplayName, _itemDocument.Name, combinedProjectName),
139+
(true, true) => string.Format(FeaturesResources._0_dash_1, itemDocument.Name, combinedProjectName),
140+
(true, false) => string.Format(FeaturesResources.in_0_1_2, item.DeclaredSymbolInfo.ContainerDisplayName, itemDocument.Name, combinedProjectName),
143141
(false, true) => string.Format(FeaturesResources.project_0, combinedProjectName),
144-
(false, false) => string.Format(FeaturesResources.in_0_project_1, _item.DeclaredSymbolInfo.ContainerDisplayName, combinedProjectName),
142+
(false, false) => string.Format(FeaturesResources.in_0_project_1, item.DeclaredSymbolInfo.ContainerDisplayName, combinedProjectName),
145143
};
146144
}
147145

148-
private string ComputeCombinedProjectName()
146+
private static string ComputeCombinedProjectName(in RoslynNavigateToItem item, Document itemDocument)
149147
{
150148
// If there aren't any additional matches in other projects, we don't need to merge anything.
151-
if (_item.AdditionalMatchingProjects.Length > 0)
149+
if (item.AdditionalMatchingProjects.Length > 0)
152150
{
153151
// First get the simple project name and flavor for the actual project we got a hit in. If we can't
154152
// figure this out, we can't create a merged name.
155-
var firstProject = _itemDocument.Project;
153+
var firstProject = itemDocument.Project;
156154
var (firstProjectName, firstProjectFlavor) = firstProject.State.NameAndFlavor;
157155

158156
if (firstProjectName != null)
@@ -165,7 +163,7 @@ private string ComputeCombinedProjectName()
165163
// Now, do the same for the other projects where we had a match. As above, if we can't figure out the
166164
// simple name/flavor, or if the simple project name doesn't match the simple project name we started
167165
// with then we can't merge these.
168-
foreach (var additionalProjectId in _item.AdditionalMatchingProjects)
166+
foreach (var additionalProjectId in item.AdditionalMatchingProjects)
169167
{
170168
var additionalProject = solution.GetRequiredProject(additionalProjectId);
171169
var (projectName, projectFlavor) = additionalProject.State.NameAndFlavor;
@@ -181,17 +179,17 @@ private string ComputeCombinedProjectName()
181179
}
182180

183181
// Couldn't compute a merged project name (or only had one project). Just return the name of hte project itself.
184-
return _itemDocument.Project.Name;
182+
return itemDocument.Project.Name;
185183
}
186184

187185
string INavigateToSearchResult.AdditionalInformation => _additionalInformation;
188186

189-
private bool IsNonNestedNamedType()
190-
=> !_item.DeclaredSymbolInfo.IsNestedType && IsNamedType();
187+
private static bool IsNonNestedNamedType(in RoslynNavigateToItem item)
188+
=> !item.DeclaredSymbolInfo.IsNestedType && IsNamedType(in item);
191189

192-
private bool IsNamedType()
190+
private static bool IsNamedType(in RoslynNavigateToItem item)
193191
{
194-
switch (_item.DeclaredSymbolInfo.Kind)
192+
switch (item.DeclaredSymbolInfo.Kind)
195193
{
196194
case DeclaredSymbolInfoKind.Class:
197195
case DeclaredSymbolInfoKind.Record:
@@ -356,7 +354,7 @@ ImmutableArray<TaggedText> INavigableItem.DisplayTaggedParts
356354
/// </summary>
357355
bool INavigableItem.IsImplicitlyDeclared => false;
358356

359-
Document INavigableItem.Document => _itemDocument;
357+
INavigableItem.NavigableDocument INavigableItem.Document => _itemDocument;
360358

361359
TextSpan INavigableItem.SourceSpan => _item.DeclaredSymbolInfo.Span;
362360

0 commit comments

Comments
 (0)