fxbois / web-mode

web template editing mode for emacs
https://web-mode.org
GNU General Public License v3.0
1.64k stars 260 forks source link

Can I disable indentation on JavaScript function calls that are chained onto a new line? - .then(), .catch() #1200

Closed aguynamedben closed 2 years ago

aguynamedben commented 3 years ago

I've been using web-mode for 3+ years now to code JavaScript. Thank you for the work you do on this project. 🙏

web-mode has trained me that Promise handling in JavaScript should be indented like this:

promiseTheWorld
  .then((planet) => { log.info(`lol you got ${planet.name} instead`); })
  .catch((error) => {
    log.error(`lol the universe exploded`);
    app.quit();
  });

But my team wants to do it like this:

promiseTheWorld
.then((planet) => { log.info(`lol you got ${planet.name} instead`); })
.catch((error) => {
  log.error(`lol the universe exploded`);
  app.quit();
});

Is there a way to disable indentation on chained method calls? The documentation describes how to disable some lineups—lineup-args, lineup-calls, lineup-concats, lineup-ternary—and I thought disabling lineup-calls might do it, but it doesn't seem to be affecting chained method calls. I also reviewed the source code and didn't see anything that seemed to do this. I'm happy to donate if this is an enhancement.

Here's my config:

(use-package web-mode
  :ensure t
  :mode
  (("\\.erb\\'" . web-mode)
   ("\\.html\\'" . web-mode)
   ("\\.tpl\\'" . web-mode)
   ("\\.js\\'" . web-mode)
   ("\\.jsx\\'" . web-mode)
   ("\\.json\\'" . web-mode)
   ("\\.hbs\\'" . web-mode))
  :custom
  (web-mode-css-indent-offset 2)
  (web-mode-markup-indent-offset 2)
  (web-mode-code-indent-offset 2)
  (web-mode-enable-auto-quoting nil)
  (web-mode-enable-current-element-highlight t)
  (web-mode-enable-literal-interpolation t)

  ;; Indent inline JS/CSS within HTML
  ;; https://stackoverflow.com/a/36725155/3516664
  (web-mode-script-padding 2)
  (web-mode-style-padding 2)
  (web-mode-block-padding 2))

Here's my config when I try disabling lineup-calls, which doesn't seem to affect chained calls:

(use-package web-mode
  :ensure t
  :mode
  (("\\.erb\\'" . web-mode)
   ("\\.html\\'" . web-mode)
   ("\\.tpl\\'" . web-mode)
   ("\\.js\\'" . web-mode)
   ("\\.jsx\\'" . web-mode)
   ("\\.json\\'" . web-mode)
   ("\\.hbs\\'" . web-mode))
  :custom
  (web-mode-css-indent-offset 2)
  (web-mode-markup-indent-offset 2)
  (web-mode-code-indent-offset 2)
  (web-mode-enable-auto-quoting nil)
  (web-mode-enable-current-element-highlight t)
  (web-mode-enable-literal-interpolation t)

  ;; Indent inline JS/CSS within HTML
  ;; https://stackoverflow.com/a/36725155/3516664
  (web-mode-script-padding 2)
  (web-mode-style-padding 2)
  (web-mode-block-padding 2)
  :config
  (add-to-list 'web-mode-indentation-params '("lineup-calls" . nil)))
aguynamedben commented 3 years ago

By my read of this post I think the thing I'm fighting here is indentation, not lineup. Basically I want to tell web-mode to not indent the line if it's JavaScript and starts with . 🤔