PowerShell / PowerShell/PowerShellEditorServices

Discussion: Different method of passing $Context to Editor Commands

未关闭
#249 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Area-Extension Terminal Issue-Discussion
主要语言
C#
星标
767
派生
266
平均合并
3 天 16 小时
30 天内合并 PR
1

描述

I wanted to start a discussion on how PSES could improve the way it passes $Context to Editor Commands.

The way $Context is made accessible to an Editor Command currently, is it is passed as a parameter to the Command. This is not immediately evident and I can see this causing confusion.

In a scriptblock, this can be handled by adding a context param.

Register-EditorCommand `
        -Name "Teamviewer.ConnectDevice" `
        -DisplayName "Connect to Teamviewer Device" `
        -SuppressOutput `
        -ScriptBlock {
            param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context)
            Connect-Teamviewer
        }

However, if you were to just specify the function you may get some results you didn't expect.

Register-EditorCommand `
        -Name "Teamviewer.ConnectDevice" `
        -DisplayName "Connect to Teamviewer Device" `
        -SuppressOutput `
        -Function Connect-Teamviewer

In the above example, Connect-Teamviewer will be passed $context as a parameter when executed as an Editor Command.

function Connect-Teamviewer
{
    [Cmdletbinding()]
    param
    (
        [parameter(Mandatory=$true)]        
        [string]$ComputerName
    )

If your function looks something like the above code the command will actually error. $context will be passed to the $CompuerName parameter. Meaning you would need to write your functions with this in mind.

function Connect-Teamviewer
{
    [Cmdletbinding()]
    param
    (
        [parameter(Mandatory=$false)]        
        [[Microsoft.PowerShell.EditorServices.Extensions.EditorContext]]$Context,

        [parameter(Mandatory=$true)]        
        [string]$ComputerName
    )

This is likely not a good solution ether. It is going to be confusing to those writing their first commands, and there could potentially be some unwanted results. I have not tested it, but I wonder of the potential issues of a parameter not checking for a type and being passed context to do something like delete something.

function Delete-Stuff
{
    [Cmdletbinding()]
    param
    (     
        $Stuff
    )

    Process
    {
        foreach ($item in $Stuff) 
        { 
            Delete-AllTheThings $item 
        }
    }
}

One question I wanted to ask, could the information in $Context, be added to $psEditor? Is there a reason it has to be passed to the function @daviwil ?

$psEditor.context.cursorposition

Something along the lines of that. When I was first learning the Editor commands, this is sort of what I thought $psEditor was doing. I didn't realize there was a difference between $Context and $psEditor. @daviwil can you better explain the exact differences between the 2? I'd like to get a better grasp of exactly how each differ.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先跟踪 Register-EditorCommand、Function 和 ScriptBlock 的路径,并将 EditorContext 与 $psEditor 进行比较;issue 未指定任何文件或测试。实现前需要 maintainer 做出决定,并记录所需的行为和完成检查项。

由索引模型根据 Issue 内容生成。

评估

技术栈
powershell
领域
developer-experience
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。