javascriptautocompletemonaco-editor

monaco-editor remove default autocompletion suggestions for custom language


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?


Solution

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