1

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. enter image description here

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

How to do that?

llesha
  • 423
  • 1
  • 15

1 Answers1

2

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.

  • 1
    Thank you! I got custom logic in `registerCompletionItemProvider`. And there is a case when I return empty array in suggestions. In this case it suggests words that I typed earlier. How to deal with that? – llesha May 03 '23 at 19:06
  • Ideally you shouldn't get any default suggestion even if you are returning suggestions as empty. You may try setting 'quickSuggestions' property in monaco-editor as false. Just add quickSuggestions: false when creating the editor using monaco.editor.create – consiglieri May 03 '23 at 19:45
  • 1
    Well, it removes all of the suggestions – llesha May 03 '23 at 19:49
  • Not sure why you are getting so. For me without setting 'quickSuggestions: false', I get 'no suggestions' like this : https://stackoverflow.com/questions/59839148/monaco-editor-disable-all-suggestions – consiglieri May 03 '23 at 20:01
  • Hi @IIesha, sorry for asking this here as I don't have enough reputation to comment on your answer. I was going through this: https://stackoverflow.com/questions/63948257/listen-to-a-suggestion-item-being-selected, what I wanted to do was when user clicks on any suggestion, get the label of that suggestion. However I can't seem to find any way to that. – consiglieri May 11 '23 at 14:09