Find next in find-replace rudimentary program


#1

I’m creating a basic plugin that does a find-replace. When the user clicks on the Find Next button the next search result should be highlighted.

This is the relevant part of the code:

  search() {
  var that = this;
  $(".search-button").click(function(evt) {
    var editor
    editor = atom.workspace.getActiveTextEditor();
    var text = editor.getText();

    // get text value
    var textValue = $('.search-box').val();
    that.collectRanges(textValue);

    // Selects all occurrences of the search term
    //editor.setSelectedScreenRanges(arr); Uncomment this

    console.log("that.rangeArray: "+that.rangeArray);
    editor.scan(new RegExp(textValue, 'ig'), iterator => {
      marker = editor.markBufferRange(iterator.range);
      decoration = editor.decorateMarker(marker, {type: 'line-number', class: 'my-line-class'});
      iterator.stop();
      that.rangeArray.splice(0, 1);
    });

    // show notification warning if no results
    if (that.rangeArray.length === 0) {
      atom.notifications.addWarning(that.rangeArray.length+ " results found");
    }

    //that.findNext(that.rangeArray);
    evt.stopImmediatePropagation();
    return that.rangeArray;
  });

},
collectRanges(val) {
let editor
var arr = []
editor = atom.workspace.getActiveTextEditor();

editor.scan(new RegExp(val, 'ig'), iterator => {
  arr.push(iterator.range);
});
this.rangeArray = arr;
return arr;

}

Full gist is at: https://gist.github.com/anonymous/0f3f7cbf7a690e1a1fd5168625fff4c5

What am I doing wrong here? Could you suggest how I go about this?


#2

I can’t seem to edit the code formatting.