Skip to content

Commit 02e693d

Browse files
committed
Support line folding only in folding ranges
1 parent 60e7cfc commit 02e693d

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/FoldingRanges/RemoteFoldingRangeService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
99
using Microsoft.CodeAnalysis.Razor.FoldingRanges;
10+
using Microsoft.CodeAnalysis.Razor.Protocol;
1011
using Microsoft.CodeAnalysis.Razor.Protocol.Folding;
1112
using Microsoft.CodeAnalysis.Razor.Remote;
1213
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
@@ -23,6 +24,7 @@ protected override IRemoteFoldingRangeService CreateService(in ServiceArgs args)
2324
}
2425

2526
private readonly IFoldingRangeService _foldingRangeService = args.ExportProvider.GetExportedValue<IFoldingRangeService>();
27+
private readonly IClientCapabilitiesService _clientCapabilitiesService = args.ExportProvider.GetExportedValue<IClientCapabilitiesService>();
2628

2729
public ValueTask<ImmutableArray<RemoteFoldingRange>> GetFoldingRangesAsync(
2830
RazorPinnedSolutionInfoWrapper solutionInfo,
@@ -44,7 +46,8 @@ private async ValueTask<ImmutableArray<RemoteFoldingRange>> GetFoldingRangesAsyn
4446
.GetGeneratedDocumentAsync(cancellationToken)
4547
.ConfigureAwait(false);
4648

47-
var csharpRanges = await ExternalHandlers.FoldingRanges.GetFoldingRangesAsync(generatedDocument, cancellationToken).ConfigureAwait(false);
49+
var lineFoldingOnly = _clientCapabilitiesService.ClientCapabilities.TextDocument?.FoldingRange?.LineFoldingOnly ?? false;
50+
var csharpRanges = await ExternalHandlers.FoldingRanges.GetFoldingRangesAsync(generatedDocument, lineFoldingOnly, cancellationToken).ConfigureAwait(false);
4851

4952
var convertedCSharp = csharpRanges.SelectAsArray(ToFoldingRange);
5053
var convertedHtml = htmlRanges.SelectAsArray(RemoteFoldingRange.ToVsFoldingRange);

src/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/Endpoints/Shared/CohostFoldingRangeEndpointTest.cs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,79 @@ public void M() {[|
239239
}|]
240240
""");
241241

242-
private async Task VerifyFoldingRangesAsync(string input, RazorFileKind? fileKind = null, bool miscellaneousFile = false, string? razorFilePath = null)
242+
[Fact]
243+
public Task CSharp_LineFoldingOnly()
244+
=> VerifyFoldingRangesAsync("""
245+
<div>{|html:
246+
Hello @_name
247+
</div>|}
248+
249+
@code {[|
250+
class C { public void M1() {[|
251+
var x = 1;
252+
|] }
253+
}
254+
}|]
255+
""",
256+
lineFoldingOnly: true);
257+
258+
[Fact]
259+
public Task CSharp_NotLineFoldingOnly()
260+
=> VerifyFoldingRangesAsync("""
261+
<div>{|html:
262+
Hello @_name
263+
</div>|}
264+
265+
@code {[|
266+
class C { public void M1() {[|
267+
var x = 1;
268+
}
269+
}|]
270+
}|]
271+
""",
272+
lineFoldingOnly: false);
273+
274+
[Fact]
275+
public Task IfElseStatements_LineFoldingOnly()
276+
=> VerifyFoldingRangesAsync("""
277+
<div>
278+
@if (true) {[|
279+
<div>
280+
Hello World
281+
</div>
282+
} else {[|
283+
<div>
284+
Goodbye World
285+
</div>
286+
|] }
287+
|] }
288+
</div>
289+
290+
@code[|
291+
{
292+
void M()[|
293+
{
294+
if (true) {[|
295+
|] var x = 1;
296+
} else {[|
297+
var y = 2;
298+
|] }
299+
|] }
300+
}|]
301+
""",
302+
lineFoldingOnly: true);
303+
304+
private async Task VerifyFoldingRangesAsync(string input, RazorFileKind? fileKind = null, bool miscellaneousFile = false, string? razorFilePath = null, bool lineFoldingOnly = false)
243305
{
306+
UpdateClientLSPInitializationOptions(c =>
307+
{
308+
c.ClientCapabilities.TextDocument!.FoldingRange = new FoldingRangeSetting()
309+
{
310+
LineFoldingOnly = lineFoldingOnly,
311+
};
312+
return c;
313+
});
314+
244315
TestFileMarkupParser.GetSpans(input, out var source, out ImmutableDictionary<string, ImmutableArray<TextSpan>> spans);
245316
var document = CreateProjectAndRazorDocument(source, fileKind, miscellaneousFile: miscellaneousFile, documentFilePath: razorFilePath);
246317
var inputText = await document.GetTextAsync(DisposalToken);

0 commit comments

Comments
 (0)