jscheid / prettier.el

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

apheleia integration #134

Closed haze closed 1 month ago

haze commented 2 months ago

Is your feature request related to a problem? Please describe. I'd like to use apheleia with prettier.el. is that possible?

Describe the solution you'd like The ability to supply a prettier.el function to apheleia that formats the buffer.

Describe alternatives you've considered For the time being I've disabled apheleia in the buffers that matter, but it would be nice if I could integrate the two.

Additional context I tried modifying prettier.el to accept a buffer to write the changes to but I got distracted and didn't get very far. Is that a viable path forward?

jscheid commented 2 months ago

Could you explain more what you're trying to achieve? Apheleia already has support for Prettier formatting.

haze commented 2 months ago

Aphelia's integration works by executing prettier on the CLI. I'd instead like to use the long lived process prettier.el spins up to format the buffer instead of shelling out to the CLI.

jscheid commented 2 months ago

OK, but why do you want to integrate the two, can't you just use prettier.el for some buffers and Apheleia for others?

haze commented 2 months ago

I could. I'm finding less and less reason to. I think I want apheleia to be the driver (so I can keep the global mode on, since it works with way more kinds of buffers.) I don't mind keeping them separate. I wonder if using with-current-buffer as the scratch provided buffer from apheleia would work without any modifications to prettier.el...

jscheid commented 2 months ago

Ah, I see now what you're aiming for. I think this is more a question for the Apheleia guys then, i.e. how do you hook into their save process to do something other than invoking an external command? From there you should get pretty far with prettier-prettify as long as you copy major-mode from the main buffer.

haze commented 2 months ago

I tried hooking the two together, but I can't seem to get prettier-prettify to work with a buffer that isn't saved to disk.

haze commented 1 month ago

Here's my integration

  (cl-defun haze--apheleia-prettier-el-wrapper (&key buffer scratch callback &allow-other-keys)
    "Wrapper function for Apheleia that uses prettier.el underneath.

Use prettier.el to format BUFFER.
Calls CALLBACK once completed."
    (with-current-buffer scratch
      (funcall (with-current-buffer buffer major-mode)) ; copy major-mode from buffer to scratch
      (setq-local exec-path (with-current-buffer buffer exec-path)) ; copy exec-path to find prettier
      (set-visited-file-name (with-current-buffer buffer buffer-file-name) t) ; copy file path so we don't try and find a global prettier
      (goto-char (point-min))
      (prettier-prettify)
      (set-visited-file-name nil)
      (funcall callback)))
jscheid commented 1 month ago

Amazing, glad you got it working, and thanks for sharing it here!

haze commented 1 week ago

For the record, the integration posted above has hiccups when the buffer has errors in it, but I haven't had the time to make it resilient