-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Add dynamic registration for razor and cshtml files #73369
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
36ae032
Add dynamic registration for razor and cshtml files
ryzngard b9a9dcc
Merge branch 'main' into hr_razor_dynamicsync
ryzngard 0522212
Move registration
ryzngard bbc35b8
Update src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageSer…
ryzngard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/VisualStudio/DevKit/Impl/EditAndContinue/DynamicDocumentSyncRegistration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.LanguageServer; | ||
using Microsoft.CodeAnalysis.LanguageServer.Handler; | ||
using Roslyn.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.DevKit.EditAndContinue; | ||
|
||
[ExportCSharpVisualBasicLspServiceFactory(typeof(OnInitialized)), Shared] | ||
[method: ImportingConstructor] | ||
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
internal sealed class DynamicDocumentSyncRegistration() : ILspServiceFactory | ||
{ | ||
public ILspService CreateILspService(LspServices lspServices, WellKnownLspServerKinds serverKind) | ||
=> new OnInitialized(); | ||
|
||
public class OnInitialized : IOnInitialized, ILspService | ||
{ | ||
public async Task OnInitializedAsync(ClientCapabilities clientCapabilities, RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
// If dynamic registration for text document synchronization is supported, register for .razor and .cshtml files | ||
// so that they are up to date for hot reload scenarios rather than depending on the file watchers to update | ||
// the contents. | ||
if (clientCapabilities.TextDocument?.Synchronization?.DynamicRegistration is true) | ||
{ | ||
var languageServerManager = context.GetRequiredLspService<IClientLanguageServerManager>(); | ||
|
||
var documentFilters = new[] { new DocumentFilter() { Pattern = "**/*.razor" }, new DocumentFilter() { Pattern = "**/*.cshtml" } }; | ||
var registrationOptions = new TextDocumentRegistrationOptions() | ||
{ | ||
DocumentSelector = documentFilters | ||
}; | ||
|
||
await languageServerManager.SendRequestAsync(Methods.ClientRegisterCapabilityName, | ||
new RegistrationParams() | ||
{ | ||
Registrations = [ | ||
new() | ||
{ | ||
Id = Guid.NewGuid().ToString(), // No need to save this for unregistering | ||
Method = Methods.TextDocumentDidOpenName, | ||
RegisterOptions = registrationOptions | ||
}, | ||
new() | ||
{ | ||
Id = Guid.NewGuid().ToString(), // No need to save this for unregistering | ||
Method = Methods.TextDocumentDidChangeName, | ||
RegisterOptions = registrationOptions | ||
}, | ||
new() | ||
{ | ||
Id = Guid.NewGuid().ToString(), // No need to save this for unregistering | ||
Method = Methods.TextDocumentDidCloseName, | ||
RegisterOptions = registrationOptions | ||
} | ||
] | ||
}, | ||
cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd prefer this to be in the main language server project. We can check if devkit is enabled using a global option, but maybe we should just have it always be on too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you just want it next to the sync handlers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we put it in the protocol project, it will light up in VS. If you want that, then yes. Otherwise put it in the LanguageServer.csproj project (probably in the language server folder).
This is the devkit global option - https://github.com/dotnet/roslyn/blob/main/src/Features/LanguageServer/Protocol/LspOptionsStorage.cs#L31