Skip to content

Commit d176678

Browse files
Move DocumentKey and clean up a bit
- Move DocumentKey to Microsoft.AspNetCore.Razor.ProjectEngineHost project alongside ProjectKey. - Move DocumentKey to the Microsoft.AspNetCore.Razor.ProjectSystem namespace - Rename DocumentFilePath to FilePath. ("Document" is a bit redundant in a "DocumentKey"). - Implement IComparable<ProjectKey> on ProjectKey. - Implement IComparable<DocumentKey> on DocumentKey.
1 parent 3cbcd8a commit d176678

File tree

11 files changed

+77
-52
lines changed

11 files changed

+77
-52
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/GeneratedDocumentPublisher.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
180180
{
181181
if (_publishedCSharpData.Remove(key))
182182
{
183-
_logger.LogDebug($"Removing previous C# publish data for {key.ProjectKey}/{key.DocumentFilePath}");
183+
_logger.LogDebug($"Removing previous C# publish data for {key.ProjectKey}/{key.FilePath}");
184184
}
185185
}
186186

@@ -208,15 +208,15 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
208208
{
209209
if (_publishedCSharpData.Remove(documentKey))
210210
{
211-
_logger.LogDebug($"Removing previous C# publish data for {documentKey.ProjectKey}/{documentKey.DocumentFilePath}");
211+
_logger.LogDebug($"Removing previous C# publish data for {documentKey.ProjectKey}/{documentKey.FilePath}");
212212
}
213213
}
214214

215215
lock (_publishedHtmlData)
216216
{
217217
if (_publishedHtmlData.Remove(documentFilePath))
218218
{
219-
_logger.LogDebug($"Removing previous Html publish data for {documentKey.ProjectKey}/{documentKey.DocumentFilePath}");
219+
_logger.LogDebug($"Removing previous Html publish data for {documentKey.ProjectKey}/{documentKey.FilePath}");
220220
}
221221
}
222222
}
@@ -249,7 +249,7 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
249249
{
250250
if (_publishedCSharpData.Remove(key))
251251
{
252-
_logger.LogDebug($"Removing previous C# publish data for {key.ProjectKey}/{key.DocumentFilePath}");
252+
_logger.LogDebug($"Removing previous C# publish data for {key.ProjectKey}/{key.FilePath}");
253253
}
254254
}
255255
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT license. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.CodeAnalysis.Razor;
6+
using Microsoft.Extensions.Internal;
7+
8+
namespace Microsoft.AspNetCore.Razor.ProjectSystem;
9+
10+
internal readonly record struct DocumentKey : IComparable<DocumentKey>
11+
{
12+
public ProjectKey ProjectKey { get; }
13+
public string FilePath { get; }
14+
15+
public DocumentKey(ProjectKey projectKey, string filePath)
16+
{
17+
ProjectKey = projectKey;
18+
FilePath = filePath;
19+
}
20+
21+
public bool Equals(DocumentKey other)
22+
=> ProjectKey.Equals(other.ProjectKey) &&
23+
FilePathComparer.Instance.Equals(FilePath, other.FilePath);
24+
25+
public override int GetHashCode()
26+
{
27+
var hash = HashCodeCombiner.Start();
28+
hash.Add(ProjectKey);
29+
hash.Add(FilePath, FilePathComparer.Instance);
30+
31+
return hash;
32+
}
33+
34+
public int CompareTo(DocumentKey other)
35+
{
36+
var comparison = ProjectKey.CompareTo(other.ProjectKey);
37+
if (comparison != 0)
38+
{
39+
return comparison;
40+
}
41+
42+
return FilePathComparer.Instance.Compare(FilePath, other.FilePath);
43+
}
44+
}

src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/ProjectSystem/ProjectKey.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Razor.ProjectSystem;
1414
/// identifier for a project.
1515
/// </summary>
1616
[DebuggerDisplay("id: {Id}")]
17-
internal readonly record struct ProjectKey
17+
internal readonly record struct ProjectKey : IComparable<ProjectKey>
1818
{
1919
public static ProjectKey Unknown { get; } = default;
2020

@@ -38,4 +38,19 @@ public override int GetHashCode()
3838

3939
public override string ToString()
4040
=> IsUnknown ? "<Unknown Project>" : Id;
41+
42+
public int CompareTo(ProjectKey other)
43+
{
44+
// Sort "unknown" project keys after other project keys.
45+
if (IsUnknown)
46+
{
47+
return other.IsUnknown ? 0 : 1;
48+
}
49+
else if (other.IsUnknown)
50+
{
51+
return -1;
52+
}
53+
54+
return FilePathComparer.Instance.Compare(Id, other.Id);
55+
}
4156
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentKey.cs

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

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Documents/EditorDocumentManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ public EditorDocument GetOrCreateDocument(
9696
}
9797

9898
// Check if the document is already open and initialized, and associate a buffer if possible.
99-
var textBuffer = GetTextBufferForOpenDocument(key.DocumentFilePath);
99+
var textBuffer = GetTextBufferForOpenDocument(key.FilePath);
100100
document = new EditorDocument(
101101
this,
102102
JoinableTaskContext,
103103
projectFilePath,
104-
key.DocumentFilePath,
104+
key.FilePath,
105105
projectKey,
106-
new FileTextLoader(key.DocumentFilePath, defaultEncoding: null),
107-
_fileChangeTrackerFactory.Create(key.DocumentFilePath),
106+
new FileTextLoader(key.FilePath, defaultEncoding: null),
107+
_fileChangeTrackerFactory.Create(key.FilePath),
108108
textBuffer,
109109
changedOnDisk,
110110
changedInEditor,
@@ -113,10 +113,10 @@ public EditorDocument GetOrCreateDocument(
113113

114114
_documents.Add(key, document);
115115

116-
if (!_documentsByFilePath.TryGetValue(key.DocumentFilePath, out var documents))
116+
if (!_documentsByFilePath.TryGetValue(key.FilePath, out var documents))
117117
{
118118
documents = new List<DocumentKey>();
119-
_documentsByFilePath.Add(key.DocumentFilePath, documents);
119+
_documentsByFilePath.Add(key.FilePath, documents);
120120
}
121121

122122
if (!documents.Contains(key))

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Documents/EditorDocumentManagerListener.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Microsoft.AspNetCore.Razor.ProjectSystem;
1313
using Microsoft.AspNetCore.Razor.Telemetry;
1414
using Microsoft.AspNetCore.Razor.Utilities;
15-
using Microsoft.CodeAnalysis.Razor;
1615
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1716
using Microsoft.CodeAnalysis.Razor.Workspaces;
1817
using Microsoft.VisualStudio.Razor.ProjectSystem;

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Documents/IEditorDocumentManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Diagnostics.CodeAnalysis;
66
using Microsoft.AspNetCore.Razor.ProjectSystem;
7-
using Microsoft.CodeAnalysis.Razor;
87

98
namespace Microsoft.VisualStudio.Razor.Documents;
109

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Documents/VisualStudioEditorDocumentManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Runtime.InteropServices;
1010
using Microsoft.AspNetCore.Razor;
11+
using Microsoft.AspNetCore.Razor.ProjectSystem;
1112
using Microsoft.CodeAnalysis.Razor;
1213
using Microsoft.VisualStudio.Editor;
1314
using Microsoft.VisualStudio.Razor.Extensions;
@@ -180,7 +181,7 @@ public void DocumentClosed(uint cookie, string? exceptFilePath = null)
180181
// We have to deal with some complications here due to renames and event ordering and such.
181182
// We we might see multiple documents open for a cookie (due to linked files), but only one of them
182183
// has been renamed. In that case, we just process the change that we know about.
183-
var filePaths = new HashSet<string>(documents.Select(d => d.DocumentFilePath));
184+
var filePaths = new HashSet<string>(documents.Select(d => d.FilePath));
184185

185186
// `Remove` can correctly handle the case when the incoming value is null without any exceptions.
186187
// The method is just not properly annotated for it,

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.Comparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5-
using Microsoft.CodeAnalysis.Razor;
5+
using Microsoft.AspNetCore.Razor.ProjectSystem;
66
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
77

88
namespace Microsoft.VisualStudio.Razor.DynamicFiles;

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/DocumentGenerator/BackgroundDocumentGeneratorTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.AspNetCore.Razor.Test.Common;
1515
using Microsoft.AspNetCore.Razor.Test.Common.VisualStudio;
1616
using Microsoft.CodeAnalysis;
17-
using Microsoft.CodeAnalysis.Razor;
1817
using Microsoft.CodeAnalysis.Razor.Logging;
1918
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
2019
using Microsoft.CodeAnalysis.Text;
@@ -254,7 +253,7 @@ await projectManager.UpdateAsync(updater =>
254253
// Wait for the work to complete.
255254
await generator.WaitUntilCurrentBatchCompletesAsync();
256255

257-
Assert.Collection(generator.CompletedWork.OrderBy(key => key.DocumentFilePath),
256+
Assert.Collection(generator.CompletedWork.OrderBy(key => key.FilePath),
258257
key => Assert.Equal(documentKey1, key),
259258
key => Assert.Equal(documentKey2, key));
260259
}
@@ -295,7 +294,7 @@ await projectManager.UpdateAsync(updater =>
295294

296295
Assert.True(generator.HasPendingWork);
297296

298-
Assert.Collection(generator.PendingWork.OrderBy(key => key.DocumentFilePath),
297+
Assert.Collection(generator.PendingWork.OrderBy(key => key.FilePath),
299298
key => Assert.Equal(new(s_hostProject1.Key, documents[0].FilePath), key),
300299
key => Assert.Equal(new(s_hostProject1.Key, documents[1].FilePath), key));
301300

@@ -306,7 +305,7 @@ await projectManager.UpdateAsync(updater =>
306305

307306
Assert.False(generator.HasPendingWork);
308307

309-
Assert.Collection(generator.CompletedWork.OrderBy(key => key.DocumentFilePath),
308+
Assert.Collection(generator.CompletedWork.OrderBy(key => key.FilePath),
310309
key => Assert.Equal(new(s_hostProject1.Key, documents[0].FilePath), key),
311310
key => Assert.Equal(new(s_hostProject1.Key, documents[1].FilePath), key));
312311
}

0 commit comments

Comments
 (0)