Unify and clean code
This commit is contained in:
parent
fb848c370b
commit
dc2f7d53b4
|
@ -43,7 +43,8 @@
|
||||||
} from "@codemirror/commands"
|
} from "@codemirror/commands"
|
||||||
import { setDiagnostics } from "@codemirror/lint"
|
import { setDiagnostics } from "@codemirror/lint"
|
||||||
import type { Diagnostic } from "@codemirror/lint"
|
import type { Diagnostic } from "@codemirror/lint"
|
||||||
import { Compartment, EditorState, Extension } from "@codemirror/state"
|
import { Compartment, EditorState } from "@codemirror/state"
|
||||||
|
import type { Extension } from "@codemirror/state"
|
||||||
import { javascript } from "@codemirror/lang-javascript"
|
import { javascript } from "@codemirror/lang-javascript"
|
||||||
import { EditorModes } from "./"
|
import { EditorModes } from "./"
|
||||||
import { themeStore } from "@/stores/portal"
|
import { themeStore } from "@/stores/portal"
|
||||||
|
|
|
@ -137,9 +137,13 @@ export const hbAutocomplete = (
|
||||||
baseCompletions: BindingCompletionOption[]
|
baseCompletions: BindingCompletionOption[]
|
||||||
): BindingCompletion => {
|
): BindingCompletion => {
|
||||||
function coreCompletion(context: CompletionContext) {
|
function coreCompletion(context: CompletionContext) {
|
||||||
let bindingStart = context.matchBefore(EditorModes.Handlebars.match)
|
if (!baseCompletions.length) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
let options = baseCompletions || []
|
const bindingStart = context.matchBefore(EditorModes.Handlebars.match)
|
||||||
|
|
||||||
|
const options = baseCompletions
|
||||||
|
|
||||||
if (!bindingStart) {
|
if (!bindingStart) {
|
||||||
return null
|
return null
|
||||||
|
@ -150,7 +154,7 @@ export const hbAutocomplete = (
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const query = bindingStart.text.replace(match[0], "")
|
const query = bindingStart.text.replace(match[0], "")
|
||||||
let filtered = bindingFilter(options, query)
|
const filtered = bindingFilter(options, query)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from: bindingStart.from + match[0].length,
|
from: bindingStart.from + match[0].length,
|
||||||
|
@ -170,8 +174,12 @@ export const jsAutocomplete = (
|
||||||
baseCompletions: BindingCompletionOption[]
|
baseCompletions: BindingCompletionOption[]
|
||||||
): BindingCompletion => {
|
): BindingCompletion => {
|
||||||
function coreCompletion(context: CompletionContext) {
|
function coreCompletion(context: CompletionContext) {
|
||||||
let jsBinding = wrappedAutocompleteMatch(context)
|
if (!baseCompletions.length) {
|
||||||
let options = baseCompletions || []
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const jsBinding = wrappedAutocompleteMatch(context)
|
||||||
|
const options = baseCompletions
|
||||||
|
|
||||||
if (jsBinding) {
|
if (jsBinding) {
|
||||||
// Accommodate spaces
|
// Accommodate spaces
|
||||||
|
@ -210,6 +218,10 @@ function setAutocomplete(
|
||||||
options: BindingCompletionOption[]
|
options: BindingCompletionOption[]
|
||||||
): BindingCompletion {
|
): BindingCompletion {
|
||||||
return function (context: CompletionContext) {
|
return function (context: CompletionContext) {
|
||||||
|
if (!options.length) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
if (wrappedAutocompleteMatch(context)) {
|
if (wrappedAutocompleteMatch(context)) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,8 +70,6 @@
|
||||||
let expressionLogs: Log[] | undefined
|
let expressionLogs: Log[] | undefined
|
||||||
let expressionError: string | undefined
|
let expressionError: string | undefined
|
||||||
let evaluating = false
|
let evaluating = false
|
||||||
let completions: BindingCompletion[] = []
|
|
||||||
let autocompleteOptions: BindingCompletionOption[] = []
|
|
||||||
|
|
||||||
$: useSnippets = allowSnippets && !$licensing.isFreePlan
|
$: useSnippets = allowSnippets && !$licensing.isFreePlan
|
||||||
$: editorModeOptions = getModeOptions(allowHBS, allowJS)
|
$: editorModeOptions = getModeOptions(allowHBS, allowJS)
|
||||||
|
@ -92,41 +90,18 @@
|
||||||
$: requestEval(runtimeExpression, context, snippets)
|
$: requestEval(runtimeExpression, context, snippets)
|
||||||
$: bindingHelpers = new BindingHelpers(getCaretPosition, insertAtPos)
|
$: bindingHelpers = new BindingHelpers(getCaretPosition, insertAtPos)
|
||||||
|
|
||||||
function getOptions(
|
$: bindingOptions = bindingsToCompletions(bindings, editorMode)
|
||||||
mode: typeof editorMode,
|
$: helperOptions = allowHelpers ? getHelperCompletions(editorMode) : []
|
||||||
bindings: EnrichedBinding[]
|
$: snippetsOptions =
|
||||||
): [BindingCompletionOption[], BindingCompletion[]] {
|
usingJS && useSnippets && snippets?.length ? snippets : []
|
||||||
const autocompleteOptions = []
|
|
||||||
const completions = []
|
|
||||||
|
|
||||||
const bindingOptions = bindingsToCompletions(bindings, editorMode)
|
$: completions = !usingJS
|
||||||
const helperOptions = getHelperCompletions(mode)
|
? [hbAutocomplete([...bindingOptions, ...helperOptions])]
|
||||||
|
: [
|
||||||
if (mode.name === "handlebars") {
|
jsAutocomplete(bindingOptions),
|
||||||
autocompleteOptions.push(...bindingOptions)
|
jsHelperAutocomplete(helperOptions),
|
||||||
autocompleteOptions.push(...helperOptions)
|
snippetAutoComplete(snippetsOptions),
|
||||||
|
]
|
||||||
completions.push(hbAutocomplete(autocompleteOptions))
|
|
||||||
} else if (mode.name === "javascript") {
|
|
||||||
if (bindingOptions.length) {
|
|
||||||
completions.push(jsAutocomplete(bindingOptions))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allowHelpers) {
|
|
||||||
completions.push(jsHelperAutocomplete(helperOptions))
|
|
||||||
}
|
|
||||||
if (useSnippets && snippets?.length) {
|
|
||||||
completions.push(snippetAutoComplete(snippets))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return [autocompleteOptions, completions]
|
|
||||||
}
|
|
||||||
|
|
||||||
$: [autocompleteOptions, completions] = getOptions(
|
|
||||||
editorMode,
|
|
||||||
enrichedBindings
|
|
||||||
)
|
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
// Ensure a valid side panel option is always selected
|
// Ensure a valid side panel option is always selected
|
||||||
|
@ -373,7 +348,6 @@
|
||||||
bind:getCaretPosition
|
bind:getCaretPosition
|
||||||
bind:insertAtPos
|
bind:insertAtPos
|
||||||
{completions}
|
{completions}
|
||||||
options={autocompleteOptions}
|
|
||||||
autofocus={autofocusEditor}
|
autofocus={autofocusEditor}
|
||||||
placeholder={placeholder ||
|
placeholder={placeholder ||
|
||||||
"Add bindings by typing {{ or use the menu on the right"}
|
"Add bindings by typing {{ or use the menu on the right"}
|
||||||
|
|
Loading…
Reference in New Issue