diff --git a/oni-php/oni-php.el b/oni-php/oni-php.el index 1f42ddb..941a6fe 100644 --- a/oni-php/oni-php.el +++ b/oni-php/oni-php.el @@ -4,7 +4,7 @@ ;; Author: Tom Willemse ;; 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")) diff --git a/oni-php/scripts/find-php-class b/oni-php/scripts/find-php-class new file mode 100755 index 0000000..65f43fc --- /dev/null +++ b/oni-php/scripts/find-php-class @@ -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)))))