Search Unity

Tab insanity in MonoDevelop packaged with Unity free

Discussion in 'Scripting' started by ohboyotero, Jul 20, 2013.

  1. ohboyotero

    ohboyotero

    Joined:
    Jul 20, 2013
    Posts:
    4
    I just downloaded Unity yesterday and have been toying with MonoDevelop's editor preferences to try to achieve the auto-formatting I'm used to and for whatever reason I can't get tabs to behave. Everything else is fine.

    I'm using MD 2.8.2 (packaged with Unity as of yesterday). I'm running Windows 7.

    Specifically, smart indentation (i.e., where the cursor jumps if I open a new block, for instance) and re-indent both add way too much space. What I get if I "Format Document" vs. what smart indentation does does not line up at all (Format Document works exactly as expected).

    Relevant settings:
    • Tab width: 2
    • Convert tabs to spaces: Yes
    • Allow tabs after non-tabs: Yes
    • Remove trailing whitespace: Yes
    • Indentation mode: either Automatic or Smart (neither works)
    • Interpret tab as reindent: either way it still misbehaves

    If there are any other settings I should mention, let me know.

    The behavior I see is that if I write the following:

    Code (csharp):
    1.  
    2. public void SomeMethod() {
    3.     |<----- auto-indent to 4 spaces instead of 2
    4. }
    5.  
    The cursor will automatically double-tab in between the two brackets. If reindent is enabled, mashing TAB a bunch of times will always bring the code back to double-indentation.

    However, if I auto Format Document, the indentation is corrected to the expected 2 spaces everywhere. That said, having extra indentation inserted all the time as you type is INFURIATING, so I have to figure this out.

    Has anybody seen this before? Any idea how to fix it?

    Thanks!
     
  2. BoNaNa

    BoNaNa

    Joined:
    Jan 8, 2014
    Posts:
    3
    Had the same problem over here, but maybe this works for you guys:

    Go to Preferences > Text Editor > Behavior and look for the INDENTATION header.
    Mine was set to 'smart' and switching it to 'automatic' seems to have fixed the problem.
     
  3. SasugaShogun

    SasugaShogun

    Joined:
    Jul 18, 2013
    Posts:
    9
    I'm having this issue right now. I've changed all the settings in: Tools -> Options -> Source Code -> Code Formatting -> C# source code -> C# Format.

    As well as all the options in: Project -> Source Code -> Code Formatting -> C# source code -> C# Format.

    As well as changing: ... C# source code -> Text Style; for both as well.

    I've tried multiple settings/forms of Custom, I've tried Mono, I've tried Microsoft Visual Studio, I've tried GNU Style.

    Nothing seems to fix this problem for me and its very annoying. I've wasted a whole work day trying to fix this issue.

    I don't want eight wasted spaces on the left hand of every line, it makes it difficult and tedious to read longer lines. Plus, it just plain looks ugly to me. Why is this the default? Why can't I change it!?
     
  4. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    Look like a bug to me. Xamarin is doing the same annoying thing too.
     
  5. InstantKarma

    InstantKarma

    Joined:
    Jan 22, 2014
    Posts:
    2
    The way i fixed the issue was going to "Project->Solution Options" and setting the number of characters to 4. Now when i use auto formatting, it will single tab indent the code.

    I think the "tools->options" menu will only affect new projects.
     
    pedromeleiro and levis501 like this.
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Yes, this threw me as well. I submitted a bug report to Unity about the settings not doing anything, and they replied showing me that if you want to change stuff on the *current* project you need to change its solution properties, not the application's settings.
     
  7. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    OMG this helped. I have run out of hair to rip out of my head.

    tools -> options -> text editor -> behavior -> indentation = Automatic ---- resolved this insanity.
     
  8. SasugaShogun

    SasugaShogun

    Joined:
    Jul 18, 2013
    Posts:
    9
    This/These bugs are very annoying...

    I've played around with the settings, it seems I'm always playing around with the settings.

    Here's the real rub. I seem to get it 'fixed' and then when I start a new project it's broke again.

    I've saved my policies, and then I'll start a new project and it'll be broke... so I'll tell it to use my saved policy. Sometimes that works, it fixes it, other times it doesn't.

    I have no idea why!

    Sometimes I go in and I change the spaces for tabs from 8 to 4, or 4 to 3 (and then change it back to 4, because I like 4) and THAT WORKS! ... AND THEN OTHER TIMES IT DOES NOT WORK!?

    Say what? I'm doing the same thing(s) to 'fix' it, and the fix only seems to work half the time.

    To be even more annoying, on projects where things were fine sometimes they'll just decide to up and break.

    =-=-=-=

    What's really, really annoying about this bug: I hit enter to make a new line and it tabs in 4 spaces, but the function or if statement, or whatever is tabbed in 8 spaces above it, so my code will look like this:

    Code (csharp):
    1.  
    2.         void Start(){
    3.     //It has me start typing here!
    4.     //So whatever I'm typing looks out of place, and it screws with my brain.
    5.    while(blah > something)
    6.    {
    7. MonoDevelopDrives(MeCrazy);
    8.    }
    9.         }
    10.  
    Its very distracting, confusing at times, and frustrating to work that way. Its extra annoying that I can't figure out how to permanently fix the problem. Sometimes I can 'fix' it, and other times doing the same exact thing doesn't fix it... then days later it decides to break again.

    Whenever I save the script it looks like this:
    Code (csharp):
    1.  
    2.        void Start(){
    3.                //It has me start typing here!
    4.                //So whatever I'm typing looks out of place, and it screws with my brain.
    5.                while(blah > something)
    6.               {
    7.                       MonoDevelopDrives(MeCrazy);
    8.               }
    9.        }
    10.  
    But then when I add more lines, it tabs like this again:
    Code (csharp):
    1.  
    2.        void Start(){
    3.                // It has me start typing here!
    4.                // So whatever I'm typing looks out of place, and it screws with my brain.
    5.                while(blah > something)
    6.               {
    7.                       MonoDevelopDrives(MeCrazy);
    8.         // It has me type here, and looks confusing, as if this line is outside the while statement.
    9.         // It's lined up with the Start() function, even though it's inside the while loop.
    10.         // Granted it will 'fix' the indenting (if indenting 8 when I want it to only indent 4 is 'fixing it) when I save.
    11.         // But until then, it screws with my brain, especially if I start to do something like this:
    12.         if(Is > ThisInsideTheWhile){
    13.              // Doesn't this look confusing?
    14.              // Do you like looking at code like this?
    15.         if(Error){
    16.         Debug.Log("MonoDevelop won't fix the indention, and I have to try to figure out the error while all the indention is screwed up.);
    17.        }
    18.        }
    19.              }
    20.        }
    21.  
    Its driving me crazy, its really messing up my productivity. I try not to let it, but it disturbs me so much that I end up spending half the day playing with settings trying to fix this... Then half my time is wasted being annoyed by it... I've thought about using other editors, but they don't have the auto-complete for Unity functions like MonoDevelop does and then I'm spending half the day making sure I've spelled function names correctly.
     
    Last edited: Mar 18, 2014
  9. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    This worked for me but some of the other settings affect other aspects of the expected behaviors:


    • I found that some white space was sort of corrupted. I am pretty sure this happens because MonoBehavior seems to allow a combination of space and tab handling. It really should convert the whole file if the option changes. Once I removed the space/line and then hit enter/tab to replace it things started working.


    • While pressing enter no longer adds an extra tab, simply moving the cursor to a blank line using the arrow keys seems to convert the whitespace from the start of the line in to an 8-space indent. This seems to be caused by the Remove whitespace option


    • I don't understand why converting tabs to spaces (a standard) can't treat the spaces as tabs for editing. What I mean is, if I hit tab it should add 4 spaces. When I hit backspace, it should remove 4 spaces if 4 are available.


    • Things seem a little more stable with the allow tabs after non-tabs OFF.

    ... In other words....turn it all off it is all broken.

    Like others have said: Don't forget to set both the main and solution options to affect both current and future projects.


    TIP
    • Use the custom policies window (on OSx it is under the Monodevelop-Unity menu) to create a custom policy with the settings you want. Then you can export it to disk as well as to solutions. I had to do it like 3 times to get it to work though. Opening the policy file (XML) in sublime or some other text editor lets you see if there is stuff in there.


    I'm not using the auto-format options (the ones that change text as you type). They seem horribly broken.
     
  10. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    moving code lines using the option+arrow shortcut still adds an extra tab....sigh.
     
  11. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    ...and I think I know what causes all the extra tab issues. I think the tabs are simply counting the embeded-code-block-levels and using that. For example, this has 3 levels, even though it only actually uses 2 tabs:

    Code (csharp):
    1.  
    2. namespace FooSpace
    3. {
    4. public class Foo
    5. {
    6.     public void Bar()
    7.     {
    8.         // Two tabs but Monodevelop uses 3 for all smart tabulation
    9.     }
    10. }
    11. }
    12.  
    Once I indented my code so that each embeded block was 1 tab in. It seems to be working. Like this:

    Code (csharp):
    1.  
    2. namespace FooSpace
    3. {
    4.     public class Foo
    5.     {
    6.         public void Bar()
    7.         {
    8.             // Tabs and block depth match. Smart tabs work
    9.         }
    10.     }
    11. }
    12.  
    Even with indentation set to not use smart, some tools appear to continue to use smart indenting, so I'm going with this. Fingers crossed that I don't find another situation where this breaks. In summary:


    • Text Editor > Behavior > Indentation Mode: Smart
    • Source Code > Code Formatting > Syntax Highlighting > ... > Convert tabs to spaces: OFF
    • Source Code > Code Formatting > Syntax Highlighting > ... > Allow tabs after non-tabs: OFF
    • Source Code > Code Formatting > Syntax Highlighting > ... > Remove trailing whitespace: OFF
    • Always tab code blocks so that number of embedded blocks is equal to number of tabs
     
    Last edited: Apr 23, 2014
  12. baconparticles

    baconparticles

    Joined:
    Mar 5, 2014
    Posts:
    4
    this worked for me
     
  13. ohboyotero

    ohboyotero

    Joined:
    Jul 20, 2013
    Posts:
    4
    Yes. This. So with MonoDevelop 4.0.1 and Unity 4.3.4 I've gotten to the point that I can get things working in a given project.

    BUT the moment I start a new project, it all goes to S***. To be clear, I've set the MonoDevelop Tools>Options settings correctly (which should apply to new projects) AND updated every project's own settings, but no matter what, every new project starts with the same broken default formatting Unity/MonoDevelop use out of the box.

    I'm not sure how the *.sln magic between Unity and MonoDevelop works, but is it possible that Unity uses its own per-project settings instead of using the user-configured MonoDevelop settings when it creates new projects?
     
  14. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Oh my gosh thank you for telling me to go to Project -> Solution Options instead of MonoDevelop -> Preferences. I can auto format my documents now, no more hitting down, tab, down, tab, down, tab, over and over again!
     
  15. monkeyThunk

    monkeyThunk

    Joined:
    Oct 6, 2011
    Posts:
    13
    Yes!,

    Project -> Solution -> Code Formatting - set tab and indent width to 4, then do the same thing in Preferences -> Code Formatting, and FINALLY I can concentrate on writing code instead of fighting with MonoDevelop formatting.

    For me it's also working with smart indentation mode so far (Preferences -> Text Editor -> Behavior -> Indentation Mode = Smart)
     
  16. sayginkarahan

    sayginkarahan

    Joined:
    Jan 23, 2015
    Posts:
    49
    Project -> Solution -> Code Formatting -> C# source code -> Policy -> Microsoft Visual Studio

    Also unchecked "Convert tabs to spaces" , all these settings just I want.

    Thanks.