Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7d63bd4
Factoring out AutoInsertService
alexgav Jul 19, 2024
7e736cf
Switch non-cohost endpoint to use new AutoInsertService
alexgav Jul 19, 2024
c2ad4ed
Adding Remote and OOB AutoInsertService classes and OnAutoInsertProvi…
alexgav Jul 21, 2024
ce9e1bf
Add common code for capabilities
alexgav Jul 26, 2024
72fdab3
Add cohost OnAutoInsert endpoint
alexgav Jul 26, 2024
c3c72dc
Parameter rename
alexgav Jul 27, 2024
4350c85
Update src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/AutoInse…
alexgav Jul 27, 2024
3dbaf87
Update src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/AutoInse…
alexgav Jul 27, 2024
0df70d6
Update src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/AutoInse…
alexgav Jul 27, 2024
32eb28f
Fixup after rebase
alexgav Jul 27, 2024
5c1deb6
More post-build cleanup
alexgav Jul 27, 2024
71fe24e
Move common HTML and C# auto-insert trigger chars per CR suggestion
alexgav Jul 27, 2024
177c0f4
More cleanup after rebase
alexgav Jul 29, 2024
1dc1044
Add IOnAutoInsertTiggerCharacterProvider per CR suggestion
alexgav Jul 29, 2024
4988389
Removing trigger characters property from RemoteAutoInsertService per…
alexgav Jul 29, 2024
325b59f
Switch to using RemoteResponse
alexgav Aug 7, 2024
d4bbe6c
Fixup bad resolve after rebase, and extra whitespace in RazorLangauge…
alexgav Aug 7, 2024
bb7f79e
Complete capabilities check in CohostingOnAutoInsertEndpoing registra…
alexgav Aug 8, 2024
4e0d7b2
Change input position type to serializable :LinePosition
alexgav Aug 8, 2024
72923a4
Fixing RemoteInsertTextEdit to use properly annotated (for serializat…
alexgav Aug 8, 2024
b230260
Support for delegating auto-insert to C#
alexgav Aug 9, 2024
d8ab694
Fixup after rebase
alexgav Aug 9, 2024
9d3b8fe
Fixup AutoClosingTagOnAutoInsertProviderTest tests
alexgav Aug 9, 2024
bc1b677
Fixinfg up CloseTextTagOnAutoInsertProviderTest tests
alexgav Aug 10, 2024
3a34c6b
Fixing up OnAutoInsertEndpointTest (which also tests the new AutoInse…
alexgav Aug 10, 2024
a5ab0df
Fixing duplicate OnAutoInsert handler registration (bad merge after r…
alexgav Aug 10, 2024
356da7c
Fixes to MEF composition issue and capabilities check
alexgav Aug 10, 2024
37f0d42
Fixing incorrect export type
alexgav Aug 12, 2024
52b251f
Minor cleanup per CR suggestions
alexgav Aug 13, 2024
477585d
Switching parameters to RazorCodeDocument and removing async in a lot…
alexgav Aug 13, 2024
19f4f46
Fixing build - removing unneeded using
alexgav Aug 14, 2024
95c810f
Fix RemoteAutoInsertService logic to follow existing code (always pre…
alexgav Aug 14, 2024
eaf408d
Check allowed trigger characters before delegating to other languages…
alexgav Aug 14, 2024
e9c8d54
Plumbing through actual option values we need and using them
alexgav Aug 14, 2024
53e5186
Fixup After Rebase
alexgav Aug 21, 2024
58883fb
Consuming RazorFormattingService in remote OnAutoInsert service
alexgav Aug 21, 2024
7e442d2
Fixing exception in RemoteProjectSnapshot.Configuration
alexgav Aug 22, 2024
292c9a7
Switch to PreferHtmlInAttributeValuesDocumentPositionInfoStrategy as …
alexgav Aug 22, 2024
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
1 change: 1 addition & 0 deletions eng/targets/Services.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
<ServiceHubService Include="Microsoft.VisualStudio.Razor.InlayHint" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteInlayHintService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.GoToDefinition" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteGoToDefinitionService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Rename" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteRenameService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.AutoInsert" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteAutoInsertService+Factory" />
</ItemGroup>
</Project>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Threading;
using Microsoft.CodeAnalysis.Razor.AutoInsert;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
Expand All @@ -26,21 +27,18 @@ internal class OnAutoInsertEndpoint(
LanguageServerFeatureOptions languageServerFeatureOptions,
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
IEnumerable<IOnAutoInsertProvider> onAutoInsertProvider,
IAutoInsertService autoInsertService,
RazorLSPOptionsMonitor optionsMonitor,
IAdhocWorkspaceFactory workspaceFactory,
IRazorFormattingService razorFormattingService,
ILoggerFactory loggerFactory)
: AbstractRazorDelegatingEndpoint<VSInternalDocumentOnAutoInsertParams, VSInternalDocumentOnAutoInsertResponseItem?>(languageServerFeatureOptions, documentMappingService, clientConnection, loggerFactory.GetOrCreateLogger<OnAutoInsertEndpoint>()), ICapabilitiesProvider
{
private static readonly HashSet<string> s_htmlAllowedTriggerCharacters = new(StringComparer.Ordinal) { "=", };
private static readonly HashSet<string> s_cSharpAllowedTriggerCharacters = new(StringComparer.Ordinal) { "'", "/", "\n" };

private readonly LanguageServerFeatureOptions _languageServerFeatureOptions = languageServerFeatureOptions;
private readonly RazorLSPOptionsMonitor _optionsMonitor = optionsMonitor;
private readonly IAdhocWorkspaceFactory _workspaceFactory = workspaceFactory;
private readonly IRazorFormattingService _razorFormattingService = razorFormattingService;
private readonly List<IOnAutoInsertProvider> _onAutoInsertProviders = onAutoInsertProvider.ToList();
private readonly IAutoInsertService _autoInsertService = autoInsertService;

protected override string CustomMessageTarget => CustomMessageNames.RazorOnAutoInsertEndpointName;

Expand All @@ -53,17 +51,16 @@ internal class OnAutoInsertEndpoint(

public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, VSInternalClientCapabilities clientCapabilities)
{
var triggerCharacters = _onAutoInsertProviders.Select(provider => provider.TriggerCharacter);
var triggerCharacters = _autoInsertService.TriggerCharacters;

if (_languageServerFeatureOptions.SingleServerSupport)
{
triggerCharacters = triggerCharacters.Concat(s_htmlAllowedTriggerCharacters).Concat(s_cSharpAllowedTriggerCharacters);
triggerCharacters = triggerCharacters
.Concat(AutoInsertService.HtmlAllowedAutoInsertTriggerCharacters)
.Concat(AutoInsertService.CSharpAllowedAutoInsertTriggerCharacters);
}

serverCapabilities.OnAutoInsertProvider = new VSInternalDocumentOnAutoInsertOptions()
{
TriggerCharacters = triggerCharacters.Distinct().ToArray()
};
serverCapabilities.EnableOnAutoInsert(triggerCharacters);
}

protected override async Task<VSInternalDocumentOnAutoInsertResponseItem?> TryHandleAsync(VSInternalDocumentOnAutoInsertParams request, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken)
Expand All @@ -84,36 +81,20 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V

var character = request.Character;

using var applicableProviders = new PooledArrayBuilder<IOnAutoInsertProvider>();
foreach (var provider in _onAutoInsertProviders)
{
if (provider.TriggerCharacter == character)
{
applicableProviders.Add(provider);
}
}

if (applicableProviders.Count == 0)
{
// There's currently a bug in the LSP platform where other language clients OnAutoInsert trigger characters influence every language clients trigger characters.
// To combat this we need to preemptively return so we don't try having our providers handle characters that they can't.
return null;
}
var insertTextEdit = _autoInsertService.TryResolveInsertion(
codeDocument,
request.Position,
character,
_optionsMonitor.CurrentValue.AutoClosingTags
);

var uri = request.TextDocument.Uri;
var position = request.Position;

using var formattingContext = FormattingContext.Create(uri, documentContext.Snapshot, codeDocument, request.Options, _workspaceFactory);
foreach (var provider in applicableProviders)
{
if (provider.TryResolveInsertion(position, formattingContext, out var textEdit, out var format))
if (insertTextEdit is not null)
{
return new VSInternalDocumentOnAutoInsertResponseItem()
{
return new VSInternalDocumentOnAutoInsertResponseItem()
{
TextEdit = textEdit,
TextEditFormat = format,
};
}
TextEdit = insertTextEdit.Value.TextEdit,
TextEditFormat = insertTextEdit.Value.InsertTextFormat,
};
}

// No provider could handle the text edit.
Expand All @@ -130,7 +111,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V

if (positionInfo.LanguageKind == RazorLanguageKind.Html)
{
if (!s_htmlAllowedTriggerCharacters.Contains(request.Character))
if (!AutoInsertService.HtmlAllowedAutoInsertTriggerCharacters.Contains(request.Character))
{
Logger.LogInformation($"Inapplicable HTML trigger char {request.Character}.");
return SpecializedTasks.Null<IDelegatedParams>();
Expand All @@ -146,7 +127,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
}
else if (positionInfo.LanguageKind == RazorLanguageKind.CSharp)
{
if (!s_cSharpAllowedTriggerCharacters.Contains(request.Character))
if (!AutoInsertService.CSharpAllowedAutoInsertTriggerCharacters.Contains(request.Character))
{
Logger.LogInformation($"Inapplicable C# trigger char {request.Character}.");
return SpecializedTasks.Null<IDelegatedParams>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down Expand Up @@ -65,4 +67,21 @@ public static void EnableMapCodeProvider(this VSInternalServerCapabilities serve
{
serverCapabilities.MapCodeProvider = true;
}

public static void EnableOnAutoInsert(
this VSInternalServerCapabilities serverCapabilities,
IEnumerable<string> triggerCharacters)
{
serverCapabilities.OnAutoInsertProvider = new VSInternalDocumentOnAutoInsertOptions()
.EnableOnAutoInsert(triggerCharacters);
}

public static VSInternalDocumentOnAutoInsertOptions EnableOnAutoInsert(
this VSInternalDocumentOnAutoInsertOptions options,
IEnumerable<string> triggerCharacters)
{
options.TriggerCharacters = triggerCharacters.Distinct().ToArray();

return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using Microsoft.AspNetCore.Razor.LanguageServer.SignatureHelp;
using Microsoft.AspNetCore.Razor.LanguageServer.WrapWithTag;
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.AutoInsert;
using Microsoft.CodeAnalysis.Razor.FoldingRanges;
using Microsoft.CodeAnalysis.Razor.GoToDefinition;
using Microsoft.CodeAnalysis.Razor.Logging;
Expand Down Expand Up @@ -135,12 +137,14 @@ protected override ILspServices ConstructLspServices()
services.AddHoverServices();
services.AddTextDocumentServices(featureOptions);

// Auto insert
services.AddSingleton<IOnAutoInsertProvider, CloseTextTagOnAutoInsertProvider>();
services.AddSingleton<IOnAutoInsertProvider, AutoClosingTagOnAutoInsertProvider>();

if (!featureOptions.UseRazorCohostServer)
{
// Auto insert
services.AddSingleton<IOnAutoInsertProvider, CloseTextTagOnAutoInsertProvider>();
services.AddSingleton<IOnAutoInsertProvider, AutoClosingTagOnAutoInsertProvider>();

services.AddSingleton<IAutoInsertService, AutoInsertService>();

// Folding Range Providers
services.AddSingleton<IRazorFoldingRangeProvider, RazorCodeBlockFoldingProvider>();
services.AddSingleton<IRazorFoldingRangeProvider, RazorCSharpStatementFoldingProvider>();
Expand Down Expand Up @@ -177,7 +181,6 @@ static void AddHandlers(IServiceCollection services, LanguageServerFeatureOption
services.AddTransient<IOnInitialized>(sp => sp.GetRequiredService<RazorConfigurationEndpoint>());

services.AddHandlerWithCapabilities<ImplementationEndpoint>();
services.AddHandlerWithCapabilities<OnAutoInsertEndpoint>();

if (!featureOptions.UseRazorCohostServer)
{
Expand All @@ -187,6 +190,7 @@ static void AddHandlers(IServiceCollection services, LanguageServerFeatureOption
services.AddSingleton<IRenameService, RenameService>();
services.AddHandlerWithCapabilities<RenameEndpoint>();

services.AddHandlerWithCapabilities<OnAutoInsertEndpoint>();
services.AddHandlerWithCapabilities<DocumentHighlightEndpoint>();
services.AddHandlerWithCapabilities<SignatureHelpEndpoint>();
services.AddHandlerWithCapabilities<LinkedEditingRangeEndpoint>();
Expand Down
Loading