Code formatting - help needed

This is how Visual Studio 2012 currently formats my code. This is formatting is applied if paste new code in, or if I explicitly choose to format the document using VS shortcut Ctrl K,D.

    private static void MyFunc()
    {
        var ints = new List<int>();
        var diffInts = ints.Select(x => new
        {
            Prop1 = x,
            Prop2 = x * x
        })
                           .ToList();

        diffInts.ForEach(x =>
        {
            if (x.Prop2 > x.Prop1)
            {
                Console.WriteLine("Whatever!");
            }
        });
    }

However I have configured R# (7.1.latest) to format the code as below

    private static void MyFunc()
    {
        var ints = new List<int>();
        var diffInts = ints.Select(x => new
                                        {
                                            Prop1 = x,
                                            Prop2 = x*x
                                        })
                           .ToList();

        diffInts.ForEach(x =>
                         {
                             if (x.Prop2 > x.Prop1)
                             {
                                 Console.WriteLine("Whatever!");
                             }
                         });
    }

R# kicks in its formatting only if I terminate a statement (ie using `;`) or if I terminate a block (ie using `}`). Otherwise I need to explicitly format the document using the R# shortcut Ctrl+Atl F.

This is quite tedious as I frequently move code around, the code looses all formatting (ie resets to VS style formatting).

What can I do to get rid of this annoyance? I presume I will need to share some R# settings values as well as some VS settings values. Please let me know which ones (and if possible, in which sub section I can find those settings)

6 comments

Hi,

I'm sorry but I didn't understand the question well - would you like to keep the ReSharper formatting (code sample #2) in all scenarios? If so, could you please describe in detail, when and after what actions the formatting is set to the VS one (like, what do you mean by 'move code around')?

Thanks in advance!

0

Yes, I would like to retain R# code formatting in all cases.

By 'move code around', I mean manually cut and then paste the code.

VS2012 has the following settings.
vs-2012-formatting.png

As I mentioned in the original post, on completing a statement or a block, R# formatting is used and not VS formatting. However, when code is pasted, VS formatting is used and not R#.

Since posting this thread, I have now disabled the third option. This way, if I paste code that adheres to R# formatting, it gets pasted as is. However if I paste code that doesn't adhere to R# formatting, R# doesn't automatically format that code.

In short, R# does not "Automatically format on paste".

0

Hello Amith,

It took some time to check it out, and I beleive there's no option in ReSharper for now. However, you may map 'CodeCleanupSilent' command to any keymap you'd like and select 'Refomat Code' for a silent code cleanup profile. At least it will allow you reformat code more easily after pasting.

Hope it helps.
Thanks!

0

However, you may map 'CodeCleanupSilent' command to any keymap you'd like and select 'Refomat Code' for a silent code cleanup profile. At least it will allow you reformat code more easily after pasting.


I didn't fully understand this. I paste code using the keyboard shortcut Ctrl V. Should I map `CodeCleanupSilent` to Ctrl V? Will that still paste AND reformat?

It seems what you are proposing is assigning `CodeCleanupSilent` to different keyboard shortcut. This way I paste my code and then execute my keyboard shortcut to reformat.

I feel R# not formatting on paste is a bug. Is there a bug tracker where I can report this?
0

Amith,

If you'd like to have both "Paste" and "Silent Code Cleanup' commands to be mapped to a single key, you may use VS macros for that.
Still, you're welcome to log this request in your YouTrack here: http://youtrack.jetbrains.com/issues/RSRP

Thank you!

0

If you'd like to have both "Paste" and "Silent Code Cleanup' commands to be mapped to a single key, you may use VS macros for that.


I just looked up VS macros. Seems VS 2012 removed all support for them. The official recommendation is to create VS extensions and use those. It just seems a lot of work!!

I did find an article that talks about using NuGet and Powershell to recreate macro like functionality (http://www.wintellect.com/blogs/jrobbins/using-nuget-powershell-to-replace-missing-macros-in-dev-11), but I think I am going to to wait for R# to include the functionality in some future version :)
0

Please sign in to leave a comment.