Skip to content

Commit 98c895c

Browse files
committed
Fold C# statement blocks
1 parent 2f71d65 commit 98c895c

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 System.Collections.Generic;
6+
using System.Collections.Immutable;
7+
using System.Linq;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Razor.Language;
11+
using Microsoft.AspNetCore.Razor.Language.Syntax;
12+
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
13+
using Microsoft.CodeAnalysis.Razor.Workspaces;
14+
using Microsoft.VisualStudio.LanguageServer.Protocol;
15+
16+
namespace Microsoft.AspNetCore.Razor.LanguageServer.Folding;
17+
18+
internal sealed class RazorCSharpStatementFoldingProvider : AbstractSyntaxNodeFoldingProvider<CSharpStatementSyntax>
19+
{
20+
protected override string GetCollapsedText(CSharpStatementSyntax node)
21+
{
22+
return "@{...}";
23+
}
24+
25+
protected override ImmutableArray<CSharpStatementSyntax> GetFoldableNodes(RazorSyntaxTree syntaxTree)
26+
{
27+
return syntaxTree.Root
28+
.DescendantNodes(node => node is RazorDocumentSyntax or MarkupBlockSyntax or MarkupElementSyntax or CSharpCodeBlockSyntax)
29+
.OfType<CSharpStatementSyntax>()
30+
.SelectAsArray(d => d);
31+
}
32+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ protected override ILspServices ConstructLspServices()
131131

132132
// Folding Range Providers
133133
services.AddSingleton<IRazorFoldingRangeProvider, RazorCodeBlockFoldingProvider>();
134+
services.AddSingleton<IRazorFoldingRangeProvider, RazorCSharpStatementFoldingProvider>();
134135
services.AddSingleton<IRazorFoldingRangeProvider, SectionDirectiveFoldingProvider>();
135136
services.AddSingleton<IRazorFoldingRangeProvider, UsingsFoldingRangeProvider>();
136137

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Folding/FoldingEndpointTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,50 @@ @using System.CodeDom|]
3333
<p>hello!</p>
3434
""");
3535

36+
[Fact]
37+
public Task CSharpStatement()
38+
=> VerifyRazorFoldsAsync("""
39+
<p>hello!</p>
40+
41+
[|@{
42+
var helloWorld = "";
43+
}|]
44+
45+
@(DateTime
46+
.Now)
47+
48+
<p>hello!</p>
49+
""");
50+
51+
[Fact]
52+
public Task CSharpStatement_Nested()
53+
=> VerifyRazorFoldsAsync("""
54+
<p>hello!</p>
55+
56+
<div>
57+
58+
[|@{
59+
var helloWorld = "";
60+
}|]
61+
62+
</div>
63+
64+
@(DateTime
65+
.Now)
66+
67+
<p>hello!</p>
68+
""");
69+
70+
[Fact]
71+
public Task CSharpStatement_NotSingleLine()
72+
=> VerifyRazorFoldsAsync("""
73+
<p>hello!</p>
74+
75+
@{ var helloWorld = ""; }
76+
77+
<p>hello!</p>
78+
""");
79+
3680
[Fact]
3781
public Task CodeBlock()
3882
=> VerifyRazorFoldsAsync("""
@@ -100,6 +144,7 @@ private async Task VerifyRazorFoldsAsync(string input, string? filePath = null)
100144
[
101145
new UsingsFoldingRangeProvider(),
102146
new RazorCodeBlockFoldingProvider(),
147+
new RazorCSharpStatementFoldingProvider(),
103148
new SectionDirectiveFoldingProvider()
104149
],
105150
LoggerFactory);

0 commit comments

Comments
 (0)