LaravelTest

Форк
0
545 строк · 16.0 Кб
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: https://codemirror.net/LICENSE
3

4
(function(mod) {
5
  if (typeof exports == "object" && typeof module == "object") // CommonJS
6
    mod(require("../lib/codemirror"));
7
  else if (typeof define == "function" && define.amd) // AMD
8
    define(["../lib/codemirror"], mod);
9
  else // Plain browser env
10
    mod(CodeMirror);
11
})(function(CodeMirror) {
12
  "use strict";
13

14
  var cmds = CodeMirror.commands;
15
  var Pos = CodeMirror.Pos;
16
  function posEq(a, b) { return a.line == b.line && a.ch == b.ch; }
17

18
  // Kill 'ring'
19

20
  var killRing = [];
21
  function addToRing(str) {
22
    killRing.push(str);
23
    if (killRing.length > 50) killRing.shift();
24
  }
25
  function growRingTop(str) {
26
    if (!killRing.length) return addToRing(str);
27
    killRing[killRing.length - 1] += str;
28
  }
29
  function getFromRing(n) { return killRing[killRing.length - (n ? Math.min(n, 1) : 1)] || ""; }
30
  function popFromRing() { if (killRing.length > 1) killRing.pop(); return getFromRing(); }
31

32
  var lastKill = null;
33

34
  // Internal generic kill function, used by several mapped kill "family" functions.
35
  function _kill(cm, from, to, ring, text) {
36
    if (text == null) text = cm.getRange(from, to);
37

38
    if (ring == "grow" && lastKill && lastKill.cm == cm && posEq(from, lastKill.pos) && cm.isClean(lastKill.gen))
39
      growRingTop(text);
40
    else if (ring !== false)
41
      addToRing(text);
42
    cm.replaceRange("", from, to, "+delete");
43

44
    if (ring == "grow") lastKill = {cm: cm, pos: from, gen: cm.changeGeneration()};
45
    else lastKill = null;
46
  }
47

48
  // Boundaries of various units
49

50
  function byChar(cm, pos, dir) {
51
    return cm.findPosH(pos, dir, "char", true);
52
  }
53

54
  function byWord(cm, pos, dir) {
55
    return cm.findPosH(pos, dir, "word", true);
56
  }
57

58
  function byLine(cm, pos, dir) {
59
    return cm.findPosV(pos, dir, "line", cm.doc.sel.goalColumn);
60
  }
61

62
  function byPage(cm, pos, dir) {
63
    return cm.findPosV(pos, dir, "page", cm.doc.sel.goalColumn);
64
  }
65

66
  function byParagraph(cm, pos, dir) {
67
    var no = pos.line, line = cm.getLine(no);
68
    var sawText = /\S/.test(dir < 0 ? line.slice(0, pos.ch) : line.slice(pos.ch));
69
    var fst = cm.firstLine(), lst = cm.lastLine();
70
    for (;;) {
71
      no += dir;
72
      if (no < fst || no > lst)
73
        return cm.clipPos(Pos(no - dir, dir < 0 ? 0 : null));
74
      line = cm.getLine(no);
75
      var hasText = /\S/.test(line);
76
      if (hasText) sawText = true;
77
      else if (sawText) return Pos(no, 0);
78
    }
79
  }
80

81
  function bySentence(cm, pos, dir) {
82
    var line = pos.line, ch = pos.ch;
83
    var text = cm.getLine(pos.line), sawWord = false;
84
    for (;;) {
85
      var next = text.charAt(ch + (dir < 0 ? -1 : 0));
86
      if (!next) { // End/beginning of line reached
87
        if (line == (dir < 0 ? cm.firstLine() : cm.lastLine())) return Pos(line, ch);
88
        text = cm.getLine(line + dir);
89
        if (!/\S/.test(text)) return Pos(line, ch);
90
        line += dir;
91
        ch = dir < 0 ? text.length : 0;
92
        continue;
93
      }
94
      if (sawWord && /[!?.]/.test(next)) return Pos(line, ch + (dir > 0 ? 1 : 0));
95
      if (!sawWord) sawWord = /\w/.test(next);
96
      ch += dir;
97
    }
98
  }
99

100
  function byExpr(cm, pos, dir) {
101
    var wrap;
102
    if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, {strict: true}))
103
        && wrap.match && (wrap.forward ? 1 : -1) == dir)
104
      return dir > 0 ? Pos(wrap.to.line, wrap.to.ch + 1) : wrap.to;
105

106
    for (var first = true;; first = false) {
107
      var token = cm.getTokenAt(pos);
108
      var after = Pos(pos.line, dir < 0 ? token.start : token.end);
109
      if (first && dir > 0 && token.end == pos.ch || !/\w/.test(token.string)) {
110
        var newPos = cm.findPosH(after, dir, "char");
111
        if (posEq(after, newPos)) return pos;
112
        else pos = newPos;
113
      } else {
114
        return after;
115
      }
116
    }
117
  }
118

119
  // Prefixes (only crudely supported)
120

121
  function getPrefix(cm, precise) {
122
    var digits = cm.state.emacsPrefix;
123
    if (!digits) return precise ? null : 1;
124
    clearPrefix(cm);
125
    return digits == "-" ? -1 : Number(digits);
126
  }
127

128
  function repeated(cmd) {
129
    var f = typeof cmd == "string" ? function(cm) { cm.execCommand(cmd); } : cmd;
130
    return function(cm) {
131
      var prefix = getPrefix(cm);
132
      f(cm);
133
      for (var i = 1; i < prefix; ++i) f(cm);
134
    };
135
  }
136

137
  function findEnd(cm, pos, by, dir) {
138
    var prefix = getPrefix(cm);
139
    if (prefix < 0) { dir = -dir; prefix = -prefix; }
140
    for (var i = 0; i < prefix; ++i) {
141
      var newPos = by(cm, pos, dir);
142
      if (posEq(newPos, pos)) break;
143
      pos = newPos;
144
    }
145
    return pos;
146
  }
147

148
  function move(by, dir) {
149
    var f = function(cm) {
150
      cm.extendSelection(findEnd(cm, cm.getCursor(), by, dir));
151
    };
152
    f.motion = true;
153
    return f;
154
  }
155

156
  function killTo(cm, by, dir, ring) {
157
    var selections = cm.listSelections(), cursor;
158
    var i = selections.length;
159
    while (i--) {
160
      cursor = selections[i].head;
161
      _kill(cm, cursor, findEnd(cm, cursor, by, dir), ring);
162
    }
163
  }
164

165
  function _killRegion(cm, ring) {
166
    if (cm.somethingSelected()) {
167
      var selections = cm.listSelections(), selection;
168
      var i = selections.length;
169
      while (i--) {
170
        selection = selections[i];
171
        _kill(cm, selection.anchor, selection.head, ring);
172
      }
173
      return true;
174
    }
175
  }
176

177
  function addPrefix(cm, digit) {
178
    if (cm.state.emacsPrefix) {
179
      if (digit != "-") cm.state.emacsPrefix += digit;
180
      return;
181
    }
182
    // Not active yet
183
    cm.state.emacsPrefix = digit;
184
    cm.on("keyHandled", maybeClearPrefix);
185
    cm.on("inputRead", maybeDuplicateInput);
186
  }
187

188
  var prefixPreservingKeys = {"Alt-G": true, "Ctrl-X": true, "Ctrl-Q": true, "Ctrl-U": true};
189

190
  function maybeClearPrefix(cm, arg) {
191
    if (!cm.state.emacsPrefixMap && !prefixPreservingKeys.hasOwnProperty(arg))
192
      clearPrefix(cm);
193
  }
194

195
  function clearPrefix(cm) {
196
    cm.state.emacsPrefix = null;
197
    cm.off("keyHandled", maybeClearPrefix);
198
    cm.off("inputRead", maybeDuplicateInput);
199
  }
200

201
  function maybeDuplicateInput(cm, event) {
202
    var dup = getPrefix(cm);
203
    if (dup > 1 && event.origin == "+input") {
204
      var one = event.text.join("\n"), txt = "";
205
      for (var i = 1; i < dup; ++i) txt += one;
206
      cm.replaceSelection(txt);
207
    }
208
  }
209

210
  function maybeRemovePrefixMap(cm, arg) {
211
    if (typeof arg == "string" && (/^\d$/.test(arg) || arg == "Ctrl-U")) return;
212
    cm.removeKeyMap(prefixMap);
213
    cm.state.emacsPrefixMap = false;
214
    cm.off("keyHandled", maybeRemovePrefixMap);
215
    cm.off("inputRead", maybeRemovePrefixMap);
216
  }
217

218
  // Utilities
219

220
  cmds.setMark = function (cm) {
221
    cm.setCursor(cm.getCursor());
222
    cm.setExtending(!cm.getExtending());
223
    cm.on("change", function() { cm.setExtending(false); });
224
  }
225

226
  function clearMark(cm) {
227
    cm.setExtending(false);
228
    cm.setCursor(cm.getCursor());
229
  }
230

231
  function makePrompt(msg) {
232
    var fragment = document.createDocumentFragment();
233
    var input = document.createElement("input");
234
    input.setAttribute("type", "text");
235
    input.style.width = "10em";
236
    fragment.appendChild(document.createTextNode(msg + ": "));
237
    fragment.appendChild(input);
238
    return fragment;
239
  }
240

241
  function getInput(cm, msg, f) {
242
    if (cm.openDialog)
243
      cm.openDialog(makePrompt(msg), f, {bottom: true});
244
    else
245
      f(prompt(msg, ""));
246
  }
247

248
  function operateOnWord(cm, op) {
249
    var start = cm.getCursor(), end = cm.findPosH(start, 1, "word");
250
    cm.replaceRange(op(cm.getRange(start, end)), start, end);
251
    cm.setCursor(end);
252
  }
253

254
  function toEnclosingExpr(cm) {
255
    var pos = cm.getCursor(), line = pos.line, ch = pos.ch;
256
    var stack = [];
257
    while (line >= cm.firstLine()) {
258
      var text = cm.getLine(line);
259
      for (var i = ch == null ? text.length : ch; i > 0;) {
260
        var ch = text.charAt(--i);
261
        if (ch == ")")
262
          stack.push("(");
263
        else if (ch == "]")
264
          stack.push("[");
265
        else if (ch == "}")
266
          stack.push("{");
267
        else if (/[\(\{\[]/.test(ch) && (!stack.length || stack.pop() != ch))
268
          return cm.extendSelection(Pos(line, i));
269
      }
270
      --line; ch = null;
271
    }
272
  }
273

274
  // Commands. Names should match emacs function names (albeit in camelCase)
275
  // except where emacs function names collide with code mirror core commands.
276

277
  cmds.killRegion = function(cm) {
278
    _kill(cm, cm.getCursor("start"), cm.getCursor("end"), true);
279
  };
280

281
  // Maps to emacs kill-line
282
  cmds.killLineEmacs = repeated(function(cm) {
283
    var start = cm.getCursor(), end = cm.clipPos(Pos(start.line));
284
    var text = cm.getRange(start, end);
285
    if (!/\S/.test(text)) {
286
      text += "\n";
287
      end = Pos(start.line + 1, 0);
288
    }
289
    _kill(cm, start, end, "grow", text);
290
  });
291

292
  cmds.killRingSave = function(cm) {
293
    addToRing(cm.getSelection());
294
    clearMark(cm);
295
  };
296

297
  cmds.yank = function(cm) {
298
    var start = cm.getCursor();
299
    cm.replaceRange(getFromRing(getPrefix(cm)), start, start, "paste");
300
    cm.setSelection(start, cm.getCursor());
301
  };
302

303
  cmds.yankPop = function(cm) {
304
    cm.replaceSelection(popFromRing(), "around", "paste");
305
  };
306

307
  cmds.forwardChar = move(byChar, 1);
308

309
  cmds.backwardChar = move(byChar, -1)
310

311
  cmds.deleteChar = function(cm) { killTo(cm, byChar, 1, false); };
312

313
  cmds.deleteForwardChar = function(cm) {
314
    _killRegion(cm, false) || killTo(cm, byChar, 1, false);
315
  };
316

317
  cmds.deleteBackwardChar = function(cm) {
318
    _killRegion(cm, false) || killTo(cm, byChar, -1, false);
319
  };
320

321
  cmds.forwardWord = move(byWord, 1);
322

323
  cmds.backwardWord = move(byWord, -1);
324

325
  cmds.killWord = function(cm) { killTo(cm, byWord, 1, "grow"); };
326

327
  cmds.backwardKillWord = function(cm) { killTo(cm, byWord, -1, "grow"); };
328

329
  cmds.nextLine = move(byLine, 1);
330

331
  cmds.previousLine = move(byLine, -1);
332

333
  cmds.scrollDownCommand = move(byPage, -1);
334

335
  cmds.scrollUpCommand = move(byPage, 1);
336

337
  cmds.backwardParagraph = move(byParagraph, -1);
338

339
  cmds.forwardParagraph = move(byParagraph, 1);
340

341
  cmds.backwardSentence = move(bySentence, -1);
342

343
  cmds.forwardSentence = move(bySentence, 1);
344

345
  cmds.killSentence = function(cm) { killTo(cm, bySentence, 1, "grow"); };
346

347
  cmds.backwardKillSentence = function(cm) {
348
    _kill(cm, cm.getCursor(), bySentence(cm, cm.getCursor(), 1), "grow");
349
  };
350

351
  cmds.killSexp = function(cm) { killTo(cm, byExpr, 1, "grow"); };
352

353
  cmds.backwardKillSexp = function(cm) { killTo(cm, byExpr, -1, "grow"); };
354

355
  cmds.forwardSexp = move(byExpr, 1);
356

357
  cmds.backwardSexp = move(byExpr, -1);
358

359
  cmds.markSexp = function(cm) {
360
    var cursor = cm.getCursor();
361
    cm.setSelection(findEnd(cm, cursor, byExpr, 1), cursor);
362
  };
363

364
  cmds.transposeSexps = function(cm) {
365
    var leftStart = byExpr(cm, cm.getCursor(), -1);
366
    var leftEnd = byExpr(cm, leftStart, 1);
367
    var rightEnd = byExpr(cm, leftEnd, 1);
368
    var rightStart = byExpr(cm, rightEnd, -1);
369
    cm.replaceRange(cm.getRange(rightStart, rightEnd) +
370
                    cm.getRange(leftEnd, rightStart) +
371
                    cm.getRange(leftStart, leftEnd), leftStart, rightEnd);
372
  };
373

374
  cmds.backwardUpList = repeated(toEnclosingExpr);
375

376
  cmds.justOneSpace = function(cm) {
377
    var pos = cm.getCursor(), from = pos.ch;
378
    var to = pos.ch, text = cm.getLine(pos.line);
379
    while (from && /\s/.test(text.charAt(from - 1))) --from;
380
    while (to < text.length && /\s/.test(text.charAt(to))) ++to;
381
    cm.replaceRange(" ", Pos(pos.line, from), Pos(pos.line, to));
382
  };
383

384
  cmds.openLine = repeated(function(cm) {
385
    cm.replaceSelection("\n", "start");
386
  });
387

388
  // maps to emacs 'transpose-chars'
389
  cmds.transposeCharsRepeatable = repeated(function(cm) {
390
    cm.execCommand("transposeChars");
391
  });
392

393
  cmds.capitalizeWord = repeated(function(cm) {
394
    operateOnWord(cm, function(w) {
395
      var letter = w.search(/\w/);
396
      if (letter == -1) return w;
397
      return w.slice(0, letter) + w.charAt(letter).toUpperCase() +
398
          w.slice(letter + 1).toLowerCase();
399
    });
400
  });
401

402
  cmds.upcaseWord = repeated(function(cm) {
403
    operateOnWord(cm, function(w) { return w.toUpperCase(); });
404
  });
405

406
  cmds.downcaseWord = repeated(function(cm) {
407
    operateOnWord(cm, function(w) { return w.toLowerCase(); });
408
  });
409

410
  // maps to emacs 'undo'
411
  cmds.undoRepeatable = repeated("undo");
412

413
  cmds.keyboardQuit = function(cm) {
414
    cm.execCommand("clearSearch");
415
    clearMark(cm);
416
  }
417

418
  cmds.newline = repeated(function(cm) { cm.replaceSelection("\n", "end"); });
419

420
  cmds.gotoLine = function(cm) {
421
    var prefix = getPrefix(cm, true);
422
    if (prefix != null && prefix > 0) return cm.setCursor(prefix - 1);
423

424
    getInput(cm, "Goto line", function(str) {
425
      var num;
426
      if (str && !isNaN(num = Number(str)) && num == (num|0) && num > 0)
427
      cm.setCursor(num - 1);
428
    });
429
  };
430

431
  cmds.indentRigidly = function(cm) {
432
    cm.indentSelection(getPrefix(cm, true) || cm.getOption("indentUnit"));
433
  };
434

435
  cmds.exchangePointAndMark = function(cm) {
436
    cm.setSelection(cm.getCursor("head"), cm.getCursor("anchor"));
437
  };
438

439
  cmds.quotedInsertTab = repeated("insertTab");
440

441
  cmds.universalArgument = function addPrefixMap(cm) {
442
    cm.state.emacsPrefixMap = true;
443
    cm.addKeyMap(prefixMap);
444
    cm.on("keyHandled", maybeRemovePrefixMap);
445
    cm.on("inputRead", maybeRemovePrefixMap);
446
  };
447

448
  CodeMirror.emacs = {kill: _kill, killRegion: _killRegion, repeated: repeated};
449

450
  // Actual keymap
451
  var keyMap = CodeMirror.keyMap.emacs = CodeMirror.normalizeKeyMap({
452
    "Ctrl-W": "killRegion",
453
    "Ctrl-K": "killLineEmacs",
454
    "Alt-W": "killRingSave",
455
    "Ctrl-Y": "yank",
456
    "Alt-Y": "yankPop",
457
    "Ctrl-Space": "setMark",
458
    "Ctrl-Shift-2": "setMark",
459
    "Ctrl-F": "forwardChar",
460
    "Ctrl-B": "backwardChar",
461
    "Right": "forwardChar",
462
    "Left": "backwardChar",
463
    "Ctrl-D": "deleteChar",
464
    "Delete": "deleteForwardChar",
465
    "Ctrl-H": "deleteBackwardChar",
466
    "Backspace": "deleteBackwardChar",
467
    "Alt-F": "forwardWord",
468
    "Alt-B": "backwardWord",
469
    "Alt-Right": "forwardWord",
470
    "Alt-Left": "backwardWord",
471
    "Alt-D": "killWord",
472
    "Alt-Backspace": "backwardKillWord",
473
    "Ctrl-N": "nextLine",
474
    "Ctrl-P": "previousLine",
475
    "Down": "nextLine",
476
    "Up": "previousLine",
477
    "Ctrl-A": "goLineStart",
478
    "Ctrl-E": "goLineEnd",
479
    "End": "goLineEnd",
480
    "Home": "goLineStart",
481
    "Alt-V": "scrollDownCommand",
482
    "Ctrl-V": "scrollUpCommand",
483
    "PageUp": "scrollDownCommand",
484
    "PageDown": "scrollUpCommand",
485
    "Ctrl-Up": "backwardParagraph",
486
    "Ctrl-Down": "forwardParagraph",
487
    "Alt-{": "backwardParagraph",
488
    "Alt-}": "forwardParagraph",
489
    "Alt-A": "backwardSentence",
490
    "Alt-E": "forwardSentence",
491
    "Alt-K": "killSentence",
492
    "Ctrl-X Delete": "backwardKillSentence",
493
    "Ctrl-Alt-K": "killSexp",
494
    "Ctrl-Alt-Backspace": "backwardKillSexp",
495
    "Ctrl-Alt-F": "forwardSexp",
496
    "Ctrl-Alt-B": "backwardSexp",
497
    "Shift-Ctrl-Alt-2": "markSexp",
498
    "Ctrl-Alt-T": "transposeSexps",
499
    "Ctrl-Alt-U": "backwardUpList",
500
    "Alt-Space": "justOneSpace",
501
    "Ctrl-O": "openLine",
502
    "Ctrl-T": "transposeCharsRepeatable",
503
    "Alt-C": "capitalizeWord",
504
    "Alt-U": "upcaseWord",
505
    "Alt-L": "downcaseWord",
506
    "Alt-;": "toggleComment",
507
    "Ctrl-/": "undoRepeatable",
508
    "Shift-Ctrl--": "undoRepeatable",
509
    "Ctrl-Z": "undoRepeatable",
510
    "Cmd-Z": "undoRepeatable",
511
    "Ctrl-X U": "undoRepeatable",
512
    "Shift-Ctrl-Z": "redo",
513
    "Shift-Alt-,": "goDocStart",
514
    "Shift-Alt-.": "goDocEnd",
515
    "Ctrl-S": "findPersistentNext",
516
    "Ctrl-R": "findPersistentPrev",
517
    "Ctrl-G": "keyboardQuit",
518
    "Shift-Alt-5": "replace",
519
    "Alt-/": "autocomplete",
520
    "Enter": "newlineAndIndent",
521
    "Ctrl-J": "newline",
522
    "Tab": "indentAuto",
523
    "Alt-G G": "gotoLine",
524
    "Ctrl-X Tab": "indentRigidly",
525
    "Ctrl-X Ctrl-X": "exchangePointAndMark",
526
    "Ctrl-X Ctrl-S": "save",
527
    "Ctrl-X Ctrl-W": "save",
528
    "Ctrl-X S": "saveAll",
529
    "Ctrl-X F": "open",
530
    "Ctrl-X K": "close",
531
    "Ctrl-X H": "selectAll",
532
    "Ctrl-Q Tab": "quotedInsertTab",
533
    "Ctrl-U": "universalArgument",
534
    "fallthrough": "default"
535
  });
536

537
  var prefixMap = {"Ctrl-G": clearPrefix};
538
  function regPrefix(d) {
539
    prefixMap[d] = function(cm) { addPrefix(cm, d); };
540
    keyMap["Ctrl-" + d] = function(cm) { addPrefix(cm, d); };
541
    prefixPreservingKeys["Ctrl-" + d] = true;
542
  }
543
  for (var i = 0; i < 10; ++i) regPrefix(String(i));
544
  regPrefix("-");
545
});
546

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.