1
0
Fork 0

oni-php: Add command to add use statement

This commit is contained in:
Tom Willemsen 2024-12-30 09:06:26 -08:00
parent 3a9faa79c3
commit ac7beb27fe
2 changed files with 39 additions and 1 deletions

View file

@ -4,7 +4,7 @@
;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local
;; Version: 2024.1224.093837
;; Version: 2024.1230.091219
;; Package-Requires: (php-mode oni-yasnippet oni-flycheck oni-company oni-hydra ggtags fic-mode company-php)
;; This program is free software; you can redistribute it and/or modify
@ -44,6 +44,10 @@
(expand-file-name "snippets" oni-php-root)
"The directory where oni-php stores its snippets.")
(defconst oni-php-scripts-dir
(expand-file-name "scripts" oni-php-root)
"The directory where oni-php stores its scripts.")
(defun oni-php-snippets-initialize ()
"Initialize the snippets for oni-php."
(when (boundp 'yas-snippet-dirs)
@ -66,6 +70,21 @@ nil for some reason."
(setq-local comment-auto-fill-only-comments t)
(auto-fill-mode))
(defun oni-php-add-use (class)
"Try to add a use statement for CLASS under point."
(interactive (list (symbol-name (symbol-at-point))))
(let* ((classes (read (shell-command-to-string (concat (expand-file-name "find-php-class" oni-php-scripts-dir) " " class))))
(class (if (> (length classes) 1)
(completing-read "Use: " classes nil t)
(car classes))))
(save-excursion
(goto-char (point-min))
(search-forward "use ")
(forward-paragraph)
(let ((start (point)))
(insert "use " class ";\n")
(pulse-momentary-highlight-region start (point))))))
(defhydra php-mode-hydra (:color blue)
("a" align-current "Align current selection"))

19
oni-php/scripts/find-php-class Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env -S scsh -e main -o srfi-1 -s
!#
(define (main args)
(let* ((class-name (cadr args))
(file-names
(run/strings (find "." -type f -name ,(string-append class-name ".php")
-exec grep "namespace" "{}" ";")
(> 2 "/dev/null"))))
(format #t "~s"
(delete-duplicates
(map (lambda (n)
(string-append
(match:substring
(regexp-search (rx "namespace " (submatch (+ any)) ";") n)
1)
"\\"
class-name))
file-names)))))