BVB Source Codes

reactide Show index.js Source code

Return Download reactide: download index.js Source code - Download reactide Source code - Type:.js
  1. "use strict";
  2.  
  3. exports.__esModule = true;
  4.  
  5. exports.default = function (rawLines, lineNumber, colNumber) {
  6.   var opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  7.  
  8.   colNumber = Math.max(colNumber, 0);
  9.  
  10.   var highlighted = opts.highlightCode && _chalk2.default.supportsColor || opts.forceColor;
  11.   var chalk = _chalk2.default;
  12.   if (opts.forceColor) {
  13.     chalk = new _chalk2.default.constructor({ enabled: true });
  14.   }
  15.   var maybeHighlight = function maybeHighlight(chalkFn, string) {
  16.     return highlighted ? chalkFn(string) : string;
  17.   };
  18.   var defs = getDefs(chalk);
  19.   if (highlighted) rawLines = highlight(defs, rawLines);
  20.  
  21.   var linesAbove = opts.linesAbove || 2;
  22.   var linesBelow = opts.linesBelow || 3;
  23.  
  24.   var lines = rawLines.split(NEWLINE);
  25.   var start = Math.max(lineNumber - (linesAbove + 1), 0);
  26.   var end = Math.min(lines.length, lineNumber + linesBelow);
  27.  
  28.   if (!lineNumber && !colNumber) {
  29.     start = 0;
  30.     end = lines.length;
  31.   }
  32.  
  33.   var numberMaxWidth = String(end).length;
  34.  
  35.   var frame = lines.slice(start, end).map(function (line, index) {
  36.     var number = start + 1 + index;
  37.     var paddedNumber = (" " + number).slice(-numberMaxWidth);
  38.     var gutter = " " + paddedNumber + " | ";
  39.     if (number === lineNumber) {
  40.       var markerLine = "";
  41.       if (colNumber) {
  42.         var markerSpacing = line.slice(0, colNumber - 1).replace(/[^\t]/g, " ");
  43.         markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), markerSpacing, maybeHighlight(defs.marker, "^")].join("");
  44.       }
  45.       return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line, markerLine].join("");
  46.     } else {
  47.       return " " + maybeHighlight(defs.gutter, gutter) + line;
  48.     }
  49.   }).join("\n");
  50.  
  51.   if (highlighted) {
  52.     return chalk.reset(frame);
  53.   } else {
  54.     return frame;
  55.   }
  56. };
  57.  
  58. var _jsTokens = require("js-tokens");
  59.  
  60. var _jsTokens2 = _interopRequireDefault(_jsTokens);
  61.  
  62. var _esutils = require("esutils");
  63.  
  64. var _esutils2 = _interopRequireDefault(_esutils);
  65.  
  66. var _chalk = require("chalk");
  67.  
  68. var _chalk2 = _interopRequireDefault(_chalk);
  69.  
  70. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  71.  
  72. function getDefs(chalk) {
  73.   return {
  74.     keyword: chalk.cyan,
  75.     capitalized: chalk.yellow,
  76.     jsx_tag: chalk.yellow,
  77.     punctuator: chalk.yellow,
  78.  
  79.     number: chalk.magenta,
  80.     string: chalk.green,
  81.     regex: chalk.magenta,
  82.     comment: chalk.grey,
  83.     invalid: chalk.white.bgRed.bold,
  84.     gutter: chalk.grey,
  85.     marker: chalk.red.bold
  86.   };
  87. }
  88.  
  89. var NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
  90.  
  91. var JSX_TAG = /^[a-z][\w-]*$/i;
  92.  
  93. var BRACKET = /^[()\[\]{}]$/;
  94.  
  95. function getTokenType(match) {
  96.   var _match$slice = match.slice(-2),
  97.       offset = _match$slice[0],
  98.       text = _match$slice[1];
  99.  
  100.   var token = (0, _jsTokens.matchToToken)(match);
  101.  
  102.   if (token.type === "name") {
  103.     if (_esutils2.default.keyword.isReservedWordES6(token.value)) {
  104.       return "keyword";
  105.     }
  106.  
  107.     if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) {
  108.       return "jsx_tag";
  109.     }
  110.  
  111.     if (token.value[0] !== token.value[0].toLowerCase()) {
  112.       return "capitalized";
  113.     }
  114.   }
  115.  
  116.   if (token.type === "punctuator" && BRACKET.test(token.value)) {
  117.     return "bracket";
  118.   }
  119.  
  120.   return token.type;
  121. }
  122.  
  123. function highlight(defs, text) {
  124.   return text.replace(_jsTokens2.default, function () {
  125.     for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  126.       args[_key] = arguments[_key];
  127.     }
  128.  
  129.     var type = getTokenType(args);
  130.     var colorize = defs[type];
  131.     if (colorize) {
  132.       return args[0].split(NEWLINE).map(function (str) {
  133.         return colorize(str);
  134.       }).join("\n");
  135.     } else {
  136.       return args[0];
  137.     }
  138.   });
  139. }
  140.  
  141. module.exports = exports["default"];
downloadindex.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top