My hard fork of JEDI Code Formatter CLI from Bee Jay, which itself is a fork of Lazarus JCF, that I slightly improved with better indentation and much needed support for modern Delphi variable declarations. It was really hard to get around in its ancient, complex, and almost completely undocumented code. Still, I managed to implement what I wanted. It’s just hardly can be extended or improved any further given how tricky input source code can be and how difficult it is to target individual edge cases in the AST without breaking something else somewhere. In the future it would be better to switch to a formatter that doesn’t require deep parsing of the source.
(The updated VSCode extension that works with this version is also available, pull request.)
Changes include:
-
Support for Delphi inline variable declarations:
var I: Integer := 10;
With type inference:
var I := 42;
Inside
for
statement:for var I := Low (myArray) to High (myArray) do
Source files with inline variables no longer cause formatter to fail.
-
New option
IndentCaseLabels
to control indentation ofcase
label statements independently from the wholecase
block. E.g., withIndentCaseLabels
set toTrue
(default):case i of 1..9: for i := 1 to i do write(i, ','); 10: begin writeln; writeln; end; else myProcedure; end;
With
IndentCaseLabels
set toFalse
:case i of 1..9: for i := 1 to i do write(i, ','); 10: begin writeln; writeln; end; else myProcedure; end;
-
New option
IndentMethodParams
, so it’s possible to write method parameters like this (IndentMethodParams
set toFalse
):function myFunction(aParam: string ; aParam2: real): boolean;
And avoid second line being indented. Otherwise (set to
True
):function myFunction(aParam: string ; aParam2: real): boolean;
-
New option
IndentInterfaceGuid
to prevent interface GUID from being indented. Set toTrue
:IFace = interface ['{5E3C2BCA-56C8-46DE-959F-338AF5F69C1A}'] procedure proc; end;
Set to
False
:IFace = interface ['{5E3C2BCA-56C8-46DE-959F-338AF5F69C1A}'] procedure proc; end;
-
Formatter now correctly processes line endings inside comments and also avoids formatting control statements with comments in-between them.
-
The formatter source code has been processed through the formatter itself to validate correct function “in the wild”.
Unless I forgot something, all my changes are “tagged” with // fix:
comments.
Binaries are available. The 64-bit Windows build is from FPC. The 32-bit Windows version is built with Delphi. Other operating systems should build with little to no changes.
How to test:
pascal-format -config=pascal-format.new.cfg -out test.fmt.pas test.pas
TODO: with GUI now removed, debugging parsed AST is impossible, unless doing it by trial and error. The AST view needs to be rewritten to output in textual form, or, better yet, simply remade in LCL (but without Delphi support). Should be fairly simple to do given it’s basically just a tree view on an empty form.
So here it is in case if someone else wants to waste their time and continue trying to make this thing smarter.
Original description follows.
I (Bee Jay) took Jedi Code Formatter (JCF) from Lazarus IDE repository and made it as CLI (command line interface) version by removing all the GUI (graphical user interface) parts from the original GUI version. The CLI version can be used as Pascal code formatter in Visual Studio Code, or as backend engine of an online Pascal code beautifier.
Original: a copy (sometimes modified) of r823 jcf2 svn tree: https://jedicodeformat.svn.sourceforge.net/svnroot/jedicodeformat/trunk/CodeFormat/Jcf2
Original author: Anthony Steele.
Original license: MPL 1.1.
- You must have Lazarus IDE already installed on your system.
- Clone or download this
jcf-cli
GitHub repo into your own folder. - Start your Lazarus IDE and open
jcf.lpi
project withinjcf-cli/CommandLine/Lazarus
folder. - Build it via Lazarus' Run → Build menu.
- Wait while Lazarus is building the JCF project.
- Take the executable
JCF
file fromjcf-cli/Output/Lazarus
folder along with thejcf.xml
configuration file. - Just to make sure, test it from Terminal using
./JCF -?
command. It should show the usage manual.
- You must have both Free Pascal compiler and VS Code already installed on your system.
- Clone or download this
jcf-cli
GitHub repo into your own folder. - Start your VS Code and open
jcf.lpr
project withinjcf-cli/CommandLine/Lazarus
folder. - Build it via VS Code's Tasks → Run Task... → JCF: Build Release menu.
- Wait while FPC is building the JCF project.
- Open
test.pas
file fromjcf-cli
folder. - Test
JCF
program using Tasks → Run Task... → JCF: Test CLI Program menu and you should see the result in thetest.pas
file.
Note: I've included the executable file for Linux, Mac, and Windows in
Output/Lazarus
folder so you don't need to build it yourself. However, it's not guaranteed using the latest modification. 😊
- Copy the
JCF
andjcf.xml
config files into your Pascal workspace folder. - Create a new VS Code task or open the
tasks.json
if you already have one. - Copy the task example below and paste it into your
tasks.json
file.
{
"label" : "JCF: Beautify Code",
"type" : "shell",
"command": "./JCF",
"args": [
"${file}",
"-clarify",
"-inplace",
"-config=jcf.xml"
],
"presentation": {
"reveal": "never"
},
"problemMatcher": []
},
- It's a task to beautify Pascal code.
- If you need a task to obfuscate code, simply make another task using the code above, but then change
-clarify
arg into-obfuscate
. - Save your
tasks.json
. Now you should have new JCF's tasks in your tasks list.
Although JCF is a good Pascal code formatter, it has one single problem that quite annoying. JCF requires the code must be compilable which means it has to be a complete program and syntactically correct. JCF will fail on code snippets or wrong code. To make it works on code snippet, it must be put between a begin..end
pair and has a correct program
header, like this:
program test;
begin
// put code snippet here
end.
Here's JCF CLI in action within VS Code (with OmniPascal):
Note: If you're also interested in my other tasks shown in the demo, see my gist about it here.
Hope it's gonna be useful to other Pascal fellows out there. Have fun! 😊