Add a list of words to hint list of Any Word Completion demo

Hello,

how can I add a custom list of words [‘one’, ‘two’, ‘three’], not included in the edited text, to the hint list of the Any Word Completion example?

https://codemirror.net/demo/anywordhint.html

Thanks!

You could wrap the completion function it with a function that adds some results to the returned value.

I added “bozo” to the returned list:

[…]
list.push(“bozo”)
return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};

(see: Edit fiddle - JSFiddle - Code Playground )

But in this way ‘bozo’ is displayed in the hint list regardless if the word I’m typing is part of it

How can I fix this?

Thanks

Hi, before pushing your additional word(s) to the list you should check if it matches current word.
E.g. you could create an option with the list of extra words:

  var WORD = /[\w$]+/, RANGE = 500;
  var EXTRAWORDS = ['bozo','cozo','dozo','toto']

  CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
    var word = options && options.word || WORD;
    var range = options && options.range || RANGE;
    var extraWords = options && options.extraWords || EXTRAWORDS;

and then check if current word is in this list before returning the result, e.g.:

    list.push(...(extraWords.filter(el => el.startsWith(curWord || ''))))
    return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};

https://jsfiddle.net/rkhfn5et/

Thank you very much!