如何检查 ALE 是否找到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
文件,但我没有看到任何方法来检查它是否成功?

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

查看 ALE 的源代码,发现他们使用

ale#c#FindCompileCommands
函数来获取
compile_commands.json
文件。如果找不到文件,该函数会返回
['','']
,因此如果我们检查返回值,我们就可以判断 ALE 是否找到了该文件。

使用该函数的示例实现可能与此类似

function s:apply_cc_options (buffer)
    let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)

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

endfunction

autocmd BufReadPost * call s:apply_cc_options(bufnr(''))
© www.soinside.com 2019 - 2024. All rights reserved.