Blame view

public/assets/addons/fastflow/plugins/codemirror/src/edit/deleteNearSelection.js 1.02 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  import { runInOp } from "../display/operations"
  import { ensureCursorVisible } from "../display/scrolling"
  import { cmp } from "../line/pos"
  import { replaceRange } from "../model/changes"
  import { lst } from "../util/misc"
  
  // Helper for deleting text near the selection(s), used to implement
  // backspace, delete, and similar functionality.
  export function deleteNearSelection(cm, compute) {
    let ranges = cm.doc.sel.ranges, kill = []
    // Build up a set of ranges to kill first, merging overlapping
    // ranges.
    for (let i = 0; i < ranges.length; i++) {
      let toKill = compute(ranges[i])
      while (kill.length && cmp(toKill.from, lst(kill).to) <= 0) {
        let replaced = kill.pop()
        if (cmp(replaced.from, toKill.from) < 0) {
          toKill.from = replaced.from
          break
        }
      }
      kill.push(toKill)
    }
    // Next, remove those actual ranges.
    runInOp(cm, () => {
      for (let i = kill.length - 1; i >= 0; i--)
        replaceRange(cm.doc, "", kill[i].from, kill[i].to, "+delete")
      ensureCursorVisible(cm)
    })
  }