LaravelTest
114 строк · 4.7 Кб
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 nonspace = /\S/g;13var repeat = String.prototype.repeat || function (n) { return Array(n + 1).join(this); };14function continueComment(cm) {15if (cm.getOption("disableInput")) return CodeMirror.Pass;16var ranges = cm.listSelections(), mode, inserts = [];17for (var i = 0; i < ranges.length; i++) {18var pos = ranges[i].head19if (!/\bcomment\b/.test(cm.getTokenTypeAt(pos))) return CodeMirror.Pass;20var modeHere = cm.getModeAt(pos)21if (!mode) mode = modeHere;22else if (mode != modeHere) return CodeMirror.Pass;23
24var insert = null, line, found;25var blockStart = mode.blockCommentStart, lineCmt = mode.lineComment;26if (blockStart && mode.blockCommentContinue) {27line = cm.getLine(pos.line);28var end = line.lastIndexOf(mode.blockCommentEnd, pos.ch - mode.blockCommentEnd.length);29// 1. if this block comment ended30// 2. if this is actually inside a line comment31if (end != -1 && end == pos.ch - mode.blockCommentEnd.length ||32lineCmt && (found = line.lastIndexOf(lineCmt, pos.ch - 1)) > -1 &&33/\bcomment\b/.test(cm.getTokenTypeAt({line: pos.line, ch: found + 1}))) {34// ...then don't continue it35} else if (pos.ch >= blockStart.length &&36(found = line.lastIndexOf(blockStart, pos.ch - blockStart.length)) > -1 &&37found > end) {38// reuse the existing leading spaces/tabs/mixed39// or build the correct indent using CM's tab/indent options40if (nonspaceAfter(0, line) >= found) {41insert = line.slice(0, found);42} else {43var tabSize = cm.options.tabSize, numTabs;44found = CodeMirror.countColumn(line, found, tabSize);45insert = !cm.options.indentWithTabs ? repeat.call(" ", found) :46repeat.call("\t", (numTabs = Math.floor(found / tabSize))) +47repeat.call(" ", found - tabSize * numTabs);48}49} else if ((found = line.indexOf(mode.blockCommentContinue)) > -1 &&50found <= pos.ch &&51found <= nonspaceAfter(0, line)) {52insert = line.slice(0, found);53}54if (insert != null) insert += mode.blockCommentContinue55}56if (insert == null && lineCmt && continueLineCommentEnabled(cm)) {57if (line == null) line = cm.getLine(pos.line);58found = line.indexOf(lineCmt);59// cursor at pos 0, line comment also at pos 0 => shift it down, don't continue60if (!pos.ch && !found) insert = "";61// continue only if the line starts with an optional space + line comment62else if (found > -1 && nonspaceAfter(0, line) >= found) {63// don't continue if there's only space(s) after cursor or the end of the line64insert = nonspaceAfter(pos.ch, line) > -1;65// but always continue if the next line starts with a line comment too66if (!insert) {67var next = cm.getLine(pos.line + 1) || '',68nextFound = next.indexOf(lineCmt);69insert = nextFound > -1 && nonspaceAfter(0, next) >= nextFound || null;70}71if (insert) {72insert = line.slice(0, found) + lineCmt +73line.slice(found + lineCmt.length).match(/^\s*/)[0];74}75}76}77if (insert == null) return CodeMirror.Pass;78inserts[i] = "\n" + insert;79}80
81cm.operation(function() {82for (var i = ranges.length - 1; i >= 0; i--)83cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");84});85}86
87function nonspaceAfter(ch, str) {88nonspace.lastIndex = ch;89var m = nonspace.exec(str);90return m ? m.index : -1;91}92
93function continueLineCommentEnabled(cm) {94var opt = cm.getOption("continueComments");95if (opt && typeof opt == "object")96return opt.continueLineComment !== false;97return true;98}99
100CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {101if (prev && prev != CodeMirror.Init)102cm.removeKeyMap("continueComment");103if (val) {104var key = "Enter";105if (typeof val == "string")106key = val;107else if (typeof val == "object" && val.key)108key = val.key;109var map = {name: "continueComment"};110map[key] = continueComment;111cm.addKeyMap(map);112}113});114});115