Skip to content

Commit 7fa57e3

Browse files
authored
Merge pull request #516 from kapilmb/update-formatter
Add request handlers for document formatting
2 parents 772383b + d74d369 commit 7fa57e3

File tree

8 files changed

+338
-82
lines changed

8 files changed

+338
-82
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
9+
{
10+
public class DocumentFormattingRequest
11+
{
12+
public static readonly RequestType<DocumentFormattingParams, TextEdit[], object, TextDocumentRegistrationOptions> Type = RequestType<DocumentFormattingParams, TextEdit[], object, TextDocumentRegistrationOptions>.Create("textDocument/formatting");
13+
}
14+
15+
public class DocumentRangeFormattingRequest
16+
{
17+
public static readonly RequestType<DocumentRangeFormattingParams, TextEdit[], object, TextDocumentRegistrationOptions> Type = RequestType<DocumentRangeFormattingParams, TextEdit[], object, TextDocumentRegistrationOptions>.Create("textDocument/rangeFormatting");
18+
19+
}
20+
21+
public class DocumentOnTypeFormattingRequest
22+
{
23+
public static readonly RequestType<DocumentOnTypeFormattingRequest, TextEdit[], object, TextDocumentRegistrationOptions> Type = RequestType<DocumentOnTypeFormattingRequest, TextEdit[], object, TextDocumentRegistrationOptions>.Create("textDocument/onTypeFormatting");
24+
25+
}
26+
27+
public class DocumentRangeFormattingParams
28+
{
29+
/// <summary>
30+
/// The document to format.
31+
/// </summary>
32+
public TextDocumentIdentifier TextDocument { get; set; }
33+
34+
/// <summary>
35+
/// The range to format.
36+
/// </summary>
37+
/// <returns></returns>
38+
public Range Range { get; set; }
39+
40+
/// <summary>
41+
/// The format options.
42+
/// </summary>
43+
public FormattingOptions Options { get; set; }
44+
}
45+
46+
public class DocumentOnTypeFormattingParams
47+
{
48+
/// <summary>
49+
/// The document to format.
50+
/// </summary>
51+
public TextDocumentIdentifier TextDocument { get; set; }
52+
53+
/// <summary>
54+
/// The position at which this request was sent.
55+
/// </summary>
56+
public Position Position { get; set; }
57+
58+
/// <summary>
59+
/// The character that has been typed.
60+
/// </summary>
61+
public string ch { get; set; }
62+
63+
/// <summary>
64+
/// The format options.
65+
/// </summary>
66+
public FormattingOptions options { get; set; }
67+
}
68+
69+
public class DocumentFormattingParams
70+
{
71+
/// <summary>
72+
/// The document to format.
73+
/// </summary>
74+
public TextDocumentIdentifier TextDocument { get; set; }
75+
76+
/// <summary>
77+
/// The format options.
78+
/// </summary>
79+
public FormattingOptions options { get; set; }
80+
}
81+
82+
public class FormattingOptions
83+
{
84+
/// <summary>
85+
/// Size of a tab in spaces.
86+
/// </summary>
87+
public int TabSize { get; set; }
88+
89+
/// <summary>
90+
/// Prefer spaces over tabs.
91+
/// </summary>
92+
public bool InsertSpaces { get; set; }
93+
}
94+
}
95+

src/PowerShellEditorServices.Protocol/LanguageServer/Hover.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Hover
2323
{
2424
public MarkedString[] Contents { get; set; }
2525

26-
public Range? Range { get; set; }
26+
public Range Range { get; set; }
2727
}
2828

2929
public class HoverRequest

src/PowerShellEditorServices.Protocol/LanguageServer/ScriptFileMarkersRequest.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/PowerShellEditorServices.Protocol/LanguageServer/TextDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public class TextDocumentChangeEvent
237237
/// Gets or sets the Range where the document was changed. Will
238238
/// be null if the server's TextDocumentSyncKind is Full.
239239
/// </summary>
240-
public Range? Range { get; set; }
240+
public Range Range { get; set; }
241241

242242
/// <summary>
243243
/// Gets or sets the length of the Range being replaced in the
@@ -267,7 +267,7 @@ public class Position
267267
}
268268

269269
[DebuggerDisplay("Start = {Start.Line}:{Start.Character}, End = {End.Line}:{End.Character}")]
270-
public struct Range
270+
public class Range
271271
{
272272
/// <summary>
273273
/// Gets or sets the starting position of the range.

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public void Start()
104104
this.messageHandlers.SetRequestHandler(HoverRequest.Type, this.HandleHoverRequest);
105105
this.messageHandlers.SetRequestHandler(WorkspaceSymbolRequest.Type, this.HandleWorkspaceSymbolRequest);
106106
this.messageHandlers.SetRequestHandler(CodeActionRequest.Type, this.HandleCodeActionRequest);
107+
this.messageHandlers.SetRequestHandler(DocumentFormattingRequest.Type, this.HandleDocumentFormattingRequest);
108+
this.messageHandlers.SetRequestHandler(
109+
DocumentRangeFormattingRequest.Type,
110+
this.HandleDocumentRangeFormattingRequest);
107111

108112
this.messageHandlers.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest);
109113
this.messageHandlers.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);
@@ -123,7 +127,6 @@ public void Start()
123127
this.messageHandlers.SetRequestHandler(GetPSSARulesRequest.Type, this.HandleGetPSSARulesRequest);
124128
this.messageHandlers.SetRequestHandler(SetPSSARulesRequest.Type, this.HandleSetPSSARulesRequest);
125129

126-
this.messageHandlers.SetRequestHandler(ScriptFileMarkersRequest.Type, this.HandleScriptFileMarkersRequest);
127130
this.messageHandlers.SetRequestHandler(ScriptRegionRequest.Type, this.HandleGetFormatScriptRegionRequest);
128131

129132
this.messageHandlers.SetRequestHandler(GetPSHostProcessesRequest.Type, this.HandleGetPSHostProcessesRequest);
@@ -200,7 +203,8 @@ await requestContext.SendResult(
200203
SignatureHelpProvider = new SignatureHelpOptions
201204
{
202205
TriggerCharacters = new string[] { " " } // TODO: Other characters here?
203-
}
206+
},
207+
DocumentFormattingProvider = false
204208
}
205209
});
206210
}
@@ -292,19 +296,6 @@ await requestContext.SendResult(new ScriptRegionRequestResult
292296
});
293297
}
294298

295-
private async Task HandleScriptFileMarkersRequest(
296-
ScriptFileMarkerRequestParams requestParams,
297-
RequestContext<ScriptFileMarkerRequestResultParams> requestContext)
298-
{
299-
var markers = await editorSession.AnalysisService.GetSemanticMarkersAsync(
300-
editorSession.Workspace.GetFile(requestParams.fileUri),
301-
AnalysisService.GetPSSASettingsHashtable(requestParams.settings));
302-
await requestContext.SendResult(new ScriptFileMarkerRequestResultParams
303-
{
304-
markers = markers
305-
});
306-
}
307-
308299
private async Task HandleGetPSSARulesRequest(
309300
object param,
310301
RequestContext<object> requestContext)
@@ -541,7 +532,7 @@ protected Task HandleDidChangeTextDocumentNotification(
541532

542533
changedFile.ApplyChange(
543534
GetFileChangeDetails(
544-
textChange.Range.Value,
535+
textChange.Range,
545536
textChange.Text));
546537

547538
changedFiles.Add(changedFile);
@@ -873,7 +864,7 @@ await editorSession
873864
textDocumentPositionParams.Position.Character + 1);
874865

875866
List<MarkedString> symbolInfo = new List<MarkedString>();
876-
Range? symbolRange = null;
867+
Range symbolRange = null;
877868

878869
if (symbolDetails != null)
879870
{
@@ -1155,7 +1146,45 @@ await requestContext.SendResult(
11551146
codeActionCommands.ToArray());
11561147
}
11571148

1158-
protected Task HandleEvaluateRequest(
1149+
protected async Task HandleDocumentFormattingRequest(
1150+
DocumentFormattingParams formattingParams,
1151+
RequestContext<TextEdit[]> requestContext)
1152+
{
1153+
var result = await Format(
1154+
formattingParams.TextDocument.Uri,
1155+
formattingParams.options,
1156+
null);
1157+
1158+
await requestContext.SendResult(new TextEdit[1]
1159+
{
1160+
new TextEdit
1161+
{
1162+
NewText = result.Item1,
1163+
Range = result.Item2
1164+
},
1165+
});
1166+
}
1167+
1168+
protected async Task HandleDocumentRangeFormattingRequest(
1169+
DocumentRangeFormattingParams formattingParams,
1170+
RequestContext<TextEdit[]> requestContext)
1171+
{
1172+
var result = await Format(
1173+
formattingParams.TextDocument.Uri,
1174+
formattingParams.Options,
1175+
formattingParams.Range);
1176+
1177+
await requestContext.SendResult(new TextEdit[1]
1178+
{
1179+
new TextEdit
1180+
{
1181+
NewText = result.Item1,
1182+
Range = result.Item2
1183+
},
1184+
});
1185+
}
1186+
1187+
protected Task HandleEvaluateRequest(
11591188
DebugAdapterMessages.EvaluateRequestArguments evaluateParams,
11601189
RequestContext<DebugAdapterMessages.EvaluateResponseBody> requestContext)
11611190
{
@@ -1193,6 +1222,49 @@ protected Task HandleEvaluateRequest(
11931222

11941223
#region Event Handlers
11951224

1225+
private async Task<Tuple<string, Range>> Format(
1226+
string documentUri,
1227+
FormattingOptions options,
1228+
Range range)
1229+
{
1230+
var scriptFile = editorSession.Workspace.GetFile(documentUri);
1231+
var pssaSettings = currentSettings.CodeFormatting.GetPSSASettingsHashTable(
1232+
options.TabSize,
1233+
options.InsertSpaces);
1234+
1235+
// TODO raise an error event in case format returns null;
1236+
string formattedScript;
1237+
Range editRange;
1238+
var rangeList = range == null ? null : new int[] {
1239+
range.Start.Line + 1,
1240+
range.Start.Character + 1,
1241+
range.End.Line + 1,
1242+
range.End.Character + 1};
1243+
var extent = scriptFile.ScriptAst.Extent;
1244+
1245+
// todo create an extension for converting range to script extent
1246+
editRange = new Range
1247+
{
1248+
Start = new Position
1249+
{
1250+
Line = extent.StartLineNumber - 1,
1251+
Character = extent.StartColumnNumber - 1
1252+
},
1253+
End = new Position
1254+
{
1255+
Line = extent.EndLineNumber - 1,
1256+
Character = extent.EndColumnNumber - 1
1257+
}
1258+
};
1259+
1260+
formattedScript = await editorSession.AnalysisService.Format(
1261+
scriptFile.Contents,
1262+
pssaSettings,
1263+
rangeList);
1264+
formattedScript = formattedScript ?? scriptFile.Contents;
1265+
return Tuple.Create(formattedScript, editRange);
1266+
}
1267+
11961268
private async void PowerShellContext_RunspaceChanged(object sender, Session.RunspaceChangedEventArgs e)
11971269
{
11981270
await this.messageSender.SendEvent(

0 commit comments

Comments
 (0)