Code coverage report for slap/lib/ui/FindForm.js

Statements: 63.93% (39 / 61)      Branches: 41.67% (10 / 24)      Functions: 57.14% (4 / 7)      Lines: 65% (39 / 60)      Ignored: none     

All files » slap/lib/ui/ » FindForm.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1021 1 1 1   1 1 1   1 1   1 1 1   1   1             1                         1   1                 1 1 1 1 1   1 1 1   1 2 2 2 2       2 2 2             2 2 2 2                                   1     1  
var blessed = require('blessed');
var _ = require('lazy.js');
var lodash = require('lodash');
var Point = require('text-buffer/lib/point');
 
var Slap = require('./Slap');
var BaseElement = require('./BaseElement');
var BaseFindForm = require('./BaseFindForm');
 
var textUtil = require('../textUtil');
var markup = require('../markup');
 
FindForm._label = " find (/.*/ for regex): ";
function FindForm (opts) {
  var self = this;
 
  Iif (!(self instanceof blessed.Node)) return new FindForm(opts);
 
  BaseFindForm.call(self, _({
      findField: {left: FindForm._label.length}
    })
    .merge(Slap.global.options.form.find || {})
    .merge(opts || {})
    .toObject());
 
  self.findLabel = new BaseElement(_({
      parent: self,
      tags: true,
      content: FindForm._label,
      top: 0,
      height: 1,
      left: 0,
      width: FindForm._label.length,
      style: self.options.style
    })
    .merge(self.options.findLabel || {})
    .toObject());
}
FindForm.prototype.__proto__ = BaseFindForm.prototype;
 
FindForm.prototype.selectRange = function (range) {
  var self = this;
  var editor = self.pane.editor;
  var selection = editor.selection;
  selection.setRange(range);
  var visibleRange = editor.visiblePos(range);
  editor.clipScroll([visibleRange.start, visibleRange.end]);
  return self;
};
FindForm.prototype._initHandlers = function () {
  var self = this;
  var header = self.slap.header;
  var editor = self.pane.editor;
  var selection = editor.selection;
 
  self.on('hide', function () {
    editor.destroyMarkers({type: 'findMatch'});
    editor._updateContent();
  });
  self.on('find', lodash.throttle(function (pattern, direction) {
    direction = direction || 0;
    editor.destroyMarkers({type: 'findMatch'});
    var regExpMatch = pattern.match(textUtil._regExpRegExp);
    pattern = regExpMatch
      ? new RegExp(regExpMatch[1], regExpMatch[2])
      : new RegExp(textUtil.escapeRegExp(pattern), 'img');
 
    var selectionRange = selection.getRange();
    var matches = [];
    editor.textBuf[direction === -1
      ? 'backwardsScan'
      : 'scan'](pattern, function (match) {
      matches.push(match);
      editor.textBuf.markRange(match.range, {type: 'findMatch'});
    });
 
    Eif (!matches.length) {
      header.message("no matches", 'warning');
      self.resetEditor();
      return;
    }
    if (!matches.some(function (match) {
      var matchRange = match.range;
      var cmp = matchRange.start.compare(selectionRange.start);
      if (cmp === direction) {
        self.selectRange(matchRange);
        return true;
      } else if (!cmp && matches.length === 1) {
        header.message("this is the only occurrence", 'info');
        return true;
      }
    })) {
      header.message("search wrapped", 'info');
      self.selectRange(matches[0].range);
    }
    editor._updateContent();
  }, self.options.perf.findThrottle));
  return BaseFindForm.prototype._initHandlers.apply(self, arguments);
};
 
module.exports = FindForm;