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

sort the edits in descending order #289

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 20 additions & 17 deletions tide.el
Original file line number Diff line number Diff line change
Expand Up @@ -1081,15 +1081,19 @@ Noise can be anything like braces, reserved keywords, etc."

(defun tide-apply-code-edits (file-code-edits)
(save-excursion
(dolist (file-code-edit file-code-edits)
(let ((file (plist-get file-code-edit :fileName)))
(with-current-buffer (tide-get-file-buffer file t)
(tide-format-regions (tide-apply-edits (plist-get file-code-edit :textChanges)))
;; Saving won't work if the current buffer is temporary or an indirect
;; buffer
(when (equal buffer-file-name file)
(basic-save-buffer))
(run-hooks 'tide-post-code-edit-hook))))))
(->>
file-code-edits
(-group-by (lambda (file-code-edit) (plist-get file-code-edit :fileName)))
(-map
(-lambda ((file . file-code-edits))
(let ((changes (-mapcat (lambda (x) (plist-get x :textChanges)) file-code-edits)))
(with-current-buffer (tide-get-file-buffer file t)
(tide-apply-edits changes)
;; Saving won't work if the current buffer is temporary or an indirect
;; buffer
(when (equal buffer-file-name file)
(basic-save-buffer))
(run-hooks 'tide-post-code-edit-hook))))))))

(defun tide-get-flycheck-errors-ids-at-point ()
(-map #'flycheck-error-id (flycheck-overlay-errors-at (point))))
Expand Down Expand Up @@ -1776,7 +1780,13 @@ code-analysis."
(defun tide-apply-edits (edits)
(save-excursion
(-map (lambda (edit) (tide-apply-edit edit))
(nreverse edits))))
(-sort
(tide-compose-comparators
(lambda (a b)
(< (tide-plist-get b :start :line) (tide-plist-get a :start :line)))
(lambda (a b)
(< (tide-plist-get b :start :offset) (tide-plist-get a :start :offset))))
(nreverse edits)))))

(defun tide-format-region (start end)
(let ((response (tide-send-command-sync
Expand All @@ -1789,13 +1799,6 @@ code-analysis."
(tide-on-response-success response
(tide-apply-edits (plist-get response :body)))))

(defun tide-format-regions (ranges)
(let ((positions (->>
ranges
(-mapcat (lambda (range) (list (marker-position (car range)) (marker-position (cdr range)))))
(-sort '<))))
(tide-format-region (-min positions) (-max positions))))

;;; JSDoc Comment Template

(defun tide-command:docCommentTemplate ()
Expand Down