LaravelTest
160 строк · 6.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 Pos = CodeMirror.Pos;15
16function findParagraph(cm, pos, options) {17var startRE = options.paragraphStart || cm.getHelper(pos, "paragraphStart");18for (var start = pos.line, first = cm.firstLine(); start > first; --start) {19var line = cm.getLine(start);20if (startRE && startRE.test(line)) break;21if (!/\S/.test(line)) { ++start; break; }22}23var endRE = options.paragraphEnd || cm.getHelper(pos, "paragraphEnd");24for (var end = pos.line + 1, last = cm.lastLine(); end <= last; ++end) {25var line = cm.getLine(end);26if (endRE && endRE.test(line)) { ++end; break; }27if (!/\S/.test(line)) break;28}29return {from: start, to: end};30}31
32function findBreakPoint(text, column, wrapOn, killTrailingSpace, forceBreak) {33var at = column34while (at < text.length && text.charAt(at) == " ") at++35for (; at > 0; --at)36if (wrapOn.test(text.slice(at - 1, at + 1))) break;37
38if (!forceBreak && at <= text.match(/^[ \t]*/)[0].length) {39// didn't find a break point before column, in non-forceBreak mode try to40// find one after 'column'.41for (at = column + 1; at < text.length - 1; ++at) {42if (wrapOn.test(text.slice(at - 1, at + 1))) break;43}44}45
46for (var first = true;; first = false) {47var endOfText = at;48if (killTrailingSpace)49while (text.charAt(endOfText - 1) == " ") --endOfText;50if (endOfText == 0 && first) at = column;51else return {from: endOfText, to: at};52}53}54
55function wrapRange(cm, from, to, options) {56from = cm.clipPos(from); to = cm.clipPos(to);57var column = options.column || 80;58var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;59var forceBreak = options.forceBreak !== false;60var killTrailing = options.killTrailingSpace !== false;61var changes = [], curLine = "", curNo = from.line;62var lines = cm.getRange(from, to, false);63if (!lines.length) return null;64var leadingSpace = lines[0].match(/^[ \t]*/)[0];65if (leadingSpace.length >= column) column = leadingSpace.length + 166
67for (var i = 0; i < lines.length; ++i) {68var text = lines[i], oldLen = curLine.length, spaceInserted = 0;69if (curLine && text && !wrapOn.test(curLine.charAt(curLine.length - 1) + text.charAt(0))) {70curLine += " ";71spaceInserted = 1;72}73var spaceTrimmed = "";74if (i) {75spaceTrimmed = text.match(/^\s*/)[0];76text = text.slice(spaceTrimmed.length);77}78curLine += text;79if (i) {80var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&81findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);82// If this isn't broken, or is broken at a different point, remove old break83if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {84changes.push({text: [spaceInserted ? " " : ""],85from: Pos(curNo, oldLen),86to: Pos(curNo + 1, spaceTrimmed.length)});87} else {88curLine = leadingSpace + text;89++curNo;90}91}92while (curLine.length > column) {93var bp = findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);94if (bp.from != bp.to ||95forceBreak && leadingSpace !== curLine.slice(0, bp.to)) {96changes.push({text: ["", leadingSpace],97from: Pos(curNo, bp.from),98to: Pos(curNo, bp.to)});99curLine = leadingSpace + curLine.slice(bp.to);100++curNo;101} else {102break;103}104}105}106if (changes.length) cm.operation(function() {107for (var i = 0; i < changes.length; ++i) {108var change = changes[i];109if (change.text || CodeMirror.cmpPos(change.from, change.to))110cm.replaceRange(change.text, change.from, change.to);111}112});113return changes.length ? {from: changes[0].from, to: CodeMirror.changeEnd(changes[changes.length - 1])} : null;114}115
116CodeMirror.defineExtension("wrapParagraph", function(pos, options) {117options = options || {};118if (!pos) pos = this.getCursor();119var para = findParagraph(this, pos, options);120return wrapRange(this, Pos(para.from, 0), Pos(para.to - 1), options);121});122
123CodeMirror.commands.wrapLines = function(cm) {124cm.operation(function() {125var ranges = cm.listSelections(), at = cm.lastLine() + 1;126for (var i = ranges.length - 1; i >= 0; i--) {127var range = ranges[i], span;128if (range.empty()) {129var para = findParagraph(cm, range.head, {});130span = {from: Pos(para.from, 0), to: Pos(para.to - 1)};131} else {132span = {from: range.from(), to: range.to()};133}134if (span.to.line >= at) continue;135at = span.from.line;136wrapRange(cm, span.from, span.to, {});137}138});139};140
141CodeMirror.defineExtension("wrapRange", function(from, to, options) {142return wrapRange(this, from, to, options || {});143});144
145CodeMirror.defineExtension("wrapParagraphsInRange", function(from, to, options) {146options = options || {};147var cm = this, paras = [];148for (var line = from.line; line <= to.line;) {149var para = findParagraph(cm, Pos(line, 0), options);150paras.push(para);151line = para.to;152}153var madeChange = false;154if (paras.length) cm.operation(function() {155for (var i = paras.length - 1; i >= 0; --i)156madeChange = madeChange || wrapRange(cm, Pos(paras[i].from, 0), Pos(paras[i].to - 1), options);157});158return madeChange;159});160});161