Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer not being replaced when formatting with lisp function? #203

Open
nafiz1001 opened this issue Aug 19, 2023 · 4 comments
Open

Buffer not being replaced when formatting with lisp function? #203

nafiz1001 opened this issue Aug 19, 2023 · 4 comments

Comments

@nafiz1001
Copy link

nafiz1001 commented Aug 19, 2023

Context

I've decided to write a lisp function for formatting with spotless because I think the list format wouldn't allow "-PspotlessIdeHook=" and filepath to be concatenated together. But when I tried this method it would insert the formatted code right below the original code.

  (cl-defun spotlessApply+ (&key buffer scratch callback &allow-other-keys)
    (let* ((filepath (buffer-file-name buffer))
	   (process-connection-type nil)
	   (stderr-buffer (generate-new-buffer "*spotlessApply stderr*"))
	   (sentinel (lambda (p e) (progn
				     (funcall callback)
				     (kill-buffer stderr-buffer))))
	   (spotless-process (make-process
			      :name "spotlessApply"
			      :buffer scratch
			      :command `(,(concat default-directory "gradlew")
					 "spotlessApply"
					 ,(concat "-PspotlessIdeHook=" filepath)
					 "-PspotlessIdeHookUseStdOut"
   					 "--quiet")
			      :sentinel sentinel
			      :stderr stderr-buffer)))))
  (push '(spotless . spotlessApply+)
  	apheleia-formatters)

;; temporarily set default-directory to root of project
  (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)

Example

Before:

package com.watchtower.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args); String hello = "world";
    }
}

After:

package com.watchtower.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args); String hello = "world";
    }
}
package com.watchtower.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
        String hello = "world";
    }
}

But when I run the cli manually, it only produces the formatted code.

@mohkale
Copy link
Contributor

mohkale commented Sep 29, 2023

Note: I plan to fix the inability to concatenate filepath in the work for #212.

@raxod502
Copy link
Member

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.

@mohkale
Copy link
Contributor

mohkale commented Oct 1, 2023

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.

@nafiz1001
Copy link
Author

nafiz1001 commented Oct 8, 2023

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))

raxod502 added a commit that referenced this issue Oct 13, 2023
Help alleviate confusion like
#203.

---------

Co-authored-by: Mohsin Kaleem <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants