Use JuliaFormatter.jl as backend for indenting code
Find a file
Alexander-Barth e958727229 update to versioned Manifest (#22)
update Manifest.toml and name it Manifest-v1.11.toml
see #20

Reviewed-on: #22
Reviewed-by: FelipeLema <felipelema@noreply.codeberg.org>
Co-authored-by: Alexander-Barth <alexander-barth@noreply.codeberg.org>
Co-committed-by: Alexander-Barth <alexander-barth@noreply.codeberg.org>
2025-05-29 15:16:45 +02:00
code_of_conduct.md add Code of Conduct 2020-10-08 00:41:11 -03:00
formatter_service.jl Fix cursor line being erased (#17) 2024-06-22 21:25:49 +00:00
julia-formatter.el remove dev remainder 2025-05-24 19:38:04 -04:00
LICENSES expand "Comentaries" section, expand on licenses 2020-09-28 17:01:49 -03:00
Makefile clean unused code 2023-02-22 00:05:57 -03:00
makem.sh migrate checks & lints to makem.sh 2021-12-09 13:07:02 -03:00
Manifest-v1.11.toml update to versioned Manifest (#22) 2025-05-29 15:16:45 +02:00
Project.toml ]add PackageCompiler 2023-02-15 17:02:42 -03:00
README.md use emacs to parse .JuliaFormatter.toml fix #14 2023-11-30 12:12:59 -03:00
toml-respects-json.el use emacs to parse .JuliaFormatter.toml fix #14 2023-11-30 12:12:59 -03:00

Use JuliaFormatter.jl to format julia code in Emacs.

Contributor Covenant

MELPA

Why?

There are several issues with current indentation with julia-mode. Several solutions (tree-sitter, SMIE, fixing current indentation engine) seem impractical.

Usage

The simplest way to use this package is by activating format on save on Julia buffers like so:

  ;; load julia-formatter.el after downloading this package
  ;; You can also install with MELPA or with straight.el and skip this line
  (load-file "/somewhere/to/julia-formatter.el/julia-formatter.el")

  (require 'julia-formatter)
  (add-hook 'julia-mode-hook #'julia-formatter-mode)

Alternatively, formatting can be done live by activating `aggressive-indent-mode' and setting the proper indentation functions (yes, indentation functionality can be leveraged for live formatting).

Like so:

  (add-hook 'julia-formatter-mode-hook #'aggressive-indent)

Julia Image compilation prompt

This package currently suffers Julia's "the first plot problem". That means that Julia will compile a bunch of code for a while just before the first executed format can be finished. This translates into Emacs freezing after the first formatting-of-julia-code.

This is why when you first load julia-formatter-mode Emacs will prompt you (the user) if you want to compile a Julia image. You should do this (press y) to avoid Emacs freezing. Compilation will take a while (and most likely turn your computer into a landing private jet), but after it's finished you won't have Emacs freezing again.

If you don't want to be prompted about the missing image, you should customize julia-formatter-should-compile-julia-image (using M-x customize-variable).

Doom Emacs

Because this package also contains project files to handle Julia dependencies, we need to explicitly tell Doom (through straight.el): Like so:

;; add the following to packages.el
(package! julia-mode) ;; you probably already have this line
(package! julia-formatter
  :recipe (:host codeberg :repo "FelipeLema/julia-formatter.el"
           :files ( "julia-formatter.el" ;; main script executed by Emacs
                    "toml-respects-json.el" ;; script to parse format config toml files
                    "formatter_service.jl" ;; script executed by Julia
                    "Manifest.toml" "Project.toml" ;; project files
                    )))

No other dependencies ?

No. Julia packages are handled with this repo's Project.toml & Manifest.toml files.

Think Python's virtual-envs.

How

This packages is comprised by several pieces.

  • Pack text formatting (JuliaFormatter.jl) into a service that will be used with live coding.
  • Expose this service with JSON-RPC on stdin / stdout.
  • Boot/start this service from Emacs
  • Send julia code from a buffer (by either manual command or automatized indent) to the service, get formatted code and paste it into the buffer.

The exposure of JuliaFormatter.jl is done as a service because calling format_text() as a standalone script takes a lot of time. This would hinder the coding process.

The code that's being formatted must be self-contained (parseable, all if's and while's must have a corresponding end, all parentheses must be closed, etc). This is a requirement from JuliaFormatter.jl since it will fail if the code cannot be fully parsed.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Addendum

See https://github.com/domluna/JuliaFormatter.jl