Skip to content

Expose FAR to cohosting #76002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text;
using Roslyn.LanguageServer.Protocol;
using Roslyn.Utilities;
using LSP = Roslyn.LanguageServer.Protocol;
Expand Down Expand Up @@ -45,7 +46,7 @@ public FindAllReferencesHandler(

public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalReferenceParams request) => request.TextDocument;

public async Task<LSP.SumType<VSInternalReferenceItem, LSP.Location>[]?> HandleRequestAsync(
public async Task<SumType<VSInternalReferenceItem, LSP.Location>[]?> HandleRequestAsync(
VSInternalReferenceParams referenceParams,
RequestContext context,
CancellationToken cancellationToken)
Expand All @@ -55,23 +56,37 @@ public FindAllReferencesHandler(
Contract.ThrowIfNull(document);
Contract.ThrowIfNull(workspace);

var linePosition = ProtocolConversions.PositionToLinePosition(referenceParams.Position);
var clientCapabilities = context.GetRequiredClientCapabilities();

using var progress = BufferedProgress.Create(referenceParams.PartialResultToken);

var findUsagesService = document.GetRequiredLanguageService<IFindUsagesLSPService>();
var position = await document.GetPositionFromLinePositionAsync(
ProtocolConversions.PositionToLinePosition(referenceParams.Position), cancellationToken).ConfigureAwait(false);
await FindReferencesAsync(progress, workspace, document, linePosition, clientCapabilities.HasVisualStudioLspCapability(), _globalOptions, _metadataAsSourceFileService, _asyncListener, cancellationToken).ConfigureAwait(false);

var clientCapabilities = context.GetRequiredClientCapabilities();
return progress.GetFlattenedValues();
}

internal static async Task FindReferencesAsync(
IProgress<SumType<VSInternalReferenceItem, LSP.Location>[]> progress,
Workspace workspace,
Document document,
LinePosition linePosition,
bool supportsVSExtensions,
IGlobalOptionService globalOptions,
IMetadataAsSourceFileService metadataAsSourceFileService,
IAsynchronousOperationListener asyncListener,
CancellationToken cancellationToken)
{
var findUsagesService = document.GetRequiredLanguageService<IFindUsagesLSPService>();
var position = await document.GetPositionFromLinePositionAsync(linePosition, cancellationToken).ConfigureAwait(false);

var findUsagesContext = new FindUsagesLSPContext(
progress, workspace, document, position, _metadataAsSourceFileService, _asyncListener, _globalOptions, clientCapabilities, cancellationToken);
progress, workspace, document, position, metadataAsSourceFileService, asyncListener, globalOptions, supportsVSExtensions, cancellationToken);

// Finds the references for the symbol at the specific position in the document, reporting them via streaming to the LSP client.
var classificationOptions = _globalOptions.GetClassificationOptionsProvider();
var classificationOptions = globalOptions.GetClassificationOptionsProvider();
await findUsagesService.FindReferencesAsync(findUsagesContext, document, position, classificationOptions, cancellationToken).ConfigureAwait(false);
await findUsagesContext.OnCompletedAsync(cancellationToken).ConfigureAwait(false);

return progress.GetFlattenedValues();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public FindUsagesLSPContext(
IMetadataAsSourceFileService metadataAsSourceFileService,
IAsynchronousOperationListener asyncListener,
IGlobalOptionService globalOptions,
ClientCapabilities clientCapabilities,
bool supportsVSExtensions,
CancellationToken cancellationToken)
{
_progress = progress;
Expand All @@ -89,7 +89,7 @@ public FindUsagesLSPContext(
_position = position;
_metadataAsSourceFileService = metadataAsSourceFileService;
_globalOptions = globalOptions;
_supportsVSExtensions = clientCapabilities.HasVisualStudioLspCapability();
_supportsVSExtensions = supportsVSExtensions;
_workQueue = new AsyncBatchingWorkQueue<SumType<VSInternalReferenceItem, LSP.Location>>(
DelayTimeSpan.Medium, ReportReferencesAsync, asyncListener, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@ public static Task<SumType<DocumentSymbol[], SymbolInformation[]>> GetDocumentSy
// with a VSImageId. This value should be retrieved from the language server's client capabilities.
return DocumentSymbolsHandler.GetDocumentSymbolsAsync(document, useHierarchicalSymbols, supportsVSExtensions, cancellationToken);
}

[Obsolete("Update to call overload that takes 'supportsVSExtensions' argument.")]
public static Task<SumType<DocumentSymbol[], SymbolInformation[]>> GetDocumentSymbolsAsync(
Document document, bool useHierarchicalSymbols, CancellationToken cancellationToken)
=> GetDocumentSymbolsAsync(document, useHierarchicalSymbols, supportsVSExtensions: false, cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.MetadataAsSource;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text;
using Roslyn.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers;

using Location = Roslyn.LanguageServer.Protocol.Location;

internal static class FindAllReferences
{
public static async Task<SumType<VSInternalReferenceItem, Location>[]?> FindReferencesAsync(Workspace workspace, Document document, LinePosition linePosition, bool supportsVSExtensions, CancellationToken cancellationToken)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might consider having a progress parameter here and passing it in from Razor - otherwise you could be waiting a while for anything to stream in. I don't think you need all the references before you can do anything with them right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now Razor doesn't support streaming at all, because the LSP client middle layer doesn't. Cohosting removes that limitation, but none of our shared code on the Razor file knows how to deal with it, plus we'd have to work out how to stream things from OOP back to devenv to send to the language client, so I just decided to punt all of that until later :)

{
var globalOptions = document.Project.Solution.Services.ExportProvider.GetService<IGlobalOptionService>();
var metadataAsSourceFileService = document.Project.Solution.Services.ExportProvider.GetService<IMetadataAsSourceFileService>();

// Passing null here means this will just collect items in an array builder
var progress = BufferedProgress.Create<SumType<VSInternalReferenceItem, Location>[]>(progress: null);

await FindAllReferencesHandler.FindReferencesAsync(progress, workspace, document, linePosition, supportsVSExtensions, globalOptions, metadataAsSourceFileService, AsynchronousOperationListenerProvider.NullListener, cancellationToken).ConfigureAwait(false);

return progress.GetFlattenedValues();
}
}
Loading