Skip to content

Commit

Permalink
Add CppFormatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clcanny committed Jun 26, 2020
1 parent 692e425 commit bccabdd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions plugin/main.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions python/cpp_formatter.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions python/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.")

0 comments on commit bccabdd

Please sign in to comment.