简体   繁体   English

Visual C ++ 2010中的预处理程序指令缩进

[英]Preprocessor directives indentation in Visual C++ 2010

I often find myself in situations where I would like to indent preprocessor directives like the rest of the code (eg #if indented like if ). 我经常发现自己在哪里,我想缩进预处理指令,如代码的其余部分情况(例如#if缩进像if )。 It seems legal , it's common sense that it's sometimes a good thing , but Visual won't make it easy. 似乎是合法的 ,这是常识,它有时是一件好事 ,但Visual不会让它变得容易。

Is there a way to prevent the Visual C++ 2010 editor from sticking the code to the left each time I innocently press # ? 有没有办法阻止Visual C ++ 2010编辑器每次我无辜地按#时将代码粘贴到左边? And to not break preprocessor directives indentation with auto-indent (CTRL+K, CTRL+F)? 并且不要使用自动缩进(CTRL + K,CTRL + F)来破坏预处理程序指令缩进? Or even better, to handle preprocessor directives like everything else? 或者更好的是,像其他一样处理预处理器指令?

My approach is to keep the # in the first column and indent the subsequent word, as in: 我的方法是将#保留在第一列中并缩进后续单词,如下所示:

#ifdef FIRST
#  include "first.h"
#else
#  include "second.h"
#endif

In the Visual Studio 2010 options (Tools->Options) 在Visual Studio 2010选项中(工具 - >选项)

Go to Text Editor -> C/C++ -> Tabs 转到文本编辑器 - > C / C ++ - >选项卡

Under indenting select Block instead of smart. 在缩进下选择Block而不是smart。

This will prevent the # key from forcing you to the start of the line, however if you use Ctrl+K Ctrl+F it will still apply smart formatting and remove the tabs. 这将阻止#键强制您到行的开头,但是如果您使用Ctrl + K Ctrl + F,它仍将应用智能格式并删除选项卡。

Edit: This will also disable automatic indenting/unindenting in other places. 编辑:这也将禁用其他地方的自动缩进/取消。 Beware. 谨防。

At some point visualstudio (checked in vs2015) acquired options > text editor > c/c++ > formatting > indentation > position of preprocessor directives. 在某些时候,visualstudio(在vs2015中检查)获得了选项>文本编辑器> c / c ++>格式化>缩进>预处理器指令的位置。 Choose 'leave indented'. 选择“保留缩进”。 The sample doesn't look exactly like what you want, but it works, just try it. 样本看起来并不像你想要的那样,但它可以正常运行。

解决方法:当您第一次键入#并且Visual Studio删除缩进时,请按ctrl + z以撤消自动格式设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM