LaravelTest

Форк
0
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) {
13
  if (typeof exports == "object" && typeof module == "object") // CommonJS
14
    mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
15
  else if (typeof define == "function" && define.amd) // AMD
16
    define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
17
  else // Plain browser env
18
    mod(CodeMirror);
19
})(function(CodeMirror) {
20
  "use strict";
21

22
  // default search panel location
23
  CodeMirror.defineOption("search", {bottom: false});
24

25
  function searchOverlay(query, caseInsensitive) {
26
    if (typeof query == "string")
27
      query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
28
    else if (!query.global)
29
      query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
30

31
    return {token: function(stream) {
32
      query.lastIndex = stream.pos;
33
      var match = query.exec(stream.string);
34
      if (match && match.index == stream.pos) {
35
        stream.pos += match[0].length || 1;
36
        return "searching";
37
      } else if (match) {
38
        stream.pos = match.index;
39
      } else {
40
        stream.skipToEnd();
41
      }
42
    }};
43
  }
44

45
  function SearchState() {
46
    this.posFrom = this.posTo = this.lastQuery = this.query = null;
47
    this.overlay = null;
48
  }
49

50
  function getSearchState(cm) {
51
    return cm.state.search || (cm.state.search = new SearchState());
52
  }
53

54
  function queryCaseInsensitive(query) {
55
    return typeof query == "string" && query == query.toLowerCase();
56
  }
57

58
  function getSearchCursor(cm, query, pos) {
59
    // Heuristic: if the query string is all lowercase, do a case insensitive search.
60
    return cm.getSearchCursor(query, pos, {caseFold: queryCaseInsensitive(query), multiline: true});
61
  }
62

63
  function persistentDialog(cm, text, deflt, onEnter, onKeyDown) {
64
    cm.openDialog(text, onEnter, {
65
      value: deflt,
66
      selectValueOnOpen: true,
67
      closeOnEnter: false,
68
      onClose: function() { clearSearch(cm); },
69
      onKeyDown: onKeyDown,
70
      bottom: cm.options.search.bottom
71
    });
72
  }
73

74
  function dialog(cm, text, shortText, deflt, f) {
75
    if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true, bottom: cm.options.search.bottom});
76
    else f(prompt(shortText, deflt));
77
  }
78

79
  function confirmDialog(cm, text, shortText, fs) {
80
    if (cm.openConfirm) cm.openConfirm(text, fs);
81
    else if (confirm(shortText)) fs[0]();
82
  }
83

84
  function parseString(string) {
85
    return string.replace(/\\([nrt\\])/g, function(match, ch) {
86
      if (ch == "n") return "\n"
87
      if (ch == "r") return "\r"
88
      if (ch == "t") return "\t"
89
      if (ch == "\\") return "\\"
90
      return match
91
    })
92
  }
93

94
  function parseQuery(query) {
95
    var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
96
    if (isRE) {
97
      try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
98
      catch(e) {} // Not a regular expression after all, do a string search
99
    } else {
100
      query = parseString(query)
101
    }
102
    if (typeof query == "string" ? query == "" : query.test(""))
103
      query = /x^/;
104
    return query;
105
  }
106

107
  function startSearch(cm, state, query) {
108
    state.queryText = query;
109
    state.query = parseQuery(query);
110
    cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
111
    state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
112
    cm.addOverlay(state.overlay);
113
    if (cm.showMatchesOnScrollbar) {
114
      if (state.annotate) { state.annotate.clear(); state.annotate = null; }
115
      state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
116
    }
117
  }
118

119
  function doSearch(cm, rev, persistent, immediate) {
120
    var state = getSearchState(cm);
121
    if (state.query) return findNext(cm, rev);
122
    var q = cm.getSelection() || state.lastQuery;
123
    if (q instanceof RegExp && q.source == "x^") q = null
124
    if (persistent && cm.openDialog) {
125
      var hiding = null
126
      var searchNext = function(query, event) {
127
        CodeMirror.e_stop(event);
128
        if (!query) return;
129
        if (query != state.queryText) {
130
          startSearch(cm, state, query);
131
          state.posFrom = state.posTo = cm.getCursor();
132
        }
133
        if (hiding) hiding.style.opacity = 1
134
        findNext(cm, event.shiftKey, function(_, to) {
135
          var dialog
136
          if (to.line < 3 && document.querySelector &&
137
              (dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&
138
              dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)
139
            (hiding = dialog).style.opacity = .4
140
        })
141
      };
142
      persistentDialog(cm, getQueryDialog(cm), q, searchNext, function(event, query) {
143
        var keyName = CodeMirror.keyName(event)
144
        var extra = cm.getOption('extraKeys'), cmd = (extra && extra[keyName]) || CodeMirror.keyMap[cm.getOption("keyMap")][keyName]
145
        if (cmd == "findNext" || cmd == "findPrev" ||
146
          cmd == "findPersistentNext" || cmd == "findPersistentPrev") {
147
          CodeMirror.e_stop(event);
148
          startSearch(cm, getSearchState(cm), query);
149
          cm.execCommand(cmd);
150
        } else if (cmd == "find" || cmd == "findPersistent") {
151
          CodeMirror.e_stop(event);
152
          searchNext(query, event);
153
        }
154
      });
155
      if (immediate && q) {
156
        startSearch(cm, state, q);
157
        findNext(cm, rev);
158
      }
159
    } else {
160
      dialog(cm, getQueryDialog(cm), "Search for:", q, function(query) {
161
        if (query && !state.query) cm.operation(function() {
162
          startSearch(cm, state, query);
163
          state.posFrom = state.posTo = cm.getCursor();
164
          findNext(cm, rev);
165
        });
166
      });
167
    }
168
  }
169

170
  function findNext(cm, rev, callback) {cm.operation(function() {
171
    var state = getSearchState(cm);
172
    var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
173
    if (!cursor.find(rev)) {
174
      cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
175
      if (!cursor.find(rev)) return;
176
    }
177
    cm.setSelection(cursor.from(), cursor.to());
178
    cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
179
    state.posFrom = cursor.from(); state.posTo = cursor.to();
180
    if (callback) callback(cursor.from(), cursor.to())
181
  });}
182

183
  function clearSearch(cm) {cm.operation(function() {
184
    var state = getSearchState(cm);
185
    state.lastQuery = state.query;
186
    if (!state.query) return;
187
    state.query = state.queryText = null;
188
    cm.removeOverlay(state.overlay);
189
    if (state.annotate) { state.annotate.clear(); state.annotate = null; }
190
  });}
191

192
  function el(tag, attrs) {
193
    var element = tag ? document.createElement(tag) : document.createDocumentFragment();
194
    for (var key in attrs) {
195
      element[key] = attrs[key];
196
    }
197
    for (var i = 2; i < arguments.length; i++) {
198
      var child = arguments[i]
199
      element.appendChild(typeof child == "string" ? document.createTextNode(child) : child);
200
    }
201
    return element;
202
  }
203

204
  function getQueryDialog(cm)  {
205
    return el("", null,
206
              el("span", {className: "CodeMirror-search-label"}, cm.phrase("Search:")), " ",
207
              el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}), " ",
208
              el("span", {style: "color: #888", className: "CodeMirror-search-hint"},
209
                 cm.phrase("(Use /re/ syntax for regexp search)")));
210
  }
211
  function getReplaceQueryDialog(cm) {
212
    return el("", null, " ",
213
              el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}), " ",
214
              el("span", {style: "color: #888", className: "CodeMirror-search-hint"},
215
                 cm.phrase("(Use /re/ syntax for regexp search)")));
216
  }
217
  function getReplacementQueryDialog(cm) {
218
    return el("", null,
219
              el("span", {className: "CodeMirror-search-label"}, cm.phrase("With:")), " ",
220
              el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}));
221
  }
222
  function getDoReplaceConfirm(cm) {
223
    return el("", null,
224
              el("span", {className: "CodeMirror-search-label"}, cm.phrase("Replace?")), " ",
225
              el("button", {}, cm.phrase("Yes")), " ",
226
              el("button", {}, cm.phrase("No")), " ",
227
              el("button", {}, cm.phrase("All")), " ",
228
              el("button", {}, cm.phrase("Stop")));
229
  }
230

231
  function replaceAll(cm, query, text) {
232
    cm.operation(function() {
233
      for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
234
        if (typeof query != "string") {
235
          var match = cm.getRange(cursor.from(), cursor.to()).match(query);
236
          cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
237
        } else cursor.replace(text);
238
      }
239
    });
240
  }
241

242
  function replace(cm, all) {
243
    if (cm.getOption("readOnly")) return;
244
    var query = cm.getSelection() || getSearchState(cm).lastQuery;
245
    var dialogText = all ? cm.phrase("Replace all:") : cm.phrase("Replace:")
246
    var fragment = el("", null,
247
                      el("span", {className: "CodeMirror-search-label"}, dialogText),
248
                      getReplaceQueryDialog(cm))
249
    dialog(cm, fragment, dialogText, query, function(query) {
250
      if (!query) return;
251
      query = parseQuery(query);
252
      dialog(cm, getReplacementQueryDialog(cm), cm.phrase("Replace with:"), "", function(text) {
253
        text = parseString(text)
254
        if (all) {
255
          replaceAll(cm, query, text)
256
        } else {
257
          clearSearch(cm);
258
          var cursor = getSearchCursor(cm, query, cm.getCursor("from"));
259
          var advance = function() {
260
            var start = cursor.from(), match;
261
            if (!(match = cursor.findNext())) {
262
              cursor = getSearchCursor(cm, query);
263
              if (!(match = cursor.findNext()) ||
264
                  (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
265
            }
266
            cm.setSelection(cursor.from(), cursor.to());
267
            cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
268
            confirmDialog(cm, getDoReplaceConfirm(cm), cm.phrase("Replace?"),
269
                          [function() {doReplace(match);}, advance,
270
                           function() {replaceAll(cm, query, text)}]);
271
          };
272
          var doReplace = function(match) {
273
            cursor.replace(typeof query == "string" ? text :
274
                           text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
275
            advance();
276
          };
277
          advance();
278
        }
279
      });
280
    });
281
  }
282

283
  CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
284
  CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
285
  CodeMirror.commands.findPersistentNext = function(cm) {doSearch(cm, false, true, true);};
286
  CodeMirror.commands.findPersistentPrev = function(cm) {doSearch(cm, true, true, true);};
287
  CodeMirror.commands.findNext = doSearch;
288
  CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
289
  CodeMirror.commands.clearSearch = clearSearch;
290
  CodeMirror.commands.replace = replace;
291
  CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
292
});
293

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.