LaravelTest
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) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14var cmds = CodeMirror.commands;15var Pos = CodeMirror.Pos;16function posEq(a, b) { return a.line == b.line && a.ch == b.ch; }17
18// Kill 'ring'19
20var killRing = [];21function addToRing(str) {22killRing.push(str);23if (killRing.length > 50) killRing.shift();24}25function growRingTop(str) {26if (!killRing.length) return addToRing(str);27killRing[killRing.length - 1] += str;28}29function getFromRing(n) { return killRing[killRing.length - (n ? Math.min(n, 1) : 1)] || ""; }30function popFromRing() { if (killRing.length > 1) killRing.pop(); return getFromRing(); }31
32var lastKill = null;33
34// Internal generic kill function, used by several mapped kill "family" functions.35function _kill(cm, from, to, ring, text) {36if (text == null) text = cm.getRange(from, to);37
38if (ring == "grow" && lastKill && lastKill.cm == cm && posEq(from, lastKill.pos) && cm.isClean(lastKill.gen))39growRingTop(text);40else if (ring !== false)41addToRing(text);42cm.replaceRange("", from, to, "+delete");43
44if (ring == "grow") lastKill = {cm: cm, pos: from, gen: cm.changeGeneration()};45else lastKill = null;46}47
48// Boundaries of various units49
50function byChar(cm, pos, dir) {51return cm.findPosH(pos, dir, "char", true);52}53
54function byWord(cm, pos, dir) {55return cm.findPosH(pos, dir, "word", true);56}57
58function byLine(cm, pos, dir) {59return cm.findPosV(pos, dir, "line", cm.doc.sel.goalColumn);60}61
62function byPage(cm, pos, dir) {63return cm.findPosV(pos, dir, "page", cm.doc.sel.goalColumn);64}65
66function byParagraph(cm, pos, dir) {67var no = pos.line, line = cm.getLine(no);68var sawText = /\S/.test(dir < 0 ? line.slice(0, pos.ch) : line.slice(pos.ch));69var fst = cm.firstLine(), lst = cm.lastLine();70for (;;) {71no += dir;72if (no < fst || no > lst)73return cm.clipPos(Pos(no - dir, dir < 0 ? 0 : null));74line = cm.getLine(no);75var hasText = /\S/.test(line);76if (hasText) sawText = true;77else if (sawText) return Pos(no, 0);78}79}80
81function bySentence(cm, pos, dir) {82var line = pos.line, ch = pos.ch;83var text = cm.getLine(pos.line), sawWord = false;84for (;;) {85var next = text.charAt(ch + (dir < 0 ? -1 : 0));86if (!next) { // End/beginning of line reached87if (line == (dir < 0 ? cm.firstLine() : cm.lastLine())) return Pos(line, ch);88text = cm.getLine(line + dir);89if (!/\S/.test(text)) return Pos(line, ch);90line += dir;91ch = dir < 0 ? text.length : 0;92continue;93}94if (sawWord && /[!?.]/.test(next)) return Pos(line, ch + (dir > 0 ? 1 : 0));95if (!sawWord) sawWord = /\w/.test(next);96ch += dir;97}98}99
100function byExpr(cm, pos, dir) {101var wrap;102if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, {strict: true}))103&& wrap.match && (wrap.forward ? 1 : -1) == dir)104return dir > 0 ? Pos(wrap.to.line, wrap.to.ch + 1) : wrap.to;105
106for (var first = true;; first = false) {107var token = cm.getTokenAt(pos);108var after = Pos(pos.line, dir < 0 ? token.start : token.end);109if (first && dir > 0 && token.end == pos.ch || !/\w/.test(token.string)) {110var newPos = cm.findPosH(after, dir, "char");111if (posEq(after, newPos)) return pos;112else pos = newPos;113} else {114return after;115}116}117}118
119// Prefixes (only crudely supported)120
121function getPrefix(cm, precise) {122var digits = cm.state.emacsPrefix;123if (!digits) return precise ? null : 1;124clearPrefix(cm);125return digits == "-" ? -1 : Number(digits);126}127
128function repeated(cmd) {129var f = typeof cmd == "string" ? function(cm) { cm.execCommand(cmd); } : cmd;130return function(cm) {131var prefix = getPrefix(cm);132f(cm);133for (var i = 1; i < prefix; ++i) f(cm);134};135}136
137function findEnd(cm, pos, by, dir) {138var prefix = getPrefix(cm);139if (prefix < 0) { dir = -dir; prefix = -prefix; }140for (var i = 0; i < prefix; ++i) {141var newPos = by(cm, pos, dir);142if (posEq(newPos, pos)) break;143pos = newPos;144}145return pos;146}147
148function move(by, dir) {149var f = function(cm) {150cm.extendSelection(findEnd(cm, cm.getCursor(), by, dir));151};152f.motion = true;153return f;154}155
156function killTo(cm, by, dir, ring) {157var selections = cm.listSelections(), cursor;158var i = selections.length;159while (i--) {160cursor = selections[i].head;161_kill(cm, cursor, findEnd(cm, cursor, by, dir), ring);162}163}164
165function _killRegion(cm, ring) {166if (cm.somethingSelected()) {167var selections = cm.listSelections(), selection;168var i = selections.length;169while (i--) {170selection = selections[i];171_kill(cm, selection.anchor, selection.head, ring);172}173return true;174}175}176
177function addPrefix(cm, digit) {178if (cm.state.emacsPrefix) {179if (digit != "-") cm.state.emacsPrefix += digit;180return;181}182// Not active yet183cm.state.emacsPrefix = digit;184cm.on("keyHandled", maybeClearPrefix);185cm.on("inputRead", maybeDuplicateInput);186}187
188var prefixPreservingKeys = {"Alt-G": true, "Ctrl-X": true, "Ctrl-Q": true, "Ctrl-U": true};189
190function maybeClearPrefix(cm, arg) {191if (!cm.state.emacsPrefixMap && !prefixPreservingKeys.hasOwnProperty(arg))192clearPrefix(cm);193}194
195function clearPrefix(cm) {196cm.state.emacsPrefix = null;197cm.off("keyHandled", maybeClearPrefix);198cm.off("inputRead", maybeDuplicateInput);199}200
201function maybeDuplicateInput(cm, event) {202var dup = getPrefix(cm);203if (dup > 1 && event.origin == "+input") {204var one = event.text.join("\n"), txt = "";205for (var i = 1; i < dup; ++i) txt += one;206cm.replaceSelection(txt);207}208}209
210function maybeRemovePrefixMap(cm, arg) {211if (typeof arg == "string" && (/^\d$/.test(arg) || arg == "Ctrl-U")) return;212cm.removeKeyMap(prefixMap);213cm.state.emacsPrefixMap = false;214cm.off("keyHandled", maybeRemovePrefixMap);215cm.off("inputRead", maybeRemovePrefixMap);216}217
218// Utilities219
220cmds.setMark = function (cm) {221cm.setCursor(cm.getCursor());222cm.setExtending(!cm.getExtending());223cm.on("change", function() { cm.setExtending(false); });224}225
226function clearMark(cm) {227cm.setExtending(false);228cm.setCursor(cm.getCursor());229}230
231function makePrompt(msg) {232var fragment = document.createDocumentFragment();233var input = document.createElement("input");234input.setAttribute("type", "text");235input.style.width = "10em";236fragment.appendChild(document.createTextNode(msg + ": "));237fragment.appendChild(input);238return fragment;239}240
241function getInput(cm, msg, f) {242if (cm.openDialog)243cm.openDialog(makePrompt(msg), f, {bottom: true});244else245f(prompt(msg, ""));246}247
248function operateOnWord(cm, op) {249var start = cm.getCursor(), end = cm.findPosH(start, 1, "word");250cm.replaceRange(op(cm.getRange(start, end)), start, end);251cm.setCursor(end);252}253
254function toEnclosingExpr(cm) {255var pos = cm.getCursor(), line = pos.line, ch = pos.ch;256var stack = [];257while (line >= cm.firstLine()) {258var text = cm.getLine(line);259for (var i = ch == null ? text.length : ch; i > 0;) {260var ch = text.charAt(--i);261if (ch == ")")262stack.push("(");263else if (ch == "]")264stack.push("[");265else if (ch == "}")266stack.push("{");267else if (/[\(\{\[]/.test(ch) && (!stack.length || stack.pop() != ch))268return 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
277cmds.killRegion = function(cm) {278_kill(cm, cm.getCursor("start"), cm.getCursor("end"), true);279};280
281// Maps to emacs kill-line282cmds.killLineEmacs = repeated(function(cm) {283var start = cm.getCursor(), end = cm.clipPos(Pos(start.line));284var text = cm.getRange(start, end);285if (!/\S/.test(text)) {286text += "\n";287end = Pos(start.line + 1, 0);288}289_kill(cm, start, end, "grow", text);290});291
292cmds.killRingSave = function(cm) {293addToRing(cm.getSelection());294clearMark(cm);295};296
297cmds.yank = function(cm) {298var start = cm.getCursor();299cm.replaceRange(getFromRing(getPrefix(cm)), start, start, "paste");300cm.setSelection(start, cm.getCursor());301};302
303cmds.yankPop = function(cm) {304cm.replaceSelection(popFromRing(), "around", "paste");305};306
307cmds.forwardChar = move(byChar, 1);308
309cmds.backwardChar = move(byChar, -1)310
311cmds.deleteChar = function(cm) { killTo(cm, byChar, 1, false); };312
313cmds.deleteForwardChar = function(cm) {314_killRegion(cm, false) || killTo(cm, byChar, 1, false);315};316
317cmds.deleteBackwardChar = function(cm) {318_killRegion(cm, false) || killTo(cm, byChar, -1, false);319};320
321cmds.forwardWord = move(byWord, 1);322
323cmds.backwardWord = move(byWord, -1);324
325cmds.killWord = function(cm) { killTo(cm, byWord, 1, "grow"); };326
327cmds.backwardKillWord = function(cm) { killTo(cm, byWord, -1, "grow"); };328
329cmds.nextLine = move(byLine, 1);330
331cmds.previousLine = move(byLine, -1);332
333cmds.scrollDownCommand = move(byPage, -1);334
335cmds.scrollUpCommand = move(byPage, 1);336
337cmds.backwardParagraph = move(byParagraph, -1);338
339cmds.forwardParagraph = move(byParagraph, 1);340
341cmds.backwardSentence = move(bySentence, -1);342
343cmds.forwardSentence = move(bySentence, 1);344
345cmds.killSentence = function(cm) { killTo(cm, bySentence, 1, "grow"); };346
347cmds.backwardKillSentence = function(cm) {348_kill(cm, cm.getCursor(), bySentence(cm, cm.getCursor(), 1), "grow");349};350
351cmds.killSexp = function(cm) { killTo(cm, byExpr, 1, "grow"); };352
353cmds.backwardKillSexp = function(cm) { killTo(cm, byExpr, -1, "grow"); };354
355cmds.forwardSexp = move(byExpr, 1);356
357cmds.backwardSexp = move(byExpr, -1);358
359cmds.markSexp = function(cm) {360var cursor = cm.getCursor();361cm.setSelection(findEnd(cm, cursor, byExpr, 1), cursor);362};363
364cmds.transposeSexps = function(cm) {365var leftStart = byExpr(cm, cm.getCursor(), -1);366var leftEnd = byExpr(cm, leftStart, 1);367var rightEnd = byExpr(cm, leftEnd, 1);368var rightStart = byExpr(cm, rightEnd, -1);369cm.replaceRange(cm.getRange(rightStart, rightEnd) +370cm.getRange(leftEnd, rightStart) +371cm.getRange(leftStart, leftEnd), leftStart, rightEnd);372};373
374cmds.backwardUpList = repeated(toEnclosingExpr);375
376cmds.justOneSpace = function(cm) {377var pos = cm.getCursor(), from = pos.ch;378var to = pos.ch, text = cm.getLine(pos.line);379while (from && /\s/.test(text.charAt(from - 1))) --from;380while (to < text.length && /\s/.test(text.charAt(to))) ++to;381cm.replaceRange(" ", Pos(pos.line, from), Pos(pos.line, to));382};383
384cmds.openLine = repeated(function(cm) {385cm.replaceSelection("\n", "start");386});387
388// maps to emacs 'transpose-chars'389cmds.transposeCharsRepeatable = repeated(function(cm) {390cm.execCommand("transposeChars");391});392
393cmds.capitalizeWord = repeated(function(cm) {394operateOnWord(cm, function(w) {395var letter = w.search(/\w/);396if (letter == -1) return w;397return w.slice(0, letter) + w.charAt(letter).toUpperCase() +398w.slice(letter + 1).toLowerCase();399});400});401
402cmds.upcaseWord = repeated(function(cm) {403operateOnWord(cm, function(w) { return w.toUpperCase(); });404});405
406cmds.downcaseWord = repeated(function(cm) {407operateOnWord(cm, function(w) { return w.toLowerCase(); });408});409
410// maps to emacs 'undo'411cmds.undoRepeatable = repeated("undo");412
413cmds.keyboardQuit = function(cm) {414cm.execCommand("clearSearch");415clearMark(cm);416}417
418cmds.newline = repeated(function(cm) { cm.replaceSelection("\n", "end"); });419
420cmds.gotoLine = function(cm) {421var prefix = getPrefix(cm, true);422if (prefix != null && prefix > 0) return cm.setCursor(prefix - 1);423
424getInput(cm, "Goto line", function(str) {425var num;426if (str && !isNaN(num = Number(str)) && num == (num|0) && num > 0)427cm.setCursor(num - 1);428});429};430
431cmds.indentRigidly = function(cm) {432cm.indentSelection(getPrefix(cm, true) || cm.getOption("indentUnit"));433};434
435cmds.exchangePointAndMark = function(cm) {436cm.setSelection(cm.getCursor("head"), cm.getCursor("anchor"));437};438
439cmds.quotedInsertTab = repeated("insertTab");440
441cmds.universalArgument = function addPrefixMap(cm) {442cm.state.emacsPrefixMap = true;443cm.addKeyMap(prefixMap);444cm.on("keyHandled", maybeRemovePrefixMap);445cm.on("inputRead", maybeRemovePrefixMap);446};447
448CodeMirror.emacs = {kill: _kill, killRegion: _killRegion, repeated: repeated};449
450// Actual keymap451var 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
537var prefixMap = {"Ctrl-G": clearPrefix};538function regPrefix(d) {539prefixMap[d] = function(cm) { addPrefix(cm, d); };540keyMap["Ctrl-" + d] = function(cm) { addPrefix(cm, d); };541prefixPreservingKeys["Ctrl-" + d] = true;542}543for (var i = 0; i < 10; ++i) regPrefix(String(i));544regPrefix("-");545});546