我使用 neovim 和 ALE 插件进行 linting。
我想根据 ALE 是否找到要使用的 compile_commands.json
文件来设置不同的 g:ale_c_cc_options
变量。如果它确实找到了该文件,我希望它运行 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 的源代码,发现他们使用 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(''))
关于vim - 如何检查 ALE 是否找到了compile_commands.json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77655147/