Forum Visual Studio
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Incorrect auto-formatting of C# code for specific functions

joolsajoolsa GBMember ✭✭

My editing style is not to have a space in front of brackets for a function call, like this:


However even after setting Xamarin studio for this so that it works in the general case, above there's a tendency for it sometimes to put a space in automatically, like this:

Debug.Log ();

It feels like I'm going nuts as:

  • it doesn't always do this
  • it does it more for some functions than others. E.g. Debug.Log() almost always gets the space, but not always!

This happens irrespective of whether the "Text Editor->Behaviour->Enable on the fly code formatting" is set.


Best Answer


  • MikeKrgerMikeKrger DEXamarin Team, Beta Xamurai

    Try to set the formatting to a different style for your project. What do you type to get the space ?

  • joolsajoolsa GBMember ✭✭

    (Not doing anything to the project yet) If I type "Debug.Log(" out in full then as I type "(" a space is inserted (incorrectly!) before the "(". If I type "Debug.L", select "Log" from the auto-complete menu, then type "(" it's all fine. No space is inserted.

    btw - Would you prefer a bug report to a forum post. If so where?

  • joolsajoolsa GBMember ✭✭

    This post has become a bit long. I'd like to prefix it by saying I think Xamarin Studio is great, way better than XCode and a big step up from Mono Develop 3.

    This is my complete setup, which exhibits the issue above:

    • I've got a custom setup for code formatting, set in Menu->Xamarin Studio->Preferences. In general if it's a function-or-similar-no-space-before(), if-or-other-non-function-gets-space-before ().
    • I do not have a custom policy set in Menu->Xamarin Studio->Custom Policies (is this global or tied to the solution?)
    • If I right click on the solution and select Options->Source Code->Code Formatting everything is set to "Custom", and the settings seem to be the defaults, not my global custom ones. Editing the settings here and restarting Xamarin Studio makes things work as expected, but I don't understand why the settings are overridden here. There is also no option to revert to inheriting the global settings.
    • If I right click on a project and select Options->Source Code->Code Formatting everything is set to "Inherited", which is good!
    • Unity is generating these projects, so I'll nuke them and take a look again.

    (This is on a Mac 4.0.3 btw!)

    There seems to be a couple of bugs here:

    • "Xamarin Studio->Preferences->Text Editor->Behaviour->Enable on the fly code formatting" doesn't turn off code formatting for me

    • When code formatting is set to "Custom" at the solution level you can't set it back to "Inherited"

    I'll also check out if Unity is writing anything to set code formatting to Custom at the solution level.

  • joolsajoolsa GBMember ✭✭

    I've found that Unity inserts this into the solution, overriding my settings:

    GlobalSection(MonoDevelopProperties) = preSolution
        StartupItem = Assembly-CSharp.csproj
        Policies = $0
        $0.TextStylePolicy = $1
        $1.inheritsSet = null
        $1.scope = text/x-csharp
        $0.CSharpFormattingPolicy = $2
        $2.inheritsSet = Mono
        $2.inheritsScope = text/x-csharp
        $2.scope = text/x-csharp
        $0.TextStylePolicy = $3
        $3.FileWidth = 120
        $3.TabWidth = 4
        $3.EolMarker = Unix
        $3.inheritsSet = Mono
        $3.inheritsScope = text/plain
        $3.scope = text/plain

    I've taken to manually deleting this out, as there doesn't seem to be any way in the solution options dialog to say "Inherited Policy" as there is in the project options. Bug?

  • mhutchmhutch USMember, Xamarin Team Xamurai

    Your user preferences are copied into the solution (i.e. you can edit them in Solution Options) when you create a solution or open one that does not have settings (e.g. from VS). I have no idea why Unity would insert/overwrite policies - you should contact them about that. They seem to take a very simplistic, heavy-handed approach to solution syncing.

    The Custom Policies dialog lets you create, edit, import and export policy sets that you can then apply to existing solutions.

    This behaviour of defaulting to solution-specific policies is something we're planning on changing since many users find it confusing - although we believe it's the best way to do things, since it ensures all users working on a shared solution use the same policies.

  • joolsajoolsa GBMember ✭✭

    Yep, it does make sense to copy these into the solution, but it's not clear at all that this happens! Adding the "inherit settings from Xamarin Studio" is a must IMO, since right now you have to edit the solution to achieve this (if I understand correctly). You're totally right about Unity. I've contacted them :)

  • AlexGAlexG USMember

    Have you got any answer from the unity team? this is still happening to me and is a very annoying behavior

  • joolsajoolsa GBMember ✭✭

    Nope, nothing from Unity.

  • mironal3xmironal3x ROMember

    Any news about the solution settings override?

    I still have the same problem and it's driving me nuts. :)

  • joolsajoolsa GBMember ✭✭

    Great Gist I found on Twitter which uses an undocumented function to post-process the project and solution files just after they are generated to remove the formatting:

Sign In or Register to comment.