Merge pull request #546 from ProgramFan/master

Add two beautifiers: clang-format and yapf
This commit is contained in:
Glavin Wiechert 2015-09-27 13:13:50 -03:00
commit 8fd7d946ba
5 changed files with 107 additions and 0 deletions

View File

@ -1,3 +1,7 @@
# dev
- Add [clang-format](http://clang.llvm.org/docs/ClangFormat.html) beautifier for C/C++/Obj-C languages.
- Add [yapf](http://github.com/google/yapf) beautifier for Python.
# v0.29.0 # v0.29.0
- Closes [#447](https://github.com/Glavin001/atom-beautify/issues/447). Improved Handlebars language support - Closes [#447](https://github.com/Glavin001/atom-beautify/issues/447). Improved Handlebars language support

View File

@ -50,6 +50,10 @@
{ {
"name": "Clemens Damke", "name": "Clemens Damke",
"url": "https://github.com/Cortys" "url": "https://github.com/Cortys"
},
{
"name": "Zhang YANG",
"url": "https://github.com/ProgramFan"
} }
], ],
"engines": { "engines": {

View File

@ -0,0 +1,72 @@
###
Requires clang-format (https://clang.llvm.org)
###
"use strict"
Beautifier = require('./beautifier')
path = require('path')
fs = require('fs')
module.exports = class ClangFormat extends Beautifier
name: "clang-format"
options: {
"C++": false
"C": false
"Objective-C": false
}
###
Dump contents to a given file
###
dumpToFile: (name = "atom-beautify-dump", contents = "") ->
return new Promise((resolve, reject) =>
fs.open(name, "w", (err, fd) =>
@debug('dumpToFile', name, err, fd)
return reject(err) if err
fs.write(fd, contents, (err) ->
return reject(err) if err
fs.close(fd, (err) ->
return reject(err) if err
resolve(name)
)
)
)
)
beautify: (text, language, options) ->
# NOTE: One may wonder why this code goes a long way to construct a file
# path and dump content using a custom `dumpToFile`. Wouldn't it be easier
# to use `@tempFile` instead? The reason here is to work around the
# clang-format config file locating mechanism. As indicated in the manual,
# clang-format (with `--style file`) tries to locate a `.clang-format`
# config file in directory and parent directories of the input file,
# and retreat to default style if not found. Projects often makes use of
# this rule to define their own style in its top directory. Users often
# put a `.clang-format` in their $HOME to define his/her style. To honor
# this rule, we HAVE TO generate the temp file in THE SAME directory as
# the editing file. However, this mechanism is not directly supported by
# atom-beautify at the moment. So we introduce lots of code here.
return new @Promise((resolve, reject) ->
editor = atom?.workspace?.getActiveTextEditor()
if editor?
fullPath = editor.getPath()
currDir = path.dirname(fullPath)
currFile = path.basename(fullPath)
dumpFile = path.join(currDir, ".atom-beautify.#{currFile}")
resolve dumpFile
else
reject(new Error("No active editor found!"))
)
.then((dumpFile) =>
# console.log("clang-format", dumpFile)
return @run("clang-format", [
@dumpToFile(dumpFile, text)
["--style=file"]
], help: {
link: "https://clang.llvm.org/docs/ClangFormat.html"
}).finally( ->
fs.unlink(dumpFile)
)
)

View File

@ -37,6 +37,7 @@ module.exports = class Beautifiers extends EventEmitter
'autopep8' 'autopep8'
'coffee-formatter' 'coffee-formatter'
'coffee-fmt' 'coffee-fmt'
'clang-format'
'htmlbeautifier' 'htmlbeautifier'
'csscomb' 'csscomb'
'gherkin' 'gherkin'
@ -54,6 +55,7 @@ module.exports = class Beautifiers extends EventEmitter
'sqlformat' 'sqlformat'
'tidy-markdown' 'tidy-markdown'
'typescript-formatter' 'typescript-formatter'
'yapf'
] ]
### ###

View File

@ -0,0 +1,25 @@
###
Requires https://github.com/google/yapf
###
"use strict"
Beautifier = require('./beautifier')
module.exports = class Yapf extends Beautifier
name: "yapf"
# I decide to support no options since yapf is configured using its
# own configure file.
options: {
Python: false
}
beautify: (text, language, options) ->
# console.log('yapf', options, text, language)
@run("yapf", [
["--style=pep8"]
@tempFile("input", text)
], help: {
link: "https://github.com/google/yapf"
})