Skip to content

Commit

Permalink
Add CMake formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clcanny committed Jun 26, 2020
1 parent c2b42ea commit ce11df2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ $(BUILD_VIRTUAL_ENV)/bin/beautysh: | $(BUILD_VIRTUAL_ENV)
docker-clang-format:
docker pull unibeautify/clang-format

$(BUILD_VIRTUAL_ENV)/bin/cmake-format: | $(BUILD_VIRTUAL_ENV)
$|/bin/python -m pip install -q cmake-format==0.6.10

isort: $(BUILD_VIRTUAL_ENV)/bin/isort
$(BUILD_VIRTUAL_ENV)/bin/isort --help

Expand All @@ -34,7 +37,10 @@ beautysh: $(BUILD_VIRTUAL_ENV)/bin/beautysh
clang-format: docker-clang-format
docker run --rm -i unibeautify/clang-format "-help"

check: isort yapf beautysh clang-format
cmake-format: $(BUILD_VIRTUAL_ENV)/bin/cmake-format
$(BUILD_VIRTUAL_ENV)/bin/cmake-format --help

check: isort yapf beautysh clang-format cmake-format

clean:
rm -rf build
Expand Down
5 changes: 5 additions & 0 deletions plugin/main.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ let g:VimFormatterCppStyle = {
\ "BinPackParameters": "false",
\ "AlignAfterOpenBracket": "true"
\ }
let g:VimFormatterCMakeStyle = {
\ "enable-sort": "True",
\ "autosort": "True",
\ "max-pargs-hwrap": 2
\ }

function! Main()
python main.main()
Expand Down
17 changes: 17 additions & 0 deletions python/cmake_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import vim
from abstract_formatter import AbstractFormatter


class CMakeFormatter(AbstractFormatter):

def __init__(self):
super(self.__class__, self).__init__()
self._cmake_format = self._getAbsPath(self._getRootDir(),
"build/venv/bin/cmake-format")

def _getFormatCommand(self, formattedFilename, guideFilname):
style = vim.eval("g:VimFormatterCMakeStyle")
style = map(lambda ele: "--{}={}".format(ele[0], ele[1]), style.items())
style = " ".join(style)
cmd = "{} {} {}".format(self._cmake_format, style, formattedFilename)
return cmd
2 changes: 0 additions & 2 deletions python/cpp_formatter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import vim
from abstract_formatter import AbstractFormatter

Expand Down
3 changes: 3 additions & 0 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 cmake_formatter import CMakeFormatter
from cpp_formatter import CppFormatter
from json_formatter import JsonFormatter
from python_formatter import PythonFormatter
Expand All @@ -19,5 +20,7 @@ def main():
PythonFormatter().run()
elif name.endswith(".h") or name.endswith(".cpp"):
CppFormatter().run()
elif name.endswith("CMakeLists.txt"):
CMakeFormatter().run()
else:
raise RuntimeError("Unknown filetype.")

0 comments on commit ce11df2

Please sign in to comment.