CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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