From 9c1965dec6233205639fd342daf20ab4b145ced6 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Sun, 7 Sep 2014 19:32:08 -0300 Subject: [PATCH] Closes #78. Improve startup performance. @Glavin001 stats: - Before: 226ms - After: 60ms --- lib/beautify.coffee | 16 ++++++++--- lib/language-options.coffee | 56 +++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/lib/beautify.coffee b/lib/beautify.coffee index d3635170a..58bf7785a 100644 --- a/lib/beautify.coffee +++ b/lib/beautify.coffee @@ -1,15 +1,17 @@ # global atom "use strict" +# Dependencies plugin = module.exports -fs = require("fs") -path = require("path") _ = require("lodash") -strip = require("strip-json-comments") -yaml = require("js-yaml") beautifier = require("./language-options") languages = beautifier.languages defaultLanguageOptions = beautifier.defaultLanguageOptions +# Lazy loaded dependencies +fs = null +path = null +strip = null +yaml = null #MessageView = require "./message-view" findFileResults = {} @@ -42,6 +44,7 @@ setCursors = (editor, posArray) -> return verifyExists = (fullPath) -> + fs ?= require("fs") (if fs.existsSync(fullPath) then fullPath else null) # Storage for memoized results from find file @@ -60,6 +63,7 @@ current working directory) @returns {string} normalized filename ### findFile = (name, dir) -> + path ?= require("path") dir = dir or process.cwd() filename = path.normalize(path.join(dir, name)) return findFileResults[filename] if findFileResults[filename] isnt `undefined` @@ -82,6 +86,7 @@ or in the home directory. Configuration files are named @returns {string} a path to the config file ### findConfig = (config, file) -> + path ?= require("path") dir = path.dirname(path.resolve(file)) envs = getUserHome() home = path.normalize(path.join(envs, config)) @@ -121,6 +126,7 @@ beautify = -> configPath = findConfig(".jsbeautifyrc", startPath) externalOptions = undefined if configPath + fs ?= require("fs") contents = fs.readFileSync(configPath, encoding: "utf8" ) @@ -128,12 +134,14 @@ beautify = -> externalOptions = {} else try + 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") externalOptions = yaml.safeLoad(contents) catch e console.log "Failed parsing config as YAML: " + configPath diff --git a/lib/language-options.coffee b/lib/language-options.coffee index 12b6f1984..f5604e13b 100644 --- a/lib/language-options.coffee +++ b/lib/language-options.coffee @@ -2,20 +2,20 @@ Language Support and default options. ### "use strict" -_ = require("lodash") -extend = require("extend") - +# Lazy loaded dependencies +_ = null +extend = null # Language Beautifiers -beautifyJS = require("js-beautify") -beautifyHTML = require("js-beautify").html -beautifyCSS = require("js-beautify").css -beautifySQL = require("./langs/sql-beautify") -beautifyPHP = require("./langs/php-beautify") -beautifyPython = require("./langs/python-beautify") -beautifyRuby = require("./langs/ruby-beautify") -beautifyLESS = require("./langs/less-beautify") -beautifyCoffeeScript = require("./langs/coffeescript-beautify") -uncrustifyBeautifier = require("./langs/uncrustify/") +beautifyJS = null +beautifyHTML = null +beautifyCSS = null +beautifySQL = null +beautifyPHP = null +beautifyPython = null +beautifyRuby = null +beautifyLESS = null +beautifyCoffeeScript = null +uncrustifyBeautifier = null # Misc Analytics = require("analytics-node") @@ -128,72 +128,85 @@ module.exports = # Process each language beautify: (text, grammar, allOptions, beautifyCompleted) -> self = this - # Beautify! unsupportedGrammar = false options = undefined switch grammar - # Treat JSON as JavaScript, because it will support comments. # And Glavin001 has tested JSON beauifying with beautifyJS. when "JSON", "JavaScript" + beautifyJS ?= require("js-beautify") text = beautifyJS(text, self.getOptions("js", allOptions)) beautifyCompleted text when "CoffeeScript" + beautifyCoffeeScript ?= require("./langs/coffeescript-beautify") beautifyCoffeeScript text, self.getOptions("js", allOptions), beautifyCompleted when "Handlebars" - # jshint ignore: start allOptions.push indent_handlebars: true # Force jsbeautify to indent_handlebars - # jshint ignore: end when "HTML (Liquid)", "HTML", "XML" + beautifyHTML ?= require("js-beautify").html text = beautifyHTML(text, self.getOptions("html", allOptions)) beautifyCompleted text when "CSS" + beautifyCSS ?= require("js-beautify").css text = beautifyCSS(text, self.getOptions("css", allOptions)) beautifyCompleted text when "Sass", "SCSS", "LESS" + beautifyLESS ?= require("./langs/less-beautify") beautifyLESS text, self.getOptions("css", allOptions), beautifyCompleted when "SQL (Rails)", "SQL" + beautifySQL ?= require("./langs/sql-beautify") beautifySQL text, self.getOptions("sql", allOptions), beautifyCompleted when "PHP" + beautifyPHP ?= require("./langs/php-beautify") beautifyPHP text, self.getOptions("php", allOptions), beautifyCompleted when "Python" + beautifyPython ?= require("./langs/python-beautify") beautifyPython text, self.getOptions("python", allOptions), beautifyCompleted when "Ruby" + beautifyRuby ?= require("./langs/ruby-beautify") beautifyRuby text, self.getOptions("ruby", allOptions), beautifyCompleted when "C" options = self.getOptions("c", allOptions) options.languageOverride = "C" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted when "C++" options = self.getOptions("cpp", allOptions) options.languageOverride = "CPP" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted when "C#" options = self.getOptions("cs", allOptions) options.languageOverride = "CS" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted when "Objective-C", "Objective-C++" options = self.getOptions("objectivec", allOptions) options.languageOverride = "OC+" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted when "D" options = self.getOptions("d", allOptions) options.languageOverride = "D" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted when "Pawn" options = self.getOptions("pawn", allOptions) options.languageOverride = "PAWN" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted when "Vala" options = self.getOptions("vala", allOptions) options.languageOverride = "VALA" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted when "Java" options = self.getOptions("java", allOptions) options.languageOverride = "JAVA" + uncrustifyBeautifier ?= require("./langs/uncrustify/") uncrustifyBeautifier text, options, beautifyCompleted else unsupportedGrammar = true @@ -225,36 +238,31 @@ module.exports = getOptions: (selection, allOptions) -> self = this - + _ ?= require("lodash") + extend ?= require("extend") # console.log(selection, allOptions); - # Reduce all options into correctly merged options. options = _.reduce(allOptions, (result, currOptions) -> containsNested = false collectedConfig = {} key = undefined - # Check to see if config file uses nested object format to split up js/css/html options for key of currOptions # Check if is supported language if _.indexOf(self.languages, key) >= 0 and typeof currOptions[key] is "object" # Check if nested object (more options in value) containsNested = true break # Found, break out of loop, no need to continue - # console.log(containsNested, currOptions); - # Create a flat object of config options if nested format was used unless containsNested _.merge collectedConfig, currOptions else - # Merge with selected options # where `selection` could be `html`, `js`, 'css', etc # console.log(selection, currOptions[selection]); _.merge collectedConfig, currOptions[selection] extend result, collectedConfig , {}) - # TODO: Clean. # There is a bug in nopt # See https://github.com/npm/nopt/issues/38#issuecomment-45971505