monaco-editor version: 0.20.0
Browser: Firefox 77.0.1
OS: Ubuntu 19.10
Playground code that reproduces the issue:
const completionSuggestions = [
{
label: 'lodash',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "The Lodash library exported as Node.js modules.",
insertText: 'lodash '
},
{
label: 'express',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "Fast, unopinionated, minimalist web framework",
insertText: 'express '
},
{
label: 'mkdirp',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "Recursively mkdir, like <code>mkdir -p</code>",
insertText: 'mkdirp '
},
{
label: 'my-third-party-library',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "Describe your library here",
insertText: 'my-third-party-library ',
}
];
monaco.languages.registerCompletionItemProvider('json', {
provideCompletionItems: function(model, position) {
return { suggestions: completionSuggestions }
}
});
monaco.editor.create(document.getElementById("container"), {
value: "{\n\t\"dependencies\": \"\"\n}\n",
language: "json",
quickSuggestions: {
comments: false,
other: true,
strings: true,
},
});
May be related to #1412 .
This code snippet is a variation on the _Completion provider example_. When we start typing, the suggestions are offered and selecting one of them works as expected.:

However, if we keep typing on the same line, no more suggestions appear. Pressing ctrl + space actually shows an empty list of suggestions:

If instead we type at the start of the string, suggestions are offered again:

This may well be just some missing configuration on my part but going through the documentation I found no way of enabling multiple suggestions on the same line.
Thank you.
For whatever unknown reason, the suggest widget decides to add more properties to the objects you return (sorry, not my code). So you need to always provide "fresh" objects. In your sample simply do:
monaco.languages.registerCompletionItemProvider('json', {
provideCompletionItems: function(model, position) {
return { suggestions: JSON.parse(JSON.stringify(completionSuggestions)) } // notice how JSON.parse / JSON.stringify is used to guarantee that fresh objects are returned
}
});
@spotqa-josem & @alexdima, In some rare cases JSON.parse, may throw an error,so to make it work simply return the suggestions from a function
const completionSuggestions = function(){
return [
{
label: 'lodash',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "The Lodash library exported as Node.js modules.",
insertText: 'lodash '
},
{
label: 'express',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "Fast, unopinionated, minimalist web framework",
insertText: 'express '
},
{
label: 'mkdirp',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "Recursively mkdir, like <code>mkdir -p</code>",
insertText: 'mkdirp '
},
{
label: 'my-third-party-library',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "Describe your library here",
insertText: 'my-third-party-library ',
}
];
}
monaco.languages.registerCompletionItemProvider('json', {
provideCompletionItems: function(model, position) {
return { suggestions: completionSuggestions() }
}
});
I think this might be a better way to solve this
I think this might be a better way to solve this
Just what I needed. Thanks! :+1:
Most helpful comment
@spotqa-josem & @alexdima, In some rare cases JSON.parse, may throw an error,so to make it work simply return the suggestions from a function
I think this might be a better way to solve this