Skip to content

Commit

Permalink
See #56. Add Atom-Message-Panel to display Unsupported Language messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Sep 8, 2014
1 parent 6b2b306 commit ffa2249
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
33 changes: 23 additions & 10 deletions lib/beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ path = null
strip = null
yaml = null
LoadingView = null
MessagePanelView = null
PlainMessageView = null
#MessageView = require "./message-view"
findFileResults = {}

Expand Down Expand Up @@ -45,7 +47,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 @@ -64,7 +66,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 @@ -87,7 +89,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 @@ -117,8 +119,12 @@ getConfigOptionsFromSettings = (langs) ->
options

beautify = ->
LoadingView ? = require "./loading-view"
@loadingView ? = new LoadingView()
MessagePanelView ?= require('atom-message-panel').MessagePanelView
PlainMessageView ?= require('atom-message-panel').PlainMessageView
LoadingView ?= require "./loading-view"
@messagePanel ?= new MessagePanelView title: 'Atom Beautify'
@messagePanel.attach();
@loadingView ?= new LoadingView()
@loadingView.show()
forceEntireFile = atom.config.get("atom-beautify.beautifyEntireFileOnSave")
# Look for .jsbeautifierrc in file and home path, check env variables
Expand All @@ -130,22 +136,21 @@ 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
Expand Down Expand Up @@ -219,7 +224,15 @@ beautify = ->
]
grammarName = editor.getGrammar().name
# Finally, beautify!
beautifier.beautify text, grammarName, allOptions, beautifyCompleted
try
beautifier.beautify text, grammarName, allOptions, beautifyCompleted
catch e
console.log(e)
@loadingView.hide()
@messagePanel.add(new PlainMessageView({
message: e.message,
className: 'text-error'
}));
return

handleSaveEvent = ->
Expand Down
6 changes: 3 additions & 3 deletions lib/language-options.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,11 @@ module.exports =

# Check if Analytics is enabled
if atom.config.get("atom-beautify.analytics")

# Setup Analytics
analytics = new Analytics(analyticsWriteKey)
unless atom.config.get("atom-beautify._analyticsUserId")
uuid = require("node-uuid")
atom.config.set "atom-beautify._analyticsUserId", uuid.v4()

# Setup Analytics User Id
userId = atom.config.get("atom-beautify._analyticsUserId")
analytics.identify userId: userId
Expand All @@ -233,7 +231,9 @@ module.exports =
options: allOptions
label: grammar
category: version

#
if unsupportedGrammar
throw new Error("Unsupported language for grammar '#{grammar}'.")
return

getOptions: (selection, allOptions) ->
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"prettydiff": "^1.0.23",
"node-uuid": "^1.4.1",
"analytics-node": "^1.0.2",
"coffee-formatter": "git+https://github.com/Glavin001/Coffee-Formatter.git"
"coffee-formatter": "git+https://github.com/Glavin001/Coffee-Formatter.git",
"atom-message-panel": "^1.1.1"
}
}

0 comments on commit ffa2249

Please sign in to comment.