Skip to content

Commit dc85b28

Browse files
committed
Plumbing through actual option values we need and using them
1 parent 36a3d5a commit dc85b28

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ ValueTask<Response> TryResolveInsertionAsync(
1818
LinePosition position,
1919
string character,
2020
bool autoCloseTags,
21+
bool formatOnType,
22+
bool indentWithTabs,
23+
int indentSize,
2124
CancellationToken cancellationToken);
2225
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
1515
using Microsoft.CodeAnalysis.Text;
1616
using Microsoft.VisualStudio.LanguageServer.Protocol;
17-
17+
using Microsoft.VisualStudio.Text.Editor;
1818
using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Microsoft.CodeAnalysis.Razor.Protocol.AutoInsert.RemoteInsertTextEdit?>;
1919
using RoslynFormattingOptions = Roslyn.LanguageServer.Protocol.FormattingOptions;
2020

@@ -42,6 +42,9 @@ public ValueTask<Response> TryResolveInsertionAsync(
4242
LinePosition linePosition,
4343
string character,
4444
bool autoCloseTags,
45+
bool formatOnType,
46+
bool indentWithTabs,
47+
int indentSize,
4548
CancellationToken cancellationToken)
4649
=> RunServiceAsync(
4750
solutionInfo,
@@ -51,6 +54,9 @@ public ValueTask<Response> TryResolveInsertionAsync(
5154
linePosition,
5255
character,
5356
autoCloseTags,
57+
formatOnType,
58+
indentWithTabs,
59+
indentSize,
5460
cancellationToken),
5561
cancellationToken);
5662

@@ -59,6 +65,9 @@ private async ValueTask<Response> TryResolveInsertionAsync(
5965
LinePosition linePosition,
6066
string character,
6167
bool autoCloseTags,
68+
bool formatOnType,
69+
bool indentWithTabs,
70+
int indentSize,
6271
CancellationToken cancellationToken)
6372
{
6473
var sourceText = await remoteDocumentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
@@ -103,12 +112,31 @@ private async ValueTask<Response> TryResolveInsertionAsync(
103112
return Response.NoFurtherHandling;
104113
}
105114

115+
// Special case for C# where we use AutoInsert for two purposes:
116+
// 1. For XML documentation comments (filling out the template when typing "///")
117+
// 2. For "on type formatting" style behavior, like adjusting indentation when pressing Enter inside empty braces
118+
//
119+
// If users have turned off on-type formatting, they don't want the behavior of number 2, but its impossible to separate
120+
// that out from number 1. Typing "///" could just as easily adjust indentation on some unrelated code higher up in the
121+
// file, which is exactly the behavior users complain about.
122+
//
123+
// Therefore we are just going to no-op if the user has turned off on type formatting. Maybe one day we can make this
124+
// smarter, but at least the user can always turn the setting back on, type their "///", and turn it back off, without
125+
// having to restart VS. Not the worst compromise (hopefully!)
126+
if (!formatOnType)
127+
{
128+
return Response.NoFurtherHandling;
129+
}
130+
106131
var csharpDocument = codeDocument.GetCSharpDocument();
107132
if (_documentMappingService.TryMapToGeneratedDocumentPosition(csharpDocument, index, out var mappedPosition, out _))
108133
{
109134
var generatedDocument = await remoteDocumentContext.GetGeneratedDocumentAsync(_filePathService, cancellationToken).ConfigureAwait(false);
110-
// TODO: use correct options rather than default
111-
var formattingOptions = new RoslynFormattingOptions();
135+
var formattingOptions = new RoslynFormattingOptions()
136+
{
137+
InsertSpaces = !indentWithTabs,
138+
TabSize = indentSize
139+
};
112140
var autoInsertResponseItem = await OnAutoInsert.GetOnAutoInsertResponseAsync(
113141
generatedDocument,
114142
mappedPosition,

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostOnAutoInsertEndpoint.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
1818
using Microsoft.VisualStudio.LanguageServer.Protocol;
1919
using Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
20-
20+
using Microsoft.VisualStudio.Razor.Settings;
2121
using RazorLSPConstants = Microsoft.VisualStudio.Razor.LanguageClient.RazorLSPConstants;
2222
using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Microsoft.CodeAnalysis.Razor.Protocol.AutoInsert.RemoteInsertTextEdit?>;
2323

@@ -32,6 +32,7 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor.LanguageClient.Cohost;
3232
#pragma warning restore RS0030 // Do not use banned APIs
3333
internal class CohostOnAutoInsertEndpoint(
3434
IRemoteServiceInvoker remoteServiceInvoker,
35+
IClientSettingsManager clientSettingsManager,
3536
#pragma warning disable RS0030 // Do not use banned APIs
3637
[ImportMany] IEnumerable<IOnAutoInsertTriggerCharacterProvider> onAutoInsertTriggerCharacterProviders,
3738
#pragma warning restore RS0030 // Do not use banned APIs
@@ -41,6 +42,7 @@ internal class CohostOnAutoInsertEndpoint(
4142
: AbstractRazorCohostDocumentRequestHandler<VSInternalDocumentOnAutoInsertParams, VSInternalDocumentOnAutoInsertResponseItem?>, IDynamicRegistrationProvider
4243
{
4344
private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
45+
private readonly IClientSettingsManager _clientSettingsManager = clientSettingsManager;
4446
private readonly IEnumerable<IOnAutoInsertTriggerCharacterProvider> _onAutoInsertTriggerCharacterProviders = onAutoInsertTriggerCharacterProviders;
4547
private readonly IHtmlDocumentSynchronizer _htmlDocumentSynchronizer = htmlDocumentSynchronizer;
4648
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
@@ -81,6 +83,12 @@ internal class CohostOnAutoInsertEndpoint(
8183

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

86+
var clientSettings = _clientSettingsManager.GetClientSettings();
87+
var enableAutoClosingTags = clientSettings.AdvancedSettings.AutoClosingTags;
88+
var formatOnType = clientSettings.AdvancedSettings.FormatOnType;
89+
var indentWithTabs = clientSettings.ClientSpaceSettings.IndentWithTabs;
90+
var indentSize = clientSettings.ClientSpaceSettings.IndentSize;
91+
8492
_logger.LogDebug($"Calling OOP to resolve insertion at {request.Position} invoked by typing '{request.Character}'");
8593
var data = await _remoteServiceInvoker.TryInvokeAsync<IRemoteAutoInsertService, Response>(
8694
razorDocument.Project.Solution,
@@ -90,7 +98,10 @@ internal class CohostOnAutoInsertEndpoint(
9098
razorDocument.Id,
9199
request.Position.ToLinePosition(),
92100
request.Character,
93-
autoCloseTags: true, // TODO: get value from client options
101+
autoCloseTags: enableAutoClosingTags,
102+
formatOnType: formatOnType,
103+
indentWithTabs: indentWithTabs,
104+
indentSize: indentSize,
94105
cancellationToken),
95106
cancellationToken).ConfigureAwait(false);
96107

0 commit comments

Comments
 (0)