LaravelTest
211 строк · 9.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 noOptions = {};15var nonWS = /[^\s\u00a0]/;16var Pos = CodeMirror.Pos, cmp = CodeMirror.cmpPos;17
18function firstNonWS(str) {19var found = str.search(nonWS);20return found == -1 ? 0 : found;21}22
23CodeMirror.commands.toggleComment = function(cm) {24cm.toggleComment();25};26
27CodeMirror.defineExtension("toggleComment", function(options) {28if (!options) options = noOptions;29var cm = this;30var minLine = Infinity, ranges = this.listSelections(), mode = null;31for (var i = ranges.length - 1; i >= 0; i--) {32var from = ranges[i].from(), to = ranges[i].to();33if (from.line >= minLine) continue;34if (to.line >= minLine) to = Pos(minLine, 0);35minLine = from.line;36if (mode == null) {37if (cm.uncomment(from, to, options)) mode = "un";38else { cm.lineComment(from, to, options); mode = "line"; }39} else if (mode == "un") {40cm.uncomment(from, to, options);41} else {42cm.lineComment(from, to, options);43}44}45});46
47// Rough heuristic to try and detect lines that are part of multi-line string48function probablyInsideString(cm, pos, line) {49return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"\`]/.test(line)50}51
52function getMode(cm, pos) {53var mode = cm.getMode()54return mode.useInnerComments === false || !mode.innerMode ? mode : cm.getModeAt(pos)55}56
57CodeMirror.defineExtension("lineComment", function(from, to, options) {58if (!options) options = noOptions;59var self = this, mode = getMode(self, from);60var firstLine = self.getLine(from.line);61if (firstLine == null || probablyInsideString(self, from, firstLine)) return;62
63var commentString = options.lineComment || mode.lineComment;64if (!commentString) {65if (options.blockCommentStart || mode.blockCommentStart) {66options.fullLines = true;67self.blockComment(from, to, options);68}69return;70}71
72var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);73var pad = options.padding == null ? " " : options.padding;74var blankLines = options.commentBlankLines || from.line == to.line;75
76self.operation(function() {77if (options.indent) {78var baseString = null;79for (var i = from.line; i < end; ++i) {80var line = self.getLine(i);81var whitespace = line.slice(0, firstNonWS(line));82if (baseString == null || baseString.length > whitespace.length) {83baseString = whitespace;84}85}86for (var i = from.line; i < end; ++i) {87var line = self.getLine(i), cut = baseString.length;88if (!blankLines && !nonWS.test(line)) continue;89if (line.slice(0, cut) != baseString) cut = firstNonWS(line);90self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut));91}92} else {93for (var i = from.line; i < end; ++i) {94if (blankLines || nonWS.test(self.getLine(i)))95self.replaceRange(commentString + pad, Pos(i, 0));96}97}98});99});100
101CodeMirror.defineExtension("blockComment", function(from, to, options) {102if (!options) options = noOptions;103var self = this, mode = getMode(self, from);104var startString = options.blockCommentStart || mode.blockCommentStart;105var endString = options.blockCommentEnd || mode.blockCommentEnd;106if (!startString || !endString) {107if ((options.lineComment || mode.lineComment) && options.fullLines != false)108self.lineComment(from, to, options);109return;110}111if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return112
113var end = Math.min(to.line, self.lastLine());114if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;115
116var pad = options.padding == null ? " " : options.padding;117if (from.line > end) return;118
119self.operation(function() {120if (options.fullLines != false) {121var lastLineHasText = nonWS.test(self.getLine(end));122self.replaceRange(pad + endString, Pos(end));123self.replaceRange(startString + pad, Pos(from.line, 0));124var lead = options.blockCommentLead || mode.blockCommentLead;125if (lead != null) for (var i = from.line + 1; i <= end; ++i)126if (i != end || lastLineHasText)127self.replaceRange(lead + pad, Pos(i, 0));128} else {129var atCursor = cmp(self.getCursor("to"), to) == 0, empty = !self.somethingSelected()130self.replaceRange(endString, to);131if (atCursor) self.setSelection(empty ? to : self.getCursor("from"), to)132self.replaceRange(startString, from);133}134});135});136
137CodeMirror.defineExtension("uncomment", function(from, to, options) {138if (!options) options = noOptions;139var self = this, mode = getMode(self, from);140var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()), start = Math.min(from.line, end);141
142// Try finding line comments143var lineString = options.lineComment || mode.lineComment, lines = [];144var pad = options.padding == null ? " " : options.padding, didSomething;145lineComment: {146if (!lineString) break lineComment;147for (var i = start; i <= end; ++i) {148var line = self.getLine(i);149var found = line.indexOf(lineString);150if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1;151if (found == -1 && nonWS.test(line)) break lineComment;152if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;153lines.push(line);154}155self.operation(function() {156for (var i = start; i <= end; ++i) {157var line = lines[i - start];158var pos = line.indexOf(lineString), endPos = pos + lineString.length;159if (pos < 0) continue;160if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length;161didSomething = true;162self.replaceRange("", Pos(i, pos), Pos(i, endPos));163}164});165if (didSomething) return true;166}167
168// Try block comments169var startString = options.blockCommentStart || mode.blockCommentStart;170var endString = options.blockCommentEnd || mode.blockCommentEnd;171if (!startString || !endString) return false;172var lead = options.blockCommentLead || mode.blockCommentLead;173var startLine = self.getLine(start), open = startLine.indexOf(startString)174if (open == -1) return false175var endLine = end == start ? startLine : self.getLine(end)176var close = endLine.indexOf(endString, end == start ? open + startString.length : 0);177var insideStart = Pos(start, open + 1), insideEnd = Pos(end, close + 1)178if (close == -1 ||179!/comment/.test(self.getTokenTypeAt(insideStart)) ||180!/comment/.test(self.getTokenTypeAt(insideEnd)) ||181self.getRange(insideStart, insideEnd, "\n").indexOf(endString) > -1)182return false;183
184// Avoid killing block comments completely outside the selection.185// Positions of the last startString before the start of the selection, and the first endString after it.186var lastStart = startLine.lastIndexOf(startString, from.ch);187var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(endString, lastStart + startString.length);188if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from.ch) return false;189// Positions of the first endString after the end of the selection, and the last startString before it.190firstEnd = endLine.indexOf(endString, to.ch);191var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd - to.ch);192lastStart = (firstEnd == -1 || almostLastStart == -1) ? -1 : to.ch + almostLastStart;193if (firstEnd != -1 && lastStart != -1 && lastStart != to.ch) return false;194
195self.operation(function() {196self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)),197Pos(end, close + endString.length));198var openEnd = open + startString.length;199if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length;200self.replaceRange("", Pos(start, open), Pos(start, openEnd));201if (lead) for (var i = start + 1; i <= end; ++i) {202var line = self.getLine(i), found = line.indexOf(lead);203if (found == -1 || nonWS.test(line.slice(0, found))) continue;204var foundEnd = found + lead.length;205if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;206self.replaceRange("", Pos(i, found), Pos(i, foundEnd));207}208});209return true;210});211});212