Recent

Author Topic: Code Prettifier / Formatter  (Read 1456 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Code Prettifier / Formatter
« on: October 15, 2024, 11:06:37 am »
There is JEDI, but its old, not updated, and barely works.

The most suitable for me I found is this one, but its not enough:
https://gitlab.com/rchastain/pcf

So are there any recommended code formatters to keep some standard while working in a team?

DomingoGP

  • Full Member
  • ***
  • Posts: 106
Re: Code Prettifier / Formatter
« Reply #1 on: October 15, 2024, 02:23:07 pm »
Have you tried the version of Jedi code format that comes with Lazarus?

components\jcf2\CommandLine\Lazarus\jcf.lpr

It is up to date and I think it works quite well (although my opinion is somewhat biased).

Thaddy

  • Hero Member
  • *****
  • Posts: 18324
  • Here stood a man who saw the Elbe and jumped it.
Re: Code Prettifier / Formatter
« Reply #2 on: October 15, 2024, 02:45:23 pm »
And it is also maintained and best suited for Lazarus.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Fred vS

  • Hero Member
  • *****
  • Posts: 3716
    • StrumPract is the musicians best friend
Re: Code Prettifier / Formatter
« Reply #3 on: October 15, 2024, 02:49:53 pm »
There is also xtop, it is a fork of fpc ptop with extended feature and without memory-leak.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: Code Prettifier / Formatter
« Reply #4 on: October 15, 2024, 02:56:35 pm »
Good, thanks, Im collecting answers and will test them all later :) Thanks and I hope for more alternatives.

Bogen85

  • Hero Member
  • *****
  • Posts: 703
Re: Code Prettifier / Formatter
« Reply #5 on: October 15, 2024, 03:06:53 pm »
There is JEDI, but its old, not updated, and barely works.

The most suitable for me I found is this one, but its not enough:
https://gitlab.com/rchastain/pcf

So are there any recommended code formatters to keep some standard while working in a team?

The command line JCF (Yes.... JEDI...) does get updates, but it breaks a lot...

Recently the updating to for lowercasing on a lot of things broke, and I need to find out what git commit broke things, and file an issue...

See https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41147 on how to build it from the command line.

You have to (well, you don't.... it is easier to the way) go into Lazarus to setup the initial xml config file, but after that you can edit it by hand...

I'll take a look at PCF.


Bogen85

  • Hero Member
  • *****
  • Posts: 703
Re: Code Prettifier / Formatter
« Reply #6 on: October 16, 2024, 02:02:00 am »
Have you tried the version of Jedi code format that comes with Lazarus?

components\jcf2\CommandLine\Lazarus\jcf.lpr

It is up to date and I think it works quite well (although my opinion is somewhat biased).

Yes! And you do fix issues with it! Thanks!

It does work well. The only current problem I have (and I need to file an issue) is that forcing of specific types to lower case stopped working, and I'm not sure when.
I looked at that section of code and it had been refactored, but I'm not sure if that is when it broke.

I will file a report.

Bogen85

  • Hero Member
  • *****
  • Posts: 703
Re: Code Prettifier / Formatter
« Reply #7 on: October 16, 2024, 02:08:50 am »
The command line JCF (Yes.... JEDI...) does get updates, but it breaks a lot...

"breaks a lot" is not completely accurate.
It could maybe use a better pipeline job.

But I should not complain, for the most part it works quite well for me, and the only one for me that works satisfactorily.

Roland57

  • Hero Member
  • *****
  • Posts: 529
    • msegui.net
Re: Code Prettifier / Formatter
« Reply #8 on: October 16, 2024, 10:33:24 am »
The most suitable for me I found is this one, but its not enough:
https://gitlab.com/rchastain/pcf

If you suggest some improvements, maybe I could take a look. (I am the maintainer of the project.)

What does it lack?
My projects are on Codeberg.

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: Code Prettifier / Formatter
« Reply #9 on: October 16, 2024, 01:03:59 pm »
I would need some time to check all the little things and make a report, but Ill give you an example right away:

Original code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. const
  8.   CP_ACP     = 0;     // default to ANSI code page
  9.   CP_OEMCP   = 1;     // default to OEM (console) code page
  10.   CP_UTF16             = 1200;  // utf-16
  11.   CP_UTF16BE           = 1201;  // unicodeFFFE
  12.   CP_UTF7 = 65000; // utf-7
  13.   CP_UTF8 = 65001; // utf-8
  14.   CP_ASCII   = 20127; // us-ascii
  15.   CP_NONE    = $FFFF; // rawbytestring encoding
  16.  
  17. procedure fpc_ansistr_assign2(   var dests:pointer;s2:pointer); stdcall; compilerproc;
  18.  
  19. implementation
  20.  
  21. procedure fpc_ansistr_assign2(var dests: pointer; s2: pointer);    stdcall; [public    , alias: 'FPC_ANSISTR_ASSIGN2'];
  22. begin
  23. end;
  24.  
  25. procedure fpc_ansistr_assign2(var dests: pointer; s2: pointer); [  external name   'FPC_ANSISTR_ASSIGN2'];
  26.  
  27. procedure test_empty;
  28. begin
  29. end;
  30.  
  31. procedure test_short;
  32. var
  33.   a: shortstring;
  34. begin
  35.   a := '----';
  36.   writeln('short = ', a);
  37. end;
  38.  
  39. end.

PCF formatted:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$MODE ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. const
  8.   CP_ACP = 0; // default to ANSI code page
  9.   CP_OEMCP = 1; // default to OEM (console) code page
  10.   CP_UTF16 = 1200; // utf-16
  11.   CP_UTF16BE = 1201; // unicodeFFFE
  12.   CP_UTF7 = 65000; // utf-7
  13.   CP_UTF8 = 65001; // utf-8
  14.   CP_ASCII = 20127; // us-ascii
  15.   CP_NONE = $FFFF; // rawbytestring encoding
  16.  
  17. procedure fpc_ansistr_assign2(var dests: pointer; s2: pointer); stdcall; compilerproc;
  18.  
  19. implementation
  20.  
  21. procedure fpc_ansistr_assign2(var dests: pointer; s2: pointer); stdcall; [public, alias: 'FPC_ANSISTR_ASSIGN2'];
  22. begin
  23. end;
  24.  
  25. procedure fpc_ansistr_assign2(var dests: pointer; s2: pointer); [external name 'FPC_ANSISTR_ASSIGN2'];
  26.  
  27.   procedure test_empty;
  28.   begin
  29.   end;
  30.  
  31.   procedure test_short;
  32.   var
  33.     a: shortstring;
  34.   begin
  35.     a := '----';
  36.     writeln('short = ', a);
  37.   end;
  38.  
  39. end.

So the first thing is the align in the consts (JCF aligns that fine). The second thing is the weird indent after "external" procedure.

I would make a much longer list with little things but it would take me some (long) time. Are you willing to fix it all?

Thaddy

  • Hero Member
  • *****
  • Posts: 18324
  • Here stood a man who saw the Elbe and jumped it.
Re: Code Prettifier / Formatter
« Reply #10 on: October 16, 2024, 01:39:57 pm »
It can be done, but it looks like it needs a huge look ahead buffer, because you do not know beforehand where to align.
(Or back prop, but that is the same, only slower, worst case you need both, on block level)
« Last Edit: October 16, 2024, 01:44:54 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Roland57

  • Hero Member
  • *****
  • Posts: 529
    • msegui.net
Re: Code Prettifier / Formatter
« Reply #11 on: October 16, 2024, 03:59:09 pm »
So the first thing is the align in the consts (JCF aligns that fine).

Thank you for the example.

The second thing is the weird indent after "external" procedure.

I never saw function headers like that.  %)

I would make a much longer list with little things but it would take me some (long) time. Are you willing to fix it all?

I cannot promise to fix all, because I am not sure of success, but I promise to take a look.

By the way, there is a bug that I have been hunting for a long time and that I would glad to fix.

Both versions of the GUI (LCL, MSEgui) would also need to be finished.
My projects are on Codeberg.

 

TinyPortal © 2005-2018