Path: blob/master/src/packages/frontend/codemirror/extensions/unindent.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";6import { cm_start_end } from "./util";78CodeMirror.defineExtension("unindent_selection", function () {9// @ts-ignore10const editor = this;1112for (let selection of editor.listSelections()) {13const { start_line, end_line } = cm_start_end(selection);14let all_need_unindent = true;15for (let n = start_line; n <= end_line; n++) {16const s = editor.getLine(n);17if (s == null) {18return;19}20if (s.length === 0 || s[0] === "\t" || s[0] === " ") {21continue;22} else {23all_need_unindent = false;24break;25}26}27if (all_need_unindent) {28for (let n = start_line; n <= end_line; n++) {29editor.indentLine(n, "subtract");30}31}32}33});343536