Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cmake-format. #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions autoload/codefmt/cmakeformat.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
" Copyright 2017 Google Inc. All rights reserved.
"
" Licensed under the Apache License, Version 2.0 (the "License");
" you may not use this file except in compliance with the License.
" You may obtain a copy of the License at
"
" http://www.apache.org/licenses/LICENSE-2.0
"
" Unless required by applicable law or agreed to in writing, software
" distributed under the License is distributed on an "AS IS" BASIS,
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
" See the License for the specific language governing permissions and
" limitations under the License.


let s:plugin = maktaba#plugin#Get('codefmt')


""
" @private
" Formatter: cmake-format
function! codefmt#cmakeformat#GetFormatter() abort
let l:formatter = {
\ 'name': 'cmake-format',
\ 'setup_instructions': 'Install cmake-format from ' .
\ 'https://cmake-format.readthedocs.io/en/latest/installation.html ' .
\ 'and configure the cmake_format_executable, ' .
\ 'cmake_format_config flags'}

function l:formatter.IsAvailable() abort
return executable(s:plugin.Flag('cmake_format_executable'))
endfunction

function l:formatter.AppliesToBuffer() abort
if &filetype is# 'cmake'
return 1
endif
endfunction

""
" Reformat buffer with cmake-format.
"
" Implements format(), and not formatrange{,s}(), because cmake-format
" doesn't provide a hook for formatting a range, and cmake files are
" supposed to be fully formatted anyway.
function l:formatter.Format() abort
let l:cmd = [s:plugin.Flag('cmake_format_executable')]

" Append configuration style.
let l:config = s:plugin.Flag('cmake_format_config')
if !empty(l:config)
if type(l:config) is# type('')
let l:cmd += ['-c', l:config]
else
throw maktaba#error#WrongType(
\ 'cmake_format_config flag must be string. Found'
\ , string(l:config))
endif
endif

" Append filename.
let l:fname = expand('%:p')
if empty(l:fname)
return
endif
let l:cmd += [l:fname]

" Generate formatted output.
let l:input = join(getline(1, line('$')), "\n")
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call()
let l:formatted = split(l:result.stdout, "\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to use split's keepempty argument here, split(STDOUT, "\n", 1), so you have consistent results if the first or last line is empty.


" Overwrite buffer.
call maktaba#buffer#Overwrite(1, line('$'), l:formatted[0:])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the [0:] slicing for? Should be a no-op except for making a copy of the list before passing it to Overwrite, which AFAIK isn't needed.

endfunction

return l:formatter
endfunction
11 changes: 11 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ call s:plugin.flags.clang_format_executable.AddCallback(
" See http://clang.llvm.org/docs/ClangFormatStyleOptions.html for details.
call s:plugin.Flag('clang_format_style', 'file')

""
" The path to the cmake-format executable. String, list, or callable that
" takes no args and returns a string or a list.
call s:plugin.Flag('cmake_format_executable', 'cmake-format')

""
" The path to the cmake-format configuration file for cmake-format to use.
" See https://cmake-format.readthedocs.io/en/latest/installation.html for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like actually https://cmake-format.readthedocs.io/en/latest/configuration.html has the details and installation.html doesn't say much about it.

" details.
call s:plugin.Flag('cmake_format_config', '')
Comment on lines +75 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flag is global, so it will be a single value used for every file you edit. Is that what you'd want?

By my reading of https://cmake-format.readthedocs.io/en/latest/configuration.html, you can pass multiple config files, and that's independent of the automatic detection of files like \.?cmake-format(.yaml|.json|.py) that it finds on the path? Can you confirm if an explicit config overrides any automatic config detection and note that in the doc comments?


""
" The path to the gofmt executable. For example, this can be changed to
" "goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to
Expand Down
1 change: 1 addition & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ call s:registry.AddExtension(codefmt#prettier#GetFormatter())
call s:registry.AddExtension(codefmt#rustfmt#GetFormatter())
call s:registry.AddExtension(codefmt#jsbeautify#GetFormatter())
call s:registry.AddExtension(codefmt#clangformat#GetFormatter())
call s:registry.AddExtension(codefmt#cmakeformat#GetFormatter())
call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
call s:registry.AddExtension(codefmt#dartfmt#GetFormatter())
call s:registry.AddExtension(codefmt#black#GetFormatter())
Expand Down