JetBrains Rider移除delegate与括号间空格的配置问题
解决Rider与Visual Studio 2022中delegate括号空格不一致的问题
问题背景
团队成员分别使用JetBrains Rider和Visual Studio 2022,执行代码清理时出现格式冲突:
- VS 2022会在
delegate与()之间添加空格:var cat = this.ExecuteAFunction("Function Name Here", delegate () {}); - Rider会自动移除该空格:
var cat = this.ExecuteAFunction("Function Name Here", delegate() {});
之前尝试调整Rider里Editor > Code Style > C#下的Before Other Parentheses -> Method call parentheses,但该设置会影响所有方法,无法单独针对delegate配置。且公司编码规范要求必须保留delegate () {}写法,不能替换为箭头函数。
解决方案
方法一:Rider图形界面单独配置
- 打开Rider设置(快捷键
Ctrl+Alt+S,或通过File → Settings) - 导航到
Editor > Code Style > C# > Braces, Parentheses and Brackets - 切换到
Parentheses标签页,找到Anonymous method declaration选项,将Before opening parenthesis设置为Add space - 点击
Apply和OK,之后Rider执行代码清理时会保留delegate与()之间的空格,与VS2022行为一致。
方法二:用.editorconfig统一团队规范
在项目根目录的.editorconfig文件中添加以下配置,确保Rider和VS2022遵循同一格式:
# 匿名方法的delegate与括号之间添加空格 csharp_space_before_parentheses_in_anonymous_method_declaration = true
该配置仅作用于匿名方法的delegate关键字后,不会影响普通方法调用的括号格式,完美解决之前设置范围过广的问题。
内容的提问来源于stack exchange,提问作者t3mp0ralflux
相关产品推荐
相关产品推荐

