aboutsummaryrefslogtreecommitdiffstats
path: root/oni-php
diff options
context:
space:
mode:
authorGravatar Tom Willemsen2024-12-30 09:06:26 -0800
committerGravatar Tom Willemsen2024-12-30 09:12:26 -0800
commitac7beb27febab26e3a5f3d1cae070336f27e9e57 (patch)
tree169136cdde1c6ccdaf0488fbc16b7e0873f3c887 /oni-php
parent3a9faa79c3ec9d40db6a07e24e2697ce7512897f (diff)
downloademacs-config-ac7beb27febab26e3a5f3d1cae070336f27e9e57.tar.gz
emacs-config-ac7beb27febab26e3a5f3d1cae070336f27e9e57.zip
oni-php: Add command to add use statement
Diffstat (limited to 'oni-php')
-rw-r--r--oni-php/oni-php.el21
-rwxr-xr-xoni-php/scripts/find-php-class19
2 files changed, 39 insertions, 1 deletions
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 <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"))
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)))))