Monaco-editor: Suggestions are not offered a second time on the same line

Created on 23 Jun 2020  路  3Comments  路  Source: microsoft/monaco-editor

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.:
monaco-1

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

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

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.

bug suggest

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

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

All 3 comments

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:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chengtie picture chengtie  路  3Comments

Spongman picture Spongman  路  3Comments

ststeiger picture ststeiger  路  3Comments

andreymarchenko picture andreymarchenko  路  3Comments

Panhaiwei picture Panhaiwei  路  3Comments