【问题标题】:Using Uncrustify with VIM在 VIM 中使用 Uncrustify
【发布时间】:2012-09-04 15:33:48
【问题描述】:

在我的 vimrc 中,我通过以下命令调用 Uncrustify:

%!uncrustify -l CPP -c D:\uncrustify\default.cfg

在某些代码之后,我得到一个 Windows 致命错误:

但是当我在控制台中使用 -f 选项对同一代码调用 uncrustify 时,没有错误。

如何更改我的 vimrc 以避免将来出现此类错误?什么会引发这个错误?

【问题讨论】:

  • 错误是什么?无论如何,我的钱是在控制台中不同的 PATH 环境变量上。

标签: vim uncrustify


【解决方案1】:

除了@Alexander Shukaev 的回答,添加以下内容将检查 uncrustify 配置的正确性,如果检测到错误,则不会自动格式化:

  let output = system('uncrustify -q -c ' . a:cfgfile)

  if v:shell_error != 0
      echo output
  endif

  return v:shell_error
endfunction

" Don't forget to add Uncrustify executable to $PATH (on Unix) or 
" %PATH% (on Windows) for this command to work.
function! Uncrustify(language)
  if CheckUncrustifyCfg(g:uncrustify_cfg_file_path)
      echo "Config file" g:uncrustify_cfg_file_path "has errors"
      echo "No formatting will be performed"
      return
  endif

  call Preserve(':silent %!uncrustify'
      \ . ' -q '
      \ . ' -l ' . a:language
      \ . ' -c ' . g:uncrustify_cfg_file_path)
endfunction

【讨论】:

    【解决方案2】:

    我发现将以下代码放入您的 .vimrc 就足够了:

    let g:uncrustifyCfgFile = '~/.uncrustify.cfg'
    function! UncrustifyFunc(options) range
        exec a:firstline.','.a:lastline.'!uncrustify '.a:options
                    \.' -c '.g:uncrustifyCfgFile.' -q -l '.&filetype
    endfunction
    
    command! -range=% UncrustifyRange <line1>,<line2>call UncrustifyFunc('--frag')
    command! Uncrustify  let s:save_cursor = getcurpos() 
                      \| %call UncrustifyFunc('') 
                      \| call setpos('.', s:save_cursor)
    

    请注意,这确实假设您的 $PATH 中有“uncrustify”二进制文件。

    它还假设您的配置文件是 ~/.uncrustify.cfg 但是您可以通过修改 g:uncrustifyCfgFile 变量来更改它。

    调用运行

    :Uncrustify
    

    它也适用于范围(这是促使我制作此功能的原因)。视觉选择示例:

    :'<,'>UncrustifyRange
    

    我只对 C、CPP 和 JAVA 感到厌烦(我认为其他人也可以)

    【讨论】:

      【解决方案3】:

      为了将 Uncrustify 与 Vim 正确集成,请将以下内容添加到您的 .vimrc

      " Restore cursor position, window position, and last search after running a
      " command.
      function! Preserve(command)
        " Save the last search.
        let search = @/
      
        " Save the current cursor position.
        let cursor_position = getpos('.')
      
        " Save the current window position.
        normal! H
        let window_position = getpos('.')
        call setpos('.', cursor_position)
      
        " Execute the command.
        execute a:command
      
        " Restore the last search.
        let @/ = search
      
        " Restore the previous window position.
        call setpos('.', window_position)
        normal! zt
      
        " Restore the previous cursor position.
        call setpos('.', cursor_position)
      endfunction
      
      " Specify path to your Uncrustify configuration file.
      let g:uncrustify_cfg_file_path =
          \ shellescape(fnamemodify('~/.uncrustify.cfg', ':p'))
      
      " Don't forget to add Uncrustify executable to $PATH (on Unix) or 
      " %PATH% (on Windows) for this command to work.
      function! Uncrustify(language)
        call Preserve(':silent %!uncrustify'
            \ . ' -q '
            \ . ' -l ' . a:language
            \ . ' -c ' . g:uncrustify_cfg_file_path)
      endfunction
      

      现在您可以将此函数 (Uncrustify) 映射到组合键,也可以使用我使用的方便技巧。创建一个文件~/.vim/after/ftplugin/cpp.vim,您可以在其中覆盖任何 Vim 设置,特别是针对 C++ 的设置,并在其中添加以下行:

      autocmd BufWritePre <buffer> :call Uncrustify('cpp')
      

      这基本上添加了一个预保存挂钩。现在,当您使用 C++ 代码保存文件时,Uncrustify 将使用您之前提供的配置文件自动对其进行格式化。

      例如,Java 也可以这样做:在~/.vim/after/ftplugin/java.vim 中添加:

      autocmd BufWritePre <buffer> :call Uncrustify('java')
      

      你明白了。

      注意:这里介绍的所有内容都经过了很好的测试,并且我每天都在使用。

      【讨论】:

      • 当 uncrustify.cfg 中存在语法错误时,此处的实现似乎无法正确处理。
      猜你喜欢
      • 2011-12-05
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      相关资源
      最近更新 更多