如何让vim对齐三元组?:运算符很好吗?

Fre*_*abe 9 c++ vim indentation

我喜欢使用三元?:运算符编写代码:

std::string result = input.empty() ? createNewItem()
                                   : processInput( input );
Run Code Online (Sandbox Code Playgroud)

我如何配置vim,以便在键入createNewItem()缩进后按下返回行,以便光标与最后一行在同一列中,?以便我可以继续键入: processInput( input );

我试着查看cinoptions-values设置,但我没有看到任何相关内容.

Fre*_*abe 1

受到一个大致相似的问题的启发,我运用了 vimscript-fu 并创建了一个小脚本来完成这项工作:

if (!exists("*CppIndentDepth"))
    function CppIndentDepth()
        let lineno = v:lnum
        let lastQuestionMark = match(getline(lineno-1), "?[^?]*")
        if lastQuestionMark != -1
            return lastQuestionMark
        endif
        return cindent(lineno)
    endfunction
endif

set indentexpr=CppIndentDepth()
Run Code Online (Sandbox Code Playgroud)

我将此文件保存为vimfiles/indent/after/cpp.vim并添加filetype indent on到我的.vimrc以切换缩进插件的加载。看起来效果已经足够好了!