Open nafiz1001 opened 1 year ago
Note: I plan to fix the inability to concatenate filepath in the work for #212.
The scratch
buffer starts with the file contents populated in it. You need to modify it in place, or erase it first. (Though if you erase it first then your formatter won't work when chained after another formatter.) If you just insert the formatted contents, you'll end up with two copies, as reported here.
Note: I don't think it's very straightforward to fix the filepath concatenation issue sadly, however you can use (apheleia-formatters-local-buffer-file-name)
to access the filepath in most situations. There's some nuances around remote files but most of the time that should work fine for you.
Note: I don't think it's very straightforward to fix the filepath concatenation issue sadly, however you can use
(apheleia-formatters-local-buffer-file-name)
to access the filepath in most situations. There's some nuances around remote files but most of the time that should work fine for you.
Thanks your idea worked. At least for my use-case :)
(defun shou/fix-apheleia-project-dir (orig-fn &rest args)
(let ((default-directory (vc-root-dir)))
(apply orig-fn args)))
(advice-add 'apheleia-format-buffer :around #'shou/fix-apheleia-project-dir)
(push '(spotless . ((concat default-directory "gradlew")
"spotlessApply"
(concat "-PspotlessIdeHook=" (apheleia-formatters-local-buffer-file-name))
"-PspotlessIdeHookUseStdIn"
"-PspotlessIdeHookUseStdOut"
"--quiet"))
apheleia-formatters)
(setf (alist-get 'java-mode apheleia-mode-alist)
'(spotless))
Context
I've decided to write a lisp function for formatting with spotless because I think the list format wouldn't allow
"-PspotlessIdeHook="
andfilepath
to be concatenated together. But when I tried this method it would insert the formatted code right below the original code.Example
Before:
After:
But when I run the cli manually, it only produces the formatted code.