Hi everyone. Can anyone help me with this problem?
Goal:
I’d like to produce additional completions in context of html document. When user start typing AttributeName
type of node I want to provide additional completion with =""
at the end.
Poblem:
Currently I have a problem with syncing default html autocompletions in my custom CompletionSource
.
When I’m trying to get currentCompletions(context.state)
I’m getting an empty array, despite having a list of autocompletions displayed in the UI, e.g. start typing “c” → I’m getting class
, contenteditable
, contextmenu
autocompletions, but array returned from currentCompletions
is empty. When I enter 2nd character, i.e. “cl” → I’m getting class
and onclick
autocompletion and array returned from currentCompletions
is not empty and contains these values.
So for some reason I’m able to get relevant list of autocompletion only after typing 2nd symbol.
Code sample (completion source):
export const getAttrCompletion: CompletionSource = (context) => {
const nodeBefore = syntaxTree(context.state).resolveInner(context.pos, -1)
if (nodeBefore.name === 'AttributeName') {
const autoCompletions = currentCompletions(context.state)
if (!autoCompletions.length) {
return null
}
const match = context.matchBefore(/\w*/)
if (!match) {
return null
}
const {from} = match
return {
from,
options: autoCompletions.map(completion => {
const attrWithQuotes = completion.label + '=""'
return [
completion,
{
label: attrWithQuotes,
boost: 99,
type: 'text',
apply: (view, completion, from, to) => {
const cursorEndPosition = from + attrWithQuotes.length - 1
view.dispatch({
changes: {from, to, insert: attrWithQuotes},
selection: {anchor: cursorEndPosition, head: cursorEndPosition}
})
},
} as Completion
]
}).flat()
}
}
return null
}
That’s the list of completion sources I have:
return [
html(),
htmlLanguage.data.of({autocomplete: getAttrCompletion}),
]
What I tried to do:
Change the order between default html()
source and my custom source, but since I’m getting correct list starting from 2nd character, I guess the order is not a problem.
Also tried to remove boost
property from options, didn’t help.
Playground link
Custom completion source
I appreciate any help!