<commit_before>Package = require './package'
path = require 'path'
_ = require 'underscore-plus'
fs = require 'fs-plus'
async = require 'async'

### Internal ###

module.exports =
class TextMatePackage extends Package
  @testName: (packageName) ->
    packageName = path.basename(packageName)
    /(^language-.+)|((\.|_|-)tmbundle$)/.test(packageName)

  @getLoadQueue: ->
    return @loadQueue if @loadQueue
    @loadQueue = async.queue (pack, done) ->
      pack.loadGrammars ->
        pack.loadScopedProperties(done)

    @loadQueue

  constructor: ->
    super
    @grammars = []
    @scopedProperties = []
    @metadata = {@name}

  getType: -> 'textmate'

  load: ({sync}={}) ->
    @measure 'loadTime', =>
      @metadata = Package.loadMetadata(@path, true)

      if sync
        @loadGrammarsSync()
        @loadScopedPropertiesSync()
      else
        TextMatePackage.getLoadQueue().push(this)

  activate: ->
    @measure 'activateTime', =>
      grammar.activate() for grammar in @grammars
      for { selector, properties } in @scopedProperties
        atom.syntax.addProperties(@path, selector, properties)

  activateConfig: -> # noop

  deactivate: ->
    grammar.deactivate() for grammar in @grammars
    atom.syntax.removeProperties(@path)

  legalGrammarExtensions: ['plist', 'tmLanguage', 'tmlanguage', 'json', 'cson']

  loadGrammars: (done) ->
    fs.isDirectory @getSyntaxesPath(), (isDirectory) =>
      if isDirectory
        fs.list @getSyntaxesPath(), @legalGrammarExtensions, (error, paths) =>
          if error?
            console.log("Error loading grammars of TextMate package '#{@path}':", error.stack, error)
            done()
          else
            async.eachSeries(paths, @loadGrammarAtPath, done)
      else
        done()

  loadGrammarAtPath: (grammarPath, done) =>
    atom.syntax.registry.readGrammar (error, grammar) =>
      return console.log("Error loading grammar at path '#{grammarPath}':", err.stack ? err) if err
      @addGrammar(grammar)
      done()

  loadGrammarsSync: ->
    for grammarPath in fs.listSync(@getSyntaxesPath(), @legalGrammarExtensions)
      @addGrammar(atom.syntax.registry.readGrammarSync(grammarPath))

  addGrammar: (grammar) ->
    @grammars.push(grammar)

  getGrammars: -> @grammars

  getSyntaxesPath: ->
    syntaxesPath = path.join(@path, "syntaxes")
    if fs.isDirectorySync(syntaxesPath)
      syntaxesPath
    else
      path.join(@path, "Syntaxes")

  getPreferencesPath: ->
    preferencesPath = path.join(@path, "preferences")
    if fs.isDirectorySync(preferencesPath)
      preferencesPath
    else
      path.join(@path, "Preferences")

  loadScopedPropertiesSync: ->
    for grammar in @getGrammars()
      if properties = @propertiesFromTextMateSettings(grammar)
        selector = atom.syntax.cssSelectorFromScopeSelector(grammar.scopeName)
        @scopedProperties.push({selector, properties})<commit_msg>Activate grammar when already active<commit_after>Package = require './package'
path = require 'path'
_ = require 'underscore-plus'
fs = require 'fs-plus'
async = require 'async'

### Internal ###

module.exports =
class TextMatePackage extends Package
  @testName: (packageName) ->
    packageName = path.basename(packageName)
    /(^language-.+)|((\.|_|-)tmbundle$)/.test(packageName)

  @getLoadQueue: ->
    return @loadQueue if @loadQueue
    @loadQueue = async.queue (pack, done) ->
      pack.loadGrammars ->
        pack.loadScopedProperties(done)

    @loadQueue

  constructor: ->
    super
    @grammars = []
    @scopedProperties = []
    @metadata = {@name}

  getType: -> 'textmate'

  load: ({sync}={}) ->
    @measure 'loadTime', =>
      @metadata = Package.loadMetadata(@path, true)

      if sync
        @loadGrammarsSync()
        @loadScopedPropertiesSync()
      else
        TextMatePackage.getLoadQueue().push(this)

  activate: ->
    @measure 'activateTime', =>
      grammar.activate() for grammar in @grammars
      for { selector, properties } in @scopedProperties
        atom.syntax.addProperties(@path, selector, properties)

  activateConfig: -> # noop

  deactivate: ->
    grammar.deactivate() for grammar in @grammars
    atom.syntax.removeProperties(@path)

  legalGrammarExtensions: ['plist', 'tmLanguage', 'tmlanguage', 'json', 'cson']

  loadGrammars: (done) ->
    fs.isDirectory @getSyntaxesPath(), (isDirectory) =>
      if isDirectory
        fs.list @getSyntaxesPath(), @legalGrammarExtensions, (error, paths) =>
          if error?
            console.log("Error loading grammars of TextMate package '#{@path}':", error.stack, error)
            done()
          else
            async.eachSeries(paths, @loadGrammarAtPath, done)
      else
        done()

  loadGrammarAtPath: (grammarPath, done) =>
    atom.syntax.registry.readGrammar grammarPath, (error, grammar) =>
      console.log error, grammar
      if error?
        console.log("Error loading grammar at path '#{grammarPath}':", error.stack ? error)
      else
        @addGrammar(grammar)
        done?()

  loadGrammarsSync: ->
    for grammarPath in fs.listSync(@getSyntaxesPath(), @legalGrammarExtensions)
      @addGrammar(atom.syntax.registry.readGrammarSync(grammarPath))

  addGrammar: (grammar) ->
    @grammars.push(grammar)
    grammar.activate() if @isActive()

  getGrammars: -> @grammars

  getSyntaxesPath: ->
    syntaxesPath = path.join(@path, "syntaxes")
    if fs.isDirectorySync(syntaxesPath)
      syntaxesPath
    else
      path.join(@path, "Syntaxes")

  getPreferencesPath: ->
    preferencesPath = path.join(@path, "preferences")
    if fs.isDirectorySync(preferencesPath)
      preferencesPath
    else
      path.join(@path, "Preferences")

  loadScopedPropertiesSync: ->
    for grammar in @getGrammars()
      if properties = @propertiesFromTextMateSettings(grammar)
        selector = atom.syntax.cssSelectorFromScopeSelector(grammar.scopeName)
        @scopedProperties.push({selector, properties})
{'fraction_non_alphanumeric': 0.1289725912224651, 'fraction_numerical': 0.0, 'mean_word_length': 3.2607449856733526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '3802435', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1820, 'n_words': 374}