Skip to content

Commit 19eb49f

Browse files
committed
cmake-format support.
1 parent 9346c38 commit 19eb49f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

autoload/codefmt/cmakeformat.vim

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
" Copyright 2017 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
16+
let s:plugin = maktaba#plugin#Get('codefmt')
17+
18+
19+
""
20+
" @private
21+
" Formatter: cmake-format
22+
function! codefmt#cmakeformat#GetFormatter() abort
23+
let l:formatter = {
24+
\ 'name': 'cmake-format',
25+
\ 'setup_instructions': 'Install cmake-format from ' .
26+
\ 'https://cmake-format.readthedocs.io/en/latest/installation.html ' .
27+
\ 'and configure the cmake_format_executable, ' .
28+
\ 'cmake_format_config flags'}
29+
30+
function l:formatter.IsAvailable() abort
31+
return executable(s:plugin.Flag('cmake_format_executable'))
32+
endfunction
33+
34+
function l:formatter.AppliesToBuffer() abort
35+
if &filetype is# 'cmake'
36+
return 1
37+
endif
38+
endfunction
39+
40+
""
41+
" Reformat buffer with cmake-format.
42+
"
43+
" Implements format(), and not formatrange{,s}(), because cmake-format
44+
" doesn't provide a hook for formatting a range, and cmake files are
45+
" supposed to be fully formatted anyway.
46+
function l:formatter.Format() abort
47+
let l:cmd = [s:plugin.Flag('cmake_format_executable')]
48+
49+
" Append configuration style.
50+
let l:config = s:plugin.Flag('cmake_format_config')
51+
if !empty(l:config)
52+
if type(l:config) is# type('')
53+
let l:cmd += ['-c', l:config]
54+
else
55+
throw maktaba#error#WrongType(
56+
\ 'cmake_format_config flag must be string. Found'
57+
\ , string(l:config))
58+
endif
59+
endif
60+
61+
" Append filename.
62+
let l:fname = expand('%:p')
63+
if empty(l:fname)
64+
return
65+
endif
66+
let l:cmd += [l:fname]
67+
68+
" Generate formatted output.
69+
let l:input = join(getline(1, line('$')), "\n")
70+
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call()
71+
let l:formatted = split(l:result.stdout, "\n")
72+
73+
" Overwrite buffer.
74+
call maktaba#buffer#Overwrite(1, line('$'), l:formatted[0:])
75+
endfunction
76+
77+
return l:formatter
78+
endfunction

instant/flags.vim

+11
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ call s:plugin.flags.clang_format_executable.AddCallback(
6666
" See http://clang.llvm.org/docs/ClangFormatStyleOptions.html for details.
6767
call s:plugin.Flag('clang_format_style', 'file')
6868

69+
""
70+
" The path to the cmake-format executable. String, list, or callable that
71+
" takes no args and returns a string or a list.
72+
call s:plugin.Flag('cmake_format_executable', 'cmake-format')
73+
74+
""
75+
" The path to the cmake-format configuration file for cmake-format to use.
76+
" See https://cmake-format.readthedocs.io/en/latest/installation.html for
77+
" details.
78+
call s:plugin.Flag('cmake_format_config', '')
79+
6980
""
7081
" The path to the gofmt executable. For example, this can be changed to
7182
" "goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to

plugin/register.vim

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ call s:registry.AddExtension(codefmt#prettier#GetFormatter())
2727
call s:registry.AddExtension(codefmt#rustfmt#GetFormatter())
2828
call s:registry.AddExtension(codefmt#jsbeautify#GetFormatter())
2929
call s:registry.AddExtension(codefmt#clangformat#GetFormatter())
30+
call s:registry.AddExtension(codefmt#cmakeformat#GetFormatter())
3031
call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
3132
call s:registry.AddExtension(codefmt#dartfmt#GetFormatter())
3233
call s:registry.AddExtension(codefmt#black#GetFormatter())

0 commit comments

Comments
 (0)