-
Notifications
You must be signed in to change notification settings - Fork 237
Comment Help and Evaluate #1015
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
Merged
TylerLeonhardt
merged 2 commits into
PowerShell:omnisharp-lsp
from
TylerLeonhardt:omni-commenthelp
Aug 23, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
35 changes: 35 additions & 0 deletions
35
src/PowerShellEditorServices.Engine/Services/PowerShellContext/Handlers/EvaluateHandler.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.PowerShell.EditorServices; | ||
|
||
namespace PowerShellEditorServices.Engine.Services.Handlers | ||
{ | ||
public class EvaluateHandler : IEvaluateHandler | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly PowerShellContextService _powerShellContextService; | ||
|
||
public EvaluateHandler(ILoggerFactory factory, PowerShellContextService powerShellContextService) | ||
{ | ||
_logger = factory.CreateLogger<EvaluateHandler>(); | ||
_powerShellContextService = powerShellContextService; | ||
} | ||
|
||
public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request, CancellationToken cancellationToken) | ||
{ | ||
await _powerShellContextService.ExecuteScriptStringAsync( | ||
request.Expression, | ||
writeInputToHost: true, | ||
writeOutputToHost: true, | ||
addToHistory: true); | ||
|
||
return new EvaluateResponseBody | ||
{ | ||
Result = "", | ||
VariablesReference = 0 | ||
}; | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...erShellEditorServices.Engine/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation.Language; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.PowerShell.EditorServices; | ||
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer; | ||
|
||
namespace PowerShellEditorServices.Engine.Services.Handlers | ||
{ | ||
public class GetCommentHelpHandler : IGetCommentHelpHandler | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly WorkspaceService _workspaceService; | ||
private readonly AnalysisService _analysisService; | ||
private readonly SymbolsService _symbolsService; | ||
|
||
public GetCommentHelpHandler( | ||
ILoggerFactory factory, | ||
WorkspaceService workspaceService, | ||
AnalysisService analysisService, | ||
SymbolsService symbolsService) | ||
{ | ||
_logger = factory.CreateLogger<GetCommentHelpHandler>(); | ||
_workspaceService = workspaceService; | ||
_analysisService = analysisService; | ||
_symbolsService = symbolsService; | ||
} | ||
|
||
public async Task<CommentHelpRequestResult> Handle(CommentHelpRequestParams request, CancellationToken cancellationToken) | ||
{ | ||
var result = new CommentHelpRequestResult(); | ||
|
||
if (!_workspaceService.TryGetFile(request.DocumentUri, out ScriptFile scriptFile)) | ||
{ | ||
return result; | ||
} | ||
|
||
int triggerLine = (int) request.TriggerPosition.Line + 1; | ||
|
||
FunctionDefinitionAst functionDefinitionAst = _symbolsService.GetFunctionDefinitionForHelpComment( | ||
scriptFile, | ||
triggerLine, | ||
out string helpLocation); | ||
|
||
if (functionDefinitionAst == null) | ||
{ | ||
return result; | ||
} | ||
|
||
IScriptExtent funcExtent = functionDefinitionAst.Extent; | ||
string funcText = funcExtent.Text; | ||
if (helpLocation.Equals("begin")) | ||
{ | ||
// check if the previous character is `<` because it invalidates | ||
// the param block the follows it. | ||
IList<string> lines = ScriptFile.GetLinesInternal(funcText); | ||
int relativeTriggerLine0b = triggerLine - funcExtent.StartLineNumber; | ||
if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<", StringComparison.OrdinalIgnoreCase) > -1) | ||
{ | ||
lines[relativeTriggerLine0b] = string.Empty; | ||
} | ||
|
||
funcText = string.Join("\n", lines); | ||
} | ||
|
||
List<ScriptFileMarker> analysisResults = await _analysisService.GetSemanticMarkersAsync( | ||
funcText, | ||
AnalysisService.GetCommentHelpRuleSettings( | ||
enable: true, | ||
exportedOnly: false, | ||
blockComment: request.BlockComment, | ||
vscodeSnippetCorrection: true, | ||
placement: helpLocation)); | ||
|
||
string helpText = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; | ||
|
||
if (helpText == null) | ||
{ | ||
return result; | ||
} | ||
|
||
result.Content = ScriptFile.GetLinesInternal(helpText).ToArray(); | ||
|
||
if (helpLocation != null && | ||
!helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
// we need to trim the leading `{` and newline when helpLocation=="begin" | ||
// we also need to trim the leading newline when helpLocation=="end" | ||
result.Content = result.Content.Skip(1).ToArray(); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/PowerShellEditorServices.Engine/Services/PowerShellContext/Handlers/IEvaluateHandler.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using OmniSharp.Extensions.Embedded.MediatR; | ||
using OmniSharp.Extensions.JsonRpc; | ||
|
||
namespace PowerShellEditorServices.Engine.Services.Handlers | ||
{ | ||
[Serial, Method("evaluate")] | ||
public interface IEvaluateHandler : IJsonRpcRequestHandler<EvaluateRequestArguments, EvaluateResponseBody> { } | ||
|
||
public class EvaluateRequestArguments : IRequest<EvaluateResponseBody> | ||
{ | ||
/// <summary> | ||
/// The expression to evaluate. | ||
/// </summary> | ||
public string Expression { get; set; } | ||
|
||
/// <summary> | ||
/// The context in which the evaluate request is run. Possible | ||
/// values are 'watch' if evaluate is run in a watch or 'repl' | ||
/// if run from the REPL console. | ||
/// </summary> | ||
public string Context { get; set; } | ||
|
||
/// <summary> | ||
/// Evaluate the expression in the context of this stack frame. | ||
/// If not specified, the top most frame is used. | ||
/// </summary> | ||
public int FrameId { get; set; } | ||
} | ||
|
||
public class EvaluateResponseBody | ||
{ | ||
/// <summary> | ||
/// The evaluation result. | ||
/// </summary> | ||
public string Result { get; set; } | ||
|
||
/// <summary> | ||
/// If variablesReference is > 0, the evaluate result is | ||
/// structured and its children can be retrieved by passing | ||
/// variablesReference to the VariablesRequest | ||
/// </summary> | ||
public int VariablesReference { get; set; } | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...rShellEditorServices.Engine/Services/PowerShellContext/Handlers/IGetCommentHelpHandler.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using OmniSharp.Extensions.Embedded.MediatR; | ||
using OmniSharp.Extensions.JsonRpc; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer | ||
{ | ||
[Serial, Method("powerShell/getCommentHelp")] | ||
public interface IGetCommentHelpHandler : IJsonRpcRequestHandler<CommentHelpRequestParams, CommentHelpRequestResult> { } | ||
|
||
public class CommentHelpRequestResult | ||
{ | ||
public string[] Content { get; set; } | ||
} | ||
|
||
public class CommentHelpRequestParams : IRequest<CommentHelpRequestResult> | ||
{ | ||
public string DocumentUri { get; set; } | ||
public Position TriggerPosition { get; set; } | ||
public bool BlockComment { get; set; } | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
Does a handler for our own thing require a document selector?
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.
Nope because we control when it gets fired