Lazarus

Using the Lazarus IDE => Editor => Topic started by: glubbish on August 13, 2020, 12:33:33 am

Title: jedi issue with align and if..then..else
Post by: glubbish on August 13, 2020, 12:33:33 am
To reproduce:
create new project
create onactivate for form1
I have not been able to find good documentation for the jcf settings. If anyone knows what to change to fix this, that would be great.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. var
  3.   s: string;
  4. begin
  5.   Top        := (screen.Height - Height) div 2;
  6.   Left := (screen.Width - Width) div 2;
  7.   if Top < 400 then s:= 'small' else s:='big';
  8.   Top  := (screen.Height - Height) div 2;
  9.   Left   := (screen.Width - Width) div 2;
  10. end;   ;
format with ctrl-d gives:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. var
  3.   s: string;
  4. begin
  5.   Top  := (screen.Height - Height) div 2;
  6.   Left := (screen.Width - Width) div 2;
  7.   if Top < 400 then s                   := 'small' else s := 'big';  //actual problem
  8.   Top := (screen.Height - Height) div 2;                             //this does not align like the same lines before the if.
  9.   Left := (screen.Width - Width) div 2;
  10. end;

jcfsettings attached.
Title: Re: jedi issue with align and if..then..else
Post by: Fred vS on August 13, 2020, 12:39:48 am
Hello.

There are some problems with jedi code beautifier.
You may try ptop, it is part of fpc project, I get better result with it.

Fre;D

Title: Re: jedi issue with align and if..then..else
Post by: ASerge on August 13, 2020, 09:30:20 pm
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. ...
  3. end;   ;
format with ctrl-d gives:
I have in case of the presence of that extra semicolon Jedi Code Format does not work at all. But if remove extra semicolon, it gives out:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. var
  3.   s: string;
  4. begin
  5.   Top := (screen.Height - Height) div 2;
  6.   Left := (screen.Width - Width) div 2;
  7.   if Top < 400 then
  8.     s := 'small'
  9.   else
  10.     s := 'big';
  11.   Top := (screen.Height - Height) div 2;
  12.   Left := (screen.Width - Width) div 2;
  13. end;
Title: Re: jedi issue with align and if..then..else
Post by: glubbish on August 14, 2020, 12:25:21 am
@Fred - I would prefer the in ide formatter. A command line one is very cumbersome.

@ASerge - The extra semicolon was a copy/paste error. My testcase does not have it either.
I want it to format simple if.then.else on a single line and only break when begin is used eg:
Code: Pascal  [Select][+][-]
  1.           if Result = 0 then
  2.           begin
  3.             prefix  := 'Updated';
  4.             prefix2 := 'Updating: ';
  5.           end
  6.           else
  7.           begin
  8.             prefix  := 'Copied';
  9.             prefix2 := 'Copying: ';
  10.           end;
If you use my settings.jcf (uploaded) you should get the same results.
Title: Re: jedi issue with align and if..then..else
Post by: Fred vS on August 14, 2020, 01:03:29 am
@Fred - I would prefer the in ide formatter. A command line one is very cumbersome.

Indeed strange that Lazarus did not integrate Ptop in his IDE.

Fre;D
Title: Re: jedi issue with align and if..then..else
Post by: ASerge on August 14, 2020, 03:15:38 pm
I want it to format simple if.then.else on a single line and only break when begin is used eg:
With your jcfsettings.cfg I have the same extra indent. But in my opinion, if.then.else on a single line is a very bad style.
Title: Re: jedi issue with align and if..then..else
Post by: glubbish on August 15, 2020, 01:59:17 am
Quote
if.then.else on a single line is a very bad style.

Horses for courses I guess.
I also like single line if.then.
Code: Pascal  [Select][+][-]
  1.  if Count = 2 then Result := True;

As to the problem. If you set align.max variance to 19, problem does not occur.
Setting to 20 or higher it does.
Title: Re: jedi issue with align and if..then..else
Post by: Paolo on October 05, 2020, 03:55:40 pm
Hi glubbish,

maube you already know, but if you like coincise code in just one line

if Count = 2 then Result := True;

you can do that

Result:=(Cont = 2);
Title: Re: jedi issue with align and if..then..else
Post by: bd4kc on December 17, 2020, 10:22:53 pm
jedi code formatting, I'm not used to writing multiple statements in a single line. Jedi separates them all, causing them to turn pages frequently while reading. I'm looking for a way to separate the extra-long code, I've debugged the confirmed parts, I don't want to see them anymore, but I don't want them to be folded up. How did you do that?
Title: Re: jedi issue with align and if..then..else
Post by: Zoran on December 18, 2020, 12:36:07 am
Hi glubbish,

maube you already know, but if you like coincise code in just one line

if Count = 2 then Result := True;

you can do that

Result:=(Count = 2);

No, wrong. No!
This would get the same behaviour:
Code: Pascal  [Select][+][-]
  1. Result := Result or (Cont = 2)
Still, not really relevant to the question, which is about code formatting.
Title: Re: jedi issue with align and if..then..else
Post by: howardpc on December 18, 2020, 07:36:49 am
Hi glubbish,

maube you already know, but if you like coincise code in just one line

if Count = 2 then Result := True;

you can do that

Result:=(Count = 2);

No, wrong. No!
This would get the same behaviour:
Code: Pascal  [Select][+][-]
  1. Result := Result or (Cont = 2)
Still, not really relevant to the question, which is about code formatting.
Unfortunately you are incorrect.

    Result := Result or (count = 2);
does not always give the same outcome or behaviour as
    Result := count = 2;

If Result is initially True, then your OR expression yields True,

but the simple comparison yields False, when count <> 2.

Title: Re: jedi issue with align and if..then..else
Post by: rvk on December 18, 2020, 09:26:21 am
Hi glubbish,

maube you already know, but if you like coincise code in just one line

if Count = 2 then Result := True;

you can do that

Result:=(Count = 2);

No, wrong. No!
This would get the same behaviour:
Code: Pascal  [Select][+][-]
  1. Result := Result or (Cont = 2)
Still, not really relevant to the question, which is about code formatting.
Unfortunately you are incorrect.

    Result := Result or (count = 2);
does not always give the same outcome or behaviour as
    Result := count = 2;

If Result is initially True, then your OR expression yields True,

but the simple comparison yields False, when count <> 2.
That's not what he said.

He said
Result := Result or (count = 2);
Is the same as
if Count = 2 then Result := True;
Which is correct.

It was a reaction to the incorrect answer that
Result := count = 2;
is the same as
if Count = 2 then Result := True;

Title: Re: jedi issue with align and if..then..else
Post by: Zoran on December 18, 2020, 11:00:37 am
Yes, thanks RVK. Howard, you missed my point.
Title: Re: jedi issue with align and if..then..else
Post by: lucamar on December 18, 2020, 11:08:35 am
Unfortunately you are incorrect.

    Result := Result or (count = 2);
does not always give the same outcome or behaviour as
    Result := count = 2;

If Result is initially True, then your OR expression yields True,

but the simple comparison yields False, when count <> 2.

The point is that to replace the "if", Result must not be changed unless Count = 2, so the OR expression is the way to go: when Count <> 2, Result keeps its value and when Count = 2 it changes to True whatever it previously was. ;)
Title: Re: jedi issue with align and if..then..else
Post by: howardpc on December 18, 2020, 12:05:45 pm
@Zoran
Apologies that I missed your (valid) point.
Title: Re: jedi issue with align and if..then..else
Post by: Zoran on December 18, 2020, 12:18:37 pm
@Zoran
Apologies that I missed your (valid) point.

I should have been more precise, as later posters were (including you, you actually said exactly what I meant, only you thought that I had meant something else).
Title: Re: jedi issue with align and if..then..else
Post by: lucamar on December 18, 2020, 03:32:56 pm
You know, I think this kind of misunderstandings are actually quite useful (and funny, to be honest) because they force one to stop and really think about the consequences of things we usually do almost by rote without giving them more than a passing thought. It's quite a (re)learning experience :D
TinyPortal © 2005-2018