Skip to content

Commit 02db999

Browse files
Fix layering of semantic model reuse service (#79086)
2 parents 637ee1e + 4993cab commit 02db999

File tree

13 files changed

+45
-47
lines changed

13 files changed

+45
-47
lines changed

src/CodeStyle/Core/CodeFixes/LanguageServices/SemanticModelWorkspaceService/SemanticModelWorkspaceServiceFactory.SemanticModelWorkspaceService.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Workspaces/SharedUtilitiesAndExtensions/Compiler/VisualBasic/Extensions/SyntaxNodeExtensions.vb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,5 +1295,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
12951295
Return node
12961296
End If
12971297
End Function
1298+
1299+
Friend Function GetAccessorList(declaration As SyntaxNode) As SyntaxList(Of AccessorBlockSyntax)
1300+
Select Case declaration.Kind
1301+
Case SyntaxKind.PropertyBlock
1302+
Return DirectCast(declaration, PropertyBlockSyntax).Accessors
1303+
Case SyntaxKind.EventBlock
1304+
Return DirectCast(declaration, EventBlockSyntax).Accessors
1305+
Case Else
1306+
Return Nothing
1307+
End Select
1308+
End Function
12981309
End Module
12991310
End Namespace

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CSharpWorkspaceExtensions.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<Compile Include="$(MSBuildThisFileDirectory)LanguageServices\CSharpReplaceDiscardDeclarationsWithAssignmentsService.cs" />
8787
<Compile Include="$(MSBuildThisFileDirectory)LanguageServices\InitializeParameter\InitializeParameterHelpers.cs" />
8888
<Compile Include="$(MSBuildThisFileDirectory)Lightup\NullableSyntaxAnnotationEx.cs" />
89+
<Compile Include="$(MSBuildThisFileDirectory)SemanticModelReuse\CSharpSemanticModelReuseLanguageService.cs" />
8990
<Compile Include="$(MSBuildThisFileDirectory)Utilities\NameSyntaxIterator.cs" />
9091
<Compile Include="$(MSBuildThisFileDirectory)Utilities\CSharpSimplificationHelpers.cs" />
9192
<Compile Include="$(MSBuildThisFileDirectory)Utilities\NullableHelpers\NullableExtensions.cs" />

src/Workspaces/Core/Portable/SemanticModelReuse/AbstractSemanticModelReuseLanguageService.cs renamed to src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/SemanticModelReuse/AbstractSemanticModelReuseLanguageService.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
using System.Diagnostics;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Microsoft.CodeAnalysis.Internal.Log;
109
using Microsoft.CodeAnalysis.LanguageService;
1110
using Microsoft.CodeAnalysis.PooledObjects;
1211

12+
#if WORKSPACE
13+
using Microsoft.CodeAnalysis.Internal.Log;
14+
#endif
15+
1316
namespace Microsoft.CodeAnalysis.SemanticModelReuse;
1417

1518
internal abstract class AbstractSemanticModelReuseLanguageService<
@@ -20,7 +23,9 @@ internal abstract class AbstractSemanticModelReuseLanguageService<
2023
where TBasePropertyDeclarationSyntax : TMemberDeclarationSyntax
2124
where TAccessorDeclarationSyntax : SyntaxNode
2225
{
26+
#if WORKSPACE
2327
private readonly CountLogAggregator<bool> _logAggregator = new();
28+
#endif
2429

2530
protected abstract ISyntaxFacts SyntaxFacts { get; }
2631

@@ -32,11 +37,13 @@ internal abstract class AbstractSemanticModelReuseLanguageService<
3237

3338
public void Dispose()
3439
{
40+
#if WORKSPACE
3541
Logger.Log(FunctionId.SemanticModelReuseLanguageService_TryGetSpeculativeSemanticModelAsync_Equivalent, KeyValueLogMessage.Create(static (m, _logAggregator) =>
3642
{
3743
foreach (var kv in _logAggregator)
3844
m[kv.Key.ToString()] = kv.Value.GetCount();
3945
}, _logAggregator));
46+
#endif
4047
}
4148

4249
public async Task<SemanticModel?> TryGetSpeculativeSemanticModelAsync(SemanticModel previousSemanticModel, SyntaxNode currentBodyNode, CancellationToken cancellationToken)
@@ -48,7 +55,10 @@ public void Dispose()
4855
// then something very bad happened as we did that document.Project.GetDependentSemanticVersionAsync was
4956
// still the same. Log information so we can be alerted if this isn't being as successful as we expect.
5057
var isEquivalentTo = previousSyntaxTree.IsEquivalentTo(currentSyntaxTree, topLevel: true);
58+
59+
#if WORKSPACE
5160
_logAggregator.IncreaseCount(isEquivalentTo);
61+
#endif
5262

5363
if (!isEquivalentTo)
5464
return null;
@@ -71,13 +81,13 @@ public void Dispose()
7181
// Given that the common use case for us is continuously editing/typing inside a method body, we believe we can be conservative
7282
// in creating speculative model with those kind of trivia change, by requiring the method body block not to shift position,
7383
// w/o sacrificing performance in those common scenarios.
74-
if (previousBodyNode.SpanStart != currentBodyNode.SpanStart)
84+
if (previousBodyNode?.SpanStart != currentBodyNode.SpanStart)
7585
return null;
7686

7787
return TryGetSpeculativeSemanticModelWorker(previousSemanticModel, previousBodyNode, currentBodyNode);
7888
}
7989

80-
protected SyntaxNode GetPreviousBodyNode(SyntaxNode previousRoot, SyntaxNode currentRoot, SyntaxNode currentBodyNode)
90+
protected SyntaxNode? GetPreviousBodyNode(SyntaxNode previousRoot, SyntaxNode currentRoot, SyntaxNode currentBodyNode)
8191
{
8292
if (currentBodyNode is TAccessorDeclarationSyntax currentAccessor)
8393
{

src/Workspaces/Core/Portable/SemanticModelReuse/SemanticModelWorkspaceServiceFactory.SemanticModelWorkspaceService.cs renamed to src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/SemanticModelReuse/SemanticModelWorkspaceServiceFactory.SemanticModelWorkspaceService.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ private sealed class SemanticModelReuseWorkspaceService : ISemanticModelReuseWor
6161
public SemanticModelReuseWorkspaceService(Workspace workspace)
6262
{
6363
_workspace = workspace;
64+
65+
#pragma warning disable RS0030 // Do not use banned APIs
66+
#if WORKSPACE
6467
_workspace.RegisterWorkspaceChangedHandler((e) =>
68+
#else
69+
_workspace.WorkspaceChanged += (sender, e) =>
70+
#endif
6571
{
6672
// if our map points at documents not in the current solution, then we want to clear things out.
6773
// this way we don't hold onto semantic models past, say, the c#/vb solutions closing.
@@ -78,7 +84,13 @@ public SemanticModelReuseWorkspaceService(Workspace workspace)
7884
return;
7985
}
8086
}
81-
});
87+
}
88+
#if WORKSPACE
89+
);
90+
#else
91+
;
92+
#endif
93+
#pragma warning restore RS0030 // Do not use banned APIs
8294
}
8395

8496
public async ValueTask<SemanticModel> ReuseExistingSpeculativeModelAsync(Document document, SyntaxNode node, CancellationToken cancellationToken)

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/WorkspaceExtensions.projitems

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,14 @@
152152
<Compile Include="$(MSBuildThisFileDirectory)Utilities\SemanticDocument.cs" />
153153
<Compile Include="$(MSBuildThisFileDirectory)Utilities\SyntacticDocument.cs" />
154154
<Compile Include="$(MSBuildThisFileDirectory)Utilities\TextReaderWithLength.cs" />
155-
<Compile Include="$(MSBuildThisFileDirectory)WorkspaceServices\SemanticModelReuse\ISemanticModelReuseWorkspaceService.cs" />
156-
<Compile Include="$(MSBuildThisFileDirectory)WorkspaceServices\SemanticModelReuse\SemanticModelReuseWorkspaceServiceFactory.cs" />
155+
<Compile Include="$(MSBuildThisFileDirectory)SemanticModelReuse\AbstractSemanticModelReuseLanguageService.cs" />
156+
<Compile Include="$(MSBuildThisFileDirectory)SemanticModelReuse\ISemanticModelReuseLanguageService.cs" />
157+
<Compile Include="$(MSBuildThisFileDirectory)SemanticModelReuse\ISemanticModelReuseWorkspaceService.cs" />
158+
<Compile Include="$(MSBuildThisFileDirectory)SemanticModelReuse\SemanticModelReuseWorkspaceServiceFactory.cs" />
157159
<Compile Include="$(MSBuildThisFileDirectory)CodeFixes\SyntaxEditorBasedCodeFixProvider.cs" />
158160
<Compile Include="$(MSBuildThisFileDirectory)LanguageServices\SyntaxFactsService\ISyntaxFactsService.cs" />
159161
<Compile Include="$(MSBuildThisFileDirectory)Utilities\NameGenerator.cs" />
162+
<Compile Include="$(MSBuildThisFileDirectory)SemanticModelReuse\SemanticModelWorkspaceServiceFactory.SemanticModelWorkspaceService.cs" />
160163
<Compile Include="$(MSBuildThisFileDirectory)Workspace\Host\SupportedChangesServiceExtensions.cs" />
161164
<Compile Include="$(MSBuildThisFileDirectory)Workspace\Mef\CodeChangeProviderMetadata.cs" />
162165
<Compile Include="$(MSBuildThisFileDirectory)Workspace\Mef\FileExtensionsMetadata.cs" />

0 commit comments

Comments
 (0)