1

I am creating a VSC extension and try to display ello World as inline as soon as I type H in a markdown file. With my current implementation it happens nothing if I type H.

Expected result

enter image description here

Actual result

enter image description here

extensions.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

{
  "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"
  }
}
Max Hager
  • 536
  • 4
  • 13

1 Answers1

1

First of all, markdown is unusual in that it requires some special settings.

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. (See, for example, https://github.com/microsoft/vscode/issues/28048#issuecomment-306616235 - the actual options have changed since that issue comment).

With that markdown-specific setting your code still does not work. 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.

making a markdown inline completion


Also, there is a inlineCompletionProvider which I haven't tested. I guess that this wouldn't make suggestions that appear in the Suggestion Widget, but I haven't used it yet.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • First I thought it's working but then I realised the suggestion came from copilot and not from the code above. Do you have the same package.json then I have? Its still not working... – Max Hager Sep 30 '22 at 10:58
  • I see you are contributing the `markdown` language. Comment the whole `contributes` part out and disable Copilot for testing and see if it'll work for you. Also, don't have the `Hello World` text in the same file. – Mark Sep 30 '22 at 15:19
  • And I would `"onStartupFinished"` to the `activationEvents` - again just for testing. – Mark Sep 30 '22 at 15:22
  • Thats weird. Still not working. In the meanwhile I also tried using TS instead of JS. It works only for me when I also implement a trigger like [here](https://github.com/microsoft/vscode-extension-samples/blob/main/completions-sample/src/extension.ts) in row 70 but with widget and not as greyed out. Can you perhaps share your full code? – Max Hager Sep 30 '22 at 20:34