Author Topic: How to set tab indent for switch() statements?  (Read 6295 times)

brianla

  • Community Member
  • Posts: 7
  • Hero Points: 0
How to set tab indent for switch() statements?
« on: September 22, 2006, 03:51:24 pm »
Hi,

  I'm a new SlickEdit user (working on a Linux workstation), making the switch from Emacs.  I've been able to solve or work around most things, but a couple still have me mystified.  For instance, I want my switch() statements to look like:
Code: [Select]
    switch(foo) {

    case 1:
        printf("case 1\n);
        break;

    default:
        printf("default\n);
        break;
    }

I have discovered that if I Tools->Beautify, my switch statements get lined up just like I want; however, if I cursor to each line and hit the tab key, it gets realigned like:
Code: [Select]
    switch(foo) {

    case 1:
    printf("case 1\n);
    break;

    default:
    printf("default\n);
    break;
    }

  I've look high and low, and played with many settings, but have been unable to find a way to make tab-indentation behave like Tools->Beautify.  I don't want to Beautify the entire file, as it changes a good many things besides those few lines I want, which can cause various problems when checking code into/out of a code base, sharing, etc.

Is there a "hidden" way to play with the nitty-gritty details of style formatting via the tab key?  Or am I missing something that should be obvious in the options menus?

Thanks for any help!
Brian

Graeme

  • Senior Community Member
  • Posts: 2295
  • Hero Points: 301
Re: How to set tab indent for switch() statements?
« Reply #1 on: September 22, 2006, 10:11:06 pm »

You are missing something that isn't very obvious in the options menus.  Go to tools -> options -> file extension setup.  Click the options button at the bottom of the window.  On the indentation tab, select "indent case from switch".

Graeme

brianla

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: How to set tab indent for switch() statements?
« Reply #2 on: September 23, 2006, 01:32:07 am »
Hello Graeme,

  Thank you for the response.  Actually, that was one of the options that I have played with.  By turning that on and hitting [Tab] on every line, I get:

Code: [Select]
    switch (foo) {
        case 1:
        printf("case 1\n");
        break;
        default:
        printf("default\n");
        break;
    }

and if I beautify that, I then get:

Code: [Select]
    switch (foo) {
        case 1:
            printf("case 1\n");
            break;
        default:
            printf("default\n");
            break;
    }

neither of which is what I want.

Well actually, I could probably live with any of these styles... what is bothering me is that when I Tools-->Beautify a selection of code, it indents differently than when I use [Tab] to re-indent the very same lines.

So, I guess the base question is how to make [Tab] re-indentation behave identically to Tools-->Beautify.

Thanks!
Brian

Graeme

  • Senior Community Member
  • Posts: 2295
  • Hero Points: 301
Re: How to set tab indent for switch() statements?
« Reply #3 on: September 23, 2006, 04:05:00 am »

Hi Brian

Tab key reindenting works perfectly for me, both for your style (case lined up with switch) and mine (case indented).  On the indent tab in the file extension options daialog, what do you have set for "when tab key reindents"?  I have "in leading blanks" selected.  Check the help on what this does to get more info.

Graeme





hs2

  • Senior Community Member
  • Posts: 2736
  • Hero Points: 288
Re: How to set tab indent for switch() statements?
« Reply #4 on: September 23, 2006, 10:28:35 am »
There is an option in the beautifier dialog: ' Indent CASE from SWITCH'. For me it's doing well.

The reason for the independent parameter set in the beautifier is that it's mostly used to 'import' 3rd party sources w/ maybe completely other styles/tab/spaces settings.

HS2

brianla

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: How to set tab indent for switch() statements?
« Reply #5 on: September 25, 2006, 05:01:55 pm »
Thanks for your responses, folks!

Let me reiterate, the problem was not whether the "case" was at the same indent level as the "switch".  The options for modifying this behavior worked fine.  The problem was that I was unable to get Tools->Beautify and [Tab] reindentation to indent the statements within each case to the same indent level (see previous examples).  Using [Tab] would align the statements with the "case x:", and using Tools->Beautify would indent statements one level in from the "case x:".

I finally discovered that turning on Syntax Expansion (Indent tab on the File Extension Setup dialog) has an impact on [Tab] reindentation.

I'm not used to having my editor fill things out for me, and I spent a pile of time deleting parens and curlys I habitually entered since they were also being automatically inserted by syntax expansion.  So I turned it off.  Well, it turns out that having it off impacts how the [Tab] key reindents at least some statements.  I turned syntax expansion back on, and set the minimum keyword length very high, so now my [Tab] key works like I want it to, and I still don't have to deal with (annoying to me) syntax expansion.

I'm sure there's some very logical explanation for this behavior, but it certainly isn't obvious to me!  The help topic for syntax expansion doesn't seem to contain any indications.  Being a new SlickEdit user, I have no idea if this is some bizarre low-profile bug, or if this is long-present and intended behavior.  Either way, I'm a happy camper now.

Again, thanks for your time and responses,
Brian

p.s. Not being one to lightly walk away from a good mystery, I'd be interested in the aforementioned logical explanation if anyone has some insight.

brianla

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: How to set tab indent for switch() statements?
« Reply #6 on: September 26, 2006, 03:30:36 pm »
Well, like an idiot, I actually believed what I read in the SlickEdit Help pages instead of actually testing this "solution".

I finally got back to some coding this morning and discovered that I am apparently unable to control Syntax Expansion as desired.  As best I can tell, the "Minimum expandable keyword length" has no effect.

So, I can make [Tab] reindentation do what I want by turning off Syntax Expansion, but then I have to deal with my editor inserting characters for me.

Hrm... maybe if I enable word wrap then Syntax Expansion will work as desired...   ::)

Time to see what support can do for me.

Dennis

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2648
  • Hero Points: 409
Re: How to set tab indent for switch() statements?
« Reply #7 on: September 26, 2006, 05:29:20 pm »
I have confirmed that this is actually a bug and it only appears when syntax expansion is turned off.  I have filed a defect report and this should be fixed in the next release of SlickEdit.

As for minimimum expandable keyword length, it works exactly as it should.  If you set it to 100, then you will not get any prefix expansion, but if you type a complete keyword, such as "if", then the statement will still expand, you just won't get expansion if you type a prefix such as "wh<space>" (since that prefix is less than the minimum length).  Syntax expansion is something programmers need to train their fingers to take advantage of.  Once you start thinking to yourself, self, I need a while statement, ok type "w<space>" instead of "w-h-i-l-e <space> ( ... ) <space> { ... }", then you start saving keystrokes and time.

I had the same difficulties you did when I started using syntax expansion, and I finally managed to retrain myself by forcing myself to undo and retype the statement taking advantage of syntax expansion every time I overtyped what syntax expansion had already done for me.  Took about two weeks to get used to it and quit tripping over myself.

As for inserting braces immediately, there is an option to turn that off if you just want the first part of the statement.  This is good for programmers who like to use one-line if statements rather than putting everything in brace blocks.

brianla

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: How to set tab indent for switch() statements?
« Reply #8 on: September 26, 2006, 06:08:57 pm »
Thanks for the clarification Dennis,

  Have you ever seen the Red Green Show?  It's a comedy show out of Canada.  They've got a "Man's Prayer" that applies to me in this situation:

Quote
I'm a man...
but I can change...
if I have to...
I guess.



Guess I'd better start my two weeks of re-training.
 :)