Skip to content

Commit fce0753

Browse files
committed
Avoid reporting results for wrong documents
1 parent 325f907 commit fce0753

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/Worker/Lab/LanguageServices.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public LanguageServices(ILogger<LanguageServices> logger)
4848
/// </returns>
4949
public async Task<string> ProvideCompletionItemsAsync(string modelUri, Position position, MonacoCompletionContext context)
5050
{
51-
if (documentId == null || Project.GetDocument(documentId) is not { } document)
51+
if (!TryGetDocument(modelUri, out var document))
5252
{
5353
return """{"suggestions":[]}""";
5454
}
@@ -145,7 +145,7 @@ public async Task<string> ProvideCompletionItemsAsync(string modelUri, Position
145145
/// </remarks>
146146
public async Task<string?> ProvideSemanticTokensAsync(string modelUri, string? rangeJson, bool debug, CancellationToken cancellationToken = default)
147147
{
148-
if (documentId == null || Project.GetDocument(documentId) is not { } document)
148+
if (!TryGetDocument(modelUri, out var document))
149149
{
150150
return string.Empty;
151151
}
@@ -397,4 +397,28 @@ private void ApplyChanges(Solution solution)
397397
logger.LogWarning("Failed to apply changes to the workspace.");
398398
}
399399
}
400+
401+
private bool TryGetDocument(string modelUri, [NotNullWhen(returnValue: true)] out Document? document)
402+
{
403+
// Try the current document first.
404+
if (documentId != null &&
405+
modelUris.TryGetValue(documentId, out var uri) &&
406+
uri == modelUri)
407+
{
408+
document = Project.GetDocument(documentId);
409+
return document != null;
410+
}
411+
412+
var id = Project.DocumentIds.FirstOrDefault(id =>
413+
modelUris.TryGetValue(id, out var uri) &&
414+
uri == modelUri);
415+
if (id != null)
416+
{
417+
document = Project.GetDocument(id);
418+
return document != null;
419+
}
420+
421+
document = null;
422+
return false;
423+
}
400424
}

0 commit comments

Comments
 (0)