Path: blob/master/src/packages/frontend/codemirror/extensions/spellcheck-highlight.ts
1496 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import * as CodeMirror from "codemirror";67// Codemirror extension that takes as input an arrow of words (or undefined)8// and visibly keeps those marked as misspelled. If given empty input, cancels this.9// If given another input, that replaces the current one.10CodeMirror.defineExtension("spellcheck_highlight", function (11words: string[] | undefined12) {13// @ts-ignore14const cm: any = this;15if (cm._spellcheck_highlight_overlay != null) {16cm.removeOverlay(cm._spellcheck_highlight_overlay);17delete cm._spellcheck_highlight_overlay;18}19if (words != null && words.length > 0) {20const v: Set<string> = new Set(words);21// define overlay mode22const token = function (stream) {23// stream.match(/^\w+/) means "begins with 1 or more word characters", and eats them all.24if (stream.match(/^\w+/) && v.has(stream.current())) {25return "spell-error";26}27// eat whitespace28while (stream.next() != null) {29// stream.match(/^\w+/, false) means "begins with 1 or more word characters", but don't eat them up30if (stream.match(/^\w+/, false)) {31return;32}33}34};35cm._spellcheck_highlight_overlay = { token };36cm.addOverlay(cm._spellcheck_highlight_overlay);37}38});394041