-
Notifications
You must be signed in to change notification settings - Fork 107
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
|
||
" Overwrite buffer. | ||
call maktaba#buffer#Overwrite(1, line('$'), l:formatted[0:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the |
||
endfunction | ||
|
||
return l:formatter | ||
endfunction |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
"" | ||
" The path to the gofmt executable. For example, this can be changed to | ||
" "goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to | ||
|
There was a problem hiding this comment.
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.