Skip to content

Commit

Permalink
Closes #37. Support upwards recursive .jsbeautifyrc fallback.
Browse files Browse the repository at this point in the history
Priority hierarchy:
- Atom editor settings
- Atom Beautify package settings
- Home directory .jsbeautifyrc file configuration
- Closest EditorConfig file (see #68)
- Project options (from closest .jsbeautifyrc file to the beautified
  editor's file to root, "/")
  • Loading branch information
Glavin001 committed Sep 9, 2014
1 parent d60fec5 commit 738deac
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions lib/beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ or hits the root.
@param {string} name filename to search for (e.g. .jshintrc)
@param {string} dir directory to start search from (default:
current working directory)
@param {boolean} upwards should recurse upwards on failure? (default: true)
@returns {string} normalized filename
###
findFile = (name, dir) ->
findFile = (name, dir, upwards=true) ->
path ?= require("path")
dir = dir or process.cwd()
filename = path.normalize(path.join(dir, name))
Expand All @@ -77,7 +78,10 @@ findFile = (name, dir) ->
if dir is parent
findFileResults[filename] = null
return null
findFile name, parent
if upwards
findFile name, parent
else
return null

###
Tries to find a configuration file in either project directory
Expand All @@ -86,14 +90,16 @@ or in the home directory. Configuration files are named
@param {string} config name of the configuration file
@param {string} file path to the file to be linted
@param {boolean} upwards should recurse upwards on failure? (default: true)
@returns {string} a path to the config file
###
findConfig = (config, file) ->
findConfig = (config, file, upwards=true) ->
path ?= require("path")
dir = path.dirname(path.resolve(file))
envs = getUserHome()
home = path.normalize(path.join(envs, config))
proj = findFile(config, dir)
proj = findFile(config, dir, upwards)
return proj if proj
return home if verifyExists(home)
null
Expand All @@ -119,6 +125,7 @@ getConfigOptionsFromSettings = (langs) ->
options

beautify = ->
path ?= require("path")
MessagePanelView ?= require('atom-message-panel').MessagePanelView
PlainMessageView ?= require('atom-message-panel').PlainMessageView
LoadingView ?= require "./loading-view"
Expand All @@ -128,12 +135,12 @@ beautify = ->
@loadingView.show()
forceEntireFile = atom.config.get("atom-beautify.beautifyEntireFileOnSave")
# Look for .jsbeautifierrc in file and home path, check env variables
getConfig = (startPath) ->
getConfig = (startPath, upwards=true) ->
# Verify that startPath is a string
startPath = (if (typeof startPath is "string") then startPath else "")
return {} unless startPath
# Get the path to the config file
configPath = findConfig(".jsbeautifyrc", startPath)
configPath = findConfig(".jsbeautifyrc", startPath, upwards)
externalOptions = undefined
if configPath
fs ?= require("fs")
Expand Down Expand Up @@ -198,8 +205,10 @@ beautify = ->
@loadingView.hide()
return
# console.log 'Beautify time!'
text = undefined

# Get current editor
editor = atom.workspace.getActiveEditor()
# Get current Atom editor configuration
isSelection = !!editor.getSelectedText()
softTabs = editor.softTabs
tabLength = editor.getTabLength()
Expand All @@ -208,20 +217,51 @@ beautify = ->
indent_char: (if softTabs then " " else "\t")
indent_with_tabs: not softTabs
configOptions = getConfigOptionsFromSettings(languages)

# Get editor path and configurations for paths
editedFilePath = editor.getPath()
projectOptions = getConfig(editedFilePath)
homeOptions = getConfig(getUserHome())
if not forceEntireFile and isSelection
text = editor.getSelectedText()
else
text = editor.getText()
oldText = text

# Get configuration in User's Home directory
userHome = getUserHome()
# FAKEFILENAME forces `path` to treat as file path and it's parent directory
# is the userHome. See implementation of findConfig
# and how path.dirname(DIRECTORY) returns the parent directory of DIRECTORY
homeOptions = getConfig(path.join(userHome,"FAKEFILENAME"), false)

# TODO: See issue #68. EditorConfig options
editorConfigOptions = {};

# Get all options in configuration files from this directory upwards to root
projectOptions = []
p = path.dirname(editedFilePath)
# Check if p is root (top directory)
while p isnt "/"
# Get config for p
pf = path.join(p, "FAKEFILENAME")
pc = getConfig(pf, false)
# Add config for p to project's config options
projectOptions.push(pc)
# console.log p, pc
# Move upwards
p = path.resolve(p,"../")

# Combine all options together
allOptions = [
editorOptions
configOptions
homeOptions
projectOptions
editorConfigOptions
]
allOptions = allOptions.concat(projectOptions)

# Get current editor's text
text = undefined
if not forceEntireFile and isSelection
text = editor.getSelectedText()
else
text = editor.getText()
oldText = text
# Get Grammar
grammarName = editor.getGrammar().name
# Finally, beautify!
try
Expand Down

0 comments on commit 738deac

Please sign in to comment.