Sha256: b0cb3058b368bb15bb161de23a6e68cda712027ae9f6c86051a2689963250ed4

Contents?: true

Size: 1.97 KB

Versions: 25

Compression:

Stored size: 1.97 KB

Contents

// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/LICENSE

// Defines jumpToLine command. Uses dialog.js if present.

(function(mod) {
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    mod(require("../../lib/codemirror"), require("../dialog/dialog"));
  else if (typeof define == "function" && define.amd) // AMD
    define(["../../lib/codemirror", "../dialog/dialog"], mod);
  else // Plain browser env
    mod(CodeMirror);
})(function(CodeMirror) {
  "use strict";

  function dialog(cm, text, shortText, deflt, f) {
    if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
    else f(prompt(shortText, deflt));
  }

  function getJumpDialog(cm) {
    return 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>';
  }

  function interpretLine(cm, string) {
    var num = Number(string)
    if (/^[-+]/.test(string)) return cm.getCursor().line + num
    else return num - 1
  }

  CodeMirror.commands.jumpToLine = function(cm) {
    var cur = cm.getCursor();
    dialog(cm, getJumpDialog(cm), cm.phrase("Jump to line:"), (cur.line + 1) + ":" + cur.ch, function(posStr) {
      if (!posStr) return;

      var match;
      if (match = /^\s*([\+\-]?\d+)\s*\:\s*(\d+)\s*$/.exec(posStr)) {
        cm.setCursor(interpretLine(cm, match[1]), Number(match[2]))
      } else if (match = /^\s*([\+\-]?\d+(\.\d+)?)\%\s*/.exec(posStr)) {
        var line = Math.round(cm.lineCount() * Number(match[1]) / 100);
        if (/^[-+]/.test(match[1])) line = cur.line + line + 1;
        cm.setCursor(line - 1, cur.ch);
      } else if (match = /^\s*\:?\s*([\+\-]?\d+)\s*/.exec(posStr)) {
        cm.setCursor(interpretLine(cm, match[1]), cur.ch);
      }
    });
  };

  CodeMirror.keyMap["default"]["Alt-G"] = "jumpToLine";
});

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
rails_db-2.4.5 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
rails_db-2.4.4 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
rails_db-2.4.3 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
wallaby-7.0.0.beta2 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
wallaby-7.0.0.beta1 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
wallaby-6.1.6 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
rails_db-2.4.2 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
wallaby-6.1.5 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
wallaby-6.1.4 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
rails_db-2.4.1 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
rails_db-2.4.0 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
wallaby-6.1.3 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
wallaby-6.1.2 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
wallaby-6.1.1 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
wallaby-6.1.0 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
rails_db-2.3.1 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
rails_db-2.3.0 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js
wallaby-6.0.2 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
wallaby-6.0.1 vendor/assets/javascripts/codemirror/5.49.2/addon/search/jump-to-line.js
rails_db-2.2.1 app/assets/javascripts/codemirror/codemirror/addons/search/jump-to-line.js