vim-autoformat / vim-autoformat

Provide easy code formatting in Vim by integrating existing code formatters.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why not find .astylerc file recursively?

SilverRainZ opened this issue · comments

As I see, vim-autoformat only find .astylerc under PWD and HOME, but most project put their .astylerc file under project root.
Neither PWD nor HOME. Is there any reason to do so?

https://github.com/Chiel92/vim-autoformat/blob/2a6f931987c1cc5e5bc0c4c44f21ac9bd4c72f3b/plugin/defaults.vim#L93-L102

I wrote the following snippet, can recursively find astylerc:

function! FindAstylerc()
    let l:astylerc = '.astylerc'
    let l:cur = '%:p:h'
    let l:home = expand('~/' . astylerc)
    let l:root = '/' . astylerc
    let l:found = 0

    while 1
        let f = expand(cur) . '/' . astylerc
        if filereadable(f)
            let found = 1
            break
        endif
        if (f == home) || (f == root)
            break
        endif
        let cur = cur . ':h' " To upper directory
    endwhile

    if !found
        return ''
    else
        return f
    endif
endfunction

let astylerc = FindAstylerc()
if filereadable(astylerc)
    let g:formatdef_my_astyle_c = '"astyle --mode=c --options=' . astylerc . '"'
    let g:formatters_c = ['my_astyle_c']
    let g:formatters_h = ['my_astyle_c']
    let g:formatters_cpp = ['my_astyle_c']
endif

Is there any reason to do so?

The reason is that the person that introduced this limited his contribution to the current functionality. If you that it needs improvement, feel free to provide a PR.