Skip to content

Commit 487454a

Browse files
Add better logging for formatter and refactor it into 1 class (#1228)
1 parent 5b7af3f commit 487454a

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public async Task StartAsync()
7373
.WithHandler<GetVersionHandler>()
7474
.WithHandler<ConfigurationHandler>()
7575
.WithHandler<FoldingRangeHandler>()
76-
.WithHandler<DocumentFormattingHandler>()
77-
.WithHandler<DocumentRangeFormattingHandler>()
76+
.WithHandler<DocumentFormattingHandlers>()
7877
.WithHandler<ReferencesHandler>()
7978
.WithHandler<DocumentSymbolHandler>()
8079
.WithHandler<DocumentHighlightHandler>()

src/PowerShellEditorServices/Services/TextDocument/Handlers/FormattingHandlers.cs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,24 @@
1414

1515
namespace Microsoft.PowerShell.EditorServices.Handlers
1616
{
17-
internal class DocumentFormattingHandler : IDocumentFormattingHandler
17+
// TODO: Add IDocumentOnTypeFormatHandler to support on-type formatting.
18+
internal class DocumentFormattingHandlers : IDocumentFormattingHandler, IDocumentRangeFormattingHandler
1819
{
1920
private readonly ILogger _logger;
2021
private readonly AnalysisService _analysisService;
2122
private readonly ConfigurationService _configurationService;
2223
private readonly WorkspaceService _workspaceService;
23-
private DocumentFormattingCapability _capability;
2424

25-
public DocumentFormattingHandler(ILoggerFactory factory, AnalysisService analysisService, ConfigurationService configurationService, WorkspaceService workspaceService)
25+
private DocumentFormattingCapability _documentFormattingCapability;
26+
private DocumentRangeFormattingCapability _documentRangeFormattingCapability;
27+
28+
public DocumentFormattingHandlers(
29+
ILoggerFactory factory,
30+
AnalysisService analysisService,
31+
ConfigurationService configurationService,
32+
WorkspaceService workspaceService)
2633
{
27-
_logger = factory.CreateLogger<DocumentFormattingHandler>();
34+
_logger = factory.CreateLogger<DocumentFormattingHandlers>();
2835
_analysisService = analysisService;
2936
_configurationService = configurationService;
3037
_workspaceService = workspaceService;
@@ -43,7 +50,8 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
4350
var scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
4451
var pssaSettings = _configurationService.CurrentSettings.CodeFormatting.GetPSSASettingsHashtable(
4552
(int)request.Options.TabSize,
46-
request.Options.InsertSpaces);
53+
request.Options.InsertSpaces,
54+
_logger);
4755

4856

4957
// TODO raise an error event in case format returns null
@@ -79,42 +87,13 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
7987
});
8088
}
8189

82-
public void SetCapability(DocumentFormattingCapability capability)
83-
{
84-
_capability = capability;
85-
}
86-
}
87-
88-
internal class DocumentRangeFormattingHandler : IDocumentRangeFormattingHandler
89-
{
90-
private readonly ILogger _logger;
91-
private readonly AnalysisService _analysisService;
92-
private readonly ConfigurationService _configurationService;
93-
private readonly WorkspaceService _workspaceService;
94-
private DocumentRangeFormattingCapability _capability;
95-
96-
public DocumentRangeFormattingHandler(ILoggerFactory factory, AnalysisService analysisService, ConfigurationService configurationService, WorkspaceService workspaceService)
97-
{
98-
_logger = factory.CreateLogger<DocumentRangeFormattingHandler>();
99-
_analysisService = analysisService;
100-
_configurationService = configurationService;
101-
_workspaceService = workspaceService;
102-
}
103-
104-
public TextDocumentRegistrationOptions GetRegistrationOptions()
105-
{
106-
return new TextDocumentRegistrationOptions
107-
{
108-
DocumentSelector = LspUtils.PowerShellDocumentSelector
109-
};
110-
}
111-
11290
public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams request, CancellationToken cancellationToken)
11391
{
11492
var scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
11593
var pssaSettings = _configurationService.CurrentSettings.CodeFormatting.GetPSSASettingsHashtable(
11694
(int)request.Options.TabSize,
117-
request.Options.InsertSpaces);
95+
request.Options.InsertSpaces,
96+
_logger);
11897

11998
// TODO raise an error event in case format returns null;
12099
string formattedScript;
@@ -137,17 +116,24 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
137116
};
138117

139118
Range range = request.Range;
140-
var rangeList = range == null ? null : new int[] {
119+
var rangeList = range == null ? null : new int[]
120+
{
141121
(int)range.Start.Line + 1,
142122
(int)range.Start.Character + 1,
143123
(int)range.End.Line + 1,
144-
(int)range.End.Character + 1};
124+
(int)range.End.Character + 1
125+
};
145126

146127
formattedScript = await _analysisService.FormatAsync(
147128
scriptFile.Contents,
148129
pssaSettings,
149130
rangeList).ConfigureAwait(false);
150-
formattedScript = formattedScript ?? scriptFile.Contents;
131+
132+
if (formattedScript == null)
133+
{
134+
_logger.LogWarning("Formatting returned null. Returning original contents for file: {0}", scriptFile.DocumentUri);
135+
formattedScript = scriptFile.Contents;
136+
}
151137

152138
return new TextEditContainer(new TextEdit
153139
{
@@ -156,9 +142,14 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
156142
});
157143
}
158144

145+
public void SetCapability(DocumentFormattingCapability capability)
146+
{
147+
_documentFormattingCapability = capability;
148+
}
149+
159150
public void SetCapability(DocumentRangeFormattingCapability capability)
160151
{
161-
_capability = capability;
152+
_documentRangeFormattingCapability = capability;
162153
}
163154
}
164155
}

src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Security;
1212
using Microsoft.Extensions.Logging;
1313
using Microsoft.PowerShell.EditorServices.Logging;
14+
using Newtonsoft.Json;
1415

1516
namespace Microsoft.PowerShell.EditorServices.Services.Configuration
1617
{
@@ -223,7 +224,8 @@ public CodeFormattingSettings(CodeFormattingSettings codeFormattingSettings)
223224
/// <returns></returns>
224225
public Hashtable GetPSSASettingsHashtable(
225226
int tabSize,
226-
bool insertSpaces)
227+
bool insertSpaces,
228+
ILogger logger)
227229
{
228230
var settings = GetCustomPSSASettingsHashtable(tabSize, insertSpaces);
229231
var ruleSettings = (Hashtable)(settings["Rules"]);
@@ -253,6 +255,7 @@ public Hashtable GetPSSASettingsHashtable(
253255
break;
254256
}
255257

258+
logger.LogDebug("Created formatting hashtable: {0}", JsonConvert.SerializeObject(settings));
256259
return settings;
257260
}
258261

0 commit comments

Comments
 (0)