Forum Visual Studio

How can I get Xamarin Studio formatting of object initializers to match Visual Studio 2013?

This is one of those questions I'm afraid to even ask for fear of how simple the answer will be. But, I've already gone WAY past my time limit of searching for the answer on my own before asking for help.

Anyway...

In Visual Studio, I have code that looks like this:

class Program
{
    static void Main(string[] args)
    {
        var classInstance = new MyClass
        {
            MyFirstProperty = 1,
            MySecondProperty = 1f
        };
    }
}

I can not for the life of me get Xamarin Studio to allow the formatting of my object initializer to match this. I want my property initializations to be indented under the invocation of the constructor following my same indentation rules.

Here's what I have enabled under Preferences.
Preferences->Behavior->"Enable on the fly code formatting"
Preferences->Source Code->Code Formatting->C#->"Use Default Settings from Text File" (NOT CHECKED)
Preferences->Source Code->Code Formatting->C#->"Convert Tabs to Spaces" (CHECKED)
Preferences->Source Code->Code Formatting->C#->"Desired file width: 80"
Preferences->Source Code->Code Formatting->C#->"Line Endings: Native"
Preferences->Source Code->Code Formatting->C#->"Tab width: 4"
Preferences->Source Code->Code Formatting->C#->"Indent width: 4"
Preferences->Source Code->Code Formatting->C#->"Convert Tabs to Spaces" (CHECKED)
Preferences->Source Code->Code Formatting->C#->"Allow tabs after non-tabs" (CHECKED)
Preferences->Source Code->Code Formatting->C#->"Remove trailing whitespace" (CHECKED)

I have done this with a completely new solution just to make sure I wasn't editing global settings, and then seeing the copied settings in the solution.

When I start to type out my object initializer, it wants to indent the curly brace under the constructor by 4, and then the initializations another four. When I close my object initializer and type the final ";", it formats everything properly. When I add additional initializers, or try to edit the initializers, it forces an indentation of 8 spaces rather than the 4 I want.

Please tell me I'm just doing something dumb. This is one of those things I keep trying to ignore, but it keeps bugging the crap out of me.

Answers

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    You can check a couple of things. First open Xamarin Studio and go to:

    "Xamarin Studio"->"Preferences"

    Then in the preferences window go to:

    "Source Control"->"C# source code"

    In Policy, choose "Microsoft Visual Studio".

    If that does not completely do what you want, you can actually edit the code templates individually. To do that, go to preferences, but choose:

    "Text Editor"->"Code Templates"

    From there you can select the C# templates and edit them to the format that you would like Intellisense to format it as.

  • Sigmund68kSigmund68k USMember

    Thanks for the response.

    I'm surprised that I didn't actually include it, but I do have the Preferences->Source Code->Code Formatting->C# source code->Policy set to "Microsoft Visual Studio." And I don't see a code template for object initializer.

    I still can't get it to format the same way I'm used to from VS 2013. I can actually select the object initializer, and force "Format Selection" to make everything line up. I've bound that to a new shortcut key in preferences.

    It still feels like I'm fighting Xamarin Studio and just missing something. Or maybe it's just that more people use the "Mono" settings, and not the "Microsoft Visual Studio" settings, and nobody else is annoyed by this.

  • mhutchmhutch USMember, Xamarin Team Xamurai

    If the indenter behaves differently to the formatter, that's a bug. Could you please file it at bugzilla.xamarin.com?

  • Sigmund68kSigmund68k USMember

    Thanks Michael. I did my best to write this up.

    https://bugzilla.xamarin.com/show_bug.cgi?id=27410

Sign In or Register to comment.