JSON 完成的自定义扩展在双引号中不起作用 [英] Custom Extension for JSON Completion Does Not Work in Double Quotes

查看:17
本文介绍了JSON 完成的自定义扩展在双引号中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 VSCode 扩展,它应该通过额外的代码完成来增强 JSON 编辑.因此,我添加了四个虚拟建议(foofoo1foo2foo3)来开始测试.在 JSON 键后键入冒号时,它按预期工作:

此外,当我在空对象中触发完成时,它会按我的意愿工作(包括来自其他扩展的建议):

但是,当我在双引号内触发完成时(无论它们是否包含文本),我的建议丢失了:

当我在 CompletionItemProvider 中设置断点时,这两种情况下的执行都会按预期暂停.所以它被正确调用,它肯定会返回我的建议.我查看了现有的 JSON 编辑器扩展(甚至 VSCode 的默认扩展),以获取如何正确实现它的建议,并查看默认的 JSON 扩展是否可能覆盖双引号内的所有建议(即使在我看来这会破坏整个扩展性的想法)但找不到任何东西.

要重现它足以按照.

I'm developing a VSCode extension that should enhance JSON editing with additional code completion. Therefore I added four dummy suggestions (foo, foo1, foo2, foo3) to start testing. When typing a colon after a JSON key it works as expected:

Also when I trigger completion in an empty object it works as I want it (including suggestions from other extensions):

However when I trigger completion within double quotes (no matter if they contain text or not) my suggestions are missing:

When I set a breakpoint in my CompletionItemProvider execution pauses as expected in both cases. So it is called correctly and it returns my suggestions for sure. I looked in existing JSON editor extensions (even VSCodes default one) to get advice how to implement it correctly and to see if the default JSON extension maybe overwrites all suggestions within double quotes (even though that would break the whole extensibility idea in my opinion) but couldn't find anything.

To reproduce it is enough to scaffold an empty extension as described here.

Here is my extension.ts:

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

class OpenApiCompletionItemProvider implements vscode.CompletionItemProvider {
    public provideCompletionItems(
        document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken):
        Thenable<vscode.CompletionItem[]> {
        return Promise.resolve([
            new vscode.CompletionItem('foo', vscode.CompletionItemKind.Class),
            new vscode.CompletionItem('foo1', vscode.CompletionItemKind.Property),
            new vscode.CompletionItem('foo2', vscode.CompletionItemKind.Interface),
            new vscode.CompletionItem('foo3', vscode.CompletionItemKind.Class),
        ]);
    }
}

export function activate(ctx: vscode.ExtensionContext): void {
    ctx.subscriptions.push(
        vscode.languages.registerCompletionItemProvider(
            { pattern: '**/*.openapi.json' },
            new OpenApiCompletionItemProvider(),
            ':',
            '"'
        )
    );
}

// this method is called when your extension is deactivated
export function deactivate() { }

解决方案

The starting quote " is part of what VSCode considers the current "word". Consequently, the completion items you return don't match the current filter string " and are not displayed.

This can be fixed by including the full word (with quotes) in the insert text:

new vscode.CompletionItem('"foo"', vscode.CompletionItemKind.Class)

(keep the label string the same and use insertText if you don't want the quotes to be displayed in the completion popup)

Alternatively, you could adjust the range accordingly:

var item = new vscode.CompletionItem('foo', vscode.CompletionItemKind.Class);
item.range = new vscode.Range(position, position);

Note: you may find document.getWordRangeAtPosition() useful. You can find the definition of a "word" in JSON here.

这篇关于JSON 完成的自定义扩展在双引号中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆