From 354275e12ca86b3f226a08dd57f0794e680afc49 Mon Sep 17 00:00:00 2001 Message-Id: <354275e12ca86b3f226a08dd57f0794e680afc49.1621740204.git.yantar92@gmail.com> From: Ihor Radchenko Date: Sun, 23 May 2021 11:03:40 +0800 Subject: [PATCH] Add comments to org-element-cache code --- lisp/org-element.el | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index ba4f0ead6..b9bb87b66 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -5322,6 +5322,9 @@ (defun org-element--cache-process-request (if (if (or (not next) (org-element--cache-key-less-p data-key next)) (<= pos end) + ;; FIXME: Isn't LAST-CONTAINER always nil here? + ;; We are inside let-binding defining + ;; last-container to be nil. (and last-container (let ((up data)) (while (and up (not (eq up last-container))) @@ -5675,7 +5678,10 @@ (defun org-element--cache-for-removal (beg end offset) any position between BEG and END. As an exception, greater elements around the changes that are robust to contents modifications are preserved and updated according to the -changes." +changes. In the latter case, the returned element is the outermost +non-robust element affected by changed. Note that the returned +element may end before END position in which case some cached element +starting after the returned may still be affected by the changes." (let* ((elements (org-element--cache-find (1- beg) 'both)) (before (car elements)) (after (cdr elements))) @@ -5722,22 +5728,39 @@ (defun org-element--cache-submit-request (beg end offset) (let ((next (car org-element--cache-sync-requests)) delete-to delete-from) (if (and next + ;; First existing sync request is in phase 0. (zerop (aref next 5)) + ;; Current changes intersect with the first sync request. (> (setq delete-to (+ (aref next 2) (aref next 3))) end) (<= (setq delete-from (aref next 1)) end)) ;; Current changes can be merged with first sync request: we ;; can save a partial cache synchronization. (progn + ;; Update OFFSET of the existing request. (cl-incf (aref next 3) offset) ;; If last change happened within area to be removed, extend ;; boundaries of robust parents, if any. Otherwise, find ;; first element to remove and update request accordingly. (if (> beg delete-from) + ;; The current modification is completely inside NEXT. + ;; (aref next 4) is supposed to be the outermost parent + ;; to be removed. Everything in cache inside that + ;; element will be removed later and we do not care + ;; about extending boundaries of robust elements in + ;; there. Also, all the upper elements are guaranteed + ;; to be robust because otherwise `org-element--cache-for-removal' + ;; would return more outer element (to be stored in + ;; (aref next 4). (let ((up (aref next 4))) (while up (org-element--cache-shift-positions up offset '(:contents-end :end)) (setq up (org-element-property :parent up)))) + ;; The current and NEXT modifications are intersecting + ;; with current modification starting before NEXT and NEXT + ;; ending after current. We need to update the common + ;; non-robust parent for the new extended modification + ;; region. (let ((first (org-element--cache-for-removal beg delete-to offset))) (when first (aset next 0 (org-element--cache-key first)) @@ -5762,8 +5785,15 @@ (defun org-element--cache-submit-request (beg end offset) ;; Otherwise, we find the first non robust ;; element containing END. All elements between ;; FIRST and this one are to be removed. + ;; + ;; The current modification is completely inside + ;; FIRST. Clear and update cached elements in + ;; region containing FIRST. ((let ((first-end (org-element-property :end first))) - (and (> first-end end) + (and (>= first-end end) + ;; FIXME: According to `org-element--cache-sync-requests' + ;; docstring, (aref 4 request) must be the + ;; parent of the first element to be removed. (vector key beg first-end offset first 0)))) (t (let* ((element (org-element--cache-find end)) @@ -5773,6 +5803,8 @@ (defun org-element--cache-submit-request (beg end offset) (>= (org-element-property :begin up) beg)) (setq end (org-element-property :end up) element up)) + ;; Extend region to remove elements to parent + ;; of the FIRST. (vector key beg end offset element 0))))) org-element--cache-sync-requests) ;; No element to remove. No need to re-parent either. -- 2.26.3