28

Is there a way to prevent ReSharper from formatting a specific code block in a file? Something like:

void MyMethod ()
{
    // ReSharper disable formatting
    PRE     =   { my top format }   /* no rules */   ;
    // ReSharper enable formatting
}
D.R.
  • 20,268
  • 21
  • 102
  • 205
  • Does this answer your question? [Is there a way to keep ReSharper from formatting a region of code](https://stackoverflow.com/questions/2090354/is-there-a-way-to-keep-resharper-from-formatting-a-region-of-code) – SWdV May 05 '21 at 20:45

2 Answers2

41

Now it is possible in version 2017.3 like this:

// @formatter:off — disable formatter after this line
// @formatter:on — enable formatter after this line
Timur Lemeshko
  • 2,747
  • 5
  • 27
  • 39
  • Where should I put it? I have a file with dapper code, sql-source intermixed with function that uses it. I do not want code R# clean-up to move all sql-strings to the top of the file. Tried putting "// @formatter:off — disable formatter after this line" early in the file. No difference. Code-cleanup reorders everything. – mattias Apr 03 '18 at 16:07
  • @mattias Please ask JetBrains via their YouTrack issue tracker and report back how it's done for non-C# files : ) – D.R. Jul 09 '18 at 15:56
  • This did not work for me...? I have R# 2019.2.1. I use CTRL-K+CTRL-D to format my code. – Jess Feb 06 '20 at 22:12
  • 1
    @Jess Ctrl+K, D is the Visual Studio formatter. The above will only apply to Resharper. – Iain Holder Apr 16 '20 at 11:18
  • It's unfortunately not working on XML (XSLT in my case) files, although HTML is supported per https://www.jetbrains.com/help/resharper/Enforcing_Code_Formatting_Rules.html#using-comments-to-configure-formatter – Marcel Dec 01 '20 at 07:19
  • 3
    The link is outdated; the current one (as of 2022) is [Use comments to configure formatter](https://www.jetbrains.com/help/resharper/Configure_Code_Formatting_Rules.html#using-comments-to-configure-formatter). – Reyhn Mar 31 '22 at 09:47
  • @Reyhn thks, i've updated link – Timur Lemeshko Apr 01 '22 at 10:58
8

At the moment this is not possible. See feature request RSRP-187963.

As a workaround, if you have a big chunk of "pre-formatted" code, you could move it to an own (partial class) file and add it to the "Generated Code" configuration list. This disables R#'s "Code Cleanup" for this file. Note that it also disables the inspections! (Unfortunately the "Generated Code Region" feature only disables the inspections, not the "Code Cleanup" for a region, as of R# version 9.2.)

ulrichb
  • 19,610
  • 8
  • 73
  • 87