Skip to content

Commit 570c1b5

Browse files
Merge pull request dotnet#62488 from MariaSolOs/contained-language-buffer
Use secondary buffer in contained language file paths
2 parents c6ac9d2 + cc0e1f8 commit 570c1b5

File tree

6 files changed

+61
-64
lines changed

6 files changed

+61
-64
lines changed

src/VisualStudio/Core/Def/ExternalAccess/VSTypeScript/Api/VSTypeScriptContainedLanguageWrapper.cs

+19-31
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
#pragma warning disable CS0618 // Type or member is obsolete
6-
75
using System;
86
using Microsoft.CodeAnalysis;
97
using Microsoft.VisualStudio.ComponentModelHost;
@@ -18,47 +16,40 @@ internal struct VSTypeScriptContainedLanguageWrapper
1816
{
1917
private readonly ContainedLanguage _underlyingObject;
2018

21-
[Obsolete("Remove once TypeScript has stopped using this.", error: true)]
19+
[Obsolete("Use the constructor that omits the IVsHierarchy and uint parameters instead.", error: true)]
2220
public VSTypeScriptContainedLanguageWrapper(
2321
IVsTextBufferCoordinator bufferCoordinator,
2422
IComponentModel componentModel,
25-
AbstractProject project,
23+
VSTypeScriptVisualStudioProjectWrapper project,
2624
IVsHierarchy hierarchy,
2725
uint itemid,
28-
Guid languageServiceGuid)
26+
Guid languageServiceGuid) : this(bufferCoordinator, componentModel, project, languageServiceGuid)
2927
{
30-
var workspace = componentModel.GetService<VisualStudioWorkspace>();
31-
var filePath = ContainedLanguage.GetFilePathFromHierarchyAndItemId(hierarchy, itemid);
32-
33-
_underlyingObject = new ContainedLanguage(
34-
bufferCoordinator,
35-
componentModel,
36-
workspace,
37-
project.Id,
38-
project.VisualStudioProject,
39-
filePath,
40-
languageServiceGuid,
41-
vbHelperFormattingRule: null);
4228
}
4329

30+
[Obsolete("Use the constructor that omits the IVsHierarchy and uint parameters instead.", error: true)]
4431
public VSTypeScriptContainedLanguageWrapper(
4532
IVsTextBufferCoordinator bufferCoordinator,
4633
IComponentModel componentModel,
47-
VSTypeScriptVisualStudioProjectWrapper project,
34+
Workspace workspace,
4835
IVsHierarchy hierarchy,
4936
uint itemid,
50-
Guid languageServiceGuid)
37+
Guid languageServiceGuid) : this(bufferCoordinator, componentModel, workspace, languageServiceGuid)
5138
{
52-
var workspace = componentModel.GetService<VisualStudioWorkspace>();
53-
var filePath = ContainedLanguage.GetFilePathFromHierarchyAndItemId(hierarchy, itemid);
39+
}
5440

41+
public VSTypeScriptContainedLanguageWrapper(
42+
IVsTextBufferCoordinator bufferCoordinator,
43+
IComponentModel componentModel,
44+
VSTypeScriptVisualStudioProjectWrapper project,
45+
Guid languageServiceGuid)
46+
{
5547
_underlyingObject = new ContainedLanguage(
5648
bufferCoordinator,
5749
componentModel,
58-
workspace,
50+
componentModel.GetService<VisualStudioWorkspace>(),
5951
project.Project.Id,
6052
project.Project,
61-
filePath,
6253
languageServiceGuid,
6354
vbHelperFormattingRule: null);
6455
}
@@ -67,28 +58,25 @@ public VSTypeScriptContainedLanguageWrapper(
6758
IVsTextBufferCoordinator bufferCoordinator,
6859
IComponentModel componentModel,
6960
Workspace workspace,
70-
IVsHierarchy hierarchy,
71-
uint itemid,
7261
Guid languageServiceGuid)
7362
{
74-
var filePath = ContainedLanguage.GetFilePathFromHierarchyAndItemId(hierarchy, itemid);
75-
var projectId = ProjectId.CreateNewId($"Project for {filePath}");
76-
workspace.OnProjectAdded(ProjectInfo.Create(projectId, VersionStamp.Default, filePath, string.Empty, InternalLanguageNames.TypeScript));
63+
var projectId = ProjectId.CreateNewId();
7764

7865
_underlyingObject = new ContainedLanguage(
7966
bufferCoordinator,
8067
componentModel,
8168
workspace,
8269
projectId,
8370
null,
84-
filePath,
8571
languageServiceGuid,
8672
vbHelperFormattingRule: null);
73+
74+
var filePath = _underlyingObject.GetFilePathFromBuffers();
75+
workspace.OnProjectAdded(ProjectInfo.Create(projectId, VersionStamp.Default, filePath, string.Empty, InternalLanguageNames.TypeScript));
8776
}
8877

8978
public bool IsDefault => _underlyingObject == null;
9079

91-
public void DisconnectHost()
92-
=> _underlyingObject.SetHost(null);
80+
public void DisconnectHost() => _underlyingObject.SetHost(null);
9381
}
9482
}

src/VisualStudio/Core/Def/LanguageService/AbstractLanguageService`2.cs

-3
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,12 @@ protected virtual IVsContainedLanguage CreateContainedLanguage(
281281
IVsTextBufferCoordinator bufferCoordinator, VisualStudioProject project,
282282
IVsHierarchy hierarchy, uint itemid)
283283
{
284-
var filePath = ContainedLanguage.GetFilePathFromHierarchyAndItemId(hierarchy, itemid);
285-
286284
return new ContainedLanguage(
287285
bufferCoordinator,
288286
this.Package.ComponentModel,
289287
this.Workspace,
290288
project.Id,
291289
project,
292-
filePath,
293290
this.LanguageServiceId);
294291
}
295292
}

src/VisualStudio/Core/Def/Venus/ContainedLanguage.cs

+29-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Diagnostics;
76
using System.Runtime.InteropServices;
87
using Microsoft.CodeAnalysis;
98
using Microsoft.CodeAnalysis.Diagnostics;
@@ -15,7 +14,6 @@
1514
using Microsoft.VisualStudio.ComponentModelHost;
1615
using Microsoft.VisualStudio.Editor;
1716
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
18-
using Microsoft.VisualStudio.Shell.Interop;
1917
using Microsoft.VisualStudio.Text;
2018
using Microsoft.VisualStudio.Text.Tagging;
2119
using Microsoft.VisualStudio.TextManager.Interop;
@@ -61,35 +59,18 @@ internal partial class ContainedLanguage
6159
/// </summary>
6260
public ITextBuffer DataBuffer { get; }
6361

64-
// Set when a TextViewFIlter is set. We hold onto this to keep our TagSource objects alive even if Venus
62+
// Set when a TextViewFilter is set. We hold onto this to keep our TagSource objects alive even if Venus
6563
// disconnects the subject buffer from the view temporarily (which they do frequently). Otherwise, we have to
6664
// re-compute all of the tag data when they re-connect it, and this causes issues like classification
6765
// flickering.
6866
private readonly ITagAggregator<ITag> _bufferTagAggregator;
6967

70-
public static string GetFilePathFromHierarchyAndItemId(IVsHierarchy hierarchy, uint itemid)
71-
{
72-
if (!ErrorHandler.Succeeded(((IVsProject)hierarchy).GetMkDocument(itemid, out var filePath)))
73-
{
74-
// we couldn't look up the document moniker from an hierarchy for an itemid.
75-
// Since we only use this moniker as a key, we could fall back to something else, like the document name.
76-
Debug.Assert(false, "Could not get the document moniker for an item from its hierarchy.");
77-
if (!hierarchy.TryGetItemName(itemid, out filePath!))
78-
{
79-
FatalError.ReportAndPropagate(new InvalidOperationException("Failed to get document moniker for a contained document"));
80-
}
81-
}
82-
83-
return filePath;
84-
}
85-
8668
internal ContainedLanguage(
8769
IVsTextBufferCoordinator bufferCoordinator,
8870
IComponentModel componentModel,
8971
Workspace workspace,
9072
ProjectId projectId,
9173
VisualStudioProject? project,
92-
string filePath,
9374
Guid languageServiceGuid,
9475
AbstractFormattingRule? vbHelperFormattingRule = null)
9576
{
@@ -115,13 +96,16 @@ internal ContainedLanguage(
11596
var bufferTagAggregatorFactory = ComponentModel.GetService<IBufferTagAggregatorFactoryService>();
11697
_bufferTagAggregator = bufferTagAggregatorFactory.CreateTagAggregator<ITag>(SubjectBuffer);
11798

99+
var filePath = GetFilePathFromBuffers();
118100
DocumentId documentId;
119101

120102
if (this.Project != null)
121103
{
122104
documentId = this.Project.AddSourceTextContainer(
123-
SubjectBuffer.AsTextContainer(), filePath,
124-
sourceCodeKind: SourceCodeKind.Regular, folders: default,
105+
SubjectBuffer.AsTextContainer(),
106+
filePath,
107+
sourceCodeKind: SourceCodeKind.Regular,
108+
folders: default,
125109
designTimeOnly: true,
126110
documentServiceProvider: new ContainedDocument.DocumentServiceProvider(DataBuffer));
127111
}
@@ -135,14 +119,14 @@ internal ContainedLanguage(
135119
}
136120

137121
this.ContainedDocument = new ContainedDocument(
138-
componentModel.GetService<IThreadingContext>(),
122+
ComponentModel.GetService<IThreadingContext>(),
139123
documentId,
140124
subjectBuffer: SubjectBuffer,
141125
dataBuffer: DataBuffer,
142-
bufferCoordinator,
126+
BufferCoordinator,
143127
Workspace,
144-
project,
145-
componentModel,
128+
Project,
129+
ComponentModel,
146130
vbHelperFormattingRule);
147131

148132
// TODO: Can contained documents be linked or shared?
@@ -179,5 +163,24 @@ private void OnDataBufferChanged(object sender, TextContentChangedEventArgs e)
179163
// when primary buffer has changed to update diagnostic positions.
180164
_diagnosticAnalyzerService.Reanalyze(this.Workspace, documentIds: SpecializedCollections.SingletonEnumerable(this.ContainedDocument.Id));
181165
}
166+
167+
public string GetFilePathFromBuffers()
168+
{
169+
var textDocumentFactoryService = ComponentModel.GetService<ITextDocumentFactoryService>();
170+
171+
// Try to get the file path from the secondary buffer
172+
if (!textDocumentFactoryService.TryGetTextDocument(SubjectBuffer, out var document))
173+
{
174+
// Fallback to the primary buffer
175+
textDocumentFactoryService.TryGetTextDocument(DataBuffer, out document);
176+
}
177+
178+
if (document == null)
179+
{
180+
FatalError.ReportAndPropagate(new InvalidOperationException("Failed to get an ITextDocument for a contained document"));
181+
}
182+
183+
return document?.FilePath ?? string.Empty;
184+
}
182185
}
183186
}

src/VisualStudio/LiveShare/Impl/Client/Razor/CSharpLspContainedLanguageProvider.cs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public IVsContainedLanguage GetLanguage(string filePath, IVsTextBufferCoordinato
4747
_razorProjectFactory.Workspace,
4848
projectId,
4949
project: null,
50-
filePath,
5150
Guids.CSharpLanguageServiceId);
5251
}
5352
}

src/VisualStudio/VisualBasic/Impl/LanguageService/VisualBasicLanguageService.vb

-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic
8282
bufferCoordinator,
8383
Me.Package.ComponentModel,
8484
project,
85-
hierarchy,
86-
itemid,
8785
Me.LanguageServiceId)
8886
End Function
8987
End Class

src/VisualStudio/VisualBasic/Impl/Venus/VisualBasicContainedLanguage.vb

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,32 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.Venus
2323
Inherits ContainedLanguage
2424
Implements IVsContainedLanguageStaticEventBinding
2525

26+
<Obsolete("Use the constructor that omits the IVsHierarchy and UInteger parameters instead.", True)>
2627
Public Sub New(bufferCoordinator As IVsTextBufferCoordinator,
2728
componentModel As IComponentModel,
2829
project As VisualStudioProject,
2930
hierarchy As IVsHierarchy,
3031
itemid As UInteger,
3132
languageServiceGuid As Guid)
3233

34+
Me.New(
35+
bufferCoordinator,
36+
componentModel,
37+
project,
38+
languageServiceGuid)
39+
End Sub
40+
41+
Public Sub New(bufferCoordinator As IVsTextBufferCoordinator,
42+
componentModel As IComponentModel,
43+
project As VisualStudioProject,
44+
languageServiceGuid As Guid)
45+
3346
MyBase.New(
3447
bufferCoordinator,
3548
componentModel,
3649
componentModel.GetService(Of VisualStudioWorkspace)(),
3750
project.Id,
3851
project,
39-
ContainedLanguage.GetFilePathFromHierarchyAndItemId(hierarchy, itemid),
4052
languageServiceGuid,
4153
VisualBasicHelperFormattingRule.Instance)
4254
End Sub

0 commit comments

Comments
 (0)