2014-08-10 15:51:44 -06:00
|
|
|
# global atom
|
|
|
|
|
2014-08-15 12:37:02 -06:00
|
|
|
"use strict"
|
2014-09-07 16:32:08 -06:00
|
|
|
# Dependencies
|
2014-08-15 12:37:02 -06:00
|
|
|
plugin = module.exports
|
|
|
|
_ = require("lodash")
|
|
|
|
beautifier = require("./language-options")
|
|
|
|
languages = beautifier.languages
|
|
|
|
defaultLanguageOptions = beautifier.defaultLanguageOptions
|
2014-09-07 16:32:08 -06:00
|
|
|
# Lazy loaded dependencies
|
|
|
|
fs = null
|
|
|
|
path = null
|
|
|
|
strip = null
|
|
|
|
yaml = null
|
2014-08-17 08:01:53 -06:00
|
|
|
#MessageView = require "./message-view"
|
2014-08-15 12:37:02 -06:00
|
|
|
findFileResults = {}
|
|
|
|
|
2014-08-10 15:51:44 -06:00
|
|
|
# CLI
|
|
|
|
getUserHome = ->
|
|
|
|
process.env.HOME or process.env.HOMEPATH or process.env.USERPROFILE
|
|
|
|
|
|
|
|
# function cleanOptions(data, types) {
|
|
|
|
# nopt.clean(data, types);
|
|
|
|
# return data;
|
|
|
|
# }
|
|
|
|
getCursors = (editor) ->
|
|
|
|
cursors = editor.getCursors()
|
|
|
|
posArray = []
|
2014-09-03 16:04:06 -06:00
|
|
|
for cursor in cursors
|
2014-08-10 15:51:44 -06:00
|
|
|
bufferPosition = cursor.getBufferPosition()
|
|
|
|
posArray.push [
|
|
|
|
bufferPosition.row
|
|
|
|
bufferPosition.column
|
|
|
|
]
|
|
|
|
posArray
|
2014-08-15 12:37:02 -06:00
|
|
|
|
2014-08-10 15:51:44 -06:00
|
|
|
setCursors = (editor, posArray) ->
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "setCursors: #{posArray}"
|
2014-09-03 16:04:06 -06:00
|
|
|
for bufferPosition, i in posArray
|
|
|
|
if i is 0
|
2014-08-10 15:51:44 -06:00
|
|
|
editor.setCursorBufferPosition bufferPosition
|
|
|
|
continue
|
|
|
|
editor.addCursorAtBufferPosition bufferPosition
|
|
|
|
return
|
2014-08-15 12:37:02 -06:00
|
|
|
|
2014-08-10 15:51:44 -06:00
|
|
|
verifyExists = (fullPath) ->
|
2014-09-07 16:32:08 -06:00
|
|
|
fs ?= require("fs")
|
2014-08-10 15:51:44 -06:00
|
|
|
(if fs.existsSync(fullPath) then fullPath else null)
|
|
|
|
|
|
|
|
# Storage for memoized results from find file
|
|
|
|
# Should prevent lots of directory traversal &
|
|
|
|
# lookups when liniting an entire project
|
|
|
|
|
|
|
|
###
|
|
|
|
Searches for a file with a specified name starting with
|
|
|
|
'dir' and going all the way up either until it finds the file
|
|
|
|
or hits the root.
|
|
|
|
|
|
|
|
@param {string} name filename to search for (e.g. .jshintrc)
|
2014-08-15 12:37:02 -06:00
|
|
|
@param {string} dir directory to start search from (default:
|
2014-08-10 15:51:44 -06:00
|
|
|
current working directory)
|
|
|
|
|
|
|
|
@returns {string} normalized filename
|
|
|
|
###
|
|
|
|
findFile = (name, dir) ->
|
2014-09-07 16:32:08 -06:00
|
|
|
path ?= require("path")
|
2014-08-10 15:51:44 -06:00
|
|
|
dir = dir or process.cwd()
|
|
|
|
filename = path.normalize(path.join(dir, name))
|
2014-08-15 12:37:02 -06:00
|
|
|
return findFileResults[filename] if findFileResults[filename] isnt `undefined`
|
2014-08-10 15:51:44 -06:00
|
|
|
parent = path.resolve(dir, "../")
|
|
|
|
if verifyExists(filename)
|
|
|
|
findFileResults[filename] = filename
|
|
|
|
return filename
|
|
|
|
if dir is parent
|
|
|
|
findFileResults[filename] = null
|
|
|
|
return null
|
|
|
|
findFile name, parent
|
|
|
|
|
|
|
|
###
|
|
|
|
Tries to find a configuration file in either project directory
|
|
|
|
or in the home directory. Configuration files are named
|
|
|
|
'.jsbeautifyrc'.
|
|
|
|
|
2014-08-15 12:37:02 -06:00
|
|
|
@param {string} config name of the configuration file
|
|
|
|
@param {string} file path to the file to be linted
|
2014-08-10 15:51:44 -06:00
|
|
|
@returns {string} a path to the config file
|
|
|
|
###
|
|
|
|
findConfig = (config, file) ->
|
2014-09-07 16:32:08 -06:00
|
|
|
path ?= require("path")
|
2014-08-10 15:51:44 -06:00
|
|
|
dir = path.dirname(path.resolve(file))
|
|
|
|
envs = getUserHome()
|
|
|
|
home = path.normalize(path.join(envs, config))
|
|
|
|
proj = findFile(config, dir)
|
2014-08-15 12:37:02 -06:00
|
|
|
return proj if proj
|
|
|
|
return home if verifyExists(home)
|
2014-08-10 15:51:44 -06:00
|
|
|
null
|
|
|
|
getConfigOptionsFromSettings = (langs) ->
|
|
|
|
config = atom.config.getSettings()["atom-beautify"]
|
|
|
|
options = {}
|
|
|
|
# console.log(langs, config);
|
|
|
|
# Iterate over keys of the settings
|
|
|
|
_.every _.keys(config), (k) ->
|
|
|
|
# Check if keys start with a language
|
|
|
|
p = k.split("_")[0]
|
|
|
|
idx = _.indexOf(langs, p)
|
|
|
|
# console.log(k, p, idx);
|
|
|
|
if idx >= 0
|
|
|
|
# Remove the language prefix and nest in options
|
|
|
|
lang = langs[idx]
|
|
|
|
opt = k.replace(new RegExp("^" + lang + "_"), "")
|
|
|
|
options[lang] = options[lang] or {}
|
|
|
|
options[lang][opt] = config[k]
|
|
|
|
# console.log(lang, opt);
|
|
|
|
true
|
|
|
|
# console.log(options);
|
|
|
|
options
|
|
|
|
|
2014-09-07 15:28:42 -06:00
|
|
|
beautify = ->
|
|
|
|
forceEntireFile = atom.config.get("atom-beautify.beautifyEntireFileOnSave")
|
2014-08-10 15:51:44 -06:00
|
|
|
# Look for .jsbeautifierrc in file and home path, check env variables
|
|
|
|
getConfig = (startPath) ->
|
|
|
|
# Verify that startPath is a string
|
|
|
|
startPath = (if (typeof startPath is "string") then startPath else "")
|
2014-08-15 12:37:02 -06:00
|
|
|
return {} unless startPath
|
2014-08-10 15:51:44 -06:00
|
|
|
# Get the path to the config file
|
|
|
|
configPath = findConfig(".jsbeautifyrc", startPath)
|
|
|
|
externalOptions = undefined
|
|
|
|
if configPath
|
2014-09-07 16:32:08 -06:00
|
|
|
fs ?= require("fs")
|
2014-08-10 15:51:44 -06:00
|
|
|
contents = fs.readFileSync(configPath,
|
|
|
|
encoding: "utf8"
|
|
|
|
)
|
|
|
|
unless contents
|
|
|
|
externalOptions = {}
|
|
|
|
else
|
|
|
|
try
|
2014-09-07 16:32:08 -06:00
|
|
|
strip ?= require("strip-json-comments")
|
2014-08-10 15:51:44 -06:00
|
|
|
externalOptions = JSON.parse(strip(contents))
|
|
|
|
catch e
|
|
|
|
console.log "Failed parsing config as JSON: " + configPath
|
|
|
|
|
|
|
|
# Attempt as YAML
|
|
|
|
try
|
2014-09-07 16:32:08 -06:00
|
|
|
yaml ?= require("js-yaml")
|
2014-08-10 15:51:44 -06:00
|
|
|
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) ->
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log 'beautifyCompleted'
|
2014-08-10 15:51:44 -06:00
|
|
|
if oldText isnt text
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "Replacing current editor's text with new text"
|
2014-08-10 15:51:44 -06:00
|
|
|
posArray = getCursors(editor)
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "posArray: #{posArray}"
|
2014-08-10 15:51:44 -06:00
|
|
|
origScrollTop = editor.getScrollTop()
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "origScrollTop: #{origScrollTop}"
|
2014-09-07 15:28:42 -06:00
|
|
|
if not forceEntireFile and isSelection
|
2014-08-15 12:37:02 -06:00
|
|
|
selectedBufferRange = editor.getSelectedBufferRange()
|
|
|
|
# console.log "selectedBufferRange: #{selectedBufferRange}"
|
|
|
|
editor.setTextInBufferRange selectedBufferRange, text
|
2014-08-10 15:51:44 -06:00
|
|
|
else
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "setText"
|
2014-08-10 15:51:44 -06:00
|
|
|
editor.setText text
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "setCursors"
|
2014-08-10 15:51:44 -06:00
|
|
|
setCursors editor, posArray
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "Done setCursors"
|
2014-08-10 15:51:44 -06:00
|
|
|
# Let the scrollTop setting run after all the save related stuff is run,
|
|
|
|
# otherwise setScrollTop is not working, probably because the cursor
|
|
|
|
# addition happens asynchronously
|
|
|
|
setTimeout (->
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "setScrollTop"
|
2014-08-10 15:51:44 -06:00
|
|
|
editor.setScrollTop origScrollTop
|
|
|
|
return
|
|
|
|
), 0
|
2014-08-15 12:37:02 -06:00
|
|
|
else
|
|
|
|
# console.log "Already Beautiful!"
|
2014-08-10 15:51:44 -06:00
|
|
|
return
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log 'Beautify time!'
|
2014-08-10 15:51:44 -06:00
|
|
|
text = undefined
|
|
|
|
editor = atom.workspace.getActiveEditor()
|
|
|
|
isSelection = !!editor.getSelectedText()
|
|
|
|
softTabs = editor.softTabs
|
|
|
|
tabLength = editor.getTabLength()
|
|
|
|
editorOptions =
|
|
|
|
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)
|
|
|
|
homeOptions = getConfig(getUserHome())
|
2014-09-07 15:28:42 -06:00
|
|
|
if not forceEntireFile and isSelection
|
2014-08-10 15:51:44 -06:00
|
|
|
text = editor.getSelectedText()
|
|
|
|
else
|
|
|
|
text = editor.getText()
|
|
|
|
oldText = text
|
|
|
|
allOptions = [
|
|
|
|
editorOptions
|
|
|
|
configOptions
|
|
|
|
homeOptions
|
|
|
|
projectOptions
|
|
|
|
]
|
2014-08-15 12:37:02 -06:00
|
|
|
grammarName = editor.getGrammar().name
|
2014-08-10 15:51:44 -06:00
|
|
|
# Finally, beautify!
|
2014-08-15 12:37:02 -06:00
|
|
|
beautifier.beautify text, grammarName, allOptions, beautifyCompleted
|
2014-08-10 15:51:44 -06:00
|
|
|
return
|
2014-09-07 15:28:42 -06:00
|
|
|
|
2014-08-10 15:51:44 -06:00
|
|
|
handleSaveEvent = ->
|
|
|
|
atom.workspace.eachEditor (editor) ->
|
|
|
|
buffer = editor.getBuffer()
|
|
|
|
plugin.unsubscribe buffer
|
|
|
|
if atom.config.get("atom-beautify.beautifyOnSave")
|
|
|
|
events = "will-be-saved"
|
|
|
|
plugin.subscribe buffer, events, beautify
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
Subscriber = require("emissary").Subscriber
|
|
|
|
Subscriber.extend plugin
|
|
|
|
plugin.configDefaults = _.merge(
|
|
|
|
analytics: true
|
|
|
|
beautifyOnSave: false
|
2014-09-07 15:28:42 -06:00
|
|
|
beautifyEntireFileOnSave: true
|
2014-08-10 15:51:44 -06:00
|
|
|
, defaultLanguageOptions)
|
|
|
|
plugin.activate = ->
|
|
|
|
handleSaveEvent()
|
|
|
|
plugin.subscribe atom.config.observe("atom-beautify.beautifyOnSave", handleSaveEvent)
|
|
|
|
atom.workspaceView.command "beautify", beautify
|