LaravelTest
201 строка · 7.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) {12var defaults = {13pairs: "()[]{}''\"\"",14closeBefore: ")]}'\":;>",15triples: "",16explode: "[]{}"17};18
19var Pos = CodeMirror.Pos;20
21CodeMirror.defineOption("autoCloseBrackets", false, function(cm, val, old) {22if (old && old != CodeMirror.Init) {23cm.removeKeyMap(keyMap);24cm.state.closeBrackets = null;25}26if (val) {27ensureBound(getOption(val, "pairs"))28cm.state.closeBrackets = val;29cm.addKeyMap(keyMap);30}31});32
33function getOption(conf, name) {34if (name == "pairs" && typeof conf == "string") return conf;35if (typeof conf == "object" && conf[name] != null) return conf[name];36return defaults[name];37}38
39var keyMap = {Backspace: handleBackspace, Enter: handleEnter};40function ensureBound(chars) {41for (var i = 0; i < chars.length; i++) {42var ch = chars.charAt(i), key = "'" + ch + "'"43if (!keyMap[key]) keyMap[key] = handler(ch)44}45}46ensureBound(defaults.pairs + "`")47
48function handler(ch) {49return function(cm) { return handleChar(cm, ch); };50}51
52function getConfig(cm) {53var deflt = cm.state.closeBrackets;54if (!deflt || deflt.override) return deflt;55var mode = cm.getModeAt(cm.getCursor());56return mode.closeBrackets || deflt;57}58
59function handleBackspace(cm) {60var conf = getConfig(cm);61if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;62
63var pairs = getOption(conf, "pairs");64var ranges = cm.listSelections();65for (var i = 0; i < ranges.length; i++) {66if (!ranges[i].empty()) return CodeMirror.Pass;67var around = charsAround(cm, ranges[i].head);68if (!around || pairs.indexOf(around) % 2 != 0) return CodeMirror.Pass;69}70for (var i = ranges.length - 1; i >= 0; i--) {71var cur = ranges[i].head;72cm.replaceRange("", Pos(cur.line, cur.ch - 1), Pos(cur.line, cur.ch + 1), "+delete");73}74}75
76function handleEnter(cm) {77var conf = getConfig(cm);78var explode = conf && getOption(conf, "explode");79if (!explode || cm.getOption("disableInput")) return CodeMirror.Pass;80
81var ranges = cm.listSelections();82for (var i = 0; i < ranges.length; i++) {83if (!ranges[i].empty()) return CodeMirror.Pass;84var around = charsAround(cm, ranges[i].head);85if (!around || explode.indexOf(around) % 2 != 0) return CodeMirror.Pass;86}87cm.operation(function() {88var linesep = cm.lineSeparator() || "\n";89cm.replaceSelection(linesep + linesep, null);90moveSel(cm, -1)91ranges = cm.listSelections();92for (var i = 0; i < ranges.length; i++) {93var line = ranges[i].head.line;94cm.indentLine(line, null, true);95cm.indentLine(line + 1, null, true);96}97});98}99
100function moveSel(cm, dir) {101var newRanges = [], ranges = cm.listSelections(), primary = 0102for (var i = 0; i < ranges.length; i++) {103var range = ranges[i]104if (range.head == cm.getCursor()) primary = i105var pos = range.head.ch || dir > 0 ? {line: range.head.line, ch: range.head.ch + dir} : {line: range.head.line - 1}106newRanges.push({anchor: pos, head: pos})107}108cm.setSelections(newRanges, primary)109}110
111function contractSelection(sel) {112var inverted = CodeMirror.cmpPos(sel.anchor, sel.head) > 0;113return {anchor: new Pos(sel.anchor.line, sel.anchor.ch + (inverted ? -1 : 1)),114head: new Pos(sel.head.line, sel.head.ch + (inverted ? 1 : -1))};115}116
117function handleChar(cm, ch) {118var conf = getConfig(cm);119if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;120
121var pairs = getOption(conf, "pairs");122var pos = pairs.indexOf(ch);123if (pos == -1) return CodeMirror.Pass;124
125var closeBefore = getOption(conf,"closeBefore");126
127var triples = getOption(conf, "triples");128
129var identical = pairs.charAt(pos + 1) == ch;130var ranges = cm.listSelections();131var opening = pos % 2 == 0;132
133var type;134for (var i = 0; i < ranges.length; i++) {135var range = ranges[i], cur = range.head, curType;136var next = cm.getRange(cur, Pos(cur.line, cur.ch + 1));137if (opening && !range.empty()) {138curType = "surround";139} else if ((identical || !opening) && next == ch) {140if (identical && stringStartsAfter(cm, cur))141curType = "both";142else if (triples.indexOf(ch) >= 0 && cm.getRange(cur, Pos(cur.line, cur.ch + 3)) == ch + ch + ch)143curType = "skipThree";144else145curType = "skip";146} else if (identical && cur.ch > 1 && triples.indexOf(ch) >= 0 &&147cm.getRange(Pos(cur.line, cur.ch - 2), cur) == ch + ch) {148if (cur.ch > 2 && /\bstring/.test(cm.getTokenTypeAt(Pos(cur.line, cur.ch - 2)))) return CodeMirror.Pass;149curType = "addFour";150} else if (identical) {151var prev = cur.ch == 0 ? " " : cm.getRange(Pos(cur.line, cur.ch - 1), cur)152if (!CodeMirror.isWordChar(next) && prev != ch && !CodeMirror.isWordChar(prev)) curType = "both";153else return CodeMirror.Pass;154} else if (opening && (next.length === 0 || /\s/.test(next) || closeBefore.indexOf(next) > -1)) {155curType = "both";156} else {157return CodeMirror.Pass;158}159if (!type) type = curType;160else if (type != curType) return CodeMirror.Pass;161}162
163var left = pos % 2 ? pairs.charAt(pos - 1) : ch;164var right = pos % 2 ? ch : pairs.charAt(pos + 1);165cm.operation(function() {166if (type == "skip") {167moveSel(cm, 1)168} else if (type == "skipThree") {169moveSel(cm, 3)170} else if (type == "surround") {171var sels = cm.getSelections();172for (var i = 0; i < sels.length; i++)173sels[i] = left + sels[i] + right;174cm.replaceSelections(sels, "around");175sels = cm.listSelections().slice();176for (var i = 0; i < sels.length; i++)177sels[i] = contractSelection(sels[i]);178cm.setSelections(sels);179} else if (type == "both") {180cm.replaceSelection(left + right, null);181cm.triggerElectric(left + right);182moveSel(cm, -1)183} else if (type == "addFour") {184cm.replaceSelection(left + left + left + left, "before");185moveSel(cm, 1)186}187});188}189
190function charsAround(cm, pos) {191var str = cm.getRange(Pos(pos.line, pos.ch - 1),192Pos(pos.line, pos.ch + 1));193return str.length == 2 ? str : null;194}195
196function stringStartsAfter(cm, pos) {197var token = cm.getTokenAt(Pos(pos.line, pos.ch + 1))198return /\bstring/.test(token.type) && token.start == pos.ch &&199(pos.ch == 0 || !/\bstring/.test(cm.getTokenTypeAt(pos)))200}201});202