|
| 1 | +import { snippetCompletion } from '@codemirror/autocomplete'; |
| 2 | +import { syntaxTree } from '@codemirror/language'; |
| 3 | + |
| 4 | +/** @typedef {(node: import('@lezer/common').SyntaxNode, context: import('@codemirror/autocomplete').CompletionContext, selected: import('./types').File) => boolean} Test */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Returns `true` if `$bindable()` is valid |
| 8 | + * @type {Test} |
| 9 | + */ |
| 10 | +function is_bindable(node, context) { |
| 11 | + // disallow outside `let { x = $bindable }` |
| 12 | + if (node.parent?.name !== 'PatternProperty') return false; |
| 13 | + if (node.parent.parent?.name !== 'ObjectPattern') return false; |
| 14 | + if (node.parent.parent.parent?.name !== 'VariableDeclaration') return false; |
| 15 | + |
| 16 | + let last = node.parent.parent.parent.lastChild; |
| 17 | + if (!last) return true; |
| 18 | + |
| 19 | + // if the declaration is incomplete, assume the best |
| 20 | + if (last.name === 'ObjectPattern' || last.name === 'Equals' || last.name === '⚠') { |
| 21 | + return true; |
| 22 | + } |
| 23 | + |
| 24 | + if (last.name === ';') { |
| 25 | + last = last.prevSibling; |
| 26 | + if (!last || last.name === '⚠') return true; |
| 27 | + } |
| 28 | + |
| 29 | + // if the declaration is complete, only return true if it is a `$props()` declaration |
| 30 | + return ( |
| 31 | + last.name === 'CallExpression' && |
| 32 | + last.firstChild?.name === 'VariableName' && |
| 33 | + context.state.sliceDoc(last.firstChild.from, last.firstChild.to) === '$props' |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Returns `true` if `$props()` is valid |
| 39 | + * TODO only allow in `.svelte` files, and only at the top level |
| 40 | + * @type {Test} |
| 41 | + */ |
| 42 | +function is_props(node, _, selected) { |
| 43 | + if (selected.type !== 'svelte') return false; |
| 44 | + |
| 45 | + return ( |
| 46 | + node.name === 'VariableName' && |
| 47 | + node.parent?.name === 'VariableDeclaration' && |
| 48 | + node.parent.parent?.name === 'Script' |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Returns `true` is this is a valid place to declare state |
| 54 | + * @type {Test} |
| 55 | + */ |
| 56 | +function is_state(node) { |
| 57 | + let parent = node.parent; |
| 58 | + |
| 59 | + if (node.name === '.' || node.name === 'PropertyName') { |
| 60 | + if (parent?.name !== 'MemberExpression') return false; |
| 61 | + parent = parent.parent; |
| 62 | + } |
| 63 | + |
| 64 | + if (!parent) return false; |
| 65 | + |
| 66 | + return parent.name === 'VariableDeclaration' || parent.name === 'PropertyDeclaration'; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Returns `true` if we're already in a valid call expression, e.g. |
| 71 | + * changing an existing `$state()` to `$state.frozen()` |
| 72 | + * @type {Test} |
| 73 | + */ |
| 74 | +function is_state_call(node) { |
| 75 | + let parent = node.parent; |
| 76 | + |
| 77 | + if (node.name === '.' || node.name === 'PropertyName') { |
| 78 | + if (parent?.name !== 'MemberExpression') return false; |
| 79 | + parent = parent.parent; |
| 80 | + } |
| 81 | + |
| 82 | + if (parent?.name !== 'CallExpression') { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + parent = parent.parent; |
| 87 | + if (!parent) return false; |
| 88 | + |
| 89 | + return parent.name === 'VariableDeclaration' || parent.name === 'PropertyDeclaration'; |
| 90 | +} |
| 91 | + |
| 92 | +/** @type {Test} */ |
| 93 | +function is_statement(node) { |
| 94 | + if (node.name === 'VariableName') { |
| 95 | + return node.parent?.name === 'ExpressionStatement'; |
| 96 | + } |
| 97 | + |
| 98 | + if (node.name === '.' || node.name === 'PropertyName') { |
| 99 | + return node.parent?.parent?.name === 'ExpressionStatement'; |
| 100 | + } |
| 101 | + |
| 102 | + return false; |
| 103 | +} |
| 104 | + |
| 105 | +/** @type {Array<{ snippet: string, test?: Test }>} */ |
| 106 | +const runes = [ |
| 107 | + { snippet: '$state(${})', test: is_state }, |
| 108 | + { snippet: '$state', test: is_state_call }, |
| 109 | + { snippet: '$props()', test: is_props }, |
| 110 | + { snippet: '$derived(${});', test: is_state }, |
| 111 | + { snippet: '$derived', test: is_state_call }, |
| 112 | + { snippet: '$derived.by(() => {\n\t${}\n});', test: is_state }, |
| 113 | + { snippet: '$derived.by', test: is_state_call }, |
| 114 | + { snippet: '$effect(() => {\n\t${}\n});', test: is_statement }, |
| 115 | + { snippet: '$effect.pre(() => {\n\t${}\n});', test: is_statement }, |
| 116 | + { snippet: '$state.frozen(${});', test: is_state }, |
| 117 | + { snippet: '$state.frozen', test: is_state_call }, |
| 118 | + { snippet: '$bindable()', test: is_bindable }, |
| 119 | + { snippet: '$effect.root(() => {\n\t${}\n})' }, |
| 120 | + { snippet: '$state.snapshot(${})' }, |
| 121 | + { snippet: '$effect.active()' }, |
| 122 | + { snippet: '$inspect(${});', test: is_statement } |
| 123 | +]; |
| 124 | + |
| 125 | +const options = runes.map(({ snippet, test }, i) => ({ |
| 126 | + option: snippetCompletion(snippet, { |
| 127 | + type: 'keyword', |
| 128 | + boost: runes.length - i, |
| 129 | + label: snippet.includes('(') ? snippet.slice(0, snippet.indexOf('(')) : snippet |
| 130 | + }), |
| 131 | + test |
| 132 | +})); |
| 133 | + |
| 134 | +/** |
| 135 | + * @param {import('@codemirror/autocomplete').CompletionContext} context |
| 136 | + * @param {import('./types.js').File} selected |
| 137 | + * @param {import('./types.js').File[]} files |
| 138 | + */ |
| 139 | +export function autocomplete(context, selected, files) { |
| 140 | + let node = syntaxTree(context.state).resolveInner(context.pos, -1); |
| 141 | + |
| 142 | + if (node.name === 'String' && node.parent?.name === 'ImportDeclaration') { |
| 143 | + const modules = [ |
| 144 | + 'svelte', |
| 145 | + 'svelte/animate', |
| 146 | + 'svelte/easing', |
| 147 | + 'svelte/legacy', |
| 148 | + 'svelte/motion', |
| 149 | + 'svelte/reactivity', |
| 150 | + 'svelte/store', |
| 151 | + 'svelte/transition' |
| 152 | + ]; |
| 153 | + |
| 154 | + for (const file of files) { |
| 155 | + if (file === selected) continue; |
| 156 | + modules.push(`./${file.name}.${file.type}`); |
| 157 | + } |
| 158 | + |
| 159 | + return { |
| 160 | + from: node.from + 1, |
| 161 | + options: modules.map((label) => ({ |
| 162 | + label, |
| 163 | + type: 'string' |
| 164 | + })) |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + if ( |
| 169 | + selected.type !== 'svelte' && |
| 170 | + (selected.type !== 'js' || !selected.name.endsWith('.svelte')) |
| 171 | + ) { |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + if (node.name === 'VariableName' || node.name === 'PropertyName' || node.name === '.') { |
| 176 | + // special case — `$inspect(...).with(...)` is the only rune that 'returns' |
| 177 | + // an 'object' with a 'method' |
| 178 | + if (node.name === 'PropertyName' || node.name === '.') { |
| 179 | + if ( |
| 180 | + node.parent?.name === 'MemberExpression' && |
| 181 | + node.parent.firstChild?.name === 'CallExpression' && |
| 182 | + node.parent.firstChild.firstChild?.name === 'VariableName' && |
| 183 | + context.state.sliceDoc( |
| 184 | + node.parent.firstChild.firstChild.from, |
| 185 | + node.parent.firstChild.firstChild.to |
| 186 | + ) === '$inspect' |
| 187 | + ) { |
| 188 | + const open = context.matchBefore(/\.\w*/); |
| 189 | + if (!open) return null; |
| 190 | + |
| 191 | + return { |
| 192 | + from: open.from, |
| 193 | + options: [snippetCompletion('.with(${})', { type: 'keyword', label: '.with' })] |
| 194 | + }; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + const open = context.matchBefore(/\$[\w\.]*/); |
| 199 | + if (!open) return null; |
| 200 | + |
| 201 | + return { |
| 202 | + from: open.from, |
| 203 | + options: options |
| 204 | + .filter((option) => (option.test ? option.test(node, context, selected) : true)) |
| 205 | + .map((option) => option.option) |
| 206 | + }; |
| 207 | + } |
| 208 | +} |
0 commit comments