From fdb9b71852a343cbe0c4f385b3e95b2cee0eb038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gutyina=20Gerg=C5=91?= Date: Sun, 10 Sep 2023 09:11:11 +0200 Subject: [PATCH] feat: add yapf python format (#3) --- README.md | 1 + lua/guard-collection/formatter.lua | 6 ++++++ test/formatter/yapf_spec.lua | 26 ++++++++++++++++++++++++++ test/setup.sh | 2 +- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/formatter/yapf_spec.lua diff --git a/README.md b/README.md index 7a0f0ff..6f128ef 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ - [ ] [swiftformat](https://github.com/nicklockwood/SwiftFormat) - [ ] [swift-format](https://github.com/apple/swift-format) - [x] [sql-formatter](https://github.com/sql-formatter-org/sql-formatter) +- [x] [yapf](https://github.com/google/yapf) ## Linters diff --git a/lua/guard-collection/formatter.lua b/lua/guard-collection/formatter.lua index dd84300..9918a11 100644 --- a/lua/guard-collection/formatter.lua +++ b/lua/guard-collection/formatter.lua @@ -139,4 +139,10 @@ M['sql-formatter'] = { stdin = true, } +M.yapf = { + cmd = 'yapf', + args = { '--quiet' }, + stdin = true, +} + return M diff --git a/test/formatter/yapf_spec.lua b/test/formatter/yapf_spec.lua new file mode 100644 index 0000000..d3c4532 --- /dev/null +++ b/test/formatter/yapf_spec.lua @@ -0,0 +1,26 @@ +describe('yapf', function() + it('can format', function() + local ft = require('guard.filetype') + ft('python'):fmt('yapf') + require('guard').setup() + + local formatted = require('test.formatter.helper').test_with('python', { + [[def foo(n):]], + [[ if n in (1,2,3):]], + [[ return n+1]], + [[a, b = 1, 2]], + [[b, a = a, b]], + [[print( f"The factorial of {a} is: {foo(a)}")]], + }) + assert.are.same({ + [[def foo(n):]], + [[ if n in (1, 2, 3):]], + [[ return n + 1]], + [[]], + [[]], + [[a, b = 1, 2]], + [[b, a = a, b]], + [[print(f"The factorial of {a} is: {foo(a)}")]], + }, formatted) + end) +end) diff --git a/test/setup.sh b/test/setup.sh index 183fad6..afc3c74 100644 --- a/test/setup.sh +++ b/test/setup.sh @@ -8,7 +8,7 @@ sudo apt-get install -qqq \ clang-format clang-tidy fish elixir & luarocks install luacheck & go install github.com/segmentio/golines@latest & -pip -qqq install autopep8 black djhtml isort pylint & +pip -qqq install autopep8 black djhtml isort pylint yapf & npm install -g --silent \ prettier @fsouza/prettierd sql-formatter shellcheck shfmt & gem install -q rubocop &