Make import sort function customizable + only sort when necessary

master
Hugo Thunnissen 4 weeks ago
parent d38b28686a
commit f7fdac911b

@ -231,6 +231,24 @@ determine the scope of the imports (global or local namespace)."
(phpinspect-add-use-interactive type buffer project namespace) (phpinspect-add-use-interactive type buffer project namespace)
(phpinspect-buffer-parse buffer 'no-interrupt)))))) (phpinspect-buffer-parse buffer 'no-interrupt))))))
(defcustom phpinspect-import-sort-comparison #'string<
"The comparison function used to sort use statements."
:type 'function
:group 'phpinspect)
(defun phpinspect-sort-compare-import-strings (str1 str2)
(funcall phpinspect-import-sort-comparison str1 str2))
(defun phpinspect-seq-sorted-p (pred seq)
(let (prev)
(catch 'sorted
(seq-doseq (el seq)
(when prev
(unless (funcall pred prev el)
(throw 'sorted nil)))
(setq prev el))
t)))
(defun phpinspect-format-use-statements (buffer first-meta) (defun phpinspect-format-use-statements (buffer first-meta)
"Format a group of use statements to be sorted in alphabetical "Format a group of use statements to be sorted in alphabetical
order and have the right amount of whitespace. order and have the right amount of whitespace.
@ -254,24 +272,36 @@ group."
current) current)
statements)) statements))
(sort statements (lambda (a b) (string< (car a) (car b)))) (setq statements (nreverse statements))
;; Re-insert use statements
(with-current-buffer (phpinspect-buffer-buffer buffer) (with-current-buffer (phpinspect-buffer-buffer buffer)
(save-excursion (save-excursion
;; Check if use statements are already sorted
(if (phpinspect-seq-sorted-p
(lambda (a b)
(phpinspect-sort-compare-import-strings (car a) (car b)))
statements)
;; statements are sorted, skip to the end of the region
(goto-char end)
;; else: sort and re-insert
(sort statements (lambda (a b) (phpinspect-sort-compare-import-strings (car a) (car b))))
(goto-char start) (goto-char start)
(combine-after-change-calls (combine-after-change-calls
(delete-region start end) (delete-region start end)
(dolist (statement statements) (dolist (statement statements)
(phpinspect-codify-token (cdr statement)) (phpinspect-codify-token (cdr statement))
(insert-char ?\n))) (insert-char ?\n)))
;; go char backward for accurate whitespace detection in the code
;; below that removes/adds whitespace.
(backward-char))
(if (looking-at "[[:blank:]\n]+") (if (looking-at "[[:blank:]\n]+")
;; Delete excess trailing whitespace (there's more than 2 between the ;; Delete excess trailing whitespace (there's more than 2 between the
;; last use statement and the next token) ;; last use statement and the next token)
(when (< 1 (- (match-end 0) (match-beginning 0))) (when (< 2 (- (match-end 0) (match-beginning 0)))
(delete-region (match-beginning 0) (match-end 0)) (delete-region (match-beginning 0) (match-end 0))
(insert-char ?\n)) (insert-char ?\n 2))
;; Insert an extra newline (there's only one between the last use ;; Insert an extra newline (there's only one between the last use
;; statement and the next token) ;; statement and the next token)
(insert-char ?\n))))))) (insert-char ?\n)))))))

@ -201,8 +201,8 @@ class Baz {
namespace Not\\App; namespace Not\\App;
use App\\Bar; use App\\Bar;
use App\\Foo as FooBar;
use App\\Foo; use App\\Foo;
use App\\Foo as FooBar;
function bar(): Bar {} function bar(): Bar {}

Loading…
Cancel
Save