diff options
Diffstat (limited to 'oni-php')
| -rw-r--r-- | oni-php/Eldev | 4 | ||||
| -rw-r--r-- | oni-php/oni-php.el | 181 | ||||
| -rwxr-xr-x | oni-php/scripts/find-php-class | 5 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/arrow-function | 6 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/casts | 7 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/constructor | 8 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/for | 7 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/function-expression | 8 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/function-statement | 11 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/if-statement | 7 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/logged-debug | 5 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/logger-info | 5 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/namespace | 9 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/php | 5 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/strict-types | 5 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/test-class | 14 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/test-comment | 9 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/test-name | 8 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/var | 5 | ||||
| -rw-r--r-- | oni-php/snippets/php-mode/wpheader.php | 13 |
20 files changed, 147 insertions, 175 deletions
diff --git a/oni-php/Eldev b/oni-php/Eldev index 1ba3134..2956775 100644 --- a/oni-php/Eldev +++ b/oni-php/Eldev @@ -4,10 +4,10 @@ (eldev-use-package-archive 'gnu-elpa) ;(eldev-use-package-archive 'nongnu-elpa) (eldev-use-package-archive 'melpa) -(eldev-use-package-archive `("oni" . ,(expand-file-name "../bin"))) +(eldev-use-package-archive `("oni" . ,(expand-file-name "../public/packages"))) (eldev-use-plugin 'autoloads) (setf eldev-files-to-package `(:or ,eldev-files-to-package - '("*.el" "./snippets/**/*"))) + '("*.el" "scripts/*"))) diff --git a/oni-php/oni-php.el b/oni-php/oni-php.el index 88af7af..89b53c6 100644 --- a/oni-php/oni-php.el +++ b/oni-php/oni-php.el @@ -4,8 +4,8 @@ ;; Author: Tom Willemse <tom@ryuslash.org> ;; Keywords: local -;; Version: 2026.0107.143829 -;; Package-Requires: (php-mode oni-yasnippet oni-flycheck oni-company oni-hydra oni-smartparens ggtags fic-mode company-php rainbow-delimiters rainbow-identifiers) +;; Version: 2026.0629.012633 +;; Package-Requires: (php-mode oni-flycheck oni-hydra fic-mode rainbow-delimiters rainbow-identifiers) ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -28,11 +28,12 @@ ;;; Code: (require 'align) -(require 'company) +(require 'grep) (require 'hydra) +(require 'map) (require 'php-mode) +(require 'project) (require 'whitespace) -(require 'yasnippet) (defconst oni-php-root (file-name-directory @@ -40,20 +41,10 @@ (buffer-file-name))) "The directory where ‘oni-php’ was loaded from.") -(defconst oni-php-snippets-dir - (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) - (add-to-list 'yas-snippet-dirs oni-php-snippets-dir)) - (yas-load-directory oni-php-snippets-dir)) - (defun oni-php--set-require-final-newline () "Set `require-final-newline' to t. This is necessary because the PHP mode configuration sets this to @@ -79,7 +70,7 @@ nil for some reason." (defun oni-php-add-use (class) "Try to add a use statement for CLASS under point." - (interactive (list (symbol-name (symbol-at-point)))) + (interactive (list (thing-at-point 'symbol))) (let* ((default-directory (project-root (project-current))) (classes (read (shell-command-to-string (concat (expand-file-name "find-php-class" oni-php-scripts-dir) " " class)))) (class (cond @@ -97,19 +88,31 @@ nil for some reason." (forward-line) (insert "\n")) - (unless (search-backward class nil t) + (unless (re-search-backward (rx "use " (literal class) ";") nil t) (let ((start (point))) (insert "use " class ";\n") (pulse-momentary-highlight-region start (point))))))) +(defun oni-php--syntax-in-string-p (syntax) + "Does SYNTAX indicate point is inside a string?" + (nth 3 syntax)) + +(defun oni-php--syntax-in-comment-p (syntax) + "Does SYNTAX indicate point is inside a comment?" + (nth 4 syntax)) + +(defun oni-php--in-string-or-comment-p () + "Return whether or not point is within a string or comment." + (let ((syntax (syntax-ppss))) + (or (oni-php--syntax-in-string-p syntax) + (oni-php--syntax-in-comment-p syntax)))) + (defun oni-php-insert-dot-dwim (N) "Insert either a concatenation or access operator depending on context. Do the insert N times." (interactive "p") - (if (or (let ((syntax (syntax-ppss))) - (or (nth 3 syntax) - (nth 4 syntax))) + (if (or (oni-php--in-string-or-comment-p) (save-excursion (skip-syntax-backward " ") (nth 3 (syntax-ppss (1- (point))))) @@ -117,7 +120,11 @@ Do the insert N times." (skip-syntax-forward " ") (nth 3 (syntax-ppss (1+ (point)))))) (self-insert-command N) - (dotimes (_ N) (insert "->")))) + (let ((op (if (looking-back (rx (or "$" ")") (zero-or-more (any whitespace "\n" alnum "(" ")" "->"))) + (save-excursion (backward-paragraph) (point))) + "->" + "::"))) + (dotimes (_ N) (insert op))))) (defun oni-php-doc-comment () "Insert a PHP documentation comment at point." @@ -129,17 +136,88 @@ Do the insert N times." (indent-region start (point)) (goto-char insert-marker)))) +(defun oni-php-doc-use-comment () + "Insert a PHP documentation comment for a use statement." + (interactive) + (let ((start (point)) + (class (save-excursion + (save-match-data + (search-forward-regexp (rx "use " (group (minimal-match (zero-or-more alnum))) ";")) + (match-string 1))))) + (insert "/**\n * @use " class) + (let ((insert-marker (point-marker))) + (insert "\n */") + (indent-region start (point)) + (goto-char insert-marker)))) + +(defun oni-php-return-belongs-to-comment () + "Insert a PHP documentation comment for a BelongsTo return type." + (interactive) + (let ((start (point))) + (insert "/**\n * @return BelongsTo<") + (save-excursion + (insert ", $this>\n */") + (indent-region start (point))))) + (defun oni-php-comment-dwim (func &rest args) "See if a PHP documentation comment should be added and add it. Otherwise call FUNC with ARGS. This is meant as advice around ‘comment-dwim’ to make it smarter for PHP code." - (if (and (derived-mode-p 'php-mode) - (looking-back (rx (minimal-match (zero-or-more blank))) (line-beginning-position)) - (looking-at (rx (minimal-match (zero-or-more (any whitespace "\n"))) - (or (regexp php-beginning-of-defun-regexp) - (regexp php--re-classlike-pattern))))) - (oni-php-doc-comment) - (apply func args))) + (cond ((looking-at (rx (minimal-match (zero-or-more (any whitespace "\n"))) + "use ")) + (oni-php-doc-use-comment)) + ((looking-at (rx (minimal-match (zero-or-more (any whitespace "\n"))) + (one-or-more nonl) ": BelongsTo" eol)) + (oni-php-return-belongs-to-comment)) + ((and (derived-mode-p 'php-mode) + (not (region-active-p)) + (looking-back (rx (minimal-match (zero-or-more blank))) (line-beginning-position)) + (looking-at (rx (minimal-match (zero-or-more (any whitespace "\n"))) + (or (regexp php-beginning-of-defun-regexp) + (regexp php--re-classlike-pattern))))) + (oni-php-doc-comment)) + (t (apply func args)))) + +(defun oni-php-generate-namespace () + (string-join + (mapcar + (lambda (s) + (let ((case-fold-search nil)) + (if (string-match-p (rx upper-case) s) + s + (capitalize s)))) + (cdr (split-string + (directory-file-name + (file-name-directory + (file-relative-name + buffer-file-name + (project-root (project-current))))) + "/"))) + "\\")) + +(defun oni-php-set-dabbrev-settings () + "Set any settings for dabbrev that make sense for PHP code." + (setq-local dabbrev-abbrev-skip-leading-regexp (rx "$"))) + +(defun oni-php-ignore-error-at-point (error-type) + (interactive + (list (let* ((error-types + (seq-uniq + (mapcar (lambda (e) + (let ((str (flycheck-error-message e))) + (string-match (rx "🪪" (one-or-more whitespace) (group (one-or-more (any alnum ".")))) str) + (match-string 1 str))) + (seq-filter (lambda (e) (not (flycheck-error-checker e))) + (flycheck-overlay-errors-at (point)))) + 'string=))) + (if (> 1 (length error-types)) + (completing-read "Ignore type: " error-types) + (car error-types))))) + (save-excursion + (goto-char (line-beginning-position)) + (indent-for-tab-command) + (insert "/** @phpstan-ignore " error-type " */\n") + (indent-for-tab-command))) (defhydra php-mode-hydra (:color blue) ("a" align-current "Align current selection")) @@ -148,22 +226,19 @@ Otherwise call FUNC with ARGS. This is meant as advice around (add-hook 'php-mode-hook #'oni-php--set-require-final-newline) (add-hook 'php-mode-hook #'oni-php--whitespace-mode) -(add-hook 'php-mode-hook 'company-mode) (add-hook 'php-mode-hook 'display-fill-column-indicator-mode) (add-hook 'php-mode-hook 'electric-indent-local-mode) (add-hook 'php-mode-hook 'fic-mode) (add-hook 'php-mode-hook 'flycheck-mode) -(add-hook 'php-mode-hook 'ggtags-mode) (add-hook 'php-mode-hook 'oni-php--auto-fill-mode) +(add-hook 'php-mode-hook 'oni-php-set-dabbrev-settings) (add-hook 'php-mode-hook 'oni-php-set-rainbow-identifier-faces) (add-hook 'php-mode-hook 'rainbow-delimiters-mode) (add-hook 'php-mode-hook 'rainbow-identifiers-mode) (add-hook 'php-mode-hook 'smartparens-mode) (add-hook 'php-mode-hook 'subword-mode) (add-hook 'php-mode-hook 'yas-minor-mode) - -(with-eval-after-load 'company - (add-to-list 'company-backends 'company-ac-php-backend)) +(add-hook 'php-mode-hook 'corfu-mode) (define-key php-mode-map (kbd "C-c m") #'php-mode-hydra/body) (define-key php-mode-map (kbd ".") #'oni-php-insert-dot-dwim) @@ -181,7 +256,7 @@ Otherwise call FUNC with ARGS. This is meant as advice around `(php-array-arrow (regexp . ,(rx any (group (zero-or-more whitespace)) "=>" any)) (group . (1)) - (modes . '(php-mode web-mode)) + (modes . '(php-mode web-mode php-mode)) (repeat . t))) ;; The WordPress coding standards specify that multiple assignments @@ -197,13 +272,9 @@ Otherwise call FUNC with ARGS. This is meant as advice around (regexp . ,(rx any (group (zero-or-more whitespace)) "=" (zero-or-more whitespace) any)) (group . (1)) - (modes . '(php-mode web-mode)) + (modes . '(php-mode web-mode php-ts-mode)) (repeat . t))) -(with-eval-after-load 'php-mode - (with-eval-after-load 'yasnippet - (oni-php-snippets-initialize))) - ;;;###autoload (add-to-list 'auto-mode-alist '("\\.inc\\'" . php-mode)) @@ -215,13 +286,25 @@ Otherwise call FUNC with ARGS. This is meant as advice around (add-to-list 'grep-files-aliases '("php" . "*.php *.inc *.module"))) (with-eval-after-load 'autoinsert - (setf (map-elt auto-insert-alist (rx "Test.php" eos)) + (setf (map-elt auto-insert-alist (rx ".php" eos)) '(nil "<?php\n" "\n" "declare(strict_types=1);\n" "\n" - "namespace " (string-join (mapcar #'capitalize (cdr (split-string (directory-file-name (file-name-directory (file-relative-name buffer-file-name (project-root (project-current))))) "/"))) "\\") ";\n" + "namespace " (oni-php-generate-namespace) ";\n" + "\n" + "class " (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) "\n" + "{\n" + > _ "\n" + "}\n") + (map-elt auto-insert-alist (rx "Test.php" eos)) + '(nil + "<?php\n" + "\n" + "declare(strict_types=1);\n" + "\n" + "namespace " (oni-php-generate-namespace) ";\n" "\n" "use Illuminate\\Foundation\\Testing\\RefreshDatabase;\n" "use Illuminate\\Foundation\\Testing\\WithFaker;\n" @@ -248,5 +331,23 @@ Otherwise call FUNC with ARGS. This is meant as advice around (defun oni-php-in-static-call-context-p () (looking-back "::\\w+" (- (point) (line-beginning-position)))) +(defun oni-php-grep-symbol (symbol) + "Use ‘rgrep’ to find SYMBOL. +If there is an active region when this command is called interactively +SYMBOL will be the text in the region. Otherwise it's the symbol at +point." + (interactive (list + (if (region-active-p) + (buffer-substring-no-properties (region-beginning) (region-end)) + (thing-at-point 'symbol)))) + (rgrep (rx (literal symbol)) + (map-elt grep-files-aliases "php") + (project-root (project-current)))) + +(define-key php-mode-map (kbd "C-c .") nil t) +(define-key php-mode-map (kbd "C-c . g") '("Search for symbol at point" . oni-php-grep-symbol)) +(define-key php-mode-map (kbd "C-c . a") '("Import symbol at point" . oni-php-add-use)) +(define-key php-mode-map (kbd "C-c . i") '("Ignore error at point" . oni-php-ignore-error-at-point)) + (provide 'oni-php) ;;; oni-php.el ends here diff --git a/oni-php/scripts/find-php-class b/oni-php/scripts/find-php-class index d8795c2..d38cb13 100755 --- a/oni-php/scripts/find-php-class +++ b/oni-php/scripts/find-php-class @@ -4,7 +4,10 @@ (define (main args) (let* ((class-name (cadr args)) (file-names - (run/strings (find "." -type f -name ,(string-append class-name ".php") + (run/strings (find "." + -path "./cdk.out" -prune -o + -type f + -name ,(string-append class-name ".php") -exec grep "^namespace" "{}" ";") (> 2 "/dev/null")))) (format #t "~s" diff --git a/oni-php/snippets/php-mode/arrow-function b/oni-php/snippets/php-mode/arrow-function deleted file mode 100644 index 3b44e01..0000000 --- a/oni-php/snippets/php-mode/arrow-function +++ /dev/null @@ -1,6 +0,0 @@ -# -*- mode: snippet -*- -# name: arrow-function -# key: f -# condition: (oni-php-in-expression-context-p) -# -- -fn($1): $2 => $3
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/casts b/oni-php/snippets/php-mode/casts deleted file mode 100644 index 2850d72..0000000 --- a/oni-php/snippets/php-mode/casts +++ /dev/null @@ -1,7 +0,0 @@ -# -*- mode: snippet -*- -# name: casts -# key: casts -# -- -protected $casts = [ - '$1' => $0, -];
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/constructor b/oni-php/snippets/php-mode/constructor deleted file mode 100644 index f919e6c..0000000 --- a/oni-php/snippets/php-mode/constructor +++ /dev/null @@ -1,8 +0,0 @@ -# -*- mode: snippet -*- -# name: constructor -# key: constructor -# -- -public function __construct($1) -{ - $0 -}
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/for b/oni-php/snippets/php-mode/for deleted file mode 100644 index 1adae91..0000000 --- a/oni-php/snippets/php-mode/for +++ /dev/null @@ -1,7 +0,0 @@ -# -*- mode: snippet -*- -# name: for -# key: for -# -- -for (${1:$i = 0}; ${2:$i < $3}; ${4:$i++}) { - $0 -}
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/function-expression b/oni-php/snippets/php-mode/function-expression deleted file mode 100644 index fa59b03..0000000 --- a/oni-php/snippets/php-mode/function-expression +++ /dev/null @@ -1,8 +0,0 @@ -# -*- mode: snippet -*- -# name: function expression -# key: f -# condition: (oni-php-in-expression-context-p) -# -- -function ($1) ${2:use ($3)} { - $0 -}
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/function-statement b/oni-php/snippets/php-mode/function-statement deleted file mode 100644 index 9c44aef..0000000 --- a/oni-php/snippets/php-mode/function-statement +++ /dev/null @@ -1,11 +0,0 @@ -# -*- mode: snippet -*- -# name: function statement -# key: f -# condition: (oni-php-in-expression-context-p) -# -- -/** - * $5 - */ -${1:private} function $2($3): ${4:void} { - $0 -}
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/if-statement b/oni-php/snippets/php-mode/if-statement deleted file mode 100644 index 6033e3a..0000000 --- a/oni-php/snippets/php-mode/if-statement +++ /dev/null @@ -1,7 +0,0 @@ -# -*- mode: snippet -*- -# name: if statement -# key: ? -# -- -if ($1) { - $2 -}
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/logged-debug b/oni-php/snippets/php-mode/logged-debug deleted file mode 100644 index d0e0b7e..0000000 --- a/oni-php/snippets/php-mode/logged-debug +++ /dev/null @@ -1,5 +0,0 @@ -# -*- mode: snippet -*- -# name: logger.debug -# key: ld -# -- -logger()->debug('$1'${2:, [ $3 ]});
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/logger-info b/oni-php/snippets/php-mode/logger-info deleted file mode 100644 index 9491d24..0000000 --- a/oni-php/snippets/php-mode/logger-info +++ /dev/null @@ -1,5 +0,0 @@ -# -*- mode: snippet -*- -# name: logger.info -# key: li -# -- -logger()->info('$1'${2:, [ $3 ]});
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/namespace b/oni-php/snippets/php-mode/namespace deleted file mode 100644 index 01abd02..0000000 --- a/oni-php/snippets/php-mode/namespace +++ /dev/null @@ -1,9 +0,0 @@ -# -*- mode: snippet -*- -# name: namespace -# key: ns -# -- -namespace ${1:`(string-join - (mapcar - (lambda (s) (let ((case-fold-search nil)) (if (string-match-p (rx upper-case) s) s (capitalize s)))) - (cdr (split-string (directory-file-name (file-name-directory (file-relative-name buffer-file-name (project-root (project-current))))) "/"))) - "\\")`};
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/php b/oni-php/snippets/php-mode/php deleted file mode 100644 index 12f7492..0000000 --- a/oni-php/snippets/php-mode/php +++ /dev/null @@ -1,5 +0,0 @@ -# -*- mode: snippet -*- -# name: <?php -# key: php -# -- -<?php diff --git a/oni-php/snippets/php-mode/strict-types b/oni-php/snippets/php-mode/strict-types deleted file mode 100644 index bdcf3f9..0000000 --- a/oni-php/snippets/php-mode/strict-types +++ /dev/null @@ -1,5 +0,0 @@ -# -*- mode: snippet -*- -# name: strict-types -# key: strict-types -# -- -declare(strict_types=1);
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/test-class b/oni-php/snippets/php-mode/test-class deleted file mode 100644 index de3bd3f..0000000 --- a/oni-php/snippets/php-mode/test-class +++ /dev/null @@ -1,14 +0,0 @@ -# -*- mode: snippet -*- -# name: test-class -# key: test -# -- - -use Illuminate\Foundation\Testing\RefreshDatabase; -use Tests\TestCase; - -class ${1:`(file-name-sans-extension (file-name-nondirectory (buffer-file-name)))`} extends TestCase -{ - use RefreshDatabase; - - $0 -}
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/test-comment b/oni-php/snippets/php-mode/test-comment deleted file mode 100644 index b1c9dfb..0000000 --- a/oni-php/snippets/php-mode/test-comment +++ /dev/null @@ -1,9 +0,0 @@ -# -*- mode: snippet -*- -# name: test-comment -# key: @t -# -- -#[Test] -public function $1(): void -{ - $0 -} diff --git a/oni-php/snippets/php-mode/test-name b/oni-php/snippets/php-mode/test-name deleted file mode 100644 index 2b9d72b..0000000 --- a/oni-php/snippets/php-mode/test-name +++ /dev/null @@ -1,8 +0,0 @@ -# -*- mode: snippet -*- -# name: test-name -# key: test -# -- -public function test_$1(): void -{ - $0 -} diff --git a/oni-php/snippets/php-mode/var b/oni-php/snippets/php-mode/var deleted file mode 100644 index ce2b8fc..0000000 --- a/oni-php/snippets/php-mode/var +++ /dev/null @@ -1,5 +0,0 @@ -# -*- mode: snippet -*- -# name: var -# key: @v -# -- -/** @var $1 `(save-excursion (and (search-forward "$" nil t) (symbol-at-point)))` */`(unless (looking-at (rx "\n")) "\n")`
\ No newline at end of file diff --git a/oni-php/snippets/php-mode/wpheader.php b/oni-php/snippets/php-mode/wpheader.php deleted file mode 100644 index 2c70f93..0000000 --- a/oni-php/snippets/php-mode/wpheader.php +++ /dev/null @@ -1,13 +0,0 @@ -# -*- mode: snippet; require-final-newline: nil -*- -# name: WP Plugin Header -# key: header -# -- -/* - * Plugin Name: $1 - * Plugin URI: $2 - * Description: $3 - * Version: ${4:1.0} - * Author: ${5:Tom Willemse} - * Author URI: ${6:https://ryuslash.org} - * License: ${7:GPLv2} - */
\ No newline at end of file |
