mirror of
https://github.com/godotengine/emacs-gdscript-mode.git
synced 2026-01-05 22:10:05 +03:00
Format buffer and region.
Closes #38. The implementation was simplified. Moreover, it is now possible to customize the path to the `gdformat` executable.
This commit is contained in:
@@ -2,6 +2,12 @@
|
||||
|
||||
This document lists new features, improvements, changes, and bug fixes in each release of the package.
|
||||
|
||||
## GDScript mode 1.?.?
|
||||
|
||||
### Features
|
||||
|
||||
- Added a command to format a selected region with `gdformat`.
|
||||
|
||||
## GDScript mode 1.2.0
|
||||
|
||||
### Features
|
||||
|
||||
@@ -110,5 +110,12 @@ PATH."
|
||||
"The path to the Godot executable.
|
||||
By default, it assumes that the executable is in the system's
|
||||
PATH."
|
||||
:type 'string
|
||||
:group 'gdscript)
|
||||
|
||||
(defcustom gdscript-gdformat-executable "gdformat"
|
||||
"The path to the gdformat executable.
|
||||
By default, it assumes that the executable is in the system's
|
||||
PATH."
|
||||
:type 'string
|
||||
:group 'gdscript)
|
||||
|
||||
@@ -31,65 +31,29 @@
|
||||
|
||||
;;; Code:
|
||||
|
||||
(defun gdscript-format--run-gdformat (buffer-input buffer-output buffer-error)
|
||||
"Call gdformat process.
|
||||
Argument BUFFER-INPUT reference to the input buffer to format.
|
||||
Argument BUFFER-OUTPUT the buffer to write the output of the gdformat call.
|
||||
Argument BUFFER-ERROR the buffer to write errors to."
|
||||
(with-current-buffer buffer-input
|
||||
(let ((process (make-process :name "gdformat"
|
||||
:command (list "gdformat" "-"):buffer
|
||||
buffer-output
|
||||
:stderr buffer-error
|
||||
:noquery t
|
||||
:sentinel (lambda (_process _event)))))
|
||||
(set-process-query-on-exit-flag (get-buffer-process buffer-error)
|
||||
nil)
|
||||
(set-process-sentinel (get-buffer-process buffer-error)
|
||||
(lambda (_process _event)))
|
||||
(save-restriction (widen)
|
||||
(process-send-region process
|
||||
(point-min)
|
||||
(point-max)))
|
||||
(process-send-eof process)
|
||||
(accept-process-output process nil nil t)
|
||||
(while (process-live-p process)
|
||||
(accept-process-output process nil nil t))
|
||||
(process-exit-status process))))
|
||||
(defun gdscript-format--format-region (start end)
|
||||
"Format the region between START and END using `gdformat'."
|
||||
(save-excursion
|
||||
(shell-command-on-region
|
||||
start end
|
||||
(concat
|
||||
"echo "
|
||||
(shell-quote-argument (buffer-substring start end))
|
||||
"|"
|
||||
gdscript-gdformat-executable " -")
|
||||
(buffer-name) t)))
|
||||
|
||||
(defun gdscript-format-buffer ()
|
||||
"Formats current buffer using 'gdformat'."
|
||||
(defun gdscript-format-region()
|
||||
"Format the selected region using `gdformat'"
|
||||
(interactive)
|
||||
(let* ((buffer-start (current-buffer))
|
||||
(point-start (point))
|
||||
(window-pos-start (window-start))
|
||||
(buffer-temp (get-buffer-create "*gdformat*"))
|
||||
(buffer-error (get-buffer-create "*gdformat-error*")))
|
||||
(dolist (buf (list buffer-temp buffer-error))
|
||||
(with-current-buffer buf
|
||||
(erase-buffer)))
|
||||
(condition-case err
|
||||
(if (/= 0 (gdscript-format--run-gdformat buffer-start buffer-temp
|
||||
buffer-error))
|
||||
(error "GDSCript formatter: failed, see buffer %s for details"
|
||||
(buffer-name buffer-error))
|
||||
(if (/= 0 (compare-buffer-substrings buffer-temp nil
|
||||
nil buffer-start nil nil))
|
||||
(progn
|
||||
(with-current-buffer buffer-temp
|
||||
(copy-to-buffer buffer-start
|
||||
(point-min)
|
||||
(point-max)))
|
||||
(goto-char point-start)
|
||||
(set-window-start (selected-window)
|
||||
window-pos-start)
|
||||
(message "gdscript formatter: success"))
|
||||
(message "gdscript formatter: nothing to do"))
|
||||
(mapc 'kill-buffer
|
||||
(list buffer-temp buffer-error)))
|
||||
(error (message "%s"
|
||||
(error-message-string err))
|
||||
(pop-to-buffer buffer-error)))))
|
||||
(gdscript-format--format-region
|
||||
(region-beginning) (region-end)))
|
||||
|
||||
(defun gdscript-format-buffer()
|
||||
"Format the entire current buffer using `gdformat'"
|
||||
(interactive)
|
||||
(gdscript-format--format-region
|
||||
(point-min) (point-max)))
|
||||
|
||||
(provide 'gdscript-format)
|
||||
|
||||
|
||||
@@ -60,6 +60,9 @@
|
||||
(define-key map (kbd "\t") 'company-complete)
|
||||
;; Insertion.
|
||||
(define-key map "\C-c\C-f" 'gdscript-completion-insert-file-path-at-point)
|
||||
;; Formatting.
|
||||
(define-key map "\C-c\C-t\C-r" 'gdscript-format-region)
|
||||
(define-key map "\C-c\C-t\C-t" 'gdscript-format-buffer)
|
||||
;; Run in Godot.
|
||||
(define-key map "\C-c\C-r\C-p" 'gdscript-godot-open-project-in-editor)
|
||||
(define-key map "\C-c\C-r\C-r" 'gdscript-godot-run-project)
|
||||
|
||||
Reference in New Issue
Block a user