You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击VS Code装饰悬停命令URI后,如何稳定内联补全?

VS Code装饰悬停命令触发内联补全后立即消失的解决办法

问题场景

我通过以下代码创建VS Code装饰:

// editor: vscode.TextEditor
editor.setDecorations(decorationType, [{
    range,
    hoverMessage: new MarkdownString('[show]($(Uri.parse(\'command:command.show\')))')
}])

已注册command.show命令用于触发内联补全:

vscode.commands.registerCommand('command.show', () => {
    commands.executeCommand('editor.action.inlineSuggest.trigger');
})

内联补全已完成注册且可手动触发,但点击命令URI后,内联补全会出现后立即消失。尝试设置editor.selections、editor.revealRange或执行cursorMove均无效,推测原因是光标聚焦在悬停消息的命令URI上。需要实现点击后将光标重新聚焦到编辑器,或隐藏悬停消息。


解决方法

方法1:强制编辑器聚焦+延迟触发补全

悬停面板点击命令后会短暂占用焦点,先强制让编辑器重新获得焦点,再延迟一点触发内联补全:

vscode.commands.registerCommand('command.show', async () => {
    // 强制激活当前编辑器,获取焦点
    await vscode.window.showTextDocument(vscode.window.activeTextEditor.document);
    // 小延迟确保焦点切换完成
    setTimeout(() => {
        vscode.commands.executeCommand('editor.action.inlineSuggest.trigger');
    }, 50);
})

方法2:先关闭悬停面板再触发补全

直接调用VS Code内置命令关闭悬停,消除焦点占用问题:

vscode.commands.registerCommand('command.show', async () => {
    // 关闭悬停面板
    await vscode.commands.executeCommand('editor.action.hideHover');
    // 触发内联补全
    await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger');
})

方法3:优化悬停消息的信任配置

如果悬停消息的命令链接未被信任,可能会有额外的焦点交互,可设置isTrusted为true配合使用:

const hoverMsg = new MarkdownString('[show](command:command.show)');
hoverMsg.isTrusted = true; // 信任命令链接,避免系统级的焦点拦截

editor.setDecorations(decorationType, [{
    range,
    hoverMessage: hoverMsg
}])

内容的提问来源于stack exchange,提问作者Jnope

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 21:37:19