Skip to content

Commit

Permalink
Plumbing through actual option values we need and using them
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgav committed Aug 14, 2024
1 parent 36a3d5a commit dc85b28
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ ValueTask<Response> TryResolveInsertionAsync(
LinePosition position,
string character,
bool autoCloseTags,
bool formatOnType,
bool indentWithTabs,
int indentSize,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

using Microsoft.VisualStudio.Text.Editor;
using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Microsoft.CodeAnalysis.Razor.Protocol.AutoInsert.RemoteInsertTextEdit?>;
using RoslynFormattingOptions = Roslyn.LanguageServer.Protocol.FormattingOptions;

Expand Down Expand Up @@ -42,6 +42,9 @@ public ValueTask<Response> TryResolveInsertionAsync(
LinePosition linePosition,
string character,
bool autoCloseTags,
bool formatOnType,
bool indentWithTabs,
int indentSize,
CancellationToken cancellationToken)
=> RunServiceAsync(
solutionInfo,
Expand All @@ -51,6 +54,9 @@ public ValueTask<Response> TryResolveInsertionAsync(
linePosition,
character,
autoCloseTags,
formatOnType,
indentWithTabs,
indentSize,
cancellationToken),
cancellationToken);

Expand All @@ -59,6 +65,9 @@ private async ValueTask<Response> TryResolveInsertionAsync(
LinePosition linePosition,
string character,
bool autoCloseTags,
bool formatOnType,
bool indentWithTabs,
int indentSize,
CancellationToken cancellationToken)
{
var sourceText = await remoteDocumentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -103,12 +112,31 @@ private async ValueTask<Response> TryResolveInsertionAsync(
return Response.NoFurtherHandling;
}

// Special case for C# where we use AutoInsert for two purposes:
// 1. For XML documentation comments (filling out the template when typing "///")
// 2. For "on type formatting" style behavior, like adjusting indentation when pressing Enter inside empty braces
//
// If users have turned off on-type formatting, they don't want the behavior of number 2, but its impossible to separate
// that out from number 1. Typing "///" could just as easily adjust indentation on some unrelated code higher up in the
// file, which is exactly the behavior users complain about.
//
// Therefore we are just going to no-op if the user has turned off on type formatting. Maybe one day we can make this
// smarter, but at least the user can always turn the setting back on, type their "///", and turn it back off, without
// having to restart VS. Not the worst compromise (hopefully!)
if (!formatOnType)
{
return Response.NoFurtherHandling;
}

var csharpDocument = codeDocument.GetCSharpDocument();
if (_documentMappingService.TryMapToGeneratedDocumentPosition(csharpDocument, index, out var mappedPosition, out _))
{
var generatedDocument = await remoteDocumentContext.GetGeneratedDocumentAsync(_filePathService, cancellationToken).ConfigureAwait(false);
// TODO: use correct options rather than default
var formattingOptions = new RoslynFormattingOptions();
var formattingOptions = new RoslynFormattingOptions()
{
InsertSpaces = !indentWithTabs,
TabSize = indentSize
};
var autoInsertResponseItem = await OnAutoInsert.GetOnAutoInsertResponseAsync(
generatedDocument,
mappedPosition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

using Microsoft.VisualStudio.Razor.Settings;
using RazorLSPConstants = Microsoft.VisualStudio.Razor.LanguageClient.RazorLSPConstants;
using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Microsoft.CodeAnalysis.Razor.Protocol.AutoInsert.RemoteInsertTextEdit?>;

Expand All @@ -32,6 +32,7 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor.LanguageClient.Cohost;
#pragma warning restore RS0030 // Do not use banned APIs
internal class CohostOnAutoInsertEndpoint(
IRemoteServiceInvoker remoteServiceInvoker,
IClientSettingsManager clientSettingsManager,
#pragma warning disable RS0030 // Do not use banned APIs
[ImportMany] IEnumerable<IOnAutoInsertTriggerCharacterProvider> onAutoInsertTriggerCharacterProviders,
#pragma warning restore RS0030 // Do not use banned APIs
Expand All @@ -41,6 +42,7 @@ internal class CohostOnAutoInsertEndpoint(
: AbstractRazorCohostDocumentRequestHandler<VSInternalDocumentOnAutoInsertParams, VSInternalDocumentOnAutoInsertResponseItem?>, IDynamicRegistrationProvider
{
private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
private readonly IClientSettingsManager _clientSettingsManager = clientSettingsManager;
private readonly IEnumerable<IOnAutoInsertTriggerCharacterProvider> _onAutoInsertTriggerCharacterProviders = onAutoInsertTriggerCharacterProviders;
private readonly IHtmlDocumentSynchronizer _htmlDocumentSynchronizer = htmlDocumentSynchronizer;
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
Expand Down Expand Up @@ -81,6 +83,12 @@ internal class CohostOnAutoInsertEndpoint(

_logger.LogDebug($"Resolving auto-insertion for {razorDocument.FilePath}");

var clientSettings = _clientSettingsManager.GetClientSettings();
var enableAutoClosingTags = clientSettings.AdvancedSettings.AutoClosingTags;
var formatOnType = clientSettings.AdvancedSettings.FormatOnType;
var indentWithTabs = clientSettings.ClientSpaceSettings.IndentWithTabs;
var indentSize = clientSettings.ClientSpaceSettings.IndentSize;

_logger.LogDebug($"Calling OOP to resolve insertion at {request.Position} invoked by typing '{request.Character}'");
var data = await _remoteServiceInvoker.TryInvokeAsync<IRemoteAutoInsertService, Response>(
razorDocument.Project.Solution,
Expand All @@ -90,7 +98,10 @@ internal class CohostOnAutoInsertEndpoint(
razorDocument.Id,
request.Position.ToLinePosition(),
request.Character,
autoCloseTags: true, // TODO: get value from client options
autoCloseTags: enableAutoClosingTags,
formatOnType: formatOnType,
indentWithTabs: indentWithTabs,
indentSize: indentSize,
cancellationToken),
cancellationToken).ConfigureAwait(false);

Expand Down

0 comments on commit dc85b28

Please sign in to comment.