有没有办法强制使用制表符而不是空格?
StyleCop 提供检查空格使用的一致性,但遗憾的是缺乏相反的想法:强制源代码使用制表符。有什么方法可以添加这个功能吗?不一定是 StyleCop,其他工具也可以。
StyleCop offers to check for consistent use of spaces, but sadly lacks the opposite idea: Force source code to use tabs. Is there some way to add this functionality? It does not have to be StyleCop, other tools are welcome as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我也是一个不使用制表符而不是空格的人,尽管有很多理由使用其中任何一种,并且还有其他地方可以解释您为什么认为其中一种比另一种更好。 :)
我实际上想要同样的东西 - 一个检查制表符缩进的规则 - 所以我根据 StyleCop 的 SpacingRules 源代码编写了它。它似乎工作得相当好,尽管到目前为止我只在几个项目中使用过它。它可能可以优化或其他什么......但它有效。
请注意,您还必须在程序集中将嵌入的 XML 文件“SpacingRules.xml”与该文件一起放置。 (有关更多信息,请阅读 StyleCop SDK 文档。)
I'm a tabs-not-spaces person, too, though there are plenty of reasons to use either one and there are other places to get into why you think one is better than the other. :)
I actually wanted the same thing - a rule to check for tab indents - so I wrote it based on the SpacingRules source from StyleCop. It seems to work reasonably well, though I've only used it on a few projects so far. It could probably be optimized or whatever... but it works.
Note that you also have to have the embedded XML file "SpacingRules.xml" in the assembly alongside this thing. (Read the StyleCop SDK doc for more on that.)
您可以使用 StyleCop+ 插件强制使用选项卡。
下载
StyleCopPlus.dll
后,将其放入 StyleCop 主文件夹C:\Program Files (x86)\StyleCop 4.7\Custom Rules
内的Custom Rules
文件夹中> 或直接在主文件夹中。现在,当使用
StyleCopSettingsEditor
打开Settings.StyleCop
时,您将能够设置规则SP2001:CheckAllowedIndentationCharacters
。此规则可以在
StyleCop+
选项卡下的更多自定义规则
子选项卡下的Formatting
标题下找到:You can use StyleCop+ plugin to enforce usage of tabs.
After downloading
StyleCopPlus.dll
place it inCustom Rules
folder inside the main StyleCop folderC:\Program Files (x86)\StyleCop 4.7\Custom Rules
or directly in the main folder.Now, when opening a
Settings.StyleCop
withStyleCopSettingsEditor
you will be able to set ruleSP2001: CheckAllowedIndentationCharacters
.This rule can be found under the
StyleCop+
tab, under theMore Custom Rules
subtab, under theFormatting
heading:假设您使用 Visual Studio 作为 IDE,并且您的队友接受这个想法,您可以做的一件事是将 VS 设置为使用制表符而不是空格,导出并共享设置文件。
该设置可以在“工具”>“工具”下找到。选项>文本编辑器>所有语言(或您希望使用的语言)>选项卡,然后在右侧您可以选择“插入空格”或“保留选项卡”。
要从视觉工作室导出设置:工具>导入和导出设置>导出选定的环境设置>选择“选项”
只是一个想法 - 但说实话,真正的问题似乎是队友的支持。否则他们总是可以恢复到他们的设置。或者,按照 Sam 的建议,在办理入住手续时,您可以进行一些自动重新格式化。
华泰
One thing you could do, assuming you are using Visual Studio as your IDE, and that your team-mates buy-in to this idea, would be to set VS to use tabs instead of spaces, export and share the settings file.
The setting can be found under Tools > Options > Text Editor > All Languages (or the language you wish to use) > Tabs and then on the right hand side you can pick to 'Insert Spaces' or 'Keep Tabs'.
To export the settings from your visual studio: Tools > Import and Export Settings > Export selected environment settings > select the 'Options'
Just a thought - but to be honest the real problem seems to be the buy-in from your team-mates. They can always revert back to their settings otherwise. Alternatively, upon check-in, as Sam suggested, you can do some automated re-formatting.
HTH
包方法:
目前的趋势似乎是通过 nuget 包来做到这一点(并且经典的 StyleCop 可能会在某个时候被淘汰)。因此,要对包执行此操作,请执行以下操作:
通过 nuget:
请注意对预发行版的引用(此时),选项卡的设置仅在测试版中可用。
将以下代码作为 ca.ruleset 添加到您的项目中:
通过编辑 .csproj 文件并添加以下内容,将其添加到您的项目文件中:
要覆盖选项卡(和其他)设置,您需要将 stylecop.json 文件添加到您的项目中。在文件属性中,将构建操作设置为“(分析器)附加文件”。根据项目类型,实际的“分析器”一词可能不存在。
将 stylecop.json 文件编辑为如下所示:
至少对于 .NET 标准项目,您需要确保 csproj 文件中包含以下内容(并且没有对文件的其他引用):
您可能必须重新加载项目和包以使它们识别 stylecop.json 文件。
参考文献:
DotNetAnalyzers/StyleCopAnalyzers
.NET Core、代码分析和 StyleCop
The package approach:
It seems that the current trend is to do this through nuget packages (and that the classic StyleCop may be phased out at some point). So, to do this with the packages, do the following:
via nuget:
Notice the reference to the pre-releaset (at this time), the settings for tabs are only available in the beta.
add the following code to your project as ca.ruleset:
Add it to your project file by editing the .csproj file and adding:
To override the tab (and other) settings, you will need to add a stylecop.json file to your project. In the file properties, set the Build Action to '(analyzer) additional file'. Depending on the project type, the actual word 'analyzer' may not be present.
Edit the stylecop.json file to something like the following:
At least for .NET standard projects, you need to insure that the following is in the csproj file (and no other references to file):
You may have to reload the project and the packages to get them to recognize the stylecop.json file.
References:
DotNetAnalyzers/StyleCopAnalyzers
.NET Core, Code Analysis and StyleCop
StyleCop 支持创建自定义规则,因此您可以添加自己的“使用前导制表符而不是空格”规则。如果您不想尝试开发自己的规则,则可以在 http://stylecopcontrib 获取现有规则。 codeplex.com/ 或 http://github.com/AArnott/nerdbank.stylecop .规则。
StyleCop supports creation of custom rules, so you can add your own "use leading tabs instead of spaces" rule. If you don't feel like trying to develop your own, you can get an existing rule at either http://stylecopcontrib.codeplex.com/ or http://github.com/AArnott/nerdbank.stylecop.rules.
好的,我设法找出问题所在,因为出现此警告的原因是,
如果您使用的是 VS2010, 开发人员有时会复制并粘贴代码
转到解决方案资源管理器
检查设置风格警察
然后修改 Cop 样式内的设置,例如禁用或取消选中设置[Spacing.....]
Ok I managed to figure out the problem because the reason why this warning appear, It appear because the developer sometimes copy and paste the code
if you are using VS2010
Go to solution explorer
Check in Setting Style Cop
then modify the setting inside the style Cop e.g disable or uncheck the setting[Spacing.....]
在源代码控制服务器中执行此操作。使用预提交脚本检查文件中是否存在以多个空格开头的行并阻止提交。
我同意制表符比空格更好。这是个人喜好,但团队的一致性非常重要。
Do it within your source control server. Use a pre-commit script to check the file for lines starting with multiple spaces and prevent the commit.
I agree tabs are better than spaces. It's a personal preference, but team consistency is very important.