diff options
Diffstat (limited to 'oni-php/oni-php.el')
| -rw-r--r-- | oni-php/oni-php.el | 82 |
1 files changed, 54 insertions, 28 deletions
diff --git a/oni-php/oni-php.el b/oni-php/oni-php.el index 612d645..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.0406.154601 -;; Package-Requires: (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,12 +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 @@ -41,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 @@ -122,8 +112,7 @@ nil for some reason." Do the insert N times." (interactive "p") - (if (or (char-equal ?\ (char-before (point))) - (oni-php--in-string-or-comment-p) + (if (or (oni-php--in-string-or-comment-p) (save-excursion (skip-syntax-backward " ") (nth 3 (syntax-ppss (1- (point))))) @@ -131,8 +120,8 @@ Do the insert N times." (skip-syntax-forward " ") (nth 3 (syntax-ppss (1+ (point)))))) (self-insert-command N) - (let ((op (if (looking-back (rx "$" (minimal-match (one-or-more (any alnum "->")))) - (line-beginning-position)) + (let ((op (if (looking-back (rx (or "$" ")") (zero-or-more (any whitespace "\n" alnum "(" ")" "->"))) + (save-excursion (backward-paragraph) (point))) "->" "::"))) (dotimes (_ N) (insert op))))) @@ -161,6 +150,15 @@ Do the insert N times." (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 @@ -168,6 +166,9 @@ Otherwise call FUNC with ARGS. This is meant as advice around (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)) @@ -194,6 +195,30 @@ Otherwise call FUNC with ARGS. This is meant as advice around "/"))) "\\")) +(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")) @@ -201,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) @@ -253,10 +275,6 @@ Otherwise call FUNC with ARGS. This is meant as advice around (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)) @@ -314,7 +332,14 @@ Otherwise call FUNC with ARGS. This is meant as advice around (looking-back "::\\w+" (- (point) (line-beginning-position)))) (defun oni-php-grep-symbol (symbol) - (interactive (list (thing-at-point '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)))) @@ -322,6 +347,7 @@ Otherwise call FUNC with ARGS. This is meant as advice around (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 |
