diff --git a/package.json b/package.json index 497e15c..95ceb9f 100644 --- a/package.json +++ b/package.json @@ -114,6 +114,10 @@ { "name": "Victor Uriarte", "url": "https://github.com/vmuriart" + }, + { + "name": "guyskk", + "url": "https://github.com/guyskk" } ], "engines": { @@ -288,6 +292,7 @@ "clojure beautifier", "nunjucks", "ux markup", + "pybeautifier", "cljfmt" ], "devDependencies": { diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 704ed86..6e1a5c2 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -57,6 +57,7 @@ module.exports = class Beautifiers extends EventEmitter 'php-cs-fixer' 'phpcbf' 'prettydiff' + 'pybeautifier' 'pug-beautify' 'puppet-fix' 'remark' diff --git a/src/beautifiers/pybeautifier.coffee b/src/beautifiers/pybeautifier.coffee new file mode 100644 index 0000000..692a8fb --- /dev/null +++ b/src/beautifiers/pybeautifier.coffee @@ -0,0 +1,66 @@ +'use strict' +net = require('net') +Beautifier = require('./beautifier') + +HOST = '127.0.0.1' +PORT = 36805 +MULTI_LINE_OUTPUT_TABLE = { + 'Grid': 0, + 'Vertical': 1, + 'Hanging Indent': 2, + 'Vertical Hanging Indent': 3, + 'Hanging Grid': 4, + 'Hanging Grid Grouped': 5, + 'NOQA': 6 +} + +format = (data, formaters) -> + return new Promise (resolve, reject) -> + client = new net.Socket() + client.on 'error', (error) -> + client.destroy() + reject(error) + client.connect PORT, HOST, -> + client.setEncoding('utf8') + client.write(JSON.stringify({'data': data, 'formaters': formaters})) + response = '' + client.on 'data', (chunk) -> + response += chunk + client.on 'end', -> + response = JSON.parse(response) + if response.error? + reject(Error(response.error)) + else + resolve(response.data) + client.destroy() + +module.exports = class PythonBeautifier extends Beautifier + + name: "pybeautifier" + link: "https://github.com/guyskk/pybeautifier" + + options: { + Python: true + } + + beautify: (text, language, options) -> + formater = {'name': options.formater} + if options.formater == 'autopep8' + formater.config = { + 'ignore': options.ignore + 'max_line_length': options.max_line_length + } + else if options.formater == 'yapf' + formater.config = {'style_config': options.style_config} + formaters = [formater] + if options.sort_imports + multi_line_output = MULTI_LINE_OUTPUT_TABLE[options.multi_line_output] + formaters.push + 'name': 'isort' + 'config': {'multi_line_output': multi_line_output} + return new @Promise (resolve, reject) -> + format(text, formaters) + .then (data) -> + resolve(data) + .catch (error) -> + reject(error) diff --git a/src/languages/python.coffee b/src/languages/python.coffee index f598815..d62e0f5 100644 --- a/src/languages/python.coffee +++ b/src/languages/python.coffee @@ -41,9 +41,30 @@ module.exports = { items: type: 'string' description: "do not fix these errors/warnings" + formater: + type: 'string' + default: 'autopep8' + enum: ['autopep8', 'yapf'] + description: "formater used by pybeautifier" + style_config: + type: 'string' + default: 'pep8' + description: "formatting style used by yapf" sort_imports: type: 'boolean' default: false description: "sort imports (requires isort installed)" - + multi_line_output: + type: 'string' + default: 'Hanging Grid Grouped' + enum: [ + 'Grid' + 'Vertical' + 'Hanging Indent' + 'Vertical Hanging Indent' + 'Hanging Grid' + 'Hanging Grid Grouped' + 'NOQA' + ] + description: "defines how from imports wrap (requires isort installed)" }