<commit_before>$ = require 'jquery'
{View} = require 'space-pen'
stringScore = require 'stringscore'
fuzzyFilter = require 'fuzzy-filter'
Editor = require 'editor'

module.exports =
class FileFinder extends View
  @content: ->
    @div class: 'file-finder', =>
      @ol outlet: 'pathList'
      @subview 'editor', new Editor(mini: true)

  paths: null
  maxResults: null

  initialize: ({@paths, @selected}) ->
    requireStylesheet 'file-finder.css'
    @maxResults = 10

    @populatePathList()

    @on 'file-finder:close', => @remove()
    @on 'move-up', => @moveUp()
    @on 'move-down', => @moveDown()
    @on 'file-finder:select-file', => @select()

    @editor.addClass 'single-line'
    @editor.buffer.on 'change', => @populatePathList()
    @editor.off 'move-up move-down'

  populatePathList: ->
    @pathList.empty()
    for path in @findMatches(@editor.buffer.getText())
      @pathList.append $("<li>#{path}</li>")

    @pathList.children('li:first').addClass 'selected'

  findSelectedLi: ->
    @pathList.children('li.selected')

  select: ->
    filePath = @findSelectedLi().text()
    @selected(filePath) if filePath and @selected
    @remove()

  moveUp: ->
    @findSelectedLi()
      .filter(':not(:first-child)')
      .removeClass('selected')
      .prev()
      .addClass('selected')

  moveDown: ->
    @findSelectedLi()
      .filter(':not(:last-child)')
      .removeClass('selected')
      .next()
      .addClass('selected')

  findMatches: (query) ->
    fuzzyFilter(@paths, query, maxResults: @maxResults)

  remove: ->
    $('#panes .editor.active').focus()
    super<commit_msg>Focus the root view when file finder closes, not an editor. Will test this when we extract it as an extenison.<commit_after>$ = require 'jquery'
{View} = require 'space-pen'
stringScore = require 'stringscore'
fuzzyFilter = require 'fuzzy-filter'
Editor = require 'editor'

module.exports =
class FileFinder extends View
  @content: ->
    @div class: 'file-finder', =>
      @ol outlet: 'pathList'
      @subview 'editor', new Editor(mini: true)

  paths: null
  maxResults: null

  initialize: ({@paths, @selected}) ->
    requireStylesheet 'file-finder.css'
    @maxResults = 10

    @populatePathList()

    @on 'file-finder:close', => @remove()
    @on 'move-up', => @moveUp()
    @on 'move-down', => @moveDown()
    @on 'file-finder:select-file', => @select()

    @editor.addClass 'single-line'
    @editor.buffer.on 'change', => @populatePathList()
    @editor.off 'move-up move-down'

  populatePathList: ->
    @pathList.empty()
    for path in @findMatches(@editor.buffer.getText())
      @pathList.append $("<li>#{path}</li>")

    @pathList.children('li:first').addClass 'selected'

  findSelectedLi: ->
    @pathList.children('li.selected')

  select: ->
    filePath = @findSelectedLi().text()
    @selected(filePath) if filePath and @selected
    @remove()

  moveUp: ->
    @findSelectedLi()
      .filter(':not(:first-child)')
      .removeClass('selected')
      .prev()
      .addClass('selected')

  moveDown: ->
    @findSelectedLi()
      .filter(':not(:last-child)')
      .removeClass('selected')
      .next()
      .addClass('selected')

  findMatches: (query) ->
    fuzzyFilter(@paths, query, maxResults: @maxResults)

  remove: ->
    $('#root-view').focus()
    super
{'fraction_non_alphanumeric': 0.172809667673716, 'fraction_numerical': 0.0012084592145015106, 'mean_word_length': 3.3168187744458932, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '6181164', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1067, 'n_words': 232}