anyword hinting while in JavaScript mode


#1

I am trying to get the anyword hinting to work along side the JavaScript hinting.

I have them both loaded, but I can’t seem to get them to function together. is there something special I must do?


#2

You could write a wrapper function that calls both and combines the result, and set that as your hint function.


#3

Can you help please, I have no idea how to do that, or where to even start.


#4

How hard would it be to internally stack hint functions as they are defined?

Similar to how brackets.io (which uses codemirror) has a hint manager that stacks hint functions. Similar to how codemirror does cm.on/cm.off handling?

So if I had multiple hint functions for a language they would all run then merge the results. Then a [“all”] mode to have it run for all languages.

I think this would be a great addition to codemirror.


#5

I’ve been struggling to get this to work, can you please help with an example please?


#6

I am trying to have the anyword hint work on all modes, filtered in with the actual hint from the other modes.

I can not get this to work. I have looked at stack, here, tried solutions, but it maybe beyond me.

Please help. Thank you greatly.


#7

Here’s what a wrapper might look like:

function hintingFunction(cm, options) {
  const anyhint = CodeMirror.hint.anyword(cm, options)
  const jshint = CodeMirror.hint.javascript(cm, options)
  const words = new Set([...anyhint.list, ...jshint.list])
  if (words.size > 0) {
    return {
      list: Array.from(words), 
      from: jshint.from, 
      to: jshint.to
    }
  }
}

CodeMirror(document.body, {
  extraKeys: {
    'Ctrl-Space': (cm) => {
      cm.showHint({hint: hintingFunction})
    }
  }
})

If you need to filter them based on mode, you can register a helper that uses a predicate:

CodeMirror.registerGlobalHelper('hint', 'allhints',
  function(mode, cm) {
    // check if helper applies to mode...
    return true
  },
  function(cm, options) {
    // filter hint functions...
    return [
      CodeMirror.hint.anyword,
      CodeMirror.hint.javascript
    ]
  })

Then use the helper in your hinting function:

const getHintFunctions = cm.getHelper(CodeMirror.Pos(0, 0), 'hint') 
for (const hint of getHintFunctions()) {
    const completion = hint(cm, options)
    // merge...
}