LaravelTest
292 строки · 11.4 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// Define search commands. Depends on dialog.js or another
5// implementation of the openDialog method.
6
7// Replace works a little oddly -- it will do the replace on the next
8// Ctrl-G (or whatever is bound to findNext) press. You prevent a
9// replace by making sure the match is no longer selected when hitting
10// Ctrl-G.
11
12(function(mod) {13if (typeof exports == "object" && typeof module == "object") // CommonJS14mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));15else if (typeof define == "function" && define.amd) // AMD16define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);17else // Plain browser env18mod(CodeMirror);19})(function(CodeMirror) {20"use strict";21
22// default search panel location23CodeMirror.defineOption("search", {bottom: false});24
25function searchOverlay(query, caseInsensitive) {26if (typeof query == "string")27query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");28else if (!query.global)29query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");30
31return {token: function(stream) {32query.lastIndex = stream.pos;33var match = query.exec(stream.string);34if (match && match.index == stream.pos) {35stream.pos += match[0].length || 1;36return "searching";37} else if (match) {38stream.pos = match.index;39} else {40stream.skipToEnd();41}42}};43}44
45function SearchState() {46this.posFrom = this.posTo = this.lastQuery = this.query = null;47this.overlay = null;48}49
50function getSearchState(cm) {51return cm.state.search || (cm.state.search = new SearchState());52}53
54function queryCaseInsensitive(query) {55return typeof query == "string" && query == query.toLowerCase();56}57
58function getSearchCursor(cm, query, pos) {59// Heuristic: if the query string is all lowercase, do a case insensitive search.60return cm.getSearchCursor(query, pos, {caseFold: queryCaseInsensitive(query), multiline: true});61}62
63function persistentDialog(cm, text, deflt, onEnter, onKeyDown) {64cm.openDialog(text, onEnter, {65value: deflt,66selectValueOnOpen: true,67closeOnEnter: false,68onClose: function() { clearSearch(cm); },69onKeyDown: onKeyDown,70bottom: cm.options.search.bottom71});72}73
74function dialog(cm, text, shortText, deflt, f) {75if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true, bottom: cm.options.search.bottom});76else f(prompt(shortText, deflt));77}78
79function confirmDialog(cm, text, shortText, fs) {80if (cm.openConfirm) cm.openConfirm(text, fs);81else if (confirm(shortText)) fs[0]();82}83
84function parseString(string) {85return string.replace(/\\([nrt\\])/g, function(match, ch) {86if (ch == "n") return "\n"87if (ch == "r") return "\r"88if (ch == "t") return "\t"89if (ch == "\\") return "\\"90return match91})92}93
94function parseQuery(query) {95var isRE = query.match(/^\/(.*)\/([a-z]*)$/);96if (isRE) {97try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }98catch(e) {} // Not a regular expression after all, do a string search99} else {100query = parseString(query)101}102if (typeof query == "string" ? query == "" : query.test(""))103query = /x^/;104return query;105}106
107function startSearch(cm, state, query) {108state.queryText = query;109state.query = parseQuery(query);110cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));111state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));112cm.addOverlay(state.overlay);113if (cm.showMatchesOnScrollbar) {114if (state.annotate) { state.annotate.clear(); state.annotate = null; }115state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));116}117}118
119function doSearch(cm, rev, persistent, immediate) {120var state = getSearchState(cm);121if (state.query) return findNext(cm, rev);122var q = cm.getSelection() || state.lastQuery;123if (q instanceof RegExp && q.source == "x^") q = null124if (persistent && cm.openDialog) {125var hiding = null126var searchNext = function(query, event) {127CodeMirror.e_stop(event);128if (!query) return;129if (query != state.queryText) {130startSearch(cm, state, query);131state.posFrom = state.posTo = cm.getCursor();132}133if (hiding) hiding.style.opacity = 1134findNext(cm, event.shiftKey, function(_, to) {135var dialog136if (to.line < 3 && document.querySelector &&137(dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&138dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)139(hiding = dialog).style.opacity = .4140})141};142persistentDialog(cm, getQueryDialog(cm), q, searchNext, function(event, query) {143var keyName = CodeMirror.keyName(event)144var extra = cm.getOption('extraKeys'), cmd = (extra && extra[keyName]) || CodeMirror.keyMap[cm.getOption("keyMap")][keyName]145if (cmd == "findNext" || cmd == "findPrev" ||146cmd == "findPersistentNext" || cmd == "findPersistentPrev") {147CodeMirror.e_stop(event);148startSearch(cm, getSearchState(cm), query);149cm.execCommand(cmd);150} else if (cmd == "find" || cmd == "findPersistent") {151CodeMirror.e_stop(event);152searchNext(query, event);153}154});155if (immediate && q) {156startSearch(cm, state, q);157findNext(cm, rev);158}159} else {160dialog(cm, getQueryDialog(cm), "Search for:", q, function(query) {161if (query && !state.query) cm.operation(function() {162startSearch(cm, state, query);163state.posFrom = state.posTo = cm.getCursor();164findNext(cm, rev);165});166});167}168}169
170function findNext(cm, rev, callback) {cm.operation(function() {171var state = getSearchState(cm);172var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);173if (!cursor.find(rev)) {174cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));175if (!cursor.find(rev)) return;176}177cm.setSelection(cursor.from(), cursor.to());178cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);179state.posFrom = cursor.from(); state.posTo = cursor.to();180if (callback) callback(cursor.from(), cursor.to())181});}182
183function clearSearch(cm) {cm.operation(function() {184var state = getSearchState(cm);185state.lastQuery = state.query;186if (!state.query) return;187state.query = state.queryText = null;188cm.removeOverlay(state.overlay);189if (state.annotate) { state.annotate.clear(); state.annotate = null; }190});}191
192function el(tag, attrs) {193var element = tag ? document.createElement(tag) : document.createDocumentFragment();194for (var key in attrs) {195element[key] = attrs[key];196}197for (var i = 2; i < arguments.length; i++) {198var child = arguments[i]199element.appendChild(typeof child == "string" ? document.createTextNode(child) : child);200}201return element;202}203
204function getQueryDialog(cm) {205return el("", null,206el("span", {className: "CodeMirror-search-label"}, cm.phrase("Search:")), " ",207el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}), " ",208el("span", {style: "color: #888", className: "CodeMirror-search-hint"},209cm.phrase("(Use /re/ syntax for regexp search)")));210}211function getReplaceQueryDialog(cm) {212return el("", null, " ",213el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}), " ",214el("span", {style: "color: #888", className: "CodeMirror-search-hint"},215cm.phrase("(Use /re/ syntax for regexp search)")));216}217function getReplacementQueryDialog(cm) {218return el("", null,219el("span", {className: "CodeMirror-search-label"}, cm.phrase("With:")), " ",220el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}));221}222function getDoReplaceConfirm(cm) {223return el("", null,224el("span", {className: "CodeMirror-search-label"}, cm.phrase("Replace?")), " ",225el("button", {}, cm.phrase("Yes")), " ",226el("button", {}, cm.phrase("No")), " ",227el("button", {}, cm.phrase("All")), " ",228el("button", {}, cm.phrase("Stop")));229}230
231function replaceAll(cm, query, text) {232cm.operation(function() {233for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {234if (typeof query != "string") {235var match = cm.getRange(cursor.from(), cursor.to()).match(query);236cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));237} else cursor.replace(text);238}239});240}241
242function replace(cm, all) {243if (cm.getOption("readOnly")) return;244var query = cm.getSelection() || getSearchState(cm).lastQuery;245var dialogText = all ? cm.phrase("Replace all:") : cm.phrase("Replace:")246var fragment = el("", null,247el("span", {className: "CodeMirror-search-label"}, dialogText),248getReplaceQueryDialog(cm))249dialog(cm, fragment, dialogText, query, function(query) {250if (!query) return;251query = parseQuery(query);252dialog(cm, getReplacementQueryDialog(cm), cm.phrase("Replace with:"), "", function(text) {253text = parseString(text)254if (all) {255replaceAll(cm, query, text)256} else {257clearSearch(cm);258var cursor = getSearchCursor(cm, query, cm.getCursor("from"));259var advance = function() {260var start = cursor.from(), match;261if (!(match = cursor.findNext())) {262cursor = getSearchCursor(cm, query);263if (!(match = cursor.findNext()) ||264(start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;265}266cm.setSelection(cursor.from(), cursor.to());267cm.scrollIntoView({from: cursor.from(), to: cursor.to()});268confirmDialog(cm, getDoReplaceConfirm(cm), cm.phrase("Replace?"),269[function() {doReplace(match);}, advance,270function() {replaceAll(cm, query, text)}]);271};272var doReplace = function(match) {273cursor.replace(typeof query == "string" ? text :274text.replace(/\$(\d)/g, function(_, i) {return match[i];}));275advance();276};277advance();278}279});280});281}282
283CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};284CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};285CodeMirror.commands.findPersistentNext = function(cm) {doSearch(cm, false, true, true);};286CodeMirror.commands.findPersistentPrev = function(cm) {doSearch(cm, true, true, true);};287CodeMirror.commands.findNext = doSearch;288CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};289CodeMirror.commands.clearSearch = clearSearch;290CodeMirror.commands.replace = replace;291CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};292});293