vimneovimvim-pluginvim-ale

How to check if ALE found a compile_commands.json file?


I am using neovim with the ALE plugin for linting.

I would like to set the g:ale_c_cc_options variable differently depending on if ALE finds a compile_commands.json file to use. If it does find the file, I'd like it to run g:ale_c_cc_options = '', that way it only uses the flags defined in the file. Then, if it cannot find the file, I'd like it to use g:ale_c_cc_option = '-ansi -pedantic -Wall' as a default option.

Is there anyway to check if ALE succeeds in finding a compile_commands.json file, using vimscript?

Something akin to this, possibly?

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

I'd checked the :help ale-c-options manual page, and it had mentioned g:ale_c_parse_compile_commands, to enable trying to find a compile_commands.json file, but I didn't see any way to check if it succeeds or not?


Solution

  • Went through the source code for ALE, found that they were using the ale#c#FindCompileCommands function to to get the compile_commands.json file. The function returns ['',''] if it cannot find the file, so if we check the return we can tell if ALE found the file or not.

    An example implementation using the function may look similar to this

    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(''))