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/unindent.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
import { cm_start_end } from "./util";
8
9
CodeMirror.defineExtension("unindent_selection", function () {
10
// @ts-ignore
11
const editor = this;
12
13
for (let selection of editor.listSelections()) {
14
const { start_line, end_line } = cm_start_end(selection);
15
let all_need_unindent = true;
16
for (let n = start_line; n <= end_line; n++) {
17
const s = editor.getLine(n);
18
if (s == null) {
19
return;
20
}
21
if (s.length === 0 || s[0] === "\t" || s[0] === " ") {
22
continue;
23
} else {
24
all_need_unindent = false;
25
break;
26
}
27
}
28
if (all_need_unindent) {
29
for (let n = start_line; n <= end_line; n++) {
30
editor.indentLine(n, "subtract");
31
}
32
}
33
}
34
});
35
36