Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update change tracking #83

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions parinfer-rust-changes.el
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,10 @@ on similar regions of texts."
;; (equal
;; (parinfer-rust--combine-changes some-changes)
;; '((lineNo 7 x 10 start 170 end 171 before-text "\n " after-text " " length 3 group t))))

(defun parinfer-rust--get-before-and-after-text (start end length)
"Builds before and after change text using START, END, and LENGTH.

Uses on `parinfer-rust--previous-buffer-text' and
`current-buffer' text to generate info."
(let* ((previous-text parinfer-rust--previous-buffer-text)
(old-region-end (parinfer-rust--bound-number previous-text (+ start length -1)))
(old-region-start (parinfer-rust--bound-number previous-text (- start 1))))
(list
(if previous-text
(substring-no-properties previous-text
old-region-start
old-region-end)
"")
(buffer-substring-no-properties start end))))
(defun parinfer-rust--get-before-text (start end)
"Text before change using START and END."
(setq-local parinfer-rust--before-text-change
(buffer-substring-no-properties start end)))

(defun parinfer-rust--build-changes (change-list)
"Convert CHANGE-LIST to a list of change structs for parinfer-rust."
Expand All @@ -177,21 +165,20 @@ previous buffer and current buffer."
(if parinfer-rust--disable
nil
;; If we're in test-mode we want the absolute position otherwise relative is fine
(let ((lineNo (- (line-number-at-pos start t)
(let* ((lineNo (- (line-number-at-pos start t)
1))
(x (save-excursion
(save-restriction
(widen)
(goto-char start)
(parinfer-rust--get-cursor-x))))
(changes (parinfer-rust--get-before-and-after-text start end length)))
(x (save-excursion
(save-restriction
(widen)
(goto-char start)
(parinfer-rust--get-cursor-x)))))
(push (list 'lineNo lineNo
'x x
'start start
'end end
'length length
'before-text (car changes)
'after-text (cadr changes)
'before-text (or parinfer-rust--before-text-change "")
'after-text (buffer-substring-no-properties start end)
'group nil)
parinfer-rust--changes))
(setq parinfer-rust--previous-buffer-text
Expand Down
4 changes: 4 additions & 0 deletions parinfer-rust-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ Checks if MODE is a valid Parinfer mode, and uses
(advice-add 'undo :around #'parinfer-rust--track-undo)
(when (fboundp 'undo-tree-undo)
(advice-add 'undo-tree-undo :around #'parinfer-rust--track-undo))
;; Track changes as they happen in the buffer
(add-hook 'before-change-functions #'parinfer-rust--get-before-text t t)
(add-hook 'after-change-functions #'parinfer-rust--track-changes t t)
;; Run parinfer after whatever command caused the change(s) has finished
(add-hook 'post-command-hook #'parinfer-rust--execute t t)
(parinfer-rust--dim-parens))

Expand All @@ -521,6 +524,7 @@ Checks if MODE is a valid Parinfer mode, and uses
(advice-remove 'undo #'parinfer-rust--track-undo)
(when (fboundp 'undo-tree-undo)
(advice-remove 'undo-tree-undo #'parinfer-rust--track-undo))
(remove-hook 'before-change-functions #'parinfer-rust--get-before-text t)
(remove-hook 'after-change-functions #'parinfer-rust--track-changes t)
(remove-hook 'post-command-hook #'parinfer-rust--execute t)
(setq-local parinfer-rust-enabled nil)
Expand Down
Loading