jscheid / prettier.el

Prettier code formatting for Emacs.
GNU General Public License v3.0
163 stars 11 forks source link

Nothing happens on save. before-save-hook is missing prettier--maybe-prettify-on-save #116

Closed vjohansen closed 1 year ago

vjohansen commented 1 year ago

A installed prettier a few days and so it working.

I added

(add-hook 'after-init-hook #'global-prettier-mode)

to my config.

A few days later I use it again and nothing happens when saving a changed file (tsx-mode).

before-save-hook is a variable defined in ‘files.el’.

Its value is (lsp--before-save t) Original value was nil Local in buffer App.tsx; global value is nil

Note the missing prettier--maybe-prettify-on-save

If i call prettier-prettify manually it works

Maybe it is related to 'local when calling add-hook

Note when i disable the minor mode the whole file is printed to Messages with an error at the first line

Global Prettier mode disabled prettier-process (local) quit unexpectedly: interrupt (import React, { useDebugValue, useEffect, useState } from 'react'; import logo from './logo.svg'; import './App.css';

GNU Emacs 28.2 (build 2, x86_64-w64-mingw32) of 2022-09-13

jscheid commented 1 year ago

The package currently gets enabled only for certain major modes. It's something I want to change but haven't time to do yet. That seems like the likely culprit. Can you check if prettier-mode is enabled in the buffer and what major mode the buffer uses?

vjohansen commented 1 year ago

I am using https://github.com/orzechowskid/tsx-mode.el which does weird stuff with multiple modes behind the scenes

I added a message call

(define-minor-mode prettier-mode
  "Runs prettier on file save when this mode is turned on."
  :lighter prettier-lighter
  (if prettier-mode
    (progn
      (message "prettier-mode TRUE '%s' '%s'" major-mode (buffer-name))

Calling global-prettier-mode in two places in a tsx-file (mode is tsx-mode) gives different messages

prettier-mode TRUE ’scss-mode’ ’ tsx-mode css’ prettier-mode TRUE ’graphql-mode’ ’ tsx-mode gql

tsx-mode is creating hidden buffers which probably is the reason that prettier is not working for this file.

My workaround is this

(add-hook 'before-save-hook #'vj-prettier-on-save)
(defun vj-prettier-on-save ()
  (when (and global-prettier-mode (equal major-mode 'tsx-mode))
    (prettier--maybe-prettify-on-save)))

Feel free to close this. I do not know enough about minor modes to give advice. The only thing could be to always remove 'local from (add-hook 'before-save-hook func) and the in fnuc do the check if prettier should run.

jscheid commented 1 year ago

OK I might close this then, a future version of this package will do something simpler not based on major modes. Glad it's working for you now.