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/find-in-line.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
/*
9
Find pos {line:line, ch:ch} of first line that contains the
10
string s, or returns undefined if no single line contains s.
11
Should be much faster than calling getLine or getValue.
12
*/
13
14
CodeMirror.defineExtension("find_in_line", function (
15
s: string
16
): CodeMirror.Position | undefined {
17
// @ts-ignore
18
const cm: any = this;
19
20
let line: number = -1;
21
let ch: number = 0;
22
let i = 0;
23
cm.eachLine(function (z) {
24
ch = z.text.indexOf(s);
25
if (ch !== -1) {
26
line = i;
27
return true; // undocumented - calling false stops iteration
28
}
29
i += 1;
30
return false;
31
});
32
if (line >= 0) {
33
return { line, ch };
34
}
35
});
36
37