LaravelTest
53 строки · 2.1 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// Defines jumpToLine command. Uses dialog.js if present.
5
6(function(mod) {7if (typeof exports == "object" && typeof module == "object") // CommonJS8mod(require("../../lib/codemirror"), require("../dialog/dialog"));9else if (typeof define == "function" && define.amd) // AMD10define(["../../lib/codemirror", "../dialog/dialog"], mod);11else // Plain browser env12mod(CodeMirror);13})(function(CodeMirror) {14"use strict";15
16// default search panel location17CodeMirror.defineOption("search", {bottom: false});18
19function dialog(cm, text, shortText, deflt, f) {20if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true, bottom: cm.options.search.bottom});21else f(prompt(shortText, deflt));22}23
24function getJumpDialog(cm) {25return cm.phrase("Jump to line:") + ' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">' + cm.phrase("(Use line:column or scroll% syntax)") + '</span>';26}27
28function interpretLine(cm, string) {29var num = Number(string)30if (/^[-+]/.test(string)) return cm.getCursor().line + num31else return num - 132}33
34CodeMirror.commands.jumpToLine = function(cm) {35var cur = cm.getCursor();36dialog(cm, getJumpDialog(cm), cm.phrase("Jump to line:"), (cur.line + 1) + ":" + cur.ch, function(posStr) {37if (!posStr) return;38
39var match;40if (match = /^\s*([\+\-]?\d+)\s*\:\s*(\d+)\s*$/.exec(posStr)) {41cm.setCursor(interpretLine(cm, match[1]), Number(match[2]))42} else if (match = /^\s*([\+\-]?\d+(\.\d+)?)\%\s*/.exec(posStr)) {43var line = Math.round(cm.lineCount() * Number(match[1]) / 100);44if (/^[-+]/.test(match[1])) line = cur.line + line + 1;45cm.setCursor(line - 1, cur.ch);46} else if (match = /^\s*\:?\s*([\+\-]?\d+)\s*/.exec(posStr)) {47cm.setCursor(interpretLine(cm, match[1]), cur.ch);48}49});50};51
52CodeMirror.keyMap["default"]["Alt-G"] = "jumpToLine";53});54