<commit_before>path = require 'path'
_ = require 'underscore-plus'
{$, TextEditorView, View} = require 'atom-space-pen-views'
fsp = require './fs'
fs = require 'fs-plus'

module.exports =
class modxComponentGeneratorView extends View
    previouslyFocusedElement: null
    mode: null

    @content: ->
        @div class: 'modx-generator', =>
            @subview 'miniEditor', new TextEditorView(mini: true)
            @div class: 'error', outlet: 'error'
            @div class: 'message', outlet: 'message'

    initialize: ->
        @commandSubscription = atom.commands.add 'atom-workspace',
            'modx-generator:scaffold-transport-package': => @attach('component'),
            'modx-generator:scaffold-theme': => @attach('theme')

        @miniEditor.on 'blur', => @close()
        atom.commands.add @element,
            'core:confirm': => @confirm()
            'core:cancel': => @close()

    destroy: ->
        @panel?.destroy()
        @commandSubscription.dispose()

    attach: (@mode) ->
        @panel ?= atom.workspace.addModalPanel(item: this, visible: false)
        @previouslyFocusedElement = $(document.activeElement)
        @panel.show()
        @message.text("Enter #{mode} path")
        if @mode is "component"
            @setPathText("my-component")
        else
            @setPathText("my-theme")
        @miniEditor.focus()

    setPathText: (placeholderName, rangeToSelect) ->
        editor = @miniEditor.getModel()
        rangeToSelect ?= [0, placeholderName.length]
        packagesDirectory = @getComponentsDirectory()
        editor.setText(path.join(packagesDirectory, placeholderName))
        pathLength = editor.getText().length
        endOfDirectoryIndex = pathLength - placeholderName.length
        editor.setSelectedBufferRange([[0, endOfDirectoryIndex + rangeToSelect[0]], [0, endOfDirectoryIndex + rangeToSelect[1]]])

    close: ->
        return unless @panel.isVisible()
        @panel.hide()
        @previouslyFocusedElement?.focus()

    confirm: ->
        if @validComponentPath()
            @createComponentFiles =>
                componentPath = @getComponentPath()
                atom.open(pathsToOpen: [componentPath])
                @close()

    getComponentPath: ->
        ComponentPath = fs.normalize(@miniEditor.getText().trim())
        ComponentName = _.dasherize(path.basename(ComponentPath))
        path.join(path.dirname(ComponentPath), ComponentName)

    getComponentsDirectory: ->
        atom.config.get('core.projectHome') or
          process.env.ATOM_REPOS_HOME or
          path.join(fs.getHomeDirectory(), 'modx-generator')

    validComponentPath: ->
        if fs.existsSync(@getComponentPath())
            @error.text("Path already exists at '#{@getComponentPath()}'")
            @error.show()
            false
        else
            true

    initComponent: (componentPath, templatePath, callback) ->
        fs.makeTreeSync(componentPath)
        fsp.cp templatePath, componentPath, ->
            callback()

    createComponentFiles: (callback) ->
        ComponentPath = @getComponentPath()
        packagesDirectory = @getComponentsDirectory()
        templatePath = path.resolve(__dirname, '..', 'templates', @mode)
        @initComponent(ComponentPath, templatePath, callback)<commit_msg>Callback call, duh<commit_after>path = require 'path'
_ = require 'underscore-plus'
{$, TextEditorView, View} = require 'atom-space-pen-views'
fsp = require './fs'
fs = require 'fs-plus'

module.exports =
class modxComponentGeneratorView extends View
    previouslyFocusedElement: null
    mode: null

    @content: ->
        @div class: 'modx-generator', =>
            @subview 'miniEditor', new TextEditorView(mini: true)
            @div class: 'error', outlet: 'error'
            @div class: 'message', outlet: 'message'

    initialize: ->
        @commandSubscription = atom.commands.add 'atom-workspace',
            'modx-generator:scaffold-transport-package': => @attach('component'),
            'modx-generator:scaffold-theme': => @attach('theme')

        @miniEditor.on 'blur', => @close()
        atom.commands.add @element,
            'core:confirm': => @confirm()
            'core:cancel': => @close()

    destroy: ->
        @panel?.destroy()
        @commandSubscription.dispose()

    attach: (@mode) ->
        @panel ?= atom.workspace.addModalPanel(item: this, visible: false)
        @previouslyFocusedElement = $(document.activeElement)
        @panel.show()
        @message.text("Enter #{mode} path")
        if @mode is "component"
            @setPathText("my-component")
        else
            @setPathText("my-theme")
        @miniEditor.focus()

    setPathText: (placeholderName, rangeToSelect) ->
        editor = @miniEditor.getModel()
        rangeToSelect ?= [0, placeholderName.length]
        packagesDirectory = @getComponentsDirectory()
        editor.setText(path.join(packagesDirectory, placeholderName))
        pathLength = editor.getText().length
        endOfDirectoryIndex = pathLength - placeholderName.length
        editor.setSelectedBufferRange([[0, endOfDirectoryIndex + rangeToSelect[0]], [0, endOfDirectoryIndex + rangeToSelect[1]]])

    close: ->
        return unless @panel.isVisible()
        @panel.hide()
        @previouslyFocusedElement?.focus()

    confirm: ->
        if @validComponentPath()
            @createComponentFiles =>
                componentPath = @getComponentPath()
                atom.open(pathsToOpen: [componentPath])
                @close()

    getComponentPath: ->
        ComponentPath = fs.normalize(@miniEditor.getText().trim())
        ComponentName = _.dasherize(path.basename(ComponentPath))
        path.join(path.dirname(ComponentPath), ComponentName)

    getComponentsDirectory: ->
        atom.config.get('core.projectHome') or
          process.env.ATOM_REPOS_HOME or
          path.join(fs.getHomeDirectory(), 'modx-generator')

    validComponentPath: ->
        if fs.existsSync(@getComponentPath())
            @error.text("Path already exists at '#{@getComponentPath()}'")
            @error.show()
            false
        else
            true

    initComponent: (componentPath, templatePath, callback) ->
        fs.makeTreeSync(componentPath)
        fsp.cp templatePath, componentPath, callback

    createComponentFiles: (callback) ->
        ComponentPath = @getComponentPath()
        packagesDirectory = @getComponentsDirectory()
        templatePath = path.resolve(__dirname, '..', 'templates', @mode)
        @initComponent(ComponentPath, templatePath, callback)
{'fraction_non_alphanumeric': 0.1282245827010622, 'fraction_numerical': 0.0015174506828528073, 'mean_word_length': 2.684181106763555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 42, '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': '7123628', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1761, 'n_words': 341}