<commit_before>_ = require 'underscore-plus'
{Point, CompositeDisposable} = require 'atom'
{
  decorateRange
  saveEditorState
} = require '../utils'

module.exports =
class Base
  wasConfirmed: false
  getName: ->
    @constructor.name

  getTitle: ->
    @constructor.name

  constructor: (@ui, @options={}) ->
    @subscriptions = new CompositeDisposable
    @editor = atom.workspace.getActiveTextEditor()
    @editorElement = atom.views.getView(@editor)
    @pane = atom.workspace.getActivePane()
    @restoreEditorState = saveEditorState(@editor)

    @initialize?()
    @ui.start(this)

  subscribe: (args...) ->
    @subscriptions.add(args...)

  getFilterKey: ->
    "text"

  filterItems: (items, words) ->
    filterKey = @getFilterKey()

    matchPattern = (item) ->
      text = item[filterKey]
      if text?
        text.match(///#{pattern}///i)
      else
        true # When without filterKey is always displayed.

    for pattern, i in words.map(_.escapeRegExp)
      items = items.filter(matchPattern)
    items

  highlightRow: (editor, row) ->
    range = [[row, 0], [row, 0]]
    decorateRange(editor, range, type: 'line', class: 'narrow-result')

  destroy: ->
    @marker?.destroy()
    @subscriptions.dispose()
    @restoreEditorState() unless @wasConfirmed
    {@editor, @editorElement, @marker, @subscriptions} = {}

  confirmed: ({point}, options={}) ->
    unless options.preview
      @wasConfirmed = true
    @marker?.destroy()
    return unless point?
    point = Point.fromObject(point)

    if options.preview?
      @pane.activateItem(@editor)
      @marker = @highlightRow(@editor, point.row)
    else
      @editor.setCursorBufferPosition(point, autoscroll: false)
      @pane.activate()
      @pane.activateItem(@editor)

    @editor.scrollToBufferPosition(point, center: true)
    @editorElement.component.updateSync()<commit_msg>new utility on base, Base::getTextForRow<commit_after>_ = require 'underscore-plus'
{Point, CompositeDisposable} = require 'atom'
{
  decorateRange
  saveEditorState
  padStringLeft
} = require '../utils'

module.exports =
class Base
  wasConfirmed: false
  lastRowNumberTextLength: null

  getName: ->
    @constructor.name

  getTitle: ->
    _.dasherize(@getName())

  constructor: (@ui, @options={}) ->
    @subscriptions = new CompositeDisposable
    @editor = atom.workspace.getActiveTextEditor()

    @subscribe @editor.onDidStopChanging(@invalidateState)

    @editorElement = atom.views.getView(@editor)
    @pane = atom.workspace.getActivePane()
    @restoreEditorState = saveEditorState(@editor)

    @initialize?()
    @ui.start(this)

  invalidateState: =>
    @lastRowNumberTextLength = null

  subscribe: (args...) ->
    @subscriptions.add(args...)

  getFilterKey: ->
    "text"

  filterItems: (items, words) ->
    filterKey = @getFilterKey()

    matchPattern = (item) ->
      text = item[filterKey]
      if text?
        text.match(///#{pattern}///i)
      else
        true # When without filterKey is always displayed.

    for pattern, i in words.map(_.escapeRegExp)
      items = items.filter(matchPattern)
    items

  highlightRow: (editor, row) ->
    range = [[row, 0], [row, 0]]
    decorateRange(editor, range, type: 'line', class: 'narrow-result')

  destroy: ->
    @marker?.destroy()
    @subscriptions.dispose()
    @restoreEditorState() unless @wasConfirmed
    {@editor, @editorElement, @marker, @subscriptions} = {}

  confirmed: ({point}, options={}) ->
    unless options.preview
      @wasConfirmed = true
    @marker?.destroy()
    return unless point?
    point = Point.fromObject(point)

    if options.preview?
      @pane.activateItem(@editor)
      @marker = @highlightRow(@editor, point.row)
    else
      @editor.setCursorBufferPosition(point, autoscroll: false)
      @pane.activate()
      @pane.activateItem(@editor)

    @editor.scrollToBufferPosition(point, center: true)
    @editorElement.component.updateSync()

  getTextForRow: (row) ->
    @lastRowNumberTextLength ?= String(@editor.getLastBufferRow()).length
    padStringLeft(String(row + 1), @lastRowNumberTextLength)
{'fraction_non_alphanumeric': 0.1378048780487805, 'fraction_numerical': 0.0012195121951219512, 'mean_word_length': 3.3720682302771854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '6166945', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1244, 'n_words': 262}