1
1
import type { CompileError , CompileResult } from 'svelte/compiler' ;
2
- import { Compartment , EditorState } from '@codemirror/state' ;
2
+ import { Compartment , EditorState , StateEffect , StateField } from '@codemirror/state' ;
3
3
import { compile_file } from './compile-worker' ;
4
4
import { BROWSER } from 'esm-env' ;
5
5
import { basicSetup , EditorView } from 'codemirror' ;
6
6
import { javascript } from '@codemirror/lang-javascript' ;
7
7
import { html } from '@codemirror/lang-html' ;
8
8
import { svelte } from '@replit/codemirror-lang-svelte' ;
9
9
import { autocomplete_for_svelte } from '@sveltejs/site-kit/codemirror' ;
10
- import { keymap } from '@codemirror/view' ;
10
+ import { Decoration , keymap , type DecorationSet } from '@codemirror/view' ;
11
11
import { acceptCompletion } from '@codemirror/autocomplete' ;
12
12
import { indentWithTab } from '@codemirror/commands' ;
13
13
import { indentUnit } from '@codemirror/language' ;
@@ -51,6 +51,32 @@ function file_type(file: Item) {
51
51
return file . name . split ( '.' ) . pop ( ) ;
52
52
}
53
53
54
+ const set_highlight = StateEffect . define < { start : number ; end : number } | null > ( ) ;
55
+
56
+ const highlight_field = StateField . define < DecorationSet > ( {
57
+ create ( ) {
58
+ return Decoration . none ;
59
+ } ,
60
+ update ( highlights , tr ) {
61
+ // Apply the effect
62
+ for ( let effect of tr . effects ) {
63
+ if ( effect . is ( set_highlight ) ) {
64
+ if ( effect . value ) {
65
+ const { start, end } = effect . value ;
66
+ const deco = Decoration . mark ( { class : 'highlight' } ) . range ( start , end ) ;
67
+ return Decoration . set ( [ deco ] ) ;
68
+ } else {
69
+ // Clear highlight
70
+ return Decoration . none ;
71
+ }
72
+ }
73
+ }
74
+ // Map decorations for document changes
75
+ return highlights . map ( tr . changes ) ;
76
+ } ,
77
+ provide : ( field ) => EditorView . decorations . from ( field )
78
+ } ) ;
79
+
54
80
const tab_behaviour = new Compartment ( ) ;
55
81
const vim_mode = new Compartment ( ) ;
56
82
@@ -60,7 +86,8 @@ const default_extensions = [
60
86
tab_behaviour . of ( keymap . of ( [ { key : 'Tab' , run : acceptCompletion } ] ) ) ,
61
87
indentUnit . of ( '\t' ) ,
62
88
theme ,
63
- vim_mode . of ( [ ] )
89
+ vim_mode . of ( [ ] ) ,
90
+ highlight_field
64
91
] ;
65
92
66
93
export interface ExposedCompilerOptions {
@@ -225,6 +252,14 @@ export class Workspace {
225
252
} ) ;
226
253
}
227
254
255
+ highlight_range ( node : { start : number ; end : number } | null ) {
256
+ if ( ! this . #view) return ;
257
+
258
+ this . #view. dispatch ( {
259
+ effects : set_highlight . of ( node )
260
+ } ) ;
261
+ }
262
+
228
263
mark_saved ( ) {
229
264
this . modified = { } ;
230
265
}
0 commit comments