Skip to content
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

Expose folding range functionality to Razor #73609

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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.Structure;
using Microsoft.CodeAnalysis.Text;
using Roslyn.LanguageServer.Protocol;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.LanguageServer.Handler
{
Expand All @@ -35,13 +36,21 @@ public FoldingRangesHandler(IGlobalOptionService globalOptions)

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

public async Task<FoldingRange[]?> HandleRequestAsync(FoldingRangeParams request, RequestContext context, CancellationToken cancellationToken)
public Task<FoldingRange[]?> HandleRequestAsync(FoldingRangeParams request, RequestContext context, CancellationToken cancellationToken)
{
var document = context.Document;
if (document is null)
return null;
return SpecializedTasks.Null<FoldingRange[]>();

var options = _globalOptions.GetBlockStructureOptions(document.Project) with
return SpecializedTasks.AsNullable(GetFoldingRangesAsync(_globalOptions, document, cancellationToken));
}

internal static Task<FoldingRange[]> GetFoldingRangesAsync(
IGlobalOptionService globalOptions,
Document document,
CancellationToken cancellationToken)
{
var options = globalOptions.GetBlockStructureOptions(document.Project) with
{
// Need to set the block structure guide options to true since the concept does not exist in vscode
// but we still want to categorize them as the correct BlockType.
Expand All @@ -50,7 +59,7 @@ public FoldingRangesHandler(IGlobalOptionService globalOptions)
ShowBlockStructureGuidesForCodeLevelConstructs = true
};

return await GetFoldingRangesAsync(document, options, cancellationToken).ConfigureAwait(false);
return GetFoldingRangesAsync(document, options, cancellationToken);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.EditorFeatures2.UnitTests" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.AspNetCore" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.FSharp" />
<!-- Full IVT is through ExternalAccess for functionality -->
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.Razor" />
<!-- Restricted IVT is direct for protocol types only -->
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.Remote.Razor" Namespace="Roslyn.LanguageServer.Protocol" Partner="Razor" Key="$(RazorKey)" />
Copy link
Member

Choose a reason for hiding this comment

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

I guess my hand is being forced, I have to write the readme of how to make breaking changes here 😆

Copy link
Member Author

Choose a reason for hiding this comment

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

You can reject this bit of the PR if you want, I can deal with that, but you could also just put off writing the readme until you make the first breaking change.. and then don't 😛

<RestrictedInternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices.Razor" Namespace="Roslyn.LanguageServer.Protocol" Partner="Razor" Key="$(RazorKey)" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.Xaml" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.CSharp.EditorFeatures" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.CSharp.Features.UnitTests" />
Expand Down
25 changes: 25 additions & 0 deletions src/Tools/ExternalAccess/Razor/Cohost/Handlers/FoldingRanges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.Options;
using Roslyn.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers
{
internal static class FoldingRanges
{
public static Task<FoldingRange[]> GetFoldingRangesAsync(Document document, CancellationToken cancellationToken)
{
// We need to manually get the IGlobalOptionsService out of the Mef composition, because Razor has its own
// composition so can't import it (and its internal anyway)
var globalOptions = document.Project.Solution.Services.ExportProvider.GetExports<IGlobalOptionService>().First().Value;

return FoldingRangesHandler.GetFoldingRangesAsync(globalOptions, document, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal override DocumentInfo CreateDocumentInfo(

internal override void AddJsonConverters(JsonSerializerOptions options)
{
VSInternalExtensionUtilities.AddVSInternalExtensionConverters(options);
ProtocolConversions.AddLspSerializerOptions(options);
}

private class RazorCapabilitiesProvider : ICapabilitiesProvider
Expand Down
Loading