diff --git a/autoload/codefmt/cmakeformat.vim b/autoload/codefmt/cmakeformat.vim new file mode 100644 index 0000000..c73ce39 --- /dev/null +++ b/autoload/codefmt/cmakeformat.vim @@ -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:]) + endfunction + + return l:formatter +endfunction diff --git a/instant/flags.vim b/instant/flags.vim index 97d0959..2278baf 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -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 +" details. +call s:plugin.Flag('cmake_format_config', '') + "" " The path to the gofmt executable. For example, this can be changed to " "goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to diff --git a/plugin/register.vim b/plugin/register.vim index 1770f4c..3d284ad 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -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())