From 6f57baec8cb40c19796fe56e61bd2df279046883 Mon Sep 17 00:00:00 2001 From: Theodore Dubois Date: Sat, 6 Jul 2019 14:49:56 -0700 Subject: [PATCH 1/2] Use vim's builtin format mapping --- autoload/codefmt.vim | 27 +++++++++++++++++++++++++++ plugin/mappings.vim | 11 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index b72978f..e0ef761 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -246,6 +246,16 @@ function! codefmt#FormatMap(type) range abort call codefmt#FormatLines(line("'["), line("']")) endfunction +"" +" @public +" To map the builtin |gq| command to invoke codefmt, set 'formatexpr' to call +" this function. Example: > +" set formatexpr=codefmt#FormatExpr() +" < +function! codefmt#FormatExpr() abort + call codefmt#FormatLines(v:lnum, v:lnum + v:count) +endfunction + "" " Generate the completion for supported formatters. Lists available formatters " that apply to the current buffer first, then unavailable formatters that @@ -260,6 +270,23 @@ function! codefmt#GetSupportedFormatters(ArgLead, CmdLine, CursorPos) abort return join(l:groups[0] + l:groups[1] + l:groups[2], "\n") endfunction +"" +" Returns whether there is a default formatter available for the current +" buffer. +function! codefmt#AvailableInCurrrentBuffer() abort + let l:formatters = s:registry.GetExtensions() + if !empty(get(b:, 'codefmt_formatter')) + let l:Predicate = {f -> f.name ==# b:codefmt_formatter} + else + let l:Predicate = {f -> f.AppliesToBuffer() && s:IsAvailable(f)} + endif + for l:formatter in s:registry.GetExtensions() + if l:Predicate(l:formatter) + return 1 + endif + endfor + return 0 +endfunction "" " @private diff --git a/plugin/mappings.vim b/plugin/mappings.vim index 535a25a..8606c48 100644 --- a/plugin/mappings.vim +++ b/plugin/mappings.vim @@ -59,3 +59,14 @@ execute 'nnoremap ' s:prefix . '=' ':FormatLines' " Format the visually selected region using the formatter associated with the " current buffer. execute 'vnoremap ' s:prefix ':FormatLines' + +function! s:SetFormatExpr() abort + if codefmt#AvailableInCurrrentBuffer() + setlocal formatexpr=codefmt#FormatExpr() + endif +endfunction + +augroup codefmt_formatexpr + autocmd! + autocmd BufEnter * call SetFormatExpr() +augroup END From 71d3b5581e64cb58caad49739e2d3b2fcea99455 Mon Sep 17 00:00:00 2001 From: Theodore Dubois Date: Sun, 1 Mar 2020 23:04:45 -0800 Subject: [PATCH 2/2] Remove autocmd --- plugin/mappings.vim | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/plugin/mappings.vim b/plugin/mappings.vim index 8606c48..535a25a 100644 --- a/plugin/mappings.vim +++ b/plugin/mappings.vim @@ -59,14 +59,3 @@ execute 'nnoremap ' s:prefix . '=' ':FormatLines' " Format the visually selected region using the formatter associated with the " current buffer. execute 'vnoremap ' s:prefix ':FormatLines' - -function! s:SetFormatExpr() abort - if codefmt#AvailableInCurrrentBuffer() - setlocal formatexpr=codefmt#FormatExpr() - endif -endfunction - -augroup codefmt_formatexpr - autocmd! - autocmd BufEnter * call SetFormatExpr() -augroup END