|
| 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 | +function! s:ClangFormatHasAtLeastVersion(minimum_version) abort |
| 20 | + if !exists('s:clang_format_version') |
| 21 | + let l:executable = s:plugin.Flag('clang_format_executable') |
| 22 | + let l:version_output = |
| 23 | + \ maktaba#syscall#Create([l:executable, '--version']).Call().stdout |
| 24 | + let l:version_string = matchstr(l:version_output, '\v\d+(.\d+)+') |
| 25 | + let s:clang_format_version = map(split(l:version_string, '\.'), 'v:val + 0') |
| 26 | + endif |
| 27 | + let l:length = min([len(a:minimum_version), len(s:clang_format_version)]) |
| 28 | + for i in range(l:length) |
| 29 | + if a:minimum_version[i] < s:clang_format_version[i] |
| 30 | + return 1 |
| 31 | + elseif a:minimum_version[i] > s:clang_format_version[i] |
| 32 | + return 0 |
| 33 | + endif |
| 34 | + endfor |
| 35 | + return len(a:minimum_version) <= len(s:clang_format_version) |
| 36 | +endfunction |
| 37 | + |
| 38 | + |
| 39 | +"" |
| 40 | +" @private |
| 41 | +" Invalidates the cached clang-format version. |
| 42 | +function! codefmt#clangformat#InvalidateVersion() abort |
| 43 | + unlet! s:clang_format_version |
| 44 | +endfunction |
| 45 | + |
| 46 | + |
| 47 | +"" |
| 48 | +" @private |
| 49 | +" Formatter: clang-format |
| 50 | +function! codefmt#clangformat#GetFormatter() abort |
| 51 | + let l:formatter = { |
| 52 | + \ 'name': 'clang-format', |
| 53 | + \ 'setup_instructions': 'Install clang-format from ' . |
| 54 | + \ 'http://clang.llvm.org/docs/ClangFormat.html and ' . |
| 55 | + \ 'configure the clang_format_executable flag'} |
| 56 | + |
| 57 | + function l:formatter.IsAvailable() abort |
| 58 | + return executable(s:plugin.Flag('clang_format_executable')) |
| 59 | + endfunction |
| 60 | + |
| 61 | + function l:formatter.AppliesToBuffer() abort |
| 62 | + if &filetype is# 'c' || &filetype is# 'cpp' || |
| 63 | + \ &filetype is# 'proto' || &filetype is# 'javascript' || |
| 64 | + \ &filetype is# 'typescript' |
| 65 | + return 1 |
| 66 | + endif |
| 67 | + " Version 3.6 adds support for java |
| 68 | + " http://llvm.org/releases/3.6.0/tools/clang/docs/ReleaseNotes.html |
| 69 | + return &filetype is# 'java' && s:ClangFormatHasAtLeastVersion([3, 6]) |
| 70 | + endfunction |
| 71 | + |
| 72 | + "" |
| 73 | + " Reformat buffer with clang-format, only targeting [ranges] if given. |
| 74 | + function l:formatter.FormatRanges(ranges) abort |
| 75 | + let l:Style_value = s:plugin.Flag('clang_format_style') |
| 76 | + if type(l:Style_value) is# type('') |
| 77 | + let l:style = l:Style_value |
| 78 | + elseif maktaba#value#IsCallable(l:Style_value) |
| 79 | + let l:style = maktaba#function#Call(l:Style_value) |
| 80 | + else |
| 81 | + throw maktaba#error#WrongType( |
| 82 | + \ 'clang_format_style flag must be string or callable. Found %s', |
| 83 | + \ string(l:Style_value)) |
| 84 | + endif |
| 85 | + if empty(a:ranges) |
| 86 | + return |
| 87 | + endif |
| 88 | + |
| 89 | + let l:cmd = [ |
| 90 | + \ s:plugin.Flag('clang_format_executable'), |
| 91 | + \ '-style', l:style] |
| 92 | + let l:fname = expand('%:p') |
| 93 | + if !empty(l:fname) |
| 94 | + let l:cmd += ['-assume-filename', l:fname] |
| 95 | + endif |
| 96 | + |
| 97 | + for [l:startline, l:endline] in a:ranges |
| 98 | + call maktaba#ensure#IsNumber(l:startline) |
| 99 | + call maktaba#ensure#IsNumber(l:endline) |
| 100 | + let l:cmd += ['-lines', l:startline . ':' . l:endline] |
| 101 | + endfor |
| 102 | + |
| 103 | + " Version 3.4 introduced support for cursor tracking |
| 104 | + " http://llvm.org/releases/3.4/tools/clang/docs/ClangFormat.html |
| 105 | + let l:supports_cursor = s:ClangFormatHasAtLeastVersion([3, 4]) |
| 106 | + if l:supports_cursor |
| 107 | + " line2byte counts bytes from 1, and col counts from 1, so -2 |
| 108 | + let l:cursor_pos = line2byte(line('.')) + col('.') - 2 |
| 109 | + let l:cmd += ['-cursor', string(l:cursor_pos)] |
| 110 | + endif |
| 111 | + |
| 112 | + let l:input = join(getline(1, line('$')), "\n") |
| 113 | + let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() |
| 114 | + let l:formatted = split(l:result.stdout, "\n") |
| 115 | + |
| 116 | + if !l:supports_cursor |
| 117 | + call maktaba#buffer#Overwrite(1, line('$'), l:formatted[0:]) |
| 118 | + else |
| 119 | + call maktaba#buffer#Overwrite(1, line('$'), l:formatted[1:]) |
| 120 | + try |
| 121 | + let l:clang_format_output_json = maktaba#json#Parse(l:formatted[0]) |
| 122 | + let l:new_cursor_pos = |
| 123 | + \ maktaba#ensure#IsNumber(l:clang_format_output_json.Cursor) + 1 |
| 124 | + execute 'goto' l:new_cursor_pos |
| 125 | + catch |
| 126 | + call maktaba#error#Warn('Unable to parse clang-format cursor pos: %s', |
| 127 | + \ v:exception) |
| 128 | + endtry |
| 129 | + endif |
| 130 | + endfunction |
| 131 | + |
| 132 | + return l:formatter |
| 133 | +endfunction |
0 commit comments