radian-software / apheleia

🌷 Run code formatter on buffer contents without moving point, using RCS patches and dynamic programming.
MIT License
517 stars 73 forks source link

Bug in apheleia-formatters-indent #274

Closed 0x210F closed 6 months ago

0x210F commented 6 months ago

The conditional clause at line 34 in apheleia-utils.el (function apheleia-formatters-indent) https://github.com/radian-software/apheleia/blob/b2d2b50f1a8b4cfcb7aa9fa815017bac26552da9/apheleia-utils.el#L34 prevents the execution of the clause's body when indent-var is nil. The problem is fixed by either making that clause the default (replace indent-var with t) or change the condition to (null indent-var), which makes the diff bigger but the logic is more explicit.

diff --git a/apheleia-utils.el b/apheleia-utils.el
index 5e8b364..0ff8808 100644
--- a/apheleia-utils.el
+++ b/apheleia-utils.el
@@ -31,29 +31,28 @@ always returns nil to defer to the formatter."
   (cond
    ((not apheleia-formatters-respect-indent-level) nil)
    (indent-tabs-mode tab-flag)
-   (indent-var
-    (unless indent-var
-      (setq indent-var
-            (cl-case major-mode
-              (cperl-mode 'cperl-indent-level)
-              (css-mode 'css-indent-offset)
-              (css-ts-mode 'css-indent-offset)
-              (js-jsx-mode 'js-indent-level)
-              (js-ts-mode 'js-indent-level)
-              (js-mode 'js-indent-level)
-              (js2-jsx-mode 'js2-basic-offset)
-              (js2-mode 'js2-basic-offset)
-              (js3-mode 'js3-indent-level)
-              (json-mode 'js-indent-level)
-              (json-ts-mode 'json-ts-mode-indent-offset)
-              (nxml-mode 'nxml-child-indent)
-              (robot-mode 'robot-mode-basic-offset)
-              (perl-mode 'perl-indent-level)
-              (scss-mode 'css-indent-offset)
-              (web-mode 'web-mode-indent-style)
-              (tsx-ts-mode 'typescript-ts-mode-indent-offset)
-              (typescript-mode 'typescript-indent-level)
-              (typescript-ts-mode 'typescript-ts-mode-indent-offset))))
+   ((null indent-var)
+     (setq indent-var
+           (cl-case major-mode
+             (cperl-mode 'cperl-indent-level)
+             (css-mode 'css-indent-offset)
+             (css-ts-mode 'css-indent-offset)
+             (js-jsx-mode 'js-indent-level)
+             (js-ts-mode 'js-indent-level)
+             (js-mode 'js-indent-level)
+             (js2-jsx-mode 'js2-basic-offset)
+             (js2-mode 'js2-basic-offset)
+             (js3-mode 'js3-indent-level)
+             (json-mode 'js-indent-level)
+             (json-ts-mode 'json-ts-mode-indent-offset)
+             (nxml-mode 'nxml-child-indent)
+             (robot-mode 'robot-mode-basic-offset)
+             (perl-mode 'perl-indent-level)
+             (scss-mode 'css-indent-offset)
+             (web-mode 'web-mode-indent-style)
+             (tsx-ts-mode 'typescript-ts-mode-indent-offset)
+             (typescript-mode 'typescript-indent-level)
+             (typescript-ts-mode 'typescript-ts-mode-indent-offset)))

     (when-let ((indent (and indent-var
                             (boundp indent-var)
raxod502 commented 6 months ago

I think since the when-let at the bottom is inside the cond clause, setting the condition to (null indent-var) won't do quite the right thing - it would skip the when-let when indent-var is passed as an optional argument to the function. But your other suggestion of setting the condition to t sounds exactly correct, see https://github.com/radian-software/apheleia/pull/277

raxod502 commented 6 months ago

Thank you!

0x210F commented 6 months ago

You're welcome. Thanks for fixing it!