Fix completion

This commit is contained in:
Adria Navarro 2025-02-10 18:09:44 +01:00
parent c6b5ca853e
commit 91a9f8b04a
2 changed files with 23 additions and 8 deletions

View File

@ -51,7 +51,6 @@
// TODO: work out what best type fits this // TODO: work out what best type fits this
export let completions: ((context: CompletionContext) => Promise<{ export let completions: ((context: CompletionContext) => Promise<{
from: number from: number
filter: boolean
options: BindingCompletion[] options: BindingCompletion[]
} | null>)[] = [] } | null>)[] = []
export let mode: EditorMode = EditorModes.Handlebars export let mode: EditorMode = EditorModes.Handlebars

View File

@ -103,11 +103,11 @@ export const helpersToCompletion = (
detail: "Function", detail: "Function",
apply: ( apply: (
view: any, view: any,
completion: BindingCompletion, _completion: BindingCompletion,
from: number, from: number,
to: number to: number
) => { ) => {
insertBinding(view, from, to, helperName, mode) insertBinding(view, from, to, helperName, mode, AutocompleteType.HELPER)
}, },
} }
}) })
@ -230,7 +230,8 @@ export const jsHelperAutocomplete = (baseCompletions: BindingCompletion[]) => {
const query = match.groups?.["helper"] || "" const query = match.groups?.["helper"] || ""
let filtered = bindingFilter(options, query) let filtered = bindingFilter(options, query)
return { return {
from: jsBinding.from + match[0].length, from: jsBinding.from,
to: jsBinding.from + match[0].length,
filter: false, filter: false,
options: filtered, options: filtered,
} }
@ -243,7 +244,7 @@ export const jsHelperAutocomplete = (baseCompletions: BindingCompletion[]) => {
} }
export const buildBindingInfoNode = ( export const buildBindingInfoNode = (
completion: BindingCompletion, _completion: BindingCompletion,
binding: any binding: any
) => { ) => {
if (!binding.valueHTML || binding.value == null) { if (!binding.valueHTML || binding.value == null) {
@ -303,18 +304,26 @@ export function jsInsert(
return parsedInsert return parsedInsert
} }
const enum AutocompleteType {
BINDING,
HELPER,
}
// Autocomplete apply behaviour // Autocomplete apply behaviour
export const insertBinding = ( export const insertBinding = (
view: any, view: any,
from: number, from: number,
to: number, to: number,
text: string, text: string,
mode: { name: "javascript" | "handlebars" } mode: { name: "javascript" | "handlebars" },
type: AutocompleteType
) => { ) => {
let parsedInsert let parsedInsert
if (mode.name == "javascript") { if (mode.name == "javascript") {
parsedInsert = jsInsert(view.state.doc?.toString(), from, to, text) parsedInsert = jsInsert(view.state.doc?.toString(), from, to, text, {
helper: type === AutocompleteType.HELPER,
})
} else if (mode.name == "handlebars") { } else if (mode.name == "handlebars") {
parsedInsert = hbInsert(view.state.doc?.toString(), from, to, text) parsedInsert = hbInsert(view.state.doc?.toString(), from, to, text)
} else { } else {
@ -412,7 +421,14 @@ export const bindingsToCompletions = (
from: number, from: number,
to: number to: number
) => { ) => {
insertBinding(view, from, to, binding.readableBinding, mode) insertBinding(
view,
from,
to,
binding.readableBinding,
mode,
AutocompleteType.BINDING
)
}, },
}) })
return acc return acc