<commit_before>_ = require 'underscore-plus'

ProviderBase = require './provider-base'
{Disposable} = require 'atom'
{getCurrentWordAndBoundary, getVisibleEditors, isTextEditor, isNarrowEditor} = require '../utils'

module.exports =
class SearchBase extends ProviderBase
  includeHeaderGrammar: true
  supportDirectEdit: true
  showLineHeader: true
  showColumnOnLineHeader: true
  regExpForSearchTerm: null

  checkReady: ->
    if @options.currentWord
      {word, boundary} = getCurrentWordAndBoundary(@editor)
      @options.wordOnly = boundary
      @options.search = word

    if @options.search
      Promise.resolve(true)
    else
      @readInput().then (input) =>
        @options.search = input
        true

  getRegExpForSearchTerm: ->
    searchTerm = @options.search
    source = _.escapeRegExp(searchTerm)
    if @options.wordOnly
      source = "\\b#{source}\\b"

    sensitivity = @getConfig('caseSensitivityForSearchTerm')
    if (sensitivity is 'sensitive') or (sensitivity is 'smartcase' and /[A-Z]/.test(searchTerm))
      new RegExp(source, 'g')
    else
      new RegExp(source, 'gi')

  clearHighlight: ->
    @markerLayerByEditor.forEach (markerLayer) ->
      for marker in markerLayer.getMarkers()
        marker.destroy()
    @markerLayerByEditor.clear()

  highlightEditor: (editor) ->
    items = @ui.getNormalItemsForPath(editor.getPath())
    return unless items.length

    @markerLayerByEditor.set(editor, markerLayer = editor.addMarkerLayer())
    editor.decorateMarkerLayer(markerLayer, type: 'highlight', class: 'narrow-search-match')
    editor.scan @regExpForSearchTerm, ({range}) ->
      if items.some(({point}) -> point.isEqual(range.start))
        markerLayer.markBufferRange(range, invalidate: 'inside')

  initialize: ->
    @markerLayerByEditor = new Map()
    @subscriptions.add new Disposable => @clearHighlight()

    @subscriptions.add @ui.onDidStopRefreshing =>
      console.log 'stop refreshing'
      @clearHighlight()
      @highlightEditor(editor) for editor in getVisibleEditors()

    @subscriptions.add @ui.onDidPreview ({editor}) =>
      unless @markerLayerByEditor.has(editor)
        console.log 'did preview'
        @highlightEditor(editor)

    @subscriptions.add atom.workspace.onDidStopChangingActivePaneItem (item) =>
      if isTextEditor(item) and not isNarrowEditor(item) and not @markerLayerByEditor.has(item)
        console.log 'active item changed'
        @highlightEditor(item)
<commit_msg>cleanup done<commit_after>_ = require 'underscore-plus'

ProviderBase = require './provider-base'
{Disposable} = require 'atom'
{getCurrentWordAndBoundary, getVisibleEditors, isTextEditor, isNarrowEditor} = require '../utils'

module.exports =
class SearchBase extends ProviderBase
  includeHeaderGrammar: true
  supportDirectEdit: true
  showLineHeader: true
  showColumnOnLineHeader: true
  regExpForSearchTerm: null

  checkReady: ->
    if @options.currentWord
      {word, boundary} = getCurrentWordAndBoundary(@editor)
      @options.wordOnly = boundary
      @options.search = word

    if @options.search
      Promise.resolve(true)
    else
      @readInput().then (input) =>
        @options.search = input
        true

  getRegExpForSearchTerm: ->
    searchTerm = @options.search
    source = _.escapeRegExp(searchTerm)
    if @options.wordOnly
      source = "\\b#{source}\\b"

    sensitivity = @getConfig('caseSensitivityForSearchTerm')
    if (sensitivity is 'sensitive') or (sensitivity is 'smartcase' and /[A-Z]/.test(searchTerm))
      new RegExp(source, 'g')
    else
      new RegExp(source, 'gi')

  clearHighlight: ->
    @markerLayerByEditor.forEach (markerLayer) ->
      marker.destroy() for marker in markerLayer.getMarkers()
    @markerLayerByEditor.clear()

  highlightEditor: (editor) ->
    # Get items shown on narrow-editor and also matching editor's filePath
    items = @ui.getNormalItemsForPath(editor.getPath())
    return unless items.length

    @markerLayerByEditor.set(editor, markerLayer = editor.addMarkerLayer())
    editor.decorateMarkerLayer(markerLayer, type: 'highlight', class: 'narrow-search-match')
    editor.scan @regExpForSearchTerm, ({range}) ->
      if items.some(({point}) -> point.isEqual(range.start))
        markerLayer.markBufferRange(range, invalidate: 'inside')

  initialize: ->
    @markerLayerByEditor = new Map()
    @subscriptions.add new Disposable => @clearHighlight()

    @subscriptions.add @ui.onDidStopRefreshing =>
      @clearHighlight()
      @highlightEditor(editor) for editor in getVisibleEditors()

    @subscriptions.add @ui.onDidPreview ({editor}) =>
      unless @markerLayerByEditor.has(editor)
        @highlightEditor(editor)

    @subscriptions.add atom.workspace.onDidStopChangingActivePaneItem (item) =>
      if isTextEditor(item) and not isNarrowEditor(item) and not @markerLayerByEditor.has(item)
        @highlightEditor(item)

{'fraction_non_alphanumeric': 0.11360850020433183, 'fraction_numerical': 0.0, 'mean_word_length': 3.841740850642928, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '948248', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1416, 'n_words': 309}