radian-software / apheleia

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

Select wrong formatter when file is `mhtml-mode` #302

Closed liuyinz closed 2 months ago

liuyinz commented 2 months ago

In mhtml-mode, the var major-mode is changed when your point moved into different position:

  1. if point at , major-mode is identidied as css-mode;
  2. if point at , major-mode is identidied as js-mode;
  3. at other place, major-mode is mhtml-mode.

so when I point at section, apheleia select prettier-css which is wrong, to fix the problem we need to always get mhtml-mode as major-mode .

;; NOTE if current point is at mhtml submode, then
;; return `mhtml-mode' instead.
(or (and (get-text-property
          (if (and (eobp) (not (bobp)))
              (1- (point))
            (point))
          'mhtml-submode)
         'mhtml-mode)
    major-mode)

@raxod502 I guess need to fix function apheleia--get-formatters, does issue https://github.com/radian-software/apheleia/issues/29 is related to this either ?

raxod502 commented 2 months ago

One option would be to add to mhtml-mode-hook a function that sets apheleia-formatter to whatever you desire it to be. The buffer-local variable overrides automatic formatter detection.

But, since mhtml-mode is built in, I guess we should make this work by default. I'd like to do it in a way that does not special-case one particular mode in the internal code. Let me add something generic for that, one moment.

raxod502 commented 2 months ago

How do you feel about the approach in https://github.com/radian-software/apheleia/pull/303? I'm not sure if it's the best, but it seems not unreasonable to me.

liuyinz commented 2 months ago

LGTM. mhtml-mode is the only mode which can fontify css/js code correctly in html file so far, thanks for your great package !

raxod502 commented 1 month ago

I use web-mode, personally, but that has its own problems.