all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Need assistance in figuring out what reformats my code on save
@ 2021-09-05 11:10 Steinar Bang
  2021-09-05 18:43 ` Tassilo Horn
  0 siblings, 1 reply; 3+ messages in thread
From: Steinar Bang @ 2021-09-05 11:10 UTC (permalink / raw)
  To: help-gnu-emacs

I am trying to remove the configuration that causes web-mode to reformat
my code when saving, because the formatting breaks in the eslint build
step (because eslint prettier and web-mode disagrees).

I have tried to remove the code which looked like it was doing the
formatting, ie.
  (add-hook 'before-save-hook 'tide-format-before-save)

But even after removing this, and with before-save-hook having the value
nil, reformatting takes place on save.

The reformatting I get are changes to indentation, and are the same
changes I get, when I press TAB on the changed lines.

Are there any other hooks that could be involved?
Is there reformatting on save that don't use hooks?
(I'm assuming it is something in my config but I haven't been able to
determine what)

Thanks!


- Steinar

More detail here: https://github.com/ananthakumaran/tide/discussions/434
Emacs version is 27.1 on mac OS/X.

The web-mode/tide configuration in my .emacs, is:
(when (locate-library "web-mode")
  (defun setup-tide-mode ()
    (interactive)
    (tide-setup)
    (flycheck-mode +1)
    (setq flycheck-check-syntax-automatically '(save mode-enabled))
    (eldoc-mode +1)
    (tide-hl-identifier-mode +1)
    (company-mode +1))

  (setq company-tooltip-align-annotations t)

  ;; formats the buffer before saving
  (add-hook 'before-save-hook 'tide-format-before-save)

  (add-hook 'typescript-mode-hook #'setup-tide-mode)

  (require 'web-mode)
  (add-to-list 'auto-mode-alist '("\\.tsx\\'" . web-mode))
  (add-hook 'web-mode-hook
            (lambda ()
              (when (string-equal "tsx" (file-name-extension buffer-file-name))
                (setup-tide-mode))))
  (flycheck-add-mode 'typescript-tslint 'web-mode))



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need assistance in figuring out what reformats my code on save
  2021-09-05 11:10 Need assistance in figuring out what reformats my code on save Steinar Bang
@ 2021-09-05 18:43 ` Tassilo Horn
  2021-09-06 14:26   ` Steinar Bang
  0 siblings, 1 reply; 3+ messages in thread
From: Tassilo Horn @ 2021-09-05 18:43 UTC (permalink / raw)
  To: Steinar Bang; +Cc: help-gnu-emacs

Steinar Bang <sb@dod.no> writes:

> Are there any other hooks that could be involved?

Maybe, although before-save-hook seems like a plausible candidate.

> Is there reformatting on save that don't use hooks?

You could use an advice but why would anyone do that given that there
are suitable hooks.

I'd try evaluating

  (add-hook 'before-change-functions #'debug nil t)

in a buffer where that formatting before save happens and then save.
That should put you in the debugger and the backtrace will hopefully
show you who and from where the formatting is triggered.

HTH,
Tassilo



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need assistance in figuring out what reformats my code on save
  2021-09-05 18:43 ` Tassilo Horn
@ 2021-09-06 14:26   ` Steinar Bang
  0 siblings, 0 replies; 3+ messages in thread
From: Steinar Bang @ 2021-09-06 14:26 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> Tassilo Horn <tsdh@gnu.org>:

> I'd try evaluating

>   (add-hook 'before-change-functions #'debug nil t)

> in a buffer where that formatting before save happens and then save.
> That should put you in the debugger and the backtrace will hopefully
> show you who and from where the formatting is triggered.

Thanks! That worked!

I did
 `M-: (add-hook 'before-change-functions #'debug nil t) RET`
in a .tsx buffer in a restarted emacs with nil before-save-hook and I
found that the reformatting is done by web-mode-on-post-command and is
run immediately after my change, and not just before save.

web-mode-on-post-command is run from the post-command-hook, and runs
indent-region on the buffer when the command is a yank
https://github.com/fxbois/web-mode/blob/master/web-mode.el#L2918

The formatting done by tide-format-before-save is different from the
formatting done by indent-region in web-mode-on-post-command, and with
tide-format-before-save in place, it's the tide-format-before-save
formatting that wins.

(In case someone finds this when googling: making tsfmt, or making
web-mode, format exactly like prettier, both seemed like too much work.
So I removed tide formatting from web-mode and used "prettier-js for
emacs" instead. See https://github.com/ananthakumaran/tide/discussions/434#discussioncomment-1285992
for details)



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-06 14:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-05 11:10 Need assistance in figuring out what reformats my code on save Steinar Bang
2021-09-05 18:43 ` Tassilo Horn
2021-09-06 14:26   ` Steinar Bang

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.