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/latex-completions.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
/*
7
Support tab/control+space completions when editing tex documents.
8
9
This just uses the file @cocalc/assets/codemirror-extra/data/latex-completions.txt,
10
and I can't remember where that came from or how to update it.
11
*/
12
13
import * as CodeMirror from "codemirror";
14
import { startswith } from "@cocalc/util/misc";
15
const data = require("@cocalc/assets/codemirror-extra/data/latex-completions.txt");
16
17
const completions: string[] = data.split("\n");
18
19
function tex_hint(editor) {
20
const cur = editor.getCursor();
21
22
// Find the most recent backslash, since all our completions start with backslash.
23
const line = editor.getLine(cur.line);
24
const s = line.slice(0, cur.ch);
25
const i = s.lastIndexOf("\\");
26
const list: string[] = [];
27
if (i == -1) {
28
// nothing to complete
29
} else {
30
// maybe something -- search
31
// First, as a convenience if user completes `\begin{it[cursor here]}` do not
32
// end up with two close braces. This helps compensate with the
33
// "Auto close brackets: automatically close brackets" mode.
34
const delete_trailing_brace = line[cur.ch] == "}";
35
const t = s.slice(i);
36
for (const word of completions) {
37
if (startswith(word, t)) {
38
if (delete_trailing_brace && word[word.length - 1] == "}") {
39
list.push(word.slice(0, word.length - 1));
40
} else {
41
list.push(word);
42
}
43
}
44
}
45
}
46
return {
47
list,
48
from: CodeMirror.Pos(cur.line, i),
49
to: CodeMirror.Pos(cur.line, cur.ch),
50
};
51
}
52
53
CodeMirror.registerHelper("hint", "stex", tex_hint);
54
55