<commit_before>_ = require 'underscore-plus'
{Point, Range, Emitter, CompositeDisposable, TextBuffer} = require 'atom'
{Patch} = TextBuffer
escapeHelper = require './escape-helper'

module.exports =
class BufferSearch
  @markerClass: 'find-result'

  constructor: (@findOptions) ->
    @emitter = new Emitter
    @patch = new Patch
    @subscriptions = null
    @markers = []
    @editor = null

    recreateMarkers = @recreateMarkers.bind(this)
    @findOptions.onDidChange (changedParams) =>
      return unless changedParams?
      return unless changedParams.findPattern? or
        changedParams.useRegex? or
        changedParams.wholeWord? or
        changedParams.caseSensitive? or
        changedParams.inCurrentSelection?
      @recreateMarkers()

  onDidUpdate: (callback) ->
    @emitter.on 'did-update', callback

  onDidError: (callback) ->
    @emitter.on 'did-error', callback

  onDidChangeCurrentResult: (callback) ->
    @emitter.on 'did-change-current-result', callback

  setEditor: (@editor) ->
    @subscriptions?.dispose()
    if @editor?
      @subscriptions = new CompositeDisposable
      @subscriptions.add @editor.buffer.onDidChange(@bufferChanged.bind(this))
      @subscriptions.add @editor.buffer.onDidStopChanging(@bufferStoppedChanging.bind(this))
      @subscriptions.add @editor.onDidAddSelection(@setCurrentMarkerFromSelection.bind(this))
      @subscriptions.add @editor.onDidChangeSelectionRange(@setCurrentMarkerFromSelection.bind(this))
    @recreateMarkers()

  getEditor: -> @editor

  setFindOptions: (newParams) -> @findOptions.set(newParams)

  getFindOptions: -> @findOptions

  search: (findPattern, otherOptions) ->
    options = {findPattern}
    if otherOptions?
      for k, v of otherOptions
        options[k] = v
    @findOptions.set(options)

  replace: (markers, replacePattern) ->
    return unless markers?.length > 0
    @findOptions.set({replacePattern})
<commit_msg>Be more defensive with editors that do not have a buffer. <commit_after>_ = require 'underscore-plus'
{Point, Range, Emitter, CompositeDisposable, TextBuffer} = require 'atom'
{Patch} = TextBuffer
escapeHelper = require './escape-helper'

module.exports =
class BufferSearch
  @markerClass: 'find-result'

  constructor: (@findOptions) ->
    @emitter = new Emitter
    @patch = new Patch
    @subscriptions = null
    @markers = []
    @editor = null

    recreateMarkers = @recreateMarkers.bind(this)
    @findOptions.onDidChange (changedParams) =>
      return unless changedParams?
      return unless changedParams.findPattern? or
        changedParams.useRegex? or
        changedParams.wholeWord? or
        changedParams.caseSensitive? or
        changedParams.inCurrentSelection?
      @recreateMarkers()

  onDidUpdate: (callback) ->
    @emitter.on 'did-update', callback

  onDidError: (callback) ->
    @emitter.on 'did-error', callback

  onDidChangeCurrentResult: (callback) ->
    @emitter.on 'did-change-current-result', callback

  setEditor: (@editor) ->
    @subscriptions?.dispose()
    if @editor?.buffer?
      @subscriptions = new CompositeDisposable
      @subscriptions.add @editor.buffer.onDidChange(@bufferChanged.bind(this))
      @subscriptions.add @editor.buffer.onDidStopChanging(@bufferStoppedChanging.bind(this))
      @subscriptions.add @editor.onDidAddSelection(@setCurrentMarkerFromSelection.bind(this))
      @subscriptions.add @editor.onDidChangeSelectionRange(@setCurrentMarkerFromSelection.bind(this))
    @recreateMarkers()

  getEditor: -> @editor

  setFindOptions: (newParams) -> @findOptions.set(newParams)

  getFindOptions: -> @findOptions

  search: (findPattern, otherOptions) ->
    options = {findPattern}
    if otherOptions?
      for k, v of otherOptions
        options[k] = v
    @findOptions.set(options)

  replace: (markers, replacePattern) ->
    return unless markers?.length > 0
    @findOptions.set({replacePattern})

{'fraction_non_alphanumeric': 0.11332821693527756, 'fraction_numerical': 0.0005116398055768739, 'mean_word_length': 3.9120603015075375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5596409', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1125, 'n_words': 239}