Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 3b0d21e

Browse files
author
Ari Archer
committedJan 15, 2022
Merge google#163
Signed-off-by: Ari Archer <[email protected]>
1 parent 956cb15 commit 3b0d21e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-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

+12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ 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+
""
71+
" The path to the cmake-format executable. String, list, or callable that
72+
" takes no args and returns a string or a list.
73+
call s:plugin.Flag('cmake_format_executable', 'cmake-format')
74+
75+
""
76+
" The path to the cmake-format configuration file for cmake-format to use.
77+
" See https://cmake-format.readthedocs.io/en/latest/installation.html for
78+
" details.
79+
call s:plugin.Flag('cmake_format_config', '')
80+
6981
""
7082
" The path to the gofmt executable. For example, this can be changed to
7183
" "goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to

‎plugin/register.vim

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ call s:registry.SetValidator('codefmt#EnsureFormatter')
5858
call s:registry.AddExtension(codefmt#rubocop#GetFormatter())
5959
call s:registry.AddExtension(codefmt#buildifier#GetFormatter())
6060
call s:registry.AddExtension(codefmt#clangformat#GetFormatter())
61+
call s:registry.AddExtension(codefmt#cmakeformat#GetFormatter())
6162
call s:registry.AddExtension(codefmt#cljstyle#GetFormatter())
6263
call s:registry.AddExtension(codefmt#zprint#GetFormatter())
6364
call s:registry.AddExtension(codefmt#dartfmt#GetFormatter())

0 commit comments

Comments
 (0)
This repository has been archived.