Add auto-formatting for d2.
This commit is contained in:
@@ -27,6 +27,29 @@
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(defun run-command-on-buffer-require-output (cmd &rest args)
|
||||||
|
"Run a command using the current buffer as stdin and replacing its contents if the command succeeds with the stdout from the command. This is useful for code formatters. This version only replaces the buffer contents if the command output some text."
|
||||||
|
(let (
|
||||||
|
(stdout-buffer (generate-new-buffer "tmp-stdout" t))
|
||||||
|
(full-cmd (append '(call-process-region nil nil cmd nil stdout-buffer nil) args))
|
||||||
|
)
|
||||||
|
(unwind-protect
|
||||||
|
(let ((exit-status (eval full-cmd)))
|
||||||
|
(if (eq exit-status 0)
|
||||||
|
(if (> (buffer-size stdout-buffer) 0)
|
||||||
|
(save-excursion
|
||||||
|
(replace-buffer-contents stdout-buffer)
|
||||||
|
)
|
||||||
|
(message "No output from command on buffer %s" (append (list cmd) args))
|
||||||
|
)
|
||||||
|
(message "FAILED running command on buffer %s" (append (list cmd) args))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(kill-buffer stdout-buffer)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
(defun run-command-in-directory (dir cmd &rest args)
|
(defun run-command-in-directory (dir cmd &rest args)
|
||||||
"Run a command in the specified directory. If the directory is nil, the directory of the file is used. The stdout result is trimmed of whitespace and returned."
|
"Run a command in the specified directory. If the directory is nil, the directory of the file is used. The stdout result is trimmed of whitespace and returned."
|
||||||
(let (
|
(let (
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
(defun d2-format-buffer ()
|
(defun d2-format-buffer ()
|
||||||
"Run prettier."
|
"Run prettier."
|
||||||
(interactive)
|
(interactive)
|
||||||
(run-command-on-buffer "d2" "fmt" "-")
|
(run-command-on-buffer-require-output "d2" "fmt" "-")
|
||||||
)
|
)
|
||||||
|
|
||||||
(use-package d2-mode
|
(use-package d2-mode
|
||||||
:commands (d2-mode)
|
:commands (d2-mode)
|
||||||
:hook (
|
:hook (
|
||||||
(d2-mode . (lambda ()
|
(d2-mode . (lambda ()
|
||||||
;; (add-hook 'before-save-hook 'd2-format-buffer nil 'local)
|
(add-hook 'before-save-hook 'd2-format-buffer nil 'local)
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -94,6 +94,9 @@ let
|
|||||||
output 'Unknown 0x095F 0x00000000' scale 1.5
|
output 'Unknown 0x095F 0x00000000' scale 1.5
|
||||||
output 'BOE 0x095F Unknown' scale 1.5
|
output 'BOE 0x095F Unknown' scale 1.5
|
||||||
output 'BOE 0x0BCA Unknown' scale 1.5
|
output 'BOE 0x0BCA Unknown' scale 1.5
|
||||||
|
|
||||||
|
# Breaks screen sharing:
|
||||||
|
# output 'Dell Inc. DELL U3014 P1V6N35M329L' render_bit_depth 10
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
window-management = pkgs.writeTextFile {
|
window-management = pkgs.writeTextFile {
|
||||||
|
|||||||
Reference in New Issue
Block a user