如何在psprovider中支持powershell选项卡扩展?

发布于 2024-11-16 08:43:35 字数 276 浏览 5 评论 0原文

我正在为一些内部分层数据实现 Powershell PSProvider。一切正常,我可以使用常用的 cd/dir 命令浏览树,唯一不起作用的是制表符完成。

我可以看到,当按下 Tab 时,Powershell 会调用路径中带有星号的函数 GetChildName() (如果我输入“dir c”并按下 Tab,GetChildName() 函数将使用字符串“c*”多次调用)。我尝试返回以“c”开头的文件夹中的所有子名称,但 Powershell 始终仅在前面显示第一个子名称。我找不到有关此行为的任何文档,我缺少什么?

I'm implementing Powershell PSProvider for some internal hierarchical data. Everything works fine, I can navigate through the tree with usual cd/dir commands, the only thing doesn't work is tab completion.

What I can see is that Powershell calls function GetChildName() with an asterisk in the path when Tab is pressed (if I type "dir c" and press Tab, GetChildName() function will be called with string "c*", several times). I tried to return all child names from the folder that begins with "c", but Powershell always displays just the first child name in the front. I can't find any documentation about this behavior, what I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2024-11-23 08:43:36

您确定您看到的不仅仅是正常行为吗?使用默认的选项卡扩展,您只会看到第一个结果。多次按 Tab 键将循环显示提供程序返回的结果列表。

提供商有一些怪癖。我一直在使用 Script Provider 项目开发一个项目。我将调试代码放入所有方法中,以查看 PowerShell 正在调用哪些方法、何时调用以及使用什么参数。

Are you sure you aren't just seeing normal behavior? With the default Tab Expansion, you will only see the first result. Pressing tab additional times will cycle through the list of returned results from the provider.

There are some quirks with providers. I have been working on one using the Script Provider project. I put debug code in all my methods to see which ones PowerShell was calling, when, and with what arguments.

白况 2024-11-23 08:43:36

我发现问题出在哪里 - 如果星号是名称的一部分,则提供程序中的函数 GetChildName() 不应尝试扩展给定的文件名;如果该函数可以找到完全匹配的子名称,则应返回子名称,或者在任何其他情况下调用 base.GetChildName()。像这样的事情:

protected override string GetChildName(string path) {
    string name = SomeFunctionThatTriesToFindExactMatchForGivenPath(path);

    if(string.IsNullOrEmpty( ret ) )
        ret = base.GetChildName( path );

    return ret;
}

顺便说一句,我发现默认的制表符扩展对于可以从 GetChildName() 函数返回的内容非常宽容 - 即使返回值的前/后有斜杠/反斜杠,制表符扩展也会起作用。但流行的选项卡扩展模块 PowerTab 对返回值更加挑剔。

I found where's the problem - function GetChildName() in the provider shouldn't try to expand given file name if asterisk is part of the name; The function should return child name if it can find an exact match, or call base.GetChildName() in any other case. Something like this:

protected override string GetChildName(string path) {
    string name = SomeFunctionThatTriesToFindExactMatchForGivenPath(path);

    if(string.IsNullOrEmpty( ret ) )
        ret = base.GetChildName( path );

    return ret;
}

BTW, I found that default tab expansion is very forgiving about stuff that can be returned from GetChildName() function - even if return value have slash/backslash in the front/back, tab expansion will work. But PowerTab, popular tab expansion module, is much more picky about return values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文