From cfb5c75a2a01a30ab4c304ffcf381b5671bc13cb Mon Sep 17 00:00:00 2001 From: Eduardo Blancas Date: Wed, 16 Aug 2023 22:08:49 -0600 Subject: [PATCH] clean up syntax highlighting --- src/editor/editor.ts | 89 ++------------------------------------------ src/editor/index.ts | 6 +-- 2 files changed, 6 insertions(+), 89 deletions(-) diff --git a/src/editor/editor.ts b/src/editor/editor.ts index bbf858f..74e0efc 100644 --- a/src/editor/editor.ts +++ b/src/editor/editor.ts @@ -1,86 +1,6 @@ -import { Extension, Facet, RangeSetBuilder } from '@codemirror/state'; -import { - Decoration, - DecorationSet, - EditorView, - ViewPlugin, - ViewUpdate -} from '@codemirror/view'; -import { sql } from '@codemirror/lang-sql'; - -// Defines new styles for this extension -const baseTheme = EditorView.baseTheme({ - // We need to set some transparency because the stripe are above - // the selection layer - '&light .cm-zebraStripe': { backgroundColor: '#d4fafaaa' }, - '&dark .cm-zebraStripe': { backgroundColor: '#1a2727aa' } -}); - -// Resolve step to use in the editor -const stepSize = Facet.define({ - combine: values => (values.length ? Math.min(...values) : 2) -}); - -// Add decoration to editor lines -const stripe = Decoration.line({ - attributes: { class: 'cm-zebraStripe' } -}); - -// Create the range of lines requiring decorations -function stripeDeco(view: EditorView) { - const step = view.state.facet(stepSize); - const builder = new RangeSetBuilder(); - for (const { from, to } of view.visibleRanges) { - for (let pos = from; pos <= to;) { - const line = view.state.doc.lineAt(pos); - if (line.number % step === 0) { - builder.add(line.from, line.from, stripe); - } - pos = line.to + 1; - } - } - return builder.finish(); -} - -// Update the decoration status of the editor view -const showStripes = ViewPlugin.fromClass( - class { - decorations: DecorationSet; - - constructor(view: EditorView) { - this.decorations = stripeDeco(view); - } - - update(update: ViewUpdate) { - // Update the stripes if the document changed, - // the viewport changed or the stripes step changed. - const oldStep = update.startState.facet(stepSize); - if ( - update.docChanged || - update.viewportChanged || - oldStep !== update.view.state.facet(stepSize) - ) { - 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()) - // }) - } - }, - { - decorations: v => v.decorations - } -); - - -import { EditorState, Compartment } from "@codemirror/state" -// import { language } from "@codemirror/language" +import { Compartment, EditorState, Extension } from "@codemirror/state" import { python } from "@codemirror/lang-python" +import { sql } from '@codemirror/lang-sql' const languageConf = new Compartment @@ -96,11 +16,8 @@ const autoLanguage = EditorState.transactionExtender.of(tr => { // Full extension composed of elemental extensions -export function zebraStripes(options: { step?: number } = {}): Extension { +export function zebraStripes(): Extension { return [ - baseTheme, - typeof options.step !== 'number' ? [] : stepSize.of(options.step), - showStripes, languageConf.of(python()), autoLanguage, ]; diff --git a/src/editor/index.ts b/src/editor/index.ts index 7a8ce10..246bb47 100644 --- a/src/editor/index.ts +++ b/src/editor/index.ts @@ -26,8 +26,8 @@ const plugin_editor: JupyterFrontEndPlugin = { default: 2, factory: () => // The factory will be called for every new CodeMirror editor - EditorExtensionRegistry.createConfigurableExtension((step: number) => - zebraStripes({ step }) + EditorExtensionRegistry.createConfigurableExtension(() => + zebraStripes() ), // JSON schema defining the CodeMirror extension parameters schema: { @@ -40,4 +40,4 @@ const plugin_editor: JupyterFrontEndPlugin = { } }; -export { plugin_editor }; \ No newline at end of file +export { plugin_editor };