11import { Compartment , EditorState , Extension } from "@codemirror/state" ;
22import { EditorView , keymap , lineNumbers } from "@codemirror/view" ;
3- import { defaultKeymap , toggleComment } from "@codemirror/commands" ;
3+ import { defaultKeymap , toggleComment , history , undo , redo } from "@codemirror/commands" ;
44import { html } from "@codemirror/lang-html" ;
55import { css } from "@codemirror/lang-css" ;
66import { javascript } from "@codemirror/lang-javascript" ;
77import { autocompletion } from "@codemirror/autocomplete" ;
8- import { foldGutter , foldKeymap , syntaxTree } from "@codemirror/language" ;
8+ import { foldGutter , foldKeymap } from "@codemirror/language" ;
99import { linter , lintGutter } from "@codemirror/lint" ;
10+ import { lintWithBiome } from "./biome" ;
1011import { monokai } from "@uiw/codemirror-theme-monokai" ;
1112import { bbedit } from "@uiw/codemirror-theme-bbedit" ;
1213import { CodeMirrorEditor , Editors } from "./types" ;
@@ -60,6 +61,7 @@ function createEditorConfig(
6061 container : HTMLElement ,
6162 content : string ,
6263 contentState : typeof htmlState | typeof cssState | typeof jsState ,
64+ fileName = "file.txt" ,
6365) : CodeMirrorEditor {
6466 const themeCompartment = new Compartment ( ) ;
6567 const autoRunCompartment = new Compartment ( ) ;
@@ -69,32 +71,17 @@ function createEditorConfig(
6971 doc : content ,
7072 extensions : [
7173 lineNumbers ( ) ,
74+ history ( ) ,
7275 foldGutter ( ) ,
73- // Linting: use syntax tree to surface parse errors from the language parser
74- linter ( ( view ) => {
75- const diags : Array < {
76- from : number ;
77- to : number ;
78- severity : "error" | "warning" | "info" ;
79- message : string ;
80- } > = [ ] ;
76+ linter ( async ( view ) => {
77+ const text = view . state . doc . toString ( ) ;
8178 try {
82- syntaxTree ( view . state ) . iterate ( {
83- enter : ( node ) => {
84- if ( node . type . isError ) {
85- diags . push ( {
86- from : node . from ,
87- to : node . to ,
88- severity : "error" ,
89- message : "Syntax error" ,
90- } ) ;
91- }
92- } ,
93- } ) ;
94- } catch ( e : unknown ) {
95- console . error ( "Error occurred while iterating syntax tree:" , e ) ;
79+ const biomeDiags = await lintWithBiome ( text , fileName ) ;
80+ return biomeDiags || [ ] ;
81+ } catch ( err ) {
82+ console . error ( "Biome linting failed:" , err ) ;
83+ return [ ] ;
9684 }
97- return diags ;
9885 } ) ,
9986 lintGutter ( ) ,
10087 language ,
@@ -112,6 +99,9 @@ function createEditorConfig(
11299 ...defaultKeymap ,
113100 ...foldKeymap ,
114101 { key : "Mod-/" , run : toggleComment } ,
102+ { key : "Mod-z" , run : undo } ,
103+ { key : "Mod-y" , run : redo } ,
104+ { key : "Mod-Shift-z" , run : redo } ,
115105 ] ) ,
116106 autoRunCompartment . of ( autoRunState . get ( ) ? autoRunListener : [ ] ) ,
117107 EditorView . updateListener . of ( ( update ) => {
@@ -161,17 +151,20 @@ export function initializeEditors(containers: EditorContainers): void {
161151 htmlContainer ,
162152 htmlState . get ( ) ,
163153 htmlState ,
154+ "index.html" ,
164155 ) ;
165156 editors . css = createEditorConfig (
166157 css ( ) ,
167158 cssContainer ,
168159 cssState . get ( ) ,
169160 cssState ,
161+ "styles.css" ,
170162 ) ;
171163 editors . js = createEditorConfig (
172164 javascript ( ) ,
173165 jsContainer ,
174166 jsState . get ( ) ,
175167 jsState ,
168+ "script.js" ,
176169 ) ;
177170}
0 commit comments