A bunch of changes relating to the codemirror editor component #54

Merged
probablycorey merged 8 commits from editor-fixes into main 2026-01-13 23:00:38 +00:00
Showing only changes of commit d1b16ce3e1 - Show all commits

View File

@ -1,5 +1,5 @@
import { autocompletion, type CompletionContext, type Completion } from '@codemirror/autocomplete'
import { Shrimp, type Value } from '#/index'
import { Shrimp } from '#/index'
const keywords = [
'import',
@ -26,48 +26,33 @@ const keywordCompletions: Completion[] = keywords.map((k) => ({
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) => {
// Build completions from all names in the shrimp scope
const scopeNames = shrimp.vm.scope.vars()
const functionCompletions: Completion[] = scopeNames.map((name) =>
buildFunctionCompletion(name, shrimp.get(name))
)
console.log(`🌭`, shrimp.vm.vars())
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]
// Get methods for a module (e.g., math, str, list)
const getModuleMethods = (moduleName: string): Completion[] => {
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) =>
buildFunctionCompletion(name, module.value.get(name))
)
return Object.keys(module).map((m) => ({ label: m, type: 'function' }))
}
const shrimpCompletionSource = (context: CompletionContext) => {