[英]How to show something grayed out inline using Visual Studio Code Extensions API?
I am creating a VSC extension and try to display ello World
as inline as soon as I type H
in a markdown file.我正在创建一个 VSC 扩展,并尝试在 markdown 文件中键入H
后立即将ello World
显示为内联。 With my current implementation it happens nothing if I type H
.在我当前的实现中,如果我键入H
则什么也不会发生。
Expected result预期结果
Actual result实际结果
extensions.js扩展.js
const vscode = require('vscode');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.languages.registerCompletionItemProvider('markdown', {
provideCompletionItems(document, position, token, context) {
if (document.getText(new vscode.Range(position.translate(0, -1), position)) === 'H') {
let completionItem = new vscode.CompletionItem('ello World');
completionItem.insertText = 'ello World';
completionItem.range = new vscode.Range(position.translate(0, -1), position);
return [completionItem];
}
}
});
context.subscriptions.push(disposable);
}
function deactivate() { }
module.exports = {
activate,
deactivate
}
package.json package.json
{
"name": "textautocompletionmarkdown",
"displayName": "TextAutocompletionMarkdown",
"description": "-",
"version": "0.0.1",
"engines": {
"vscode": "^1.71.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onLanguage:markdown"
],
"main": "./extension.js",
"contributes": {
"languages": [
{
"id": "markdown",
"aliases": [
"Markdown",
"markdown"
],
"extensions": [
".md",
".markdown"
],
"configuration": "./language-configuration.json"
}
]
},
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "node ./test/runTest.js"
},
"devDependencies": {
"@types/vscode": "^1.71.0",
"@types/glob": "^7.2.0",
"@types/mocha": "^9.1.1",
"@types/node": "16.x",
"eslint": "^8.20.0",
"glob": "^8.0.3",
"mocha": "^10.0.0",
"typescript": "^4.7.4",
"@vscode/test-electron": "^2.1.5"
}
}
First of all, markdown
is unusual in that it requires some special settings.首先, markdown
的不寻常之处在于它需要一些特殊设置。
You need this setting:你需要这个设置:
"[markdown]": { // markdown-specific setting
"editor.quickSuggestions": {
"other": "inline", // to get inline suggestions
"comments": "on",
"strings": "on"
}
}
In my testing it isn't enough to have that "editor.quickSuggestions"
setting alone - it still won't apply to markdown
.在我的测试中,单独设置"editor.quickSuggestions"
是不够的——它仍然不适用于markdown
。 (See, for example, https://github.com/microsoft/vscode/issues/28048#issuecomment-306616235 - the actual options have changed since that issue comment). (例如,参见https://github.com/microsoft/vscode/issues/28048#issuecomment-306616235 - 自该问题评论以来,实际选项已更改)。
With that markdown
-specific setting your code still does not work.使用特定于markdown
的设置,您的代码仍然不起作用。 I believe that this line is the problem:我相信这条线是问题所在:
completionItem.range = new vscode.Range(position.translate(0, -1), position);
Substituting this line works:替换此行有效:
completionItem.range = new vscode.Range(position, position);
So this code works for me:所以这段代码对我有用:
if (document.getText(new vscode.Range(position.translate(0, -1), position)) === 'H') {
let completionItem = new vscode.CompletionItem('ello World');
completionItem.insertText = 'ello World';
completionItem.range = new vscode.Range(position, position);
return [completionItem];
}
Finally, it only works on the second character He
- I seem to recall this was implemented to increase performance without so many early wrong guesses.最后,它只适用于第二个字符He
- 我似乎记得这是为了提高性能而没有那么多早期错误猜测的实现。
Also, there is a inlineCompletionProvider which I haven't tested.此外,还有一个我尚未测试的inlineCompletionProvider 。 I guess that this wouldn't make suggestions that appear in the Suggestion Widget, but I haven't used it yet.我想这不会提出建议小部件中出现的建议,但我还没有使用过它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.