Skip to content

Commit

Permalink
Use acm-backend-lsp-make-sure-descending adjust edits, not just for c…
Browse files Browse the repository at this point in the history
…ode format.
  • Loading branch information
manateelazycat committed Jul 16, 2023
1 parent 4164fad commit 9cc17d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
21 changes: 17 additions & 4 deletions acm/acm-backend-lsp.el
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,26 @@ If optional MARKER, return a marker instead"
(line-end-position)))))
(if marker (copy-marker (point-marker)) (point)))))

(defun acm-backend-lsp-apply-text-edits (edits &optional is-decreasing)
;; NOTE:
;; We need reverse edits before apply, otherwise the row inserted before will affect the position of the row inserted later.
(dolist (edit (if is-decreasing edits (reverse edits)))
(defun acm-backend-lsp-apply-text-edits (edits)
(dolist (edit (acm-backend-lsp-make-sure-descending edits))
(let* ((range (plist-get edit :range)))
(acm-backend-lsp-insert-new-text (plist-get range :start) (plist-get range :end) (plist-get edit :newText)))))

(defun acm-backend-lsp-make-sure-descending (edits)
"If `edits' is increasing, reverse `edits', otherwise the row inserted before will affect the position of the row inserted later."
(if (<= (length edits) 1)
;; Return origin value of `edits' if length 0 or 1.
edits
(let* ((first-element-range (plist-get (nth 0 edits) :range))
(second-element-range (plist-get (nth 1 edits) :range))
(first-element-pos (acm-backend-lsp-position-to-point (plist-get first-element-range :start)))
(second-element-pos (acm-backend-lsp-position-to-point (plist-get first-element-range :start))))
(if (< first-element-pos second-element-pos)
;; Only reverse edits if `edits' is increasing.
(reverse edits)
;; Otherwise return origin value of `edits'.
edits))))

(defun acm-backend-lsp-snippet-expansion-fn ()
"Compute a function to expand snippets.
Doubles as an indicator of snippet support."
Expand Down
7 changes: 1 addition & 6 deletions core/handler/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,4 @@ def process_request(self, tab_size) -> dict:

def process_response(self, response) -> None:
if response and len(response) > 0:
eval_in_emacs("lsp-bridge-format--update", self.file_action.filepath, response, self.is_decreasing(response))

def is_decreasing(self, data):
start_values = [(item['range']['start']['line'], item['range']['start']['character']) for item in data ]

return start_values != sorted(start_values)
eval_in_emacs("lsp-bridge-format--update", self.file_action.filepath, response)
8 changes: 4 additions & 4 deletions lsp-bridge.el
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ So we build this macro to restore postion after code format."

filename)

(defun lsp-bridge-file-apply-edits (filename edits &optional temp-buffer is-decreasing)
(defun lsp-bridge-file-apply-edits (filename edits &optional temp-buffer)
(if temp-buffer
;; Apply edits to temp buffer.
(with-current-buffer temp-buffer
Expand All @@ -1623,7 +1623,7 @@ So we build this macro to restore postion after code format."
(find-file-noselect filename)
(save-excursion
(find-file filename)
(acm-backend-lsp-apply-text-edits edits is-decreasing)))
(acm-backend-lsp-apply-text-edits edits)))

(setq-local lsp-bridge-prohibit-completion t))

Expand Down Expand Up @@ -2000,12 +2000,12 @@ SymbolKind (defined in the LSP)."
4
indent)))))

(defun lsp-bridge-format--update (filename edits is-decreasing)
(defun lsp-bridge-format--update (filename edits)
;; We need set `inhibit-modification-hooks' to t to avoid GC freeze Emacs.
(lsp-bridge-save-position
(let ((inhibit-modification-hooks t))
;; Apply code format edits, not sort, just reverse order.
(lsp-bridge-file-apply-edits filename edits nil is-decreasing)
(lsp-bridge-file-apply-edits filename edits)
;; Make LSP server update full content.
(lsp-bridge-call-file-api "update_file" (buffer-name))
;; Notify format complete.
Expand Down

0 comments on commit 9cc17d7

Please sign in to comment.