Skip to content

Commit

Permalink
See #56. Add "Beautification in progress" view.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Sep 8, 2014
1 parent 05ee8f2 commit 6b2b306
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fs = null
path = null
strip = null
yaml = null
LoadingView = null
#MessageView = require "./message-view"
findFileResults = {}

Expand Down Expand Up @@ -44,7 +45,7 @@ setCursors = (editor, posArray) ->
return

verifyExists = (fullPath) ->
fs ?= require("fs")
fs ? = require("fs")
(if fs.existsSync(fullPath) then fullPath else null)

# Storage for memoized results from find file
Expand All @@ -63,7 +64,7 @@ current working directory)
@returns {string} normalized filename
###
findFile = (name, dir) ->
path ?= require("path")
path ? = require("path")
dir = dir or process.cwd()
filename = path.normalize(path.join(dir, name))
return findFileResults[filename] if findFileResults[filename] isnt `undefined`
Expand All @@ -86,7 +87,7 @@ or in the home directory. Configuration files are named
@returns {string} a path to the config file
###
findConfig = (config, file) ->
path ?= require("path")
path ? = require("path")
dir = path.dirname(path.resolve(file))
envs = getUserHome()
home = path.normalize(path.join(envs, config))
Expand Down Expand Up @@ -116,6 +117,9 @@ getConfigOptionsFromSettings = (langs) ->
options

beautify = ->
LoadingView ? = require "./loading-view"
@loadingView ? = new LoadingView()
@loadingView.show()
forceEntireFile = atom.config.get("atom-beautify.beautifyEntireFileOnSave")
# Look for .jsbeautifierrc in file and home path, check env variables
getConfig = (startPath) ->
Expand All @@ -126,42 +130,39 @@ beautify = ->
configPath = findConfig(".jsbeautifyrc", startPath)
externalOptions = undefined
if configPath
fs ?= require("fs")
fs ? = require("fs")
contents = fs.readFileSync(configPath,
encoding: "utf8"
)
unless contents
externalOptions = {}
else
try
strip ?= require("strip-json-comments")
strip ? = require("strip-json-comments")
externalOptions = JSON.parse(strip(contents))
catch e
console.log "Failed parsing config as JSON: " + configPath

# Attempt as YAML
try
yaml ?= require("js-yaml")
yaml ? = require("js-yaml")
externalOptions = yaml.safeLoad(contents)
catch e
console.log "Failed parsing config as YAML: " + configPath
externalOptions = {}
else
externalOptions = {}
externalOptions

# Get the path to the config file

# All of the options
# Listed in order from default (base) to the one with the highest priority
# Left = Default, Right = Will override the left.
# Atom Editor
#
# User's Home path
# Project path

# Asynchronously and callback-style
beautifyCompleted = (text) ->
beautifyCompleted = (text) =>
# console.log 'beautifyCompleted'
if oldText isnt text
# console.log "Replacing current editor's text with new text"
Expand All @@ -187,8 +188,9 @@ beautify = ->
editor.setScrollTop origScrollTop
return
), 0
else
# else
# console.log "Already Beautiful!"
@loadingView.hide()
return
# console.log 'Beautify time!'
text = undefined
Expand All @@ -200,7 +202,6 @@ beautify = ->
indent_size: (if softTabs then tabLength else 1)
indent_char: (if softTabs then " " else "\t")
indent_with_tabs: not softTabs

configOptions = getConfigOptionsFromSettings(languages)
editedFilePath = editor.getPath()
projectOptions = getConfig(editedFilePath)
Expand Down
38 changes: 38 additions & 0 deletions lib/loading-view.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{View} = require "atom"

module.exports =
class LoadingView extends View
@content: ->
@div
class: 'atom-beautify message-panel', =>
@div
class: 'overlay from-top', =>
@div class: "tool-panel panel-bottom", =>
@div class: "inset-panel", =>
@div class: "panel-heading", =>
@div class: 'btn-toolbar pull-right', =>
@button
class: 'btn'
click: 'hide'
'Hide'
@span
class: 'text-primary'
outlet: 'title'
'Atom Beautify'
@div
class: "panel-body padded select-list text-center"
outlet: 'body'
=>
@div =>
@span
class: 'text-center loading loading-spinner-large inline-block'
@div
class: ''
'Beautification in progress.'

hide: (event, element) =>
@detach()

show: =>
if not @.hasParent()
atom.workspaceView.appendToTop @

0 comments on commit 6b2b306

Please sign in to comment.