Vim 在 C++ class 中的 formatting/auto-indent 似乎不正确。这是正常的吗?
Vim's formatting/auto-indent in C++ class seems incorrect. is this normal?
Vim 中似乎有很多选项可以使用 cindent 和 cinoptions 变量缩进 c 代码。我没有对这些设置进行任何修改,并且我已经通过全新安装 Vim 并且没有 vimrc 来测试它。我似乎找不到以下行为的解释:
如果在 class 正文的第一行使用 public,则缩进格式似乎正确。像这样使用 gg=G 格式化正文:
class Thing
{
public:
int a;
};
但是,如果上面有内容,vim 不会缩进 public 关键字后的行。像这样使用 gg=G 格式化正文:
class Thing
{
int b;
public:
int a; //WHY is there no additional indentation level anymore???
};
有谁知道为什么会这样?这让我发疯。
谢谢!
安装 clang-format 并使用 autocmd 解决了我的问题。我想应用于 c 或 c++ 文件的任何新设置都可以简单地添加到 CPPFormatSettings 函数中。 Google 的格式对我来说最有意义,但还有 5 或 6 种其他样式可供选择。更改为 -style=microsoft 会将代码格式化为看起来更像经典的 C++ 格式,并在函数定义下方加上方括号。
这是我添加到我的 vimrc 中的内容:
fun! CPPFormatSettings()
setlocal equalprg=clang-format\ -style=google
endfun
autocmd FileType c,cpp call CPPFormatSettings()
Vim 中似乎有很多选项可以使用 cindent 和 cinoptions 变量缩进 c 代码。我没有对这些设置进行任何修改,并且我已经通过全新安装 Vim 并且没有 vimrc 来测试它。我似乎找不到以下行为的解释:
如果在 class 正文的第一行使用 public,则缩进格式似乎正确。像这样使用 gg=G 格式化正文:
class Thing
{
public:
int a;
};
但是,如果上面有内容,vim 不会缩进 public 关键字后的行。像这样使用 gg=G 格式化正文:
class Thing
{
int b;
public:
int a; //WHY is there no additional indentation level anymore???
};
有谁知道为什么会这样?这让我发疯。
谢谢!
安装 clang-format 并使用 autocmd 解决了我的问题。我想应用于 c 或 c++ 文件的任何新设置都可以简单地添加到 CPPFormatSettings 函数中。 Google 的格式对我来说最有意义,但还有 5 或 6 种其他样式可供选择。更改为 -style=microsoft 会将代码格式化为看起来更像经典的 C++ 格式,并在函数定义下方加上方括号。
这是我添加到我的 vimrc 中的内容:
fun! CPPFormatSettings()
setlocal equalprg=clang-format\ -style=google
endfun
autocmd FileType c,cpp call CPPFormatSettings()