Skip to content

Commit f3aa647

Browse files
authored
Add feature flag to turn on the new Roslyn tokenizer (dotnet#11185)
This makes the feature work in VS and VS Code (with corresponding VS Code change to pass the right flag, coming once this is merged) VS Code PR: dotnet/vscode-csharp#7748 /cc @danroth27, @leslierichardson95, @333fred, @chsienki and anyone else who wants, for wording: VS Code: https://github.com/dotnet/vscode-csharp/pull/7748/files#diff-9b29d8b47a74097a04da353b72a8632d485d658282457b97b4caf26575a42dd0R131 VS: https://github.com/dotnet/razor/pull/11185/files#diff-18b9b146f4d27a6a5b6248ece31b30570d84c2a409b25bf5b660de86e608fa4cR80
2 parents aeebb01 + 4b7e93d commit f3aa647

File tree

13 files changed

+41
-2
lines changed

13 files changed

+41
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ internal class DefaultLanguageServerFeatureOptions : LanguageServerFeatureOption
3838
public override bool DisableRazorLanguageServer => false;
3939

4040
public override bool ForceRuntimeCodeGeneration => false;
41+
42+
public override bool UseRoslynTokenizer => false;
4143
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/ConfigurableLanguageServerFeatureOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class ConfigurableLanguageServerFeatureOptions : LanguageServerFeatureO
2323
private readonly bool? _useRazorCohostServer;
2424
private readonly bool? _disableRazorLanguageServer;
2525
private readonly bool? _forceRuntimeCodeGeneration;
26+
private readonly bool? _useRoslynTokenizer;
2627

2728
public override bool SupportsFileManipulation => _supportsFileManipulation ?? _defaults.SupportsFileManipulation;
2829
public override string CSharpVirtualDocumentSuffix => _csharpVirtualDocumentSuffix ?? DefaultLanguageServerFeatureOptions.DefaultCSharpVirtualDocumentSuffix;
@@ -37,6 +38,7 @@ internal class ConfigurableLanguageServerFeatureOptions : LanguageServerFeatureO
3738
public override bool UseRazorCohostServer => _useRazorCohostServer ?? _defaults.UseRazorCohostServer;
3839
public override bool DisableRazorLanguageServer => _disableRazorLanguageServer ?? _defaults.DisableRazorLanguageServer;
3940
public override bool ForceRuntimeCodeGeneration => _forceRuntimeCodeGeneration ?? _defaults.ForceRuntimeCodeGeneration;
41+
public override bool UseRoslynTokenizer => _useRoslynTokenizer ?? _defaults.UseRoslynTokenizer;
4042

4143
public ConfigurableLanguageServerFeatureOptions(string[] args)
4244
{
@@ -60,6 +62,7 @@ public ConfigurableLanguageServerFeatureOptions(string[] args)
6062
TryProcessBoolOption(nameof(UseRazorCohostServer), ref _useRazorCohostServer, option, args, i);
6163
TryProcessBoolOption(nameof(DisableRazorLanguageServer), ref _disableRazorLanguageServer, option, args, i);
6264
TryProcessBoolOption(nameof(ForceRuntimeCodeGeneration), ref _forceRuntimeCodeGeneration, option, args, i);
65+
TryProcessBoolOption(nameof(UseRoslynTokenizer), ref _useRoslynTokenizer, option, args, i);
6366
}
6467
}
6568

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/LanguageServerFeatureOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ internal abstract class LanguageServerFeatureOptions
3636
public abstract bool DisableRazorLanguageServer { get; }
3737

3838
/// <summary>
39-
/// When enabled, design time code will not be generated. All tooling will be using runtime code generation.
39+
/// When enabled, design time code will not be generated. All tooling, except formatting, will be using runtime code generation.
4040
/// </summary>
4141
public abstract bool ForceRuntimeCodeGeneration { get; }
42+
43+
public abstract bool UseRoslynTokenizer { get; }
4244
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectState.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.CodeAnalysis.CSharp;
1515
using Microsoft.CodeAnalysis.Razor.Workspaces;
1616
using Microsoft.CodeAnalysis.Text;
17+
using Microsoft.NET.Sdk.Razor.SourceGenerators;
1718

1819
namespace Microsoft.CodeAnalysis.Razor.ProjectSystem;
1920

@@ -171,12 +172,14 @@ RazorProjectEngine CreateProjectEngine()
171172
{
172173
var configuration = HostProject.Configuration;
173174
var rootDirectoryPath = Path.GetDirectoryName(HostProject.FilePath).AssumeNotNull();
175+
var useRoslynTokenizer = LanguageServerFeatureOptions.UseRoslynTokenizer;
174176

175177
return _projectEngineFactoryProvider.Create(configuration, rootDirectoryPath, builder =>
176178
{
177179
builder.SetRootNamespace(HostProject.RootNamespace);
178180
builder.SetCSharpLanguageVersion(CSharpLanguageVersion);
179181
builder.SetSupportLocalizedComponentNames();
182+
builder.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer, CSharpParseOptions.Default));
180183
});
181184
}
182185
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/RemoteClientInitializationOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ internal struct RemoteClientInitializationOptions
3333

3434
[JsonPropertyName("showAllCSharpCodeActions")]
3535
public required bool ShowAllCSharpCodeActions { get; set; }
36+
37+
[JsonPropertyName("useRoslynTokenizer")]
38+
public required bool UseRoslynTokenizer { get; set; }
3639
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Initialization/RemoteLanguageServerFeatureOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ internal class RemoteLanguageServerFeatureOptions : LanguageServerFeatureOptions
4242
public override bool DisableRazorLanguageServer => throw new InvalidOperationException("This option has not been synced to OOP.");
4343

4444
public override bool ForceRuntimeCodeGeneration => _options.ForceRuntimeCodeGeneration;
45+
46+
public override bool UseRoslynTokenizer => _options.UseRoslynTokenizer;
4547
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Microsoft.CodeAnalysis.Razor;
2020
using Microsoft.CodeAnalysis.Razor.Compiler.CSharp;
2121
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
22+
using Microsoft.NET.Sdk.Razor.SourceGenerators;
2223
using Microsoft.VisualStudio.Threading;
2324

2425
namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
@@ -51,6 +52,7 @@ public RemoteProjectSnapshot(Project project, RemoteSolutionSnapshot solutionSna
5152
_lazyProjectEngine = new AsyncLazy<RazorProjectEngine>(async () =>
5253
{
5354
var configuration = await _lazyConfiguration.GetValueAsync();
55+
var useRoslynTokenizer = SolutionSnapshot.SnapshotManager.LanguageServerFeatureOptions.UseRoslynTokenizer;
5456
return ProjectEngineFactories.DefaultProvider.Create(
5557
configuration,
5658
rootDirectoryPath: Path.GetDirectoryName(FilePath).AssumeNotNull(),
@@ -59,6 +61,7 @@ public RemoteProjectSnapshot(Project project, RemoteSolutionSnapshot solutionSna
5961
builder.SetRootNamespace(RootNamespace);
6062
builder.SetCSharpLanguageVersion(CSharpLanguageVersion);
6163
builder.SetSupportLocalizedComponentNames();
64+
builder.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer, CSharpParseOptions.Default));
6265
});
6366
},
6467
joinableTaskFactory: null);

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Remote/RemoteServiceInvoker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ private async Task InitializeRemoteClientAsync(RazorRemoteHostClient remoteClien
141141
ForceRuntimeCodeGeneration = _languageServerFeatureOptions.ForceRuntimeCodeGeneration,
142142
SupportsFileManipulation = _languageServerFeatureOptions.SupportsFileManipulation,
143143
ShowAllCSharpCodeActions = _languageServerFeatureOptions.ShowAllCSharpCodeActions,
144+
UseRoslynTokenizer = _languageServerFeatureOptions.UseRoslynTokenizer,
144145
};
145146

146147
_logger.LogDebug($"First OOP call, so initializing OOP service.");

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/VisualStudioLanguageServerFeatureOptions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class VisualStudioLanguageServerFeatureOptions : LanguageServerFeatureO
1919
private readonly Lazy<bool> _useRazorCohostServer;
2020
private readonly Lazy<bool> _disableRazorLanguageServer;
2121
private readonly Lazy<bool> _forceRuntimeCodeGeneration;
22+
private readonly Lazy<bool> _useRoslynTokenizer;
2223

2324
[ImportingConstructor]
2425
public VisualStudioLanguageServerFeatureOptions(ILspEditorFeatureDetector lspEditorFeatureDetector)
@@ -71,12 +72,18 @@ public VisualStudioLanguageServerFeatureOptions(ILspEditorFeatureDetector lspEdi
7172
var forceRuntimeCodeGeneration = featureFlags.IsFeatureEnabled(WellKnownFeatureFlagNames.ForceRuntimeCodeGeneration, defaultValue: false);
7273
return forceRuntimeCodeGeneration;
7374
});
75+
76+
_useRoslynTokenizer = new Lazy<bool>(() =>
77+
{
78+
var featureFlags = (IVsFeatureFlags)Package.GetGlobalService(typeof(SVsFeatureFlags));
79+
var useRoslynTokenizer = featureFlags.IsFeatureEnabled(WellKnownFeatureFlagNames.UseRoslynTokenizer, defaultValue: false);
80+
return useRoslynTokenizer;
81+
});
7482
}
7583

7684
// We don't currently support file creation operations on VS Codespaces or VS Liveshare
7785
public override bool SupportsFileManipulation => !IsCodespacesOrLiveshare;
7886

79-
8087
public override string CSharpVirtualDocumentSuffix => ".ide.g.cs";
8188

8289
public override string HtmlVirtualDocumentSuffix => "__virtual.html";
@@ -103,4 +110,6 @@ public VisualStudioLanguageServerFeatureOptions(ILspEditorFeatureDetector lspEdi
103110

104111
/// <inheritdoc />
105112
public override bool ForceRuntimeCodeGeneration => _forceRuntimeCodeGeneration.Value;
113+
114+
public override bool UseRoslynTokenizer => _useRoslynTokenizer.Value;
106115
}

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/WellKnownFeatureFlagNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ internal static class WellKnownFeatureFlagNames
1111
public const string UseRazorCohostServer = "Razor.LSP.UseRazorCohostServer";
1212
public const string DisableRazorLanguageServer = "Razor.LSP.DisableRazorLanguageServer";
1313
public const string ForceRuntimeCodeGeneration = "Razor.LSP.ForceRuntimeCodeGeneration";
14+
public const string UseRoslynTokenizer = "Razor.LSP.UseRoslynTokenizer";
1415
}

0 commit comments

Comments
 (0)