-
-
Notifications
You must be signed in to change notification settings - Fork 95
(Emacs) Better Curried Indentation
Default behavior for indentation in Emacs for js-mode and js2-mode is abysmal with having the arguments to a curried function span multiple lines, so here's a quick fix for that you can add into your config.
(If you're better than Elisp than I am, feel free to improve on this code here, because I know it's not perfect!)
(require 'js)
(advice-add 'js--chained-expression-p :after-until
(lambda ()
(save-excursion
(when (and (eq (char-after) ?\()
(js--find-newline-backward)
(progn
(skip-syntax-backward " \t")
(eq (char-before) ?\))))
(progn
(backward-list)
(current-column))))))
Basically, if 'js--chained-expression-p
returns nil (it's the function that checks for dot-chaining, so I found it to be most similar and a good starting point), it'll call my lambda there, which goes:
If the first character of the line is a (
, check if the last non-whitespace character of the previous line is a )
. If so, tell the indention method to indent to the beginning of that ()
block.
This has some slightly incorrect behavior that's not too hard to see if you think about how it works, but it's miles better than the existing behavior and doesn't actually override any other behavior since it's using the advice feature.
Example of how it looks:
const add =
def ('add')
({})
($.Number, $.Number, $.Number)
(x => y => x + y);