Awesome
Jedi Code Formatter CLI
I 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.
How To Build Using Lazarus
- 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.
How To Build Using VS Code
- 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. 😊
How To Use JCF in VS Code
- 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.
The Problem With JCF
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.
Demo
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! 😊