Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better logging for formatter and refactor it into 1 class #1228

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public async Task StartAsync()
.WithHandler<GetVersionHandler>()
.WithHandler<ConfigurationHandler>()
.WithHandler<FoldingRangeHandler>()
.WithHandler<DocumentFormattingHandler>()
.WithHandler<DocumentRangeFormattingHandler>()
.WithHandler<DocumentFormattingHandlers>()
.WithHandler<ReferencesHandler>()
.WithHandler<DocumentSymbolHandler>()
.WithHandler<DocumentHighlightHandler>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@

namespace Microsoft.PowerShell.EditorServices.Handlers
{
internal class DocumentFormattingHandler : IDocumentFormattingHandler
// TODO: Add IDocumentOnTypeFormatHandler to support on-type formatting.
internal class DocumentFormattingHandlers : IDocumentFormattingHandler, IDocumentRangeFormattingHandler
{
private readonly ILogger _logger;
private readonly AnalysisService _analysisService;
private readonly ConfigurationService _configurationService;
private readonly WorkspaceService _workspaceService;
private DocumentFormattingCapability _capability;

public DocumentFormattingHandler(ILoggerFactory factory, AnalysisService analysisService, ConfigurationService configurationService, WorkspaceService workspaceService)
private DocumentFormattingCapability _documentFormattingCapability;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private DocumentRangeFormattingCapability _documentRangeFormattingCapability;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


public DocumentFormattingHandlers(
ILoggerFactory factory,
AnalysisService analysisService,
ConfigurationService configurationService,
WorkspaceService workspaceService)
{
_logger = factory.CreateLogger<DocumentFormattingHandler>();
_logger = factory.CreateLogger<DocumentFormattingHandlers>();
_analysisService = analysisService;
_configurationService = configurationService;
_workspaceService = workspaceService;
Expand All @@ -43,7 +50,8 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
var scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
var pssaSettings = _configurationService.CurrentSettings.CodeFormatting.GetPSSASettingsHashtable(
(int)request.Options.TabSize,
request.Options.InsertSpaces);
request.Options.InsertSpaces,
_logger);


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

public void SetCapability(DocumentFormattingCapability capability)
{
_capability = capability;
}
}

internal class DocumentRangeFormattingHandler : IDocumentRangeFormattingHandler
{
private readonly ILogger _logger;
private readonly AnalysisService _analysisService;
private readonly ConfigurationService _configurationService;
private readonly WorkspaceService _workspaceService;
private DocumentRangeFormattingCapability _capability;

public DocumentRangeFormattingHandler(ILoggerFactory factory, AnalysisService analysisService, ConfigurationService configurationService, WorkspaceService workspaceService)
{
_logger = factory.CreateLogger<DocumentRangeFormattingHandler>();
_analysisService = analysisService;
_configurationService = configurationService;
_workspaceService = workspaceService;
}

public TextDocumentRegistrationOptions GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions
{
DocumentSelector = LspUtils.PowerShellDocumentSelector
};
}

public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams request, CancellationToken cancellationToken)
{
var scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
var pssaSettings = _configurationService.CurrentSettings.CodeFormatting.GetPSSASettingsHashtable(
(int)request.Options.TabSize,
request.Options.InsertSpaces);
request.Options.InsertSpaces,
_logger);

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

Range range = request.Range;
var rangeList = range == null ? null : new int[] {
var rangeList = range == null ? null : new int[]
{
(int)range.Start.Line + 1,
(int)range.Start.Character + 1,
(int)range.End.Line + 1,
(int)range.End.Character + 1};
(int)range.End.Character + 1
};

formattedScript = await _analysisService.FormatAsync(
scriptFile.Contents,
pssaSettings,
rangeList).ConfigureAwait(false);
formattedScript = formattedScript ?? scriptFile.Contents;

if (formattedScript == null)
{
_logger.LogWarning("Formatting returned null. Returning original contents for file: {0}", scriptFile.DocumentUri);
formattedScript = scriptFile.Contents;
}

return new TextEditContainer(new TextEdit
{
Expand All @@ -156,9 +142,14 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
});
}

public void SetCapability(DocumentFormattingCapability capability)
{
_documentFormattingCapability = capability;
}

public void SetCapability(DocumentRangeFormattingCapability capability)
{
_capability = capability;
_documentRangeFormattingCapability = capability;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Security;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Logging;
using Newtonsoft.Json;

namespace Microsoft.PowerShell.EditorServices.Services.Configuration
{
Expand Down Expand Up @@ -223,7 +224,8 @@ public CodeFormattingSettings(CodeFormattingSettings codeFormattingSettings)
/// <returns></returns>
public Hashtable GetPSSASettingsHashtable(
int tabSize,
bool insertSpaces)
bool insertSpaces,
ILogger logger)
{
var settings = GetCustomPSSASettingsHashtable(tabSize, insertSpaces);
var ruleSettings = (Hashtable)(settings["Rules"]);
Expand Down Expand Up @@ -253,6 +255,7 @@ public Hashtable GetPSSASettingsHashtable(
break;
}

logger.LogDebug("Created formatting hashtable: {0}", JsonConvert.SerializeObject(settings));
return settings;
}

Expand Down