Mega Search
23.2 Million


Sign Up

Make a donation  
Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general

Me again...still trying to get Delphi 2007 to work the way I need it to.

One of my CodeRush templates was "bg".  It causes a begin/end block to be 
inserted.  Now, Delphi 2007 has two features to already do this.  If I type 
"begin" and press Enter, then it completes the "end" block.  I'll call this 
"block completion" for lack of a better term.  I can also use the Live 
Template by typing "begin" and pressing Tab.  So, I copied the template for 
"begin" and created my own called "bg" and set it to auto-complete when 
pressing the space key.

The problem is with the indentation.  If I have an existing line of code 
like this:

if (a=1) then

and I place the cursor after the "then" and type "begin" and then press 
Enter to use the "block completion", then I properly get this:

if (a=1) then begin
  _
end;

However, if I go back to the original line and type "being" and press Tab 
(or type my own "bg" template and press Space), then I get this mess:

if (a=1) then begin
                 _
               end;

Now, the spacing probably won't show up properly in the newsgroup, but the 
problem is that Live Templates are taking the starting indentation position 
as the position of the cursor (the beginning of the "begin" word) rather 
than looking at the indentation of the line we are adding to.

This is a huge problem.  Expecially since I always use this less-common 
style of "dangling" begin statements.  And I can't actually use the normal 
Delphi "block completion" feature because a) my fingers are trained to type 
a space and not Enter, and b) I actually use the perverse indentation rule 
where the "end" lines up with the code block that it completes, rather than 
lining up with the original begin.  So I actually write my code blocks in 
the following style:

if (a = 1) then begin
  code block goes here
  end;

rather than the "standard"

if (a = 1) then
  begin
    code block goes here
  end;

I won't get into a style debate.  There are many reasons I prefer the 
dangling begin (less lines and less indentation creep) and that I prefer 
lining up the "end" with the code block above it (since the "begin" doesn't 
appear on it's own line so it doesn't really have any indentation value).

CodeRush allows you to customize your coding "style" and it actually 
accomodated my style.  Since Delphi doesn't allow this customization, I 
can't use the "block completion" since it doesn't indent the "end" the way I 
want it.  And I can't seem to use the Live Templates because it completely 
screws up the indentation of the entire template.

Anyone know of any fix for the indentation of the Live Templates being 
messed up?  Is this something I should add to QC? 


Vote for best question.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2008, at 4:33 PM EST
From: Zugg
 
Re: Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general
You can simply change the 'if' template. Replace the CDATA by:

[CDATA[if |expr| then begin
|selected||*||end|
end;]]

Make sure you keep the line breaks.

Then in your source type 'if', space and you end up with:

if [True] then begin

end;

When you leave the place holder with Tab, the cursor gets properly placed.

You might instead want to change the 'ifb' template accordingly.

-- 
Security check: ---INTRUDER ALERT!

....und wech
          Danny 

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 11:02 PM EST
From: ISO-8859-1QDanny_StrFCmpel
 
Re: Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general
Zugg schrieb:
> Yes, I know I can just type "begin" and then press Enter.  The problem 
> is that I have many many years of training my fingers that are used to 
> typing "bg" instead.  Yes, I will probably be able to eventually 
> train my fingers to do something else.  But it's just another example 
> where I'm losing productivity with Delphi 2007.  All of these "little 
> things" really add up when you are used to typing code so fast with 
> CodeRush and then try to use Delphi 2007 without it.

You can define a 'bg' template that works like this. Only, it will still 
work like the 'begin' template (or any other). The inserted code will 
always be aligned with the current cursor position. In fact, it doesn't 
have anything to with coding style and what CodeGear thinks it a good 
style. There's no way to reduce indentation on the following lines.

For your other thread, the request for automatic braces: this is not 
possible in a straight-forward manner, since the editor doesn't allow 
whitespaces in the shortcut name. Your shortcut in this case would have 
to be ") and", which won't work. You could remove the space though, but 
you would need to place a space before the closing bracket, like

if (a=1 )and

will be

if (a=1 ) and (_)

In this case it will work. But does it look the way most people want it?

So I guess it's better to find a different solution, as you already 
mentioned.

p.s.: A technical explanation of live templates can be found here:

http://delphi.wikia.com/wiki/Live_Templates_Technical_Info

-- 
Technological progress has merely provided us with more efficient means
for going backwards.
		-- Aldous Huxley

....und wech
          Danny 

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 9:01 PM EST
From: ISO-8859-1QDanny_StrFCmpel
 
Re: Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general
Yes, I know I can just type "begin" and then press Enter.  The problem is 
that I have many many years of training my fingers that are used to typing 
"bg" instead.  Yes, I will probably be able to eventually train my 
fingers to do something else.  But it's just another example where I'm 
losing productivity with Delphi 2007.  All of these "little things" really 
add up when you are used to typing code so fast with CodeRush and then try 
to use Delphi 2007 without it.

"Danny Strümpel"  wrote in message 
news:478473fb$1@newsgroups.borland.com...
> Zugg schrieb:
>> And no, the existing code doesn't have "DoSomething" in it yet.  I like 
>> to close my blocks as I type code.  So when I get to the end of an "if" 
>> statement (or while, or any other statement that might need a begin/end 
>> block) I just type "bg" to expand it to a begin/end block with the proper 
>> indentation and put the cursor into the block.
>
> Well, then it is not "existing" code, when you're just typing it. Why make 
> it so difficult? Just type:
>
> if ... then begin
>
> and hit Enter and you will get a properly formatted:
>
> if ... then begin
>   _
> end;
>
> 'begin' is not much longer than 'bg' and you can easily type it since it 
> contains no difficult letter combinations.
>
>> And yes, I know that simple a=1 doesn't need (), but I use them from a 
>> better style point of view since it's easier for me to read.
>
> Then, go on! Readability is why I also use the if formatting above rather 
> than putting begin onto the next line, especially in combination with 
> 'else begin...end'.
>
> -- 
> Ich habe immer Spaß, weil ich bekloppt bin!
>
> ...und wech
>          Danny  


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 10:13 AM EST
From: Zugg
 
Re: Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general
Zugg


Silly thought but why worry about indenting at this stage - use GExperts to tidy it up. BTW its nice to see a couple of other begin..end heretics. I stopped using the templates because it was more effective to forget them than spend the time "fixing" them.

Roy Lambert


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 8:36 AM EST
From: Roy Lambert
 
Re: Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general
Zugg schrieb:
> And no, the existing code doesn't have "DoSomething" in it yet.  I like 
> to close my blocks as I type code.  So when I get to the end of an "if" 
> statement (or while, or any other statement that might need a begin/end 
> block) I just type "bg" to expand it to a begin/end block with the 
> proper indentation and put the cursor into the block.

Well, then it is not "existing" code, when you're just typing it. Why 
make it so difficult? Just type:

if ... then begin

and hit Enter and you will get a properly formatted:

if ... then begin
   _
end;

'begin' is not much longer than 'bg' and you can easily type it since it 
contains no difficult letter combinations.

> And yes, I know that simple a=1 doesn't need (), but I use them from a 
> better style point of view since it's easier for me to read.

Then, go on! Readability is why I also use the if formatting above 
rather than putting begin onto the next line, especially in combination 
with 'else begin...end'.

-- 
Ich habe immer Spaß, weil ich bekloppt bin!

....und wech
          Danny 

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 8:12 AM EST
From: ISO-8859-1QDanny_StrFCmpel
 
Re: Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general
> This is by design, I think. Borland suggests that each 'begin' stands on 
> its own line and that the corresponding 'end' aligns with it. So it 
> "properly" aligns the 'end' with its 'begin' here.

Yes, that's exactly the problem.  I hate that CodeGear "assumes" a 
particular style.  None of the 3rd party template systems make this 
assumption, so I don't know why CG did.  Since there doesn't seem to be any 
fix for this, I have abandoned using the built-in Live Template system in 
Delphi 2007 and went ahead and just bought Castilia just to get a basic 
template system that works.  It has it's own problems, but at least it 
allows me to fix this basic begin/end indentation problem.

And no, the existing code doesn't have "DoSomething" in it yet.  I like to 
close my blocks as I type code.  So when I get to the end of an "if" 
statement (or while, or any other statement that might need a begin/end 
block) I just type "bg" to expand it to a begin/end block with the proper 
indentation and put the cursor into the block.  So the block doesn't exist 
yet when I am typing.  It's similar to my other post about having ( 
automatically expand into (_).  I am just used to completing all blocks, 
balancing parens, etc as I type.  Then I never need to worry about missing a 
closing "end" or paren.

And yes, I know that simple a=1 doesn't need (), but I use them from a 
better style point of view since it's easier for me to read.

"Danny Strümpel"  wrote in message 
news:478460ee@newsgroups.borland.com...
> Zugg schrieb:
>> The problem is with the indentation.  If I have an existing line of code 
>> like this:
>>
>> if (a=1) then
>
> I guess your existing code completely is:
>
> if (a=1) then DoSomething;
>
> or
>
> if (a=1) then
>   DoSomething;
>
>> and I place the cursor after the "then" and type "begin" and then press 
>> Enter to use the "block completion", then I properly get this:
>>
>> if (a=1) then begin
>>  _
>> end;
>
> Yes.
>
>> However, if I go back to the original line and type "being" and press Tab 
>> (or type my own "bg" template and press Space), then I get this mess:
>>
>> if (a=1) then begin
>>                 _
>>               end;
>
> This is by design, I think. Borland suggests that each 'begin' stands on 
> its own line and that the corresponding 'end' aligns with it. So it 
> "properly" aligns the 'end' with its 'begin' here.
>
>> Anyone know of any fix for the indentation of the Live Templates being 
>> messed up?
>
> Bring your existing 'if' statement to this formatting without pressing Tab 
> and place the cursor accordingly:
>
> if (a=1) then begin
>   DoSomething;|
>
> Then, press Enter.
>
>> Is this something I should add to QC?
>
> Maybe. For consistency the IDE should check whether 'begin' is lonely 
> placed on its own line or not...
>
> HTH
> Danny
>
> P.S.: You are over-bracing here. For a simple condition like 'a = 1' there 
> are no braces needed and thus shall not be written.
>
> -- 
> You know you've been spending too much time on the computer when your
> friend misdates a check, and you suggest adding a "++" to fix it.
>
> ...und wech
>          Danny  


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2008, at 11:40 PM EST
From: Zugg
 
Re: Inproper indentation in Live Templates  
News Group: borland.public.delphi.ide.general
Zugg schrieb:
> The problem is with the indentation.  If I have an existing line of code 
> like this:
> 
> if (a=1) then

I guess your existing code completely is:

if (a=1) then DoSomething;

or

if (a=1) then
   DoSomething;

> and I place the cursor after the "then" and type "begin" and then press 
> Enter to use the "block completion", then I properly get this:
> 
> if (a=1) then begin
>  _
> end;

Yes.

> However, if I go back to the original line and type "being" and press 
> Tab (or type my own "bg" template and press Space), then I get this mess:
> 
> if (a=1) then begin
>                 _
>               end;

This is by design, I think. Borland suggests that each 'begin' stands on 
its own line and that the corresponding 'end' aligns with it. So it 
"properly" aligns the 'end' with its 'begin' here.

> Anyone know of any fix for the indentation of the Live Templates being messed up?

Bring your existing 'if' statement to this formatting without pressing 
Tab and place the cursor accordingly:

if (a=1) then begin
   DoSomething;|

Then, press Enter.

> Is this something I should add to QC?

Maybe. For consistency the IDE should check whether 'begin' is lonely 
placed on its own line or not...

HTH
Danny

P.S.: You are over-bracing here. For a simple condition like 'a = 1' 
there are no braces needed and thus shall not be written.

-- 
You know you've been spending too much time on the computer when your
friend misdates a check, and you suggest adding a "++" to fix it.

....und wech
          Danny 

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 6:51 AM EST
From: ISO-8859-1QDanny_StrFCmpel