Resharper代码清理在插值字符串中添加空格问题求助
ReSharper代码清理导致插值字符串被添加空格的解决办法
使用ReSharper的代码清理功能时,遇到了一个问题:editorconfig中的某些设置冲突,导致插值字符串里被自动添加了空格,破坏了预期的输出结果。这个问题在Rider里不会出现,而且已经排除了VS自带代码清理的影响(已关闭VS的代码清理功能),求无需放弃自动格式化的解决设置方案。

以下是我的editorconfig部分配置:
resharper_space_in_singleline_accessorholder = false resharper_space_in_singleline_anonymous_method = false resharper_space_in_singleline_method = false resharper_space_near_postfix_and_prefix_op = false resharper_space_within_array_initialization_braces = false resharper_space_within_array_rank_empty_parentheses = false resharper_space_within_array_rank_parentheses = false resharper_space_within_attribute_angles = false resharper_space_within_checked_parentheses = false resharper_space_within_default_parentheses = false resharper_space_within_empty_braces = false resharper_space_within_empty_initializer_braces = false resharper_space_within_empty_invocation_parentheses = false resharper_space_within_empty_method_parentheses = false resharper_space_within_empty_template_params = false resharper_space_within_expression_parentheses = false resharper_space_within_initializer_braces = false resharper_space_within_invocation_parentheses = false resharper_space_within_method_parentheses = false resharper_space_within_nameof_parentheses = false resharper_space_within_new_parentheses = false resharper_space_within_parentheses = false resharper_space_within_single_line_array_initializer_braces = false resharper_space_within_sizeof_parentheses = false resharper_space_within_template_args = false resharper_space_within_template_params = false resharper_space_within_tuple_parentheses = false resharper_space_within_typeof_parentheses = false resharper_space_within_type_argument_angles = false resharper_space_within_type_parameter_angles = false resharper_space_within_type_parameter_parentheses = false
可行的解决方法
添加插值表达式专属空格设置
现有配置遗漏了插值字符串的特定空格控制项,在editorconfig中添加以下配置:resharper_space_within_interpolation_expression = false这个设置专门控制插值表达式
${}内部是否添加空格,设置为false就能避免自动插入空格破坏格式。排查配置冲突
如果添加上述配置后问题仍存在,可以暂时注释掉现有所有与resharper_space_within_*相关的配置,然后逐个恢复并测试代码清理效果,定位到冲突的具体配置项。核对ReSharper可视化设置
打开ReSharper的设置面板(Extensions -> ReSharper -> Options),进入Code Editing -> C# -> Formatting Style -> Spaces -> Within,找到"Interpolation expression"选项,确保未勾选,再同步到editorconfig中。
内容的提问来源于stack exchange,提问作者xkk
相关产品推荐
相关产品推荐

