nvim: after/ftplugin/cabal: Add cabalfmt plugin

Since we reverted cabal-fmt support via HLS in the previous commit
add the cabalfmt plugin. Just add it to our own ftplugin instead of
adding via the plugin list.
This commit is contained in:
Sanchayan Maity 2023-04-25 13:49:45 +05:30
parent 5bba0b1789
commit 95cb836e8d
Signed by: sanchayanmaity
GPG Key ID: 6F6A0609C12038F3
1 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,55 @@
" Taken from https://github.com/sdiehl/vim-cabalfmt
if !exists("g:cabalfmt_command")
let g:cabalfmt_command = "cabal-fmt"
endif
if !exists("g:cabalfmt_options")
let g:cabalfmt_options = [""]
endif
function! s:OverwriteBuffer(output)
if &modifiable
let l:curw = winsaveview()
try | silent undojoin | catch | endtry
let splitted = split(a:output, '\n')
if line('$') > len(splitted)
execute len(splitted) .',$delete'
endif
call setline(1, splitted)
call winrestview(l:curw)
else
echom "Cannot write to non-modifiable buffer"
endif
endfunction
function! s:RunCabal()
if exists("bufname")
let output = system(g:cabalfmt_command . " " . join(g:cabalfmt_options, ' ') . " " . bufname("%"))
else
let stdin = join(getline(1, '$'), "\n")
let output = system(g:cabalfmt_command . " " . join(g:cabalfmt_options, ' '), stdin)
endif
if v:shell_error != 0
echom output
else
call s:OverwriteBuffer(output)
if exists("bufname")
write
endif
endif
endfunction
function! s:CabalHaskell()
if executable(g:cabalfmt_command)
call s:RunCabal()
else
echom "cabal-fmt executable not found"
endif
endfunction
function! RunCabal()
call s:CabalHaskell()
endfunction
nnoremap <buffer> <silent> gq :call RunCabal()<CR>