Skip to content

Instantly share code, notes, and snippets.

@crazyboycjr
Last active September 11, 2019 13:34
Show Gist options
  • Save crazyboycjr/ff37b9fb8be5fdcb7823501cbdba7dcb to your computer and use it in GitHub Desktop.
Save crazyboycjr/ff37b9fb8be5fdcb7823501cbdba7dcb to your computer and use it in GitHub Desktop.
hindent.vim for codefmt
" Copyright 2014 Google Inc. All rights reserved.
" Copyright 2019 Jingrong Chen i@cjr.host
"
" 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.
""
" @section Introduction, intro
" @order intro config formatters dicts commands autocmds functions mappings
" Provides a @command(FormatCode) command to intelligently reformat code.
""
" @setting b:codefmt_formatter
" You can override the default formatter by defining this variable. For
" instance, to explicitly select the clang-format formatter for Java, add >
" autocmd FileType java let b:codefmt_formatter = 'clang-format'
" < to your vimrc. You can also set the value to an empty string to disable all
" formatting.
let [s:plugin, s:enter] = maktaba#plugin#Enter(expand('<sfile>:p'))
if !s:enter
finish
endif
" Shout if maktaba is too old. Done here to ensure it's always triggered.
if !maktaba#IsAtLeastVersion('1.10.0')
call maktaba#error#Shout('Codefmt requires maktaba version 1.10.0.')
call maktaba#error#Shout('You have maktaba version %s.', maktaba#VERSION)
call maktaba#error#Shout('Please update your maktaba install.')
endif
""
" The path to the autopep8 executable.
call s:plugin.Flag('autopep8_executable', 'autopep8')
" Invalidate cache of detected autopep8 version when this is changed, regardless
" of {value} arg.
call s:plugin.flags.autopep8_executable.AddCallback(
\ maktaba#function#FromExpr('codefmt#autopep8#InvalidateVersion()'), 0)
""
" The path to the clang-format executable.
call s:plugin.Flag('clang_format_executable', 'clang-format')
" Invalidate cache of detected clang-format version when this is changed, regardless
" of {value} arg.
call s:plugin.flags.clang_format_executable.AddCallback(
\ maktaba#function#FromExpr('codefmt#clangformat#InvalidateVersion()'), 0)
""
" Formatting style for clang-format to use. Either a string or callable that
" takes no args and returns a style name for the current buffer.
" See http://clang.llvm.org/docs/ClangFormatStyleOptions.html for details.
call s:plugin.Flag('clang_format_style', 'file')
""
" The path to the gofmt executable. For example, this can be changed to
" "goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to
" additionally adjust imports when formatting.
call s:plugin.Flag('gofmt_executable', 'gofmt')
""
" The path to the dartfmt executable.
call s:plugin.Flag('dartfmt_executable', 'dartfmt')
""
" The path to the js-beautify executable.
call s:plugin.Flag('js_beautify_executable', 'js-beautify')
""
" The path to the yapf executable.
call s:plugin.Flag('yapf_executable', 'yapf')
""
" The path to the gn executable.
call s:plugin.Flag('gn_executable', 'gn')
""
" The path to the buildifier executable.
call s:plugin.Flag('buildifier_executable', 'buildifier')
""
" The path to the google-java executable. Generally, this should have the
" form:
" `java -jar /path/to/google-java`
call s:plugin.Flag('google_java_executable', 'google-java-format')
""
" Command line arguments to feed shfmt. Either a list or callable that
" takes no args and returns a list with command line arguments. By default, uses
" the Google's style.
" See https://github.com/mvdan/sh for details.
call s:plugin.Flag('shfmt_options', ['-i', '2', '-sr', '-ci'])
""
" The path to the shfmt executable.
call s:plugin.Flag('shfmt_executable', 'shfmt')
""
" Command line arguments to feed prettier. Either a list or callable that
" takes no args and returns a list with command line arguments.
call s:plugin.Flag('prettier_options', [
\ '--single-quote', '--trailing-comma=all',
\ '--arrow-parens=always', '--print-width=80'])
""
" The path to the prettier executable.
call s:plugin.Flag('prettier_executable', 'prettier')
""
" The path to the hindent executable.
call s:plugin.Flag('hindent_executable', 'hindent')
call s:plugin.Flag('hindent_indent_size', '2')
call s:plugin.Flag('hindent_line_length', '80')
" Copyright 2017 Google Inc. All rights reserved.
" Copyright 2019 Jingrong Chen i@cjr.host
"
" 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 provider for Bazel BUILD files using buildifier.
function! codefmt#hindent#GetFormatter() abort
let l:formatter = {
\ 'name': 'hindent',
\ 'setup_instructions': 'Install hindent. ' .
\ '(https://github.com/chrisdone/hindent).'}
function l:formatter.IsAvailable() abort
return executable(s:plugin.Flag('hindent_executable'))
endfunction
function l:formatter.AppliesToBuffer() abort
return &filetype is# 'haskell'
endfunction
""
" Reformat the current buffer with yapf or the binary named in
" @flag(hindent_executable), only targeting the range between {startline} and
" {endline}.
" @throws ShellError
function l:formatter.FormatRange(startline, endline) abort
let l:executable = s:plugin.Flag('hindent_executable')
let l:indent_size = s:plugin.Flag('hindent_indent_size')
let l:line_length = s:plugin.Flag('hindent_line_length')
call maktaba#ensure#IsNumber(a:startline)
call maktaba#ensure#IsNumber(a:endline)
let l:lines = getline(1, line('$'))
let l:cmd = [l:executable, "--indent-size", l:indent_size, "--line-length", l:line_length]
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call(0)
if v:shell_error == 1 " Indicates an error with parsing
call maktaba#error#Shout('Error formatting file: %s', l:result.stderr)
return
endif
let l:formatted = split(l:result.stdout, "\n")
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2 ] : []
let l:full_formatted = l:before + l:formatted + l:lines[a:endline :]
call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
endfunction
return l:formatter
endfunction
" Copyright 2015 Google Inc. All rights reserved.
" Copyright 2019 Jingrong Chen i@cjr.host
"
" 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, s:enter] = maktaba#plugin#Enter(expand('<sfile>:p'))
if !s:enter
finish
endif
let s:registry = s:plugin.GetExtensionRegistry()
call s:registry.SetValidator('codefmt#EnsureFormatter')
" Formatters that are registered later are given more priority when deciding
" what the default formatter will be for a particular file type.
call s:registry.AddExtension(codefmt#prettier#GetFormatter())
call s:registry.AddExtension(codefmt#jsbeautify#GetFormatter())
call s:registry.AddExtension(codefmt#clangformat#GetFormatter())
call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
call s:registry.AddExtension(codefmt#dartfmt#GetFormatter())
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
call s:registry.AddExtension(codefmt#gn#GetFormatter())
call s:registry.AddExtension(codefmt#buildifier#GetFormatter())
call s:registry.AddExtension(codefmt#googlejava#GetFormatter())
call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
call s:registry.AddExtension(codefmt#hindent#GetFormatter())
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" codefmt
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:plugin = maktaba#plugin#Get('codefmt')
call s:plugin.Flag('hindent_indent_size', '4')
call s:plugin.Flag('hindent_line_length', '100')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment