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
|
2014-10-21 12:35:08 -06:00
|
|
|
path = require("path")
|
2014-09-07 16:32:08 -06:00
|
|
|
strip = null
|
|
|
|
yaml = null
|
2014-09-07 21:28:10 -06:00
|
|
|
LoadingView = null
|
2014-09-07 22:02:23 -06:00
|
|
|
MessagePanelView = null
|
|
|
|
PlainMessageView = null
|
2014-09-08 22:26:51 -06:00
|
|
|
editorconfig = 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 22:02:23 -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)
|
2014-09-08 21:29:39 -06:00
|
|
|
@param {boolean} upwards should recurse upwards on failure? (default: true)
|
2014-08-10 15:51:44 -06:00
|
|
|
|
|
|
|
@returns {string} normalized filename
|
|
|
|
###
|
2014-09-08 21:29:39 -06:00
|
|
|
findFile = (name, dir, upwards=true) ->
|
2014-09-07 22:02:23 -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
|
2014-09-08 21:29:39 -06:00
|
|
|
if upwards
|
|
|
|
findFile name, parent
|
|
|
|
else
|
|
|
|
return null
|
2014-08-10 15:51:44 -06:00
|
|
|
|
|
|
|
###
|
|
|
|
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-09-08 21:29:39 -06:00
|
|
|
@param {boolean} upwards should recurse upwards on failure? (default: true)
|
|
|
|
|
2014-08-10 15:51:44 -06:00
|
|
|
@returns {string} a path to the config file
|
|
|
|
###
|
2014-09-08 21:29:39 -06:00
|
|
|
findConfig = (config, file, upwards=true) ->
|
2014-09-07 22:02:23 -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))
|
2014-09-08 21:29:39 -06:00
|
|
|
proj = findFile(config, dir, upwards)
|
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]
|
2014-09-14 21:04:28 -06:00
|
|
|
# console.log(lang, opt);
|
2014-08-10 15:51:44 -06:00
|
|
|
true
|
|
|
|
# console.log(options);
|
|
|
|
options
|
|
|
|
|
2014-09-07 15:28:42 -06:00
|
|
|
beautify = ->
|
2014-09-08 21:29:39 -06:00
|
|
|
path ?= require("path")
|
2014-09-07 22:02:23 -06:00
|
|
|
MessagePanelView ?= require('atom-message-panel').MessagePanelView
|
|
|
|
PlainMessageView ?= require('atom-message-panel').PlainMessageView
|
|
|
|
LoadingView ?= require "./loading-view"
|
2014-09-10 14:07:42 -06:00
|
|
|
@messagePanel ?= new MessagePanelView title: 'Atom Beautify Error Messages'
|
2014-09-07 22:02:23 -06:00
|
|
|
@loadingView ?= new LoadingView()
|
2014-09-07 21:28:10 -06:00
|
|
|
@loadingView.show()
|
2014-09-07 15:28:42 -06:00
|
|
|
forceEntireFile = atom.config.get("atom-beautify.beautifyEntireFileOnSave")
|
2014-09-10 14:07:42 -06:00
|
|
|
# Show error
|
|
|
|
showError = (e) =>
|
|
|
|
@loadingView.hide()
|
2014-09-28 13:22:29 -06:00
|
|
|
if not atom.config.get("atom-beautify.muteAllErrors")
|
|
|
|
# console.log(e)
|
|
|
|
@messagePanel.attach()
|
|
|
|
@messagePanel.add(new PlainMessageView({
|
|
|
|
message: e.message,
|
|
|
|
className: 'text-error'
|
|
|
|
}))
|
2014-08-10 15:51:44 -06:00
|
|
|
# Look for .jsbeautifierrc in file and home path, check env variables
|
2014-09-08 21:29:39 -06:00
|
|
|
getConfig = (startPath, upwards=true) ->
|
2014-08-10 15:51:44 -06:00
|
|
|
# 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
|
2014-09-08 21:29:39 -06:00
|
|
|
configPath = findConfig(".jsbeautifyrc", startPath, upwards)
|
2014-08-10 15:51:44 -06:00
|
|
|
externalOptions = undefined
|
|
|
|
if configPath
|
2014-09-07 22:02:23 -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 22:02:23 -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 22:02:23 -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
|
2014-09-07 21:28:10 -06:00
|
|
|
beautifyCompleted = (text) =>
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log 'beautifyCompleted'
|
2014-09-28 13:22:29 -06:00
|
|
|
if not text?
|
|
|
|
# Do nothing, is undefined
|
|
|
|
else if text instanceof Error
|
2014-09-10 14:07:42 -06:00
|
|
|
showError(text)
|
|
|
|
else 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-09-07 21:28:10 -06:00
|
|
|
# else
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log "Already Beautiful!"
|
2014-09-07 21:28:10 -06:00
|
|
|
@loadingView.hide()
|
2014-08-10 15:51:44 -06:00
|
|
|
return
|
2014-08-15 12:37:02 -06:00
|
|
|
# console.log 'Beautify time!'
|
2014-09-08 21:29:39 -06:00
|
|
|
|
|
|
|
# Get current editor
|
2014-08-10 15:51:44 -06:00
|
|
|
editor = atom.workspace.getActiveEditor()
|
2014-09-08 21:29:39 -06:00
|
|
|
# Get current Atom editor configuration
|
2014-08-10 15:51:44 -06:00
|
|
|
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)
|
2014-09-08 21:29:39 -06:00
|
|
|
|
|
|
|
# Get editor path and configurations for paths
|
2014-08-10 15:51:44 -06:00
|
|
|
editedFilePath = editor.getPath()
|
2014-09-08 21:29:39 -06:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2014-09-10 07:25:43 -06:00
|
|
|
if editedFilePath?
|
|
|
|
# Handle EditorConfig options
|
|
|
|
# http://editorconfig.org/
|
|
|
|
editorconfig ?= require('editorconfig');
|
|
|
|
editorConfigOptions = editorconfig.parse(editedFilePath);
|
|
|
|
# Transform EditorConfig to Atom Beautify's config structure and naming
|
|
|
|
if editorConfigOptions.indent_style is 'space'
|
|
|
|
editorConfigOptions.indent_char = " "
|
|
|
|
# if (editorConfigOptions.indent_size)
|
|
|
|
# editorConfigOptions.indent_size = config.indent_size
|
|
|
|
else if editorConfigOptions.indent_style is 'tab'
|
|
|
|
editorConfigOptions.indent_char = "\t"
|
|
|
|
editorConfigOptions.indent_with_tabs = true
|
|
|
|
if (editorConfigOptions.tab_width)
|
2014-09-12 00:34:26 -06:00
|
|
|
editorConfigOptions.indent_size = editorConfigOptions.tab_width
|
2014-09-08 21:29:39 -06:00
|
|
|
|
2014-09-10 07:25:43 -06:00
|
|
|
# Get all options in configuration files from this directory upwards to root
|
|
|
|
projectOptions = []
|
|
|
|
p = path.dirname(editedFilePath)
|
|
|
|
# Check if p is root (top directory)
|
2014-09-10 13:27:24 -06:00
|
|
|
while p isnt path.resolve(p,"../")
|
2014-09-10 07:25:43 -06:00
|
|
|
# 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,"../")
|
|
|
|
else
|
|
|
|
editorConfigOptions = {}
|
|
|
|
projectOptions = []
|
2014-09-08 21:29:39 -06:00
|
|
|
|
|
|
|
# Combine all options together
|
2014-08-10 15:51:44 -06:00
|
|
|
allOptions = [
|
|
|
|
editorOptions
|
|
|
|
configOptions
|
|
|
|
homeOptions
|
2014-09-08 21:29:39 -06:00
|
|
|
editorConfigOptions
|
2014-08-10 15:51:44 -06:00
|
|
|
]
|
2014-09-08 21:29:39 -06:00
|
|
|
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
|
2014-08-15 12:37:02 -06:00
|
|
|
grammarName = editor.getGrammar().name
|
2014-08-10 15:51:44 -06:00
|
|
|
# Finally, beautify!
|
2014-09-07 22:02:23 -06:00
|
|
|
try
|
|
|
|
beautifier.beautify text, grammarName, allOptions, beautifyCompleted
|
|
|
|
catch e
|
2014-09-10 14:07:42 -06:00
|
|
|
showError(e)
|
2014-08-10 15:51:44 -06:00
|
|
|
return
|
2014-09-07 15:28:42 -06:00
|
|
|
|
2014-09-08 13:25:06 -06:00
|
|
|
handleSaveEvent = =>
|
|
|
|
atom.workspace.eachEditor (editor) =>
|
2014-08-10 15:51:44 -06:00
|
|
|
buffer = editor.getBuffer()
|
|
|
|
plugin.unsubscribe buffer
|
|
|
|
if atom.config.get("atom-beautify.beautifyOnSave")
|
|
|
|
events = "will-be-saved"
|
2014-09-08 13:25:06 -06:00
|
|
|
plugin.subscribe buffer, events, beautify.bind(@)
|
2014-08-10 15:51:44 -06:00
|
|
|
return
|
|
|
|
return
|
|
|
|
|
2014-10-21 12:35:08 -06:00
|
|
|
{Subscriber} = require path.join(atom.packages.resourcePath, 'node_modules', 'emissary')
|
2014-08-10 15:51:44 -06:00
|
|
|
Subscriber.extend plugin
|
|
|
|
plugin.configDefaults = _.merge(
|
|
|
|
analytics: true
|
|
|
|
beautifyOnSave: false
|
2014-09-07 15:28:42 -06:00
|
|
|
beautifyEntireFileOnSave: true
|
2014-09-28 13:22:29 -06:00
|
|
|
muteUnsupportedLanguageErrors: false
|
|
|
|
muteAllErrors: false
|
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
|