使用什么变量来检查ALE(vim插件)是否成功找到compile_commands.json文件?

问题描述 投票:0回答:1

我正在使用 neovim 和 ALE 插件进行 linting。

我想根据 ALE 是否找到要使用的

g:ale_c_cc_options
文件来不同地设置
compile_commands.json
变量。如果它确实找到了该文件,我会运行
g:ale_c_cc_options = ''
,这样它只使用文件中定义的标志。然后,如果它找不到该文件,我希望它使用
g:ale_c_cc_option = '-ansi -pedantic -Wall'
作为默认选项。

有没有办法使用 vimscript 检查 ALE 是否成功找到

compile_commands.json
文件?

可能有类似的东西吗?

if g:ale_found_compile_commands_file
    let g:ale_c_cc_options = ''
else
    let g:ale_c_cc_options = '-ansi -pedantic -Wall'
endif

我检查了

:help ale-c-options
手册页,它提到了
g:ale_c_parse_compile_commands
,以启用尝试查找
compile_commands.json
文件,但我没有看到任何方法来检查它是否成功?

编辑:

通读了一些 ALE 源代码,找到了一个可行的解决方案,感觉很混乱,但似乎有效 =DD

这是我正在使用 atm 的解决方案

function s:apply_cc_options (buffer)

    " try to find 'compile_commands.json' file
    let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)

    " set values based on whether it's found or not
    if l:json_file==''
        let g:ale_c_cc_options = '-ansi -pedantic -Wall'
    else
        let g:ale_c_cc_options = ''
    endif

endfunction

" check for each opened file
autocmd BufReadPost * call s:apply_cc_options(bufnr(''))

vim neovim vim-plugin
1个回答
0
投票

我读了一些ALE源代码,找到了一个似乎有效的解决方案,感觉很乱,但似乎有效=DD

这是我正在使用 atm 的解决方案

function s:apply_cc_options (buffer)

    " try to find 'compile_commands.json' file
    let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)

    " set values based on whether it's found or not
    if l:json_file==''
        let g:ale_c_cc_options = '-ansi -pedantic -Wall'
    else
        let g:ale_c_cc_options = ''
    endif

endfunction

" check for each opened file
autocmd BufReadPost * call s:apply_cc_options(bufnr(''))

© www.soinside.com 2019 - 2024. All rights reserved.