From aed4e9c684ce8f4d2ba93bc6f54057da9e4a2b8a Mon Sep 17 00:00:00 2001 From: Eduardo Blancas Date: Wed, 16 Aug 2023 21:57:49 -0600 Subject: [PATCH] highlight working --- src/editor/editor.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/editor/editor.ts b/src/editor/editor.ts index 013c73f..bbf858f 100644 --- a/src/editor/editor.ts +++ b/src/editor/editor.ts @@ -62,6 +62,14 @@ const showStripes = ViewPlugin.fromClass( ) { this.decorations = stripeDeco(update.view); } + + + // let docIsSQL = /^\s*SELECT/.test(update.state.doc.sliceString(0, 100)) + // // let stateIsSQL = tr.startState.facet(language) == htmlLanguage + // // if (docIsSQL == stateIsSQL) return null + // update.view.dispatch({ + // effects: languageConf.reconfigure(docIsSQL ? sql() : sql()) + // }) } }, { @@ -69,12 +77,31 @@ const showStripes = ViewPlugin.fromClass( } ); + +import { EditorState, Compartment } from "@codemirror/state" +// import { language } from "@codemirror/language" +import { python } from "@codemirror/lang-python" + +const languageConf = new Compartment + +const autoLanguage = EditorState.transactionExtender.of(tr => { + // if (!tr.docChanged) return null + let docIsSQL = /^\s*%{1,2}sql/.test(tr.newDoc.sliceString(0, 100)) + // let stateIsSQL = tr.startState.facet(language) == htmlLanguage + // if (docIsSQL == stateIsSQL) return null + return { + effects: languageConf.reconfigure(docIsSQL ? sql() : python()) + } +}) + + // Full extension composed of elemental extensions export function zebraStripes(options: { step?: number } = {}): Extension { return [ baseTheme, typeof options.step !== 'number' ? [] : stepSize.of(options.step), showStripes, - sql() + languageConf.of(python()), + autoLanguage, ]; }