From ca1bc43f53eb2fa3a647737cd047e85e21910821 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Tue, 8 Apr 2025 02:19:45 +0900 Subject: [PATCH] Add phpstan-copy-dumped-type command --- CHANGELOG.md | 6 +++++- README.org | 8 ++++++++ flycheck-phpstan.el | 1 + phpstan.el | 25 +++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 072c5d5..4b4b524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,11 @@ All notable changes of the `phpstan.el` are documented in this file using the [Keep a Changelog](https://keepachangelog.com/) principles. - +## Unreleased + +### Added + +* Add `phpstan-copy-dumped-type` command to copy the nearest dumped type from `PHPStan\dumpType()` or `PHPStan\dumpPhpDocType()` messages. ## [0.8.2] diff --git a/README.org b/README.org index 9a6efb1..f75efff 100644 --- a/README.org +++ b/README.org @@ -105,6 +105,14 @@ Insert a ~@phpstan-ignore~ tag to suppress any PHPStan errors on the current lin By default it inserts the tag on the previous line, but if there is already a tag at the end of the current line or on the previous line, the identifiers will be appended there. If there is no existing tag and ~C-u~ is pressed before the command, it will be inserted at the end of the line. +*** Command ~phpstan-copy-dumped-type~ +Copy the nearest dumped type message from PHPStan's output. + +This command looks for messages like ~Dumped type: int|string|null~ reported by ~PHPStan\dumpType()~ or ~PHPStan\dumpPhpDocType()~, and copies the type string to the kill ring. + +If there are multiple dumped types in the buffer, it selects the one closest to the current line. + +If no dumped type messages are found, the command signals an error. ** API Most variables defined in this package are buffer local. If you want to set it for multiple projects, use [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Default-Value.html][setq-default]]. diff --git a/flycheck-phpstan.el b/flycheck-phpstan.el index b56243a..d004744 100644 --- a/flycheck-phpstan.el +++ b/flycheck-phpstan.el @@ -83,6 +83,7 @@ (errors (phpstan--plist-to-alist (plist-get data :files)))) (unless phpstan-disable-buffer-errors (phpstan-update-ignorebale-errors-from-json-buffer errors)) + (phpstan-update-dumped-types errors) (flycheck-phpstan--build-errors errors))) (defun flycheck-phpstan--temp-buffer () diff --git a/phpstan.el b/phpstan.el index 765aff6..d080adb 100644 --- a/phpstan.el +++ b/phpstan.el @@ -169,6 +169,7 @@ have unexpected behaviors or performance implications." (defvar-local phpstan--use-xdebug-option nil) (defvar-local phpstan--ignorable-errors '()) +(defvar-local phpstan--dumped-types '()) ;;;###autoload (progn @@ -523,6 +524,17 @@ it returns the value of `SOURCE' as it is." (setq phpstan--ignorable-errors (mapcar (lambda (v) (cons (car v) (mapcar #'cdr (cdr v)))) (seq-group-by #'car identifiers))))) +(defun phpstan-update-dumped-types (errors) + "Update `phpstan--dumped-types' variable by ERRORS." + (save-match-data + (setq phpstan--dumped-types + (cl-loop for (_ . entry) in errors + append (cl-loop for message in (plist-get entry :messages) + for msg = (plist-get message :message) + if (string-match (eval-when-compile (rx bos "Dumped type: ")) msg) + collect (cons (plist-get message :line) + (substring-no-properties msg (match-end 0)))))))) + (defconst phpstan--re-ignore-tag (eval-when-compile (rx (* (syntax whitespace)) "//" (* (syntax whitespace)) @@ -590,6 +602,19 @@ POSITION determines where to insert the comment and can be either `this-line' or (if new-position (if append ", " " ") "// @phpstan-ignore ") (string-join identifiers ", "))))))) +;;;###autoload +(defun phpstan-copy-dumped-type () + "Copy a dumped PHPStan type." + (interactive) + (if phpstan--dumped-types + (let ((type (if (eq 1 (length phpstan--dumped-types)) + (cdar phpstan--dumped-types) + (let ((linum (line-number-at-pos))) + (cdar (seq-sort-by (lambda (elm) (abs (- linum (car elm)))) #'< phpstan--dumped-types)))))) + (kill-new type) + (message "Copied %s" type)) + (user-error "No dumped PHPStan types"))) + ;;;###autoload (defun phpstan-insert-dumptype (&optional expression prefix-num) "Insert PHPStan\\dumpType() expression-statement by EXPRESSION and PREFIX-NUM."