From ffa2249c3e87d217cc544a71e23e389f97d978d7 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Mon, 8 Sep 2014 01:02:23 -0300 Subject: [PATCH] See #56. Add Atom-Message-Panel to display Unsupported Language messages --- lib/beautify.coffee | 33 +++++++++++++++++++++++---------- lib/language-options.coffee | 6 +++--- package.json | 3 ++- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/beautify.coffee b/lib/beautify.coffee index f638ff5..f2806e2 100644 --- a/lib/beautify.coffee +++ b/lib/beautify.coffee @@ -13,6 +13,8 @@ path = null strip = null yaml = null LoadingView = null +MessagePanelView = null +PlainMessageView = null #MessageView = require "./message-view" findFileResults = {} @@ -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 @@ -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` @@ -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)) @@ -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 @@ -130,7 +136,7 @@ beautify = -> configPath = findConfig(".jsbeautifyrc", startPath) externalOptions = undefined if configPath - fs ? = require("fs") + fs ?= require("fs") contents = fs.readFileSync(configPath, encoding: "utf8" ) @@ -138,14 +144,13 @@ beautify = -> 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 @@ -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 = -> diff --git a/lib/language-options.coffee b/lib/language-options.coffee index f5604e1..9795d76 100644 --- a/lib/language-options.coffee +++ b/lib/language-options.coffee @@ -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 @@ -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) -> diff --git a/package.json b/package.json index 9b0a18f..7181d92 100644 --- a/package.json +++ b/package.json @@ -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" } }