|
| 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 |
0 commit comments