Extract completion function

This commit is contained in:
Adria Navarro 2025-02-10 11:14:17 +01:00
parent 973f6e6ae8
commit d09288542b
1 changed files with 31 additions and 26 deletions

View File

@ -244,6 +244,36 @@
]
}
function handleCompletions(
view: EditorView,
from: number,
to: number,
insert: string
) {
if (jsBindingWrapping && insert === "$") {
let { text } = view.state.doc.lineAt(from)
const left = from ? text.substring(0, from) : ""
const right = to ? text.substring(to) : ""
const wrap = !left.includes('$("') || !right.includes('")')
const tr = view.state.update(
{
changes: [{ from, insert: wrap ? '$("")' : "$" }],
selection: {
anchor: from + (wrap ? 3 : 1),
},
},
{
scrollIntoView: true,
userEvent: "input.type",
}
)
view.dispatch(tr)
return true
}
return false
}
// None of this is reactive, but it never has been, so we just assume most
// config flags aren't changed at runtime
// TODO: work out type for base
@ -262,32 +292,7 @@
: "autocomplete-option",
})
)
complete.push(
EditorView.inputHandler.of((view, from, to, insert) => {
if (jsBindingWrapping && insert === "$") {
let { text } = view.state.doc.lineAt(from)
const left = from ? text.substring(0, from) : ""
const right = to ? text.substring(to) : ""
const wrap = !left.includes('$("') || !right.includes('")')
const tr = view.state.update(
{
changes: [{ from, insert: wrap ? '$("")' : "$" }],
selection: {
anchor: from + (wrap ? 3 : 1),
},
},
{
scrollIntoView: true,
userEvent: "input.type",
}
)
view.dispatch(tr)
return true
}
return false
})
)
complete.push(EditorView.inputHandler.of(handleCompletions))
}
// JS only plugins