<commit_before>fs = require 'fs'
_ = require 'underscore'
$ = require 'jquery'

Layout = require 'layout'
Editor = require 'editor'

# This a weirdo file. We don't create a Window class, we just add stuff to
# the DOM window.

windowAdditions =
  editor: null
  keyBindings: null
  layout: null
  menuItemActions: null

  startup: ->
    @keyBindings = {}
    @menuItemActions = {}
    @layout = Layout.attach()
    @editor = new Editor $atomController.url?.toString()
    @registerEventHandlers()
    @bindKeys()
    @bindMenuItems()
    $(window).focus()

  shutdown: ->
    @layout.remove()
    @editor.shutdown()
    $(window).unbind('focus')
    $(window).unbind('blur')
    $(window).unbind('keydown')

  bindKeys: ->
    @bindKey 'meta+s', => @editor.save()
    @bindKey 'meta+w', => @close()

  bindMenuItems: ->
    @bindMenuItem "File > Save", => @editor.save()

  bindMenuItem: (path, action) ->
    @menuItemActions[path] = action

  bindKey: (pattern, action) ->
    @keyBindings[pattern] = action

  keyEventMatchesPattern: (event, pattern) ->
    [modifiers..., key] = pattern.split '+'
    patternModifiers =
      ctrlKey: 'ctrl' in modifiers
      altKey: 'alt' in modifiers
      shiftKey: 'shift' in modifiers
      metaKey: 'meta' in modifiers

    patternModifiers.ctrlKey == event.ctrlKey and
      patternModifiers.altKey == event.altKey and
      patternModifiers.shiftKey == event.shiftKey and
      patternModifiers.metaKey == event.metaKey and
      event.which == key.toUpperCase().charCodeAt 0

  registerEventHandlers: ->
    $(document).bind 'keydown', (event) =>
      for pattern, action of @keyBindings
        action() if @keyEventMatchesPattern(event, pattern)

    $(window).focus => @registerMenuItems()
    $(window).blur -> atom.native.resetMainMenu()

  registerMenuItems: ->
    for path of @menuItemActions
      atom.native.addMenuItem(path)

  performActionForMenuItemPath: (path) ->
    @menuItemActions[path]()

  showConsole: ->
    $atomController.webView.inspector.showConsole true

  onerror: ->
    @showConsole true

for key, value of windowAdditions
  console.warn "DOMWindow already has a key named `#{key}`" if window[key]
  window[key] = value<commit_msg>Factor out `parseKeyPattern` from `keyEventMatchesPattern` method<commit_after>fs = require 'fs'
_ = require 'underscore'
$ = require 'jquery'

Layout = require 'layout'
Editor = require 'editor'

# This a weirdo file. We don't create a Window class, we just add stuff to
# the DOM window.

windowAdditions =
  editor: null
  keyBindings: null
  layout: null
  menuItemActions: null

  startup: ->
    @keyBindings = {}
    @menuItemActions = {}
    @layout = Layout.attach()
    @editor = new Editor $atomController.url?.toString()
    @registerEventHandlers()
    @bindKeys()
    @bindMenuItems()
    $(window).focus()

  shutdown: ->
    @layout.remove()
    @editor.shutdown()
    $(window).unbind('focus')
    $(window).unbind('blur')
    $(window).unbind('keydown')

  bindKeys: ->
    @bindKey 'meta+s', => @editor.save()
    @bindKey 'meta+w', => @close()

  bindMenuItems: ->
    @bindMenuItem "File > Save", => @editor.save()

  bindMenuItemAndKey: (path, pattern, action) ->
    @bindMenuItem path, action
    @bindKey pattern, action

  bindMenuItem: (path, action) ->
    @menuItemActions[path] = action

  bindKey: (pattern, action) ->
    @keyBindings[pattern] = action

  keyEventMatchesPattern: (event, pattern) ->
    keys = @parseKeyPattern pattern

    keys.ctrlKey == event.ctrlKey and
      keys.altKey == event.altKey and
      keys.shiftKey == event.shiftKey and
      keys.metaKey == event.metaKey and
      event.which == keys.key.toUpperCase().charCodeAt 0

  parseKeyPattern: (pattern) ->
    [modifiers..., key] = pattern.split '+'

    ctrlKey: 'ctrl' in modifiers
    altKey: 'alt' in modifiers
    shiftKey: 'shift' in modifiers
    metaKey: 'meta' in modifiers
    key: key

  registerEventHandlers: ->
    $(document).bind 'keydown', (event) =>
      for pattern, action of @keyBindings
        action() if @keyEventMatchesPattern(event, pattern)

    $(window).focus => @registerMenuItems()
    $(window).blur -> atom.native.resetMainMenu()

  registerMenuItems: ->
    for path of @menuItemActions
      atom.native.addMenuItem(path)

  performActionForMenuItemPath: (path) ->
    @menuItemActions[path]()

  showConsole: ->
    $atomController.webView.inspector.showConsole true

  onerror: ->
    @showConsole true

for key, value of windowAdditions
  console.warn "DOMWindow already has a key named `#{key}`" if window[key]
  window[key] = value
{'fraction_non_alphanumeric': 0.13508810093539264, 'fraction_numerical': 0.00043506634761801175, 'mean_word_length': 3.4382239382239383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '7622680', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1422, 'n_words': 356}