From c20f010ee4ab326f04c4bec176d44586a91230ba Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Wed, 14 Aug 2024 10:38:43 +0200 Subject: [PATCH] Introduce `phpinspect-buffer-fresh-p' and `phpinspect-buffer-reparse-if-not-fresh' + use in `phpinspect-fix-imports' instead of `phpinspect-buffer-reparse' --- phpinspect-buffer.el | 31 +++++++++++++++++++++++++++++-- phpinspect-imports.el | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/phpinspect-buffer.el b/phpinspect-buffer.el index 058d31c..d894021 100644 --- a/phpinspect-buffer.el +++ b/phpinspect-buffer.el @@ -76,12 +76,39 @@ emacs buffer." (phpinspect--cache-get-project-create (phpinspect--get-or-create-global-cache) (phpinspect-current-project-root))))) +(defun phpinspect-buffer-tainted-p (buffer) + "Whether or not BUFFER's current tree needs updating to incorporate edits." + (and (phpinspect-edtrack-taint-pool (phpinspect-buffer-edit-tracker buffer)) t)) + +(defun phpinspect-buffer-needs-parse-p (buffer) + "Whether or not BUFFER needs to be parsed for an updated tree to be present." + (or (not (phpinspect-buffer-tree buffer)) + (phpinspect-buffer-tainted-p buffer))) + +(defun phpinspect-buffer-fresh-p (buffer) + "Whether or not BUFFER's metadata is fresh. + +A buffer's metadata is fresh when the buffer's last parsed tree +was parsed from scratch and no edits have been applied +afterwards. An incrementally parsed tree that has incorporated +edits does not count as fresh (because incremental parsing has its flaws)." + (and (not (phpinspect-buffer-needs-parse-p buffer)) + ;; If the buffer map has overlays, the last parsed tree has incorporated + ;; edits and is not fresh. + (phpinspect-splayt-empty-p (phpinspect-bmap-overlays (phpinspect-buffer-map buffer))))) + +(defun phpinspect-buffer-reparse-if-not-fresh (buffer) + "If BUFFER's tree is fresh, return it. Otherwise reparse the + buffer and return the result." + (if (phpinspect-buffer-fresh-p buffer) + (phpinspect-buffer-tree buffer) + (phpinspect-buffer-reparse buffer))) + (cl-defmethod phpinspect-buffer-parse ((buffer phpinspect-buffer) &optional no-interrupt) "Parse the PHP code in the the emacs buffer that this object is linked with." (let ((tree)) - (if (or (not (phpinspect-buffer-tree buffer)) - (phpinspect-edtrack-taint-pool (phpinspect-buffer-edit-tracker buffer))) + (if (phpinspect-buffer-needs-parse-p buffer) (with-current-buffer (phpinspect-buffer-buffer buffer) (let* ((map (phpinspect-make-bmap)) (buffer-map (phpinspect-buffer-map buffer)) diff --git a/phpinspect-imports.el b/phpinspect-imports.el index 6a5f251..81ba8ba 100644 --- a/phpinspect-imports.el +++ b/phpinspect-imports.el @@ -194,7 +194,7 @@ that there are import (\"use\") statements for them." ;; ;; FIXME: Change to buffer-parse when this particular problem in ;; incremental parsing has been solved - (tree (phpinspect-buffer-reparse buffer)) + (tree (phpinspect-buffer-reparse-if-not-fresh buffer)) (index (phpinspect--index-tokens tree nil (phpinspect-buffer-location-resolver buffer))) (classes (alist-get 'classes index))