-
Notifications
You must be signed in to change notification settings - Fork 223
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
TylerLeonhardt
merged 1 commit into
PowerShell:master
from
TylerLeonhardt:add-better-logging-for-formatting
Mar 11, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
private DocumentRangeFormattingCapability _documentRangeFormattingCapability; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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 | ||
|
@@ -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; | ||
|
@@ -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 | ||
{ | ||
|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue found: Remove this unread private field '_documentFormattingCapability' or refactor the code to use its value.