Simplify function completions in completions.ts
Removed the buildFunctionCompletion helper and inlined function completion creation for both scope and module methods. Updated module method extraction to use Object.keys for compatibility with the new module structure.
This commit is contained in:
parent
2c7508c2a4
commit
d1b16ce3e1
|
|
@ -1,5 +1,5 @@
|
||||||
import { autocompletion, type CompletionContext, type Completion } from '@codemirror/autocomplete'
|
import { autocompletion, type CompletionContext, type Completion } from '@codemirror/autocomplete'
|
||||||
import { Shrimp, type Value } from '#/index'
|
import { Shrimp } from '#/index'
|
||||||
|
|
||||||
const keywords = [
|
const keywords = [
|
||||||
'import',
|
'import',
|
||||||
|
|
@ -26,48 +26,33 @@ const keywordCompletions: Completion[] = keywords.map((k) => ({
|
||||||
boost: -1,
|
boost: -1,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const buildFunctionCompletion = (name: string, value: unknown): Completion => {
|
|
||||||
let detail: string | undefined
|
|
||||||
|
|
||||||
console.log(`🌭`, { name, fn: value?.toString() })
|
|
||||||
|
|
||||||
// console.log(`🌭`, { name, value })
|
|
||||||
|
|
||||||
// const isFunction = value?.type === 'function'
|
|
||||||
// if (isFunction) {
|
|
||||||
// const paramStrs = value.params.map((p) => (p in value.defaults ? `${p}?` : p))
|
|
||||||
|
|
||||||
// if (value.variadic && paramStrs.length > 0) {
|
|
||||||
// paramStrs[paramStrs.length - 1] = `...${paramStrs[paramStrs.length - 1]}`
|
|
||||||
// }
|
|
||||||
|
|
||||||
// detail = `(${paramStrs.join(', ')})`
|
|
||||||
// }
|
|
||||||
|
|
||||||
return {
|
|
||||||
label: name,
|
|
||||||
type: 'function',
|
|
||||||
detail,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const createShrimpCompletions = (shrimp: Shrimp) => {
|
export const createShrimpCompletions = (shrimp: Shrimp) => {
|
||||||
// Build completions from all names in the shrimp scope
|
// Build completions from all names in the shrimp scope
|
||||||
const scopeNames = shrimp.vm.scope.vars()
|
const scopeNames = shrimp.vm.scope.vars()
|
||||||
const functionCompletions: Completion[] = scopeNames.map((name) =>
|
console.log(`🌭`, shrimp.vm.vars())
|
||||||
buildFunctionCompletion(name, shrimp.get(name))
|
|
||||||
)
|
const functionCompletions: Completion[] = scopeNames.map((name) => {
|
||||||
|
const value = shrimp.vm.scope.get(name)
|
||||||
|
let type
|
||||||
|
if (value?.type === 'function' || value?.type === 'native') {
|
||||||
|
type = 'function'
|
||||||
|
} else if (value?.type === 'dict') {
|
||||||
|
type = 'namespace'
|
||||||
|
} else {
|
||||||
|
type = 'variable'
|
||||||
|
}
|
||||||
|
|
||||||
|
return { type, label: name }
|
||||||
|
})
|
||||||
|
|
||||||
const allCompletions = [...keywordCompletions, ...functionCompletions]
|
const allCompletions = [...keywordCompletions, ...functionCompletions]
|
||||||
|
|
||||||
// Get methods for a module (e.g., math, str, list)
|
// Get methods for a module (e.g., math, str, list)
|
||||||
const getModuleMethods = (moduleName: string): Completion[] => {
|
const getModuleMethods = (moduleName: string): Completion[] => {
|
||||||
const module = shrimp.get(moduleName)
|
const module = shrimp.get(moduleName)
|
||||||
if (!module || module.type !== 'dict') return []
|
if (!module || typeof module !== 'object') return []
|
||||||
|
|
||||||
return Array.from(module.value.keys()).map((name) =>
|
return Object.keys(module).map((m) => ({ label: m, type: 'function' }))
|
||||||
buildFunctionCompletion(name, module.value.get(name))
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const shrimpCompletionSource = (context: CompletionContext) => {
|
const shrimpCompletionSource = (context: CompletionContext) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user