From 855529095ae36d5a0e393e5f2a35006079218e02 Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Tue, 20 Aug 2024 00:26:29 +0200 Subject: [PATCH] Index types used in class property initializers and combine change calls for fix-imports --- phpinspect-imports.el | 15 +++++++++++---- phpinspect-index.el | 12 ++++++++++-- phpinspect-parser.el | 6 +++--- test/test-buffer.el | 2 +- test/test-index.el | 8 ++++++-- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/phpinspect-imports.el b/phpinspect-imports.el index cf15322..f45b4ce 100644 --- a/phpinspect-imports.el +++ b/phpinspect-imports.el @@ -260,10 +260,11 @@ group." (with-current-buffer (phpinspect-buffer-buffer buffer) (save-excursion (goto-char start) - (delete-region start end) - (dolist (statement statements) - (phpinspect-codify-token (cdr statement)) - (insert-char ?\n)) + (combine-after-change-calls + (delete-region start end) + (dolist (statement statements) + (phpinspect-codify-token (cdr statement)) + (insert-char ?\n))) (if (looking-at "[[:blank:]\n]+") ;; Delete excess trailing whitespace (there's more than 2 between the @@ -279,6 +280,7 @@ group." "Find types that are used in the current buffer and make sure that there are import (\"use\") statements for them." (interactive) + (if phpinspect-current-buffer (let* ((buffer phpinspect-current-buffer) ;; use buffer-reparse to ensure fully structurally correct @@ -346,6 +348,11 @@ that there are import (\"use\") statements for them." (phpinspect-add-use-statements-for-missing-types used-types buffer (append imports namespace-imports) project token-meta) + (with-current-buffer (get-buffer-create "attempt-before-namespace-removal-code") + (erase-buffer) + (insert (with-current-buffer (phpinspect-buffer-buffer buffer) (buffer-string)))) + + (phpinspect-remove-unneeded-use-statements used-types buffer (append imports namespace-imports) token-meta) diff --git a/phpinspect-index.el b/phpinspect-index.el index a866281..de032a2 100644 --- a/phpinspect-index.el +++ b/phpinspect-index.el @@ -261,7 +261,9 @@ SCOPE should be a scope token (`phpinspect-scope-p')." (t (setq variable-name (cadr (seq-find #'phpinspect-variable-p scope))))) - (phpinspect--log "calling resolver from index-variable-from-scope") + (when add-used-types + (funcall add-used-types (phpinspect--find-used-types-in-tokens scope))) + (phpinspect--make-variable ;; Static class variables are always prefixed with dollar signs when ;; referenced. @@ -589,6 +591,7 @@ Returns a list of type name strings." (while tokens (let ((token (pop tokens)) (previous-token (car previous-tokens))) + (cond ((and (phpinspect-word-p previous-token) (member (cadr previous-token) `("new" "instanceof")) (phpinspect-word-p token)) @@ -633,7 +636,12 @@ Returns a list of type name strings." (nconc used-types-rear (phpinspect--find-used-types-in-tokens (cdr (phpinspect-function-block token)))) used-types-rear (last used-types-rear))) - ((or (phpinspect-list-p token) (phpinspect-block-p token) (phpinspect-array-p token)) + ((or (phpinspect-list-p token) + (phpinspect-block-p token) + (phpinspect-array-p token) + (phpinspect-scope-p token) + (phpinspect-static-p token) + (phpinspect-const-p token)) (setq used-types-rear (nconc used-types-rear (phpinspect--find-used-types-in-tokens (cdr token))) used-types-rear (last used-types-rear)))) diff --git a/phpinspect-parser.el b/phpinspect-parser.el index bb8328f..a8cd508 100644 --- a/phpinspect-parser.el +++ b/phpinspect-parser.el @@ -798,19 +798,19 @@ Returns the consumed text string without face properties." (phpinspect-defparser scope-public :tree-keyword "public" :handlers '(function-keyword static-keyword const-keyword class-variable here-doc - string terminator tag comment word) + string terminator tag comment assignment-operator array word) :delimiter-predicate #'phpinspect--scope-terminator-p) (phpinspect-defparser scope-private :tree-keyword "private" :handlers '(function-keyword static-keyword const-keyword class-variable here-doc - string terminator tag comment word) + string terminator tag comment assignment-operator array word) :delimiter-predicate #'phpinspect--scope-terminator-p) (phpinspect-defparser scope-protected :tree-keyword "protected" :handlers '(function-keyword static-keyword const-keyword class-variable here-doc - string terminator tag comment word) + string terminator tag comment assignment-operator array word) :delimiter-predicate #'phpinspect--scope-terminator-p) (phpinspect-defhandler scope-keyword (start-token max-point) diff --git a/test/test-buffer.el b/test/test-buffer.el index 7048456..3c34f6d 100644 --- a/test/test-buffer.el +++ b/test/test-buffer.el @@ -311,7 +311,7 @@ use CCC; (let ((switch nil) (delta 0)) - (dotimes (i 100) + (dotimes (_i 100) (if switch (progn (setq delta 0) diff --git a/test/test-index.el b/test/test-index.el index fe57c66..96c217e 100644 --- a/test/test-index.el +++ b/test/test-index.el @@ -92,6 +92,8 @@ use UsedTrait; +private array $bong = [ PropertyArray::class => PropertyArrayItem::class ]; + private PropertyType $property; /** @param ParamAnnotation $par @@ -106,7 +108,7 @@ return StaticThing::create(new ThingFactory())->makeThing((((new Potato())->anti if ($param instanceof InstanceOffed) { /** @var VarAnnotation $bing */ try { - $bing = [ 'bong' => [ 'nested' => (CastedType) NestedArray::call(), ], ]; + $bing = [ ArrayKey::class => [ NestedArrayKey::class => (CastedType) NestedArray::call(), 'ba' => ArrayItem::class ], ]; } catch (CaughtException $e) { // nothing } @@ -122,7 +124,9 @@ try { "StaticThing" "Thing" "ThingFactory" "Potato" "OtherThing" "InnerFunctionParam" "PropertyType" "InstanceOffed" "NestedArray" "UsedTrait" "VarAnnotation" "ParamAnnotation" - "ThrowAnnotationException" "CaughtException" "CastedType")) + "ThrowAnnotationException" "CaughtException" "CastedType" + "NestedArrayKey" "ArrayKey" "ArrayItem" "PropertyArray" + "PropertyArrayItem" "array")) #'string<)) (sort used-types (lambda (s1 s2) (string< (phpinspect-name-string s1) (phpinspect-name-string s2))))))))