Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic Indentation #9

Closed
dannyfreeman opened this issue May 14, 2023 · 10 comments
Closed

Semantic Indentation #9

dannyfreeman opened this issue May 14, 2023 · 10 comments
Assignees
Labels
enhancement New feature or request

Comments

@dannyfreeman
Copy link
Contributor

dannyfreeman commented May 14, 2023

Clojure-ts-mode currently only uses a simple fixed indentation style made up of two basic rules taken from Tonsky's blog post on Clojure indentation [0].

  • Multi-line lists that start with a symbol are always indented with two spaces,
  • Other multi-line lists, vectors, maps and sets are aligned with the first element (1 or 2 spaces).

This was a great way to get clojure-ts-mode off the ground. However, most Clojure developers are adjusted to formatting rules similar to those outlined in the Clojure Style Guide [1].

This ticket is for a feature to allow users to choose between the simple fixed indentation rules that exist now, and semantic indentation rules that match (as closely as possible) those laid out in the Clojure Style Guide.

[0] Better Clojure Formatting
[1] Clojure Style Guide

@dannyfreeman dannyfreeman self-assigned this May 14, 2023
@dannyfreeman dannyfreeman mentioned this issue May 14, 2023
14 tasks
@dawranliou
Copy link

Hey @dannyfreeman thank you for the great work! I've been using clojure-ts-mode as my daily driver since I saw the announcement blog post. It solved a pain point for me when working with humongous map literals. 🙏 Would you share how much collaboration you seek for this project? Would you like bug reports, or do you accept PRs at the current stage of the project?

Just for the people looking for a way to hack the formatting rules to comply with the Clojure Style Guide [1] before this issue #9 is resolved, here's the relevant code snippet from my config:

(with-eval-after-load 'clojure-ts-mode
  (require 'clojure-mode)
  (add-hook 'clojure-ts-mode-hook #'clojure-mode-variables)
  ;; ...
  )

This sets up many clojure-mode local variables just as the clojure-mode does, most noticeably the code and comment formats.

@dannyfreeman
Copy link
Contributor Author

It solved a pain point for me when working with humongous map literals.

Happy to hear this is doing some good out in the real world!

Would you share how much collaboration you seek for this project? Would you like bug reports, or do you accept PRs at the current stage of the project?

I haven't given it much thought, so far I have only received small bug reports. Happy to look at PRs though. Currently the only things I'm really thinking about are this semantic indentation issue, as well as bug fixes and improvements to the current code.

I will say that I have a pretty busy schedule between my work and my family. Usually I only have a couple weekend hours to get work done on this. If you end up opening a PR it may take some time for me to get around to reviewing it, but I will get around to it eventually.

@dannyfreeman dannyfreeman added the enhancement New feature or request label May 29, 2023
@jasonjckn
Copy link
Contributor

jasonjckn commented Jul 30, 2023

What do we have for prior art, for complex indentation rules, I know https://github.com/mickeynp/combobulate has complex indentation, as I recall, it's doing a lot of calculations crawling the treesit node/graph and working directly with the AST. Is this kind of what we had in mind? Or are there simpler ways to accomplish complex indentation rules. Combobulate indentation works really well in practice for languages it supports like Python, et al, but i'm still trying to wrap my head around its source code.

@dannyfreeman
Copy link
Contributor Author

dannyfreeman commented Jul 31, 2023

I haven't had much chance to look at it, but the plan was to see how much I could get away with implementing using the provided simple indent functionality. I would hope that is enough to say stuff like "if the parent list first child is a ->, indent with this special rule". I have no idea if the simple indent rules mechanisms will be flexible enough to do this. Other languages much more complex than clojure have gotten away with just using them so far

@dannyfreeman
Copy link
Contributor Author

Work in progress branch for implementing semantic indentation: https://github.com/clojure-emacs/clojure-ts-mode/compare/semantic-indentation

(Warning: I love to rebase branches as I work, commit history is subject to change)

@dannyfreeman
Copy link
Contributor Author

dannyfreeman commented Aug 6, 2023

My initial plan was to allow 2 formats.

  1. Fixed indentation (currently on main branch) 'fixed
  2. Semantic indentation 'semantic

But when looking at clojure-mode and cljfmt I notice some differences.
I'm thinking of creating three preconfigured nodes now

  1. Fixed indentation 'fixed
  2. Cljfmt default settings indentation 'cljfmt
  3. Clojure-mode style indentation 'clojure-mode

I don't think it will be too bad to implement both, I'll see what that looks.
I think cljfmt would be the default.

EDIT: I have a setting (setq clojure-indent-style 'align-arguments) in my emacs configuration that is not the default. I think default clojure-indent-style is more in line with cljfmt. Maybe I turned that on to fight less some former coworkers using intellij? Either way, I will at least name the second formatting style cljfmt instead of semantic to be clear about what it should do

@jasonjckn
Copy link
Contributor

jasonjckn commented Aug 12, 2023

Looking good so far... will you be able to support 'clojure-align-forms-automatically t' as well?

That's been the biggest usability issue for me with clojure-ts-mode, because I can always use on-save formatter like https://github.com/radian-software/apheleia with cljfmt + clojure-ts-mode to regain the cljfmt functionality, but clojure-mode.el is the only formatter i'm aware of with 'clojure-align-forms-automatically' indenting.

@jasonjckn
Copy link
Contributor

jasonjckn commented Aug 12, 2023

I was playing around with lisp-indent-function, and it seems to matter greatly what it is set to... I don't understand why that matters, but beware of it, here's the experiment

https://github.com/clojure-emacs/clojure-ts-mode/assets/550839/8613f35d-bdb6-4601-8cb5-e95117835133
higher res: https://drive.google.com/file/d/191_I0IwfxYVrv1BRCoiGgFSGtl0ln7xz/view?usp=sharing

(defun my/set-lisp-indent-lisp ()
  (interactive)

  (setq-local
  lisp-indent-function #'lisp-indent-function ; <-----------------

   indent-line-function #'treesit-indent
   indent-region-function #'treesit-indent-region
  ))

(defun my/set-lisp-indent-clojure ()
  (interactive)

  (setq-local
  lisp-indent-function #'clojure-indent-function ; <-----------------

   indent-line-function #'treesit-indent
   indent-region-function #'treesit-indent-region
  ))

(defun my/set-lisp-indent-treesit ()
  (interactive)

  (setq-local
  lisp-indent-function #'treesit-indent ; <-----------------

   indent-line-function #'treesit-indent
   indent-region-function #'treesit-indent-region
  ))

@dannyfreeman
Copy link
Contributor Author

Looking good so far... will you be able to support 'clojure-align-forms-automatically t' as well?

That's been the biggest usability issue for me with clojure-ts-mode, because I can always use on-save formatter like https://github.com/radian-software/apheleia with cljfmt + clojure-ts-mode to regain the cljfmt functionality, but clojure-mode.el is the only formatter i'm aware of with 'clojure-align-forms-automatically' indenting.

I don't think that will be part of the initial pass at semantic indentation. But it is probably do-able? I think we make a new issue for this.

I was playing around with lisp-indent-function, and it seems to matter greatly what it is set to... I don't understand why that matters, but beware of it, here's the experiment

Clojure-ts-mode doesn't use lisp-indent-function (i'm 90% sure). When it's value is the function from clojure-mode we'll want to match that. Either way, it is a lot to watch out for.

@dannyfreeman
Copy link
Contributor Author

The primary work for this is DONE now I believe. It is enabled by default on the main branch. A release has not yet been cut, but is available for testing (and should be on unstable melpa soon enough). Please open a new issue to report bugs with this.

I am currently aware that indentation within docstrings is a little too aggressive. I'm not sure how to handle it but I will open a new issue to track it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants