admin管理员组

文章数量:1289558

There are similar questions 1, 2, 3 but the answers described there work only for non-custom languages like C#, JavaScript and TypeScript.

What I want to achieve is to get rid of suggestions based on the previously typed words.

And I also want to keep my custom suggestions added with registerCompletionItemProvider.

How to do that?

There are similar questions 1, 2, 3 but the answers described there work only for non-custom languages like C#, JavaScript and TypeScript.

What I want to achieve is to get rid of suggestions based on the previously typed words.

And I also want to keep my custom suggestions added with registerCompletionItemProvider.

How to do that?

Share Improve this question edited Apr 28, 2023 at 16:59 waterstopper asked Apr 28, 2023 at 11:33 waterstopperwaterstopper 5884 silver badges20 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Monaco-editor by default provide suggestions based on the previously typed words on the editor. In order to get only the custom added suggestions, you can provide the suggestions through registerCompletionItemProvider and it will override any default suggestions provided by moncao-editor.

Example:

monaco.languages.registerCompletionItemProvider('myCustomLanguage', {
  provideCompletionItems: function(model, position) {
    const suggestions = [
      {
        label: 'console',
        kind: monaco.languages.CompletionItemKind.Function,
        documentation: 'Logs a message to the console.',
        insertText: 'console.log()',
      },
      {
        label: 'setTimeout',
        kind: monaco.languages.CompletionItemKind.Function,
        documentation: 'Executes a function after a specified time interval.',
        insertText: 'setTimeout(() => {\n\n}, 1000)',
      }
    ];

    return { suggestions: suggestions };
  }
});

If you type something now, it will not suggest anything that was earlier typed. On pressing Ctrl+Space, you would see only the above two suggestions("console" & "setTimeOut") on the editor.

I had the same problem, but after some searching and (try and fail) I finally get it.

Just set 'showWords: false' in the 'suggest' configuration, inside the editor creation function:

var editor = monaco.editor.create(document.getElementById("editorContainer"), {
  value: jsCode,
  language: "typescript", //can use your custom language here
  suggest: {
    showWords: false
  }
});

I have the same problems when I set monaco editor pletion providers return like return { suggetions: [] }.

This will make pletion menu show some matched word based typed code.

You can set options like this to avoid, related issue Disable autoplete of all used words:

const editor = monaco.editor.create(document.getElementById("editorElement"), {
  value: "some codes.",
  language: "customLanguage",
// other options:  'currentDocument' | 'matchingDocuments' | 'allDocuments'
  wordBasedSuggestions: 'off', // set off means only show pletion items form pletion providers instead of matching words in current code.
});

@Dexxe's answer worked for me as did quickSuggestions: false:

var editor = monaco.editor.create(document.getElementById("editorContainer"), {
  value: jsCode,
  language: "typescript", //can use your custom language here
  quickSuggestions: false,
});

本文标签: javascriptmonacoeditor remove default autocompletion suggestions for custom languageStack Overflow