Skip to content

Commit c989b19

Browse files
committed
WIP on using the 'before-change' hook
Hooks in place, logging to *Messages*, store the before change value
1 parent 99ea111 commit c989b19

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

lsp-methods.el

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ for a new workspace."
8989
:group 'lsp-mode)
9090

9191
;;;###autoload
92-
(defcustom lsp-document-sync-method 'full
92+
(defcustom lsp-document-sync-method nil
9393
"How to sync the document with the language server."
9494
:type '(choice (const :tag "Documents should not be synced at all." 'none)
9595
(const :tag "Documents are synced by always sending the full content of the document." 'full)
@@ -437,6 +437,7 @@ disappearing, unset all the variables related to it."
437437
(add-hook 'completion-at-point-functions #'lsp-completion-at-point))
438438

439439
;; Make sure the hook is local (last param) otherwise we see all changes for all buffers
440+
(add-hook 'before-change-functions #'lsp-before-change nil t)
440441
(add-hook 'after-change-functions #'lsp-on-change nil t)
441442
(lsp--set-sync-method))
442443

@@ -581,6 +582,15 @@ interface Range {
581582
;; ,"end" :{"line":7,"character":0}}
582583
;; ,"rangeLength":7
583584
;; ,"text":""}
585+
;;
586+
;; Adding text:
587+
;; lsp-before-change:(start,end)=(33,33)
588+
;; lsp-on-change:(start,end,length)=(33,34,0)
589+
;;
590+
;; Deleting text:
591+
;; lsp-before-change:(start,end)=(19,27)
592+
;; lsp-on-change:(start,end,length)=(19,19,8)
593+
584594
(if (eq length 0)
585595
;; Adding something, work from start only
586596
`(:range ,(lsp--range (lsp--point-to-position start)
@@ -676,6 +686,37 @@ to a text document."
676686
;; (message "lsp--push-change entered")
677687
(setq lsp--changes (vconcat lsp--changes `(,change-event))))
678688

689+
(defvar-local lsp--before-change-vals nil
690+
"Store the positions from the `lsp-before-change' function
691+
call, for validation and use in the `lsp-on-change' function.")
692+
(defun lsp-before-change (start end)
693+
"Executed before a file is changed.
694+
Added to `before-change-functions'"
695+
;; Note:
696+
;;
697+
;; This variable holds a list of functions to call when Emacs is about to
698+
;; modify a buffer. Each function gets two arguments, the beginning and end of
699+
;; the region that is about to change, represented as integers. The buffer
700+
;; that is about to change is always the current buffer when the function is
701+
;; called.
702+
;;
703+
;; WARNING:
704+
;;
705+
;; Do not expect the before-change hooks and the after-change hooks be called
706+
;; in balanced pairs around each buffer change. Also don't expect the
707+
;; before-change hooks to be called for every chunk of text Emacs is about to
708+
;; delete. These hooks are provided on the assumption that Lisp programs will
709+
;; use either before- or the after-change hooks, but not both, and the
710+
;; boundaries of the region where the changes happen might include more than
711+
;; just the actual changed text, or even lump together several changes done
712+
;; piecemeal.
713+
(message "lsp-before-change:(start,end)=(%s,%s)" start end)
714+
(setq lsp--before-change-vals
715+
`(:start start
716+
:end end
717+
:start-pos ,(lsp--point-to-position start)
718+
:end-pos ,(lsp--point-to-position end))))
719+
679720
(defun lsp-on-change (start end length)
680721
"Executed when a file is changed.
681722
Added to `after-change-functions'"
@@ -693,7 +734,7 @@ to a text document."
693734
;;
694735
;; So (47 54 0) means add 7 chars starting at pos 47
695736
;; So (47 47 7) means delete 7 chars starting at pos 47
696-
;; (message "lsp-on-change:(start,end,length)=(%s,%s,%s)" start end length)
737+
(message "lsp-on-change:(start,end,length)=(%s,%s,%s)" start end length)
697738
(lsp--flush-other-workspace-changes)
698739
(when (and lsp--cur-workspace
699740
(not (or (eq lsp--server-sync-method 'none)

0 commit comments

Comments
 (0)