/jcf-cli

Jedi Code Formatter CLI

Primary LanguagePascalMIT LicenseMIT

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

  1. You must have Lazarus IDE already installed on your system.
  2. Clone or download this jcf-cli GitHub repo into your own folder.
  3. Start your Lazarus IDE and open jcf.lpi project within jcf-cli/CommandLine/Lazarus folder.
  4. Build it via Lazarus' RunBuild menu.
  5. Wait while Lazarus is building the JCF project.
  6. Take the executable JCF file from jcf-cli/Output/Lazarus folder along with the jcf.xml configuration file.
  7. Just to make sure, test it from Terminal using ./JCF -? command. It should show the usage manual.

How To Build Using VS Code

  1. You must have both Free Pascal compiler and VS Code already installed on your system.
  2. Clone or download this jcf-cli GitHub repo into your own folder.
  3. Start your VS Code and open jcf.lpr project within jcf-cli/CommandLine/Lazarus folder.
  4. Build it via VS Code's TasksRun Task...JCF: Build Release menu.
  5. Wait while FPC is building the JCF project.
  6. Open test.pas file from jcf-cli folder.
  7. Test JCF program using TasksRun Task...JCF: Test CLI Program menu and you should see the result in the test.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

  1. Copy the JCF and jcf.xml config files into your Pascal workspace folder.
  2. Create a new VS Code task or open the tasks.json if you already have one.
  3. 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": []
},
  1. It's a task to beautify Pascal code.
  2. If you need a task to obfuscate code, simply make another task using the code above, but then change -clarify arg into -obfuscate.
  3. 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! 😊