Skip to content

Commit 87b33a5

Browse files
committed
autocomplete imports
1 parent 92bac84 commit 87b33a5

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

sites/svelte-5-preview/src/lib/CodeMirror.svelte

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import { javascriptLanguage } from '@codemirror/lang-javascript';
1212
import { createEventDispatcher, tick } from 'svelte';
1313
import { writable } from 'svelte/store';
14+
import { get_repl_context } from '$lib/context.js';
1415
import Message from './Message.svelte';
1516
import { svelteTheme } from './theme.js';
1617
import { autocomplete } from './autocomplete.js';
@@ -192,12 +193,16 @@
192193
}
193194
});
194195
196+
const { files, selected } = get_repl_context();
197+
195198
const svelte_rune_completions = svelteLanguage.data.of({
196-
autocomplete
199+
/** @param {import('@codemirror/autocomplete').CompletionContext} context */
200+
autocomplete: (context) => autocomplete(context, $selected, $files)
197201
});
198202
199203
const js_rune_completions = javascriptLanguage.data.of({
200-
autocomplete
204+
/** @param {import('@codemirror/autocomplete').CompletionContext} context */
205+
autocomplete: (context) => autocomplete(context, $selected, $files)
201206
});
202207
</script>
203208

sites/svelte-5-preview/src/lib/autocomplete.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,38 @@ const options = runes.map(({ snippet, test }, i) => ({
125125
test
126126
}));
127127

128-
/** @param {import('@codemirror/autocomplete').CompletionContext} context */
129-
export function autocomplete(context) {
128+
/**
129+
* @param {import('@codemirror/autocomplete').CompletionContext} context
130+
* @param {import('./types.js').File} selected
131+
* @param {import('./types.js').File[]} files
132+
*/
133+
export function autocomplete(context, selected, files) {
130134
let node = syntaxTree(context.state).resolveInner(context.pos, -1);
131135

132136
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;
137+
const modules = [
138+
'svelte',
139+
'svelte/animate',
140+
'svelte/easing',
141+
'svelte/legacy',
142+
'svelte/motion',
143+
'svelte/reactivity',
144+
'svelte/store',
145+
'svelte/transition'
146+
];
147+
148+
for (const file of files) {
149+
if (file === selected) continue;
150+
modules.push(`./${file.name}.${file.type}`);
151+
}
152+
153+
return {
154+
from: node.from + 1,
155+
options: modules.map((label) => ({
156+
label,
157+
type: 'string'
158+
}))
159+
};
135160
}
136161

137162
if (node.name === 'VariableName' || node.name === 'PropertyName' || node.name === '.') {

0 commit comments

Comments
 (0)