|
| 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) => 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) { |
| 43 | + return node.name === 'VariableName' && node.parent?.name === 'VariableDeclaration'; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Returns `true` is this is a valid place to declare state |
| 48 | + * @type {Test} |
| 49 | + */ |
| 50 | +function is_state(node) { |
| 51 | + let parent = node.parent; |
| 52 | + |
| 53 | + if (node.name === '.' || node.name === 'PropertyName') { |
| 54 | + if (parent?.name !== 'MemberExpression') return false; |
| 55 | + parent = parent.parent; |
| 56 | + } |
| 57 | + |
| 58 | + if (!parent) return false; |
| 59 | + |
| 60 | + return parent.name === 'VariableDeclaration' || parent.name === 'PropertyDeclaration'; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Returns `true` if we're already in a valid call expression, e.g. |
| 65 | + * changing an existing `$state()` to `$state.frozen()` |
| 66 | + * @type {Test} |
| 67 | + */ |
| 68 | +function is_state_call(node) { |
| 69 | + let parent = node.parent; |
| 70 | + |
| 71 | + if (node.name === '.' || node.name === 'PropertyName') { |
| 72 | + if (parent?.name !== 'MemberExpression') return false; |
| 73 | + parent = parent.parent; |
| 74 | + } |
| 75 | + |
| 76 | + if (parent?.name !== 'CallExpression') { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + parent = parent.parent; |
| 81 | + if (!parent) return false; |
| 82 | + |
| 83 | + return parent.name === 'VariableDeclaration' || parent.name === 'PropertyDeclaration'; |
| 84 | +} |
| 85 | + |
| 86 | +/** @type {Test} */ |
| 87 | +function is_statement(node) { |
| 88 | + if (node.name === 'VariableName') { |
| 89 | + return node.parent?.name === 'ExpressionStatement'; |
| 90 | + } |
| 91 | + |
| 92 | + if (node.name === '.' || node.name === 'PropertyName') { |
| 93 | + return node.parent?.parent?.name === 'ExpressionStatement'; |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | +} |
| 98 | + |
| 99 | +/** @type {Array<{ snippet: string, test?: Test }>} */ |
| 100 | +const runes = [ |
| 101 | + { snippet: '$state(${})', test: is_state }, |
| 102 | + { snippet: '$state', test: is_state_call }, |
| 103 | + { snippet: '$props()', test: is_props }, |
| 104 | + { snippet: '$derived(${});', test: is_state }, |
| 105 | + { snippet: '$derived', test: is_state_call }, |
| 106 | + { snippet: '$derived.by(() => {\n\t${}\n});', test: is_state }, |
| 107 | + { snippet: '$derived.by', test: is_state_call }, |
| 108 | + { snippet: '$effect(() => {\n\t${}\n});', test: is_statement }, |
| 109 | + { snippet: '$effect.pre(() => {\n\t${}\n});', test: is_statement }, |
| 110 | + { snippet: '$state.frozen(${});', test: is_state }, |
| 111 | + { snippet: '$state.frozen', test: is_state_call }, |
| 112 | + { snippet: '$bindable()', test: is_bindable }, |
| 113 | + { snippet: '$effect.root(() => {\n\t${}\n})' }, |
| 114 | + { snippet: '$state.snapshot(${})' }, |
| 115 | + { snippet: '$effect.active()' }, |
| 116 | + { snippet: '$inspect(${});', test: is_statement } |
| 117 | +]; |
| 118 | + |
| 119 | +const options = runes.map(({ snippet, test }, i) => ({ |
| 120 | + option: snippetCompletion(snippet, { |
| 121 | + type: 'keyword', |
| 122 | + boost: runes.length - i, |
| 123 | + label: snippet.includes('(') ? snippet.slice(0, snippet.indexOf('(')) : snippet |
| 124 | + }), |
| 125 | + test |
| 126 | +})); |
| 127 | + |
| 128 | +/** @param {import('@codemirror/autocomplete').CompletionContext} context */ |
| 129 | +export function autocomplete(context) { |
| 130 | + let node = syntaxTree(context.state).resolveInner(context.pos, -1); |
| 131 | + |
| 132 | + if (node.name === 'String' && node.parent?.name === 'ImportDeclaration') { |
| 133 | + // TODO autocomplete `svelte` and any files in the REPL that aren't this one |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (node.name === 'VariableName' || node.name === 'PropertyName' || node.name === '.') { |
| 138 | + const open = context.matchBefore(/\$[\w\.]*/); |
| 139 | + if (!open) return null; |
| 140 | + |
| 141 | + return { |
| 142 | + from: open.from, |
| 143 | + options: options |
| 144 | + .filter((option) => (option.test ? option.test(node, context) : true)) |
| 145 | + .map((option) => option.option) |
| 146 | + }; |
| 147 | + } |
| 148 | +} |
0 commit comments