[TxMt] Tab Always Indent and indent-region

Travis P woodtrail at jetemail.net
Fri Oct 29 13:34:35 UTC 2004


On Oct 29, 2004, at 3:41 AM, Jeroen wrote:

> Travis P wrote:
>> I'm talking about a syntax-aware indentation.  It will change the 
>> indentation only if the line is not already properly indented 
>> (properly is language syntax dependent naturally).  If the line is 
>> not properly indented, it will be indented as appropriate with 
>> whitespace.
>> With the "Tab Always Indent" option, pressing tab anywhere on a line 
>> causes that line to be syntax-aware re-indented (which might not do 
>> anything if the line is already indented correctly).
>
> This sounds like it would require a whole lot of coding to get this 
> done properly. And it's only of use to you if it's done properly, 
> otherwise you'll turn it off after two uses and never look at it 
> again.

I'm afraid I don't know how much code it is.  It would involve some 
code, yes.  If might not be that much though.  Looks to me like it 
might actually involve not much at all.  Playing with XCode, it does 
not appear to be language customized for the most part.  It just counts 
(){}[]-depth and then indents a level of tab-size x depth.

For example in XCode, it properly indents this "Java":
public class test {
   hey there() {
     iffy ( smash !@
            grab ) {
       do it
     }
   }
}

There is no Java keyword "iffy".  There no Java operator !@.  But it 
all works anyway for this style.
However, if one prefers

   if ( ... )
   {
     statements;
   }

rather than

   if ( ... ) {
     statements;
   }

Then XCode does need to recognize the "if" syntax as taking a block of 
statements or else the { }'s themselves are incorrectly placed.  But 
I'm not sure why.  XCode appears to be following some exceptional rule 
to intentionally misplace them in that instance.  For languages like 
C/C++/Java, it does need to know about commands for which it needs to 
indent the first statement if that first statement is on the following 
line and is not {}'ed, as if/for/while statements don't require the 
{}'es unless one is supplying a block of multiple statements.

There are a few exceptions that are language specific.  HTML <pre> 
sections, LaTeX verbatim, Perl/Shell here-files all should not have any 
auto-indentation done on them.  Also, the indentation-code would have 
to be careful of (){}[] in strings and regular expressions where they 
shouldn't affect the indentation.

> Also, this is something that would only work in languages where the 
> indentation is defined by brackets or something. And then only for 
> people with the same coding style.
>
> Perhaps I'm a bit biased, because I mostly code in python where it 
> simply is not possible to have syntax-aware indentation. (Because the 
> indentation *is* syntax).

It works well Perl, Tcl, Java, C, C++.  I use it for all 5 of those 
languages extensively.  I believe it also works for shell scripts 
(bash/ksh/csh), though I try to keep such scripts short enough that its 
not an issue for me.

I'm only very minimally familiar with Python.  Sounds Python is more 
the exception, so if that is your primary language then yes, maybe you 
wouldn't find this useful.  The five languages I mentioned are not rare 
birds though. :-)

>> XCode has these features too (I'm looking at version 1.1):
>>   Tab Always Indent:  Indentation Preference panel: "Syntax-aware 
>> indents"  and "Tab indents: Always"
>>   indent-region:  ", make a selection and choose Format" menu -> 
>> "Re-indent"
>
>> This feature has the nifty side-effect that it really helps finding 
>> unbalanced parens/braces/etc or other mistakes (forgetting to put 
>> braces around multiple statement if/else/while/for-statement bodies 
>> in languages where such braces are optional if the desired body is a 
>> single statement: C/C++/Java) because all of a sudden the indentation 
>> will be off unexpectedly.
>
> There already is a feature for this: when you move over a 
> parens/brace/etc, the matching one lights up briefly.

I use that too.  The highlighted-matching doesn't help the other 
exceptional case I mention (bodies that are mistakenly not bracketed) 
and doesn't work particularly well for large grouped things like whole 
functions and whole C++/Java classes/namespaces.

Based on your earlier comment about Python, maybe this does not apply 
to you.

-Travis




More information about the textmate mailing list