From bccabdd2de41a9abfcda7d59d2d7f8c3f6299998 Mon Sep 17 00:00:00 2001 From: demons <837940593@qq.com> Date: Fri, 26 Jun 2020 13:12:16 +0800 Subject: [PATCH] Add CppFormatter. --- Makefile | 8 +++++++- plugin/main.vim | 16 ++++++++++++++++ python/cpp_formatter.py | 18 ++++++++++++++++++ python/main.py | 7 +++++-- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 python/cpp_formatter.py diff --git a/Makefile b/Makefile index 2717728..3b2ab3b 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,9 @@ $(BUILD_VIRTUAL_ENV)/bin/yapf: | $(BUILD_VIRTUAL_ENV) $(BUILD_VIRTUAL_ENV)/bin/beautysh: | $(BUILD_VIRTUAL_ENV) $|/bin/python -m pip install -q beautysh==6.0.1 +docker-clang-format: + docker pull unibeautify/clang-format + isort: $(BUILD_VIRTUAL_ENV)/bin/isort $(BUILD_VIRTUAL_ENV)/bin/isort --help @@ -28,7 +31,10 @@ yapf: $(BUILD_VIRTUAL_ENV)/bin/yapf beautysh: $(BUILD_VIRTUAL_ENV)/bin/beautysh $(BUILD_VIRTUAL_ENV)/bin/beautysh --help -check: isort yapf beautysh +clang-format: docker-clang-format + docker run --rm -i unibeautify/clang-format "-help" + +check: isort yapf beautysh clang-format clean: rm -rf build diff --git a/plugin/main.vim b/plugin/main.vim index a00841e..10c9dbc 100644 --- a/plugin/main.vim +++ b/plugin/main.vim @@ -14,12 +14,28 @@ python << EOF import main EOF +" Refer to davidhalter/jedi-vim. +" Settings initialization. let g:VimFormatterJsonIndent = 2 let g:VimFormatterXmlIndent = 2 exec 'let g:VimFormatterPythonStyle = "'.s:pluginRootDir.'/../config/yapf.cfg"' let g:VimFormatterBashIndent = 4 " See beautysh documentation. let g:VimFormatterBashFuncStyle = "fnpar" +let g:VimFormatterCppStyle = { + \ "IndentWidth": 4, + \ "TabWidth": 4, + \ "AccessModifierOffset": -3, + \ "SpacesBeforeTrailingComments": 2, + \ "PointerAlignment": "Left", + \ "AlwaysBreakTemplateDeclarations": "true", + \ "SortUsingDeclarations": "true", + \ "FixNamespaceComments": "true", + \ "AllowShortFunctionsOnASingleLine": "false", + \ "BinPackArguments": "false", + \ "BinPackParameters": "false", + \ "AlignAfterOpenBracket": "true", + \ } function! Main() python main.main() diff --git a/python/cpp_formatter.py b/python/cpp_formatter.py new file mode 100644 index 0000000..2024309 --- /dev/null +++ b/python/cpp_formatter.py @@ -0,0 +1,18 @@ +import json + +import vim +from abstract_formatter import AbstractFormatter + + +class CppFormatter(AbstractFormatter): + + def __init__(self): + super(self.__class__, self).__init__() + + def _getFormatCommand(self, formattedFilename, guideFilename): + style = vim.eval("g:VimFormatterCppStyle") + style = map(lambda ele: "{}: {}".format(ele[0], ele[1]), style.items()) + style = "{" + ", ".join(style) + "}" + cmd = 'docker run --rm -i unibeautify/clang-format < {} "-style={}"'.format( + formattedFilename, style) + return cmd diff --git a/python/main.py b/python/main.py index ffe2323..295f7d3 100644 --- a/python/main.py +++ b/python/main.py @@ -1,5 +1,6 @@ import vim from bash_formatter import BashFormatter +from cpp_formatter import CppFormatter from json_formatter import JsonFormatter from python_formatter import PythonFormatter from xml_formatter import XmlFormatter @@ -12,9 +13,11 @@ def main(): JsonFormatter().run() elif name.endswith(".xml"): XmlFormatter().run() - elif name.endswith(".py"): - PythonFormatter().run() elif name.endswith(".sh"): BashFormatter().run() + elif name.endswith(".py"): + PythonFormatter().run() + elif name.endswith(".h") or name.endswith(".cpp"): + CppFormatter().run() else: raise RuntimeError("Unknown filetype.")