mirror of
git://git.sv.gnu.org/emacs.git
synced 2025-12-06 06:20:55 -08:00
Eglot: janitorial cleanup
* lisp/progmodes/eglot.el (eglot--lsp-position-to-point): Use eglot--widening. (eglot--lsp-position-to-point): Use eglot--widening. (eglot-range-region): New util. (eglot-format) (eglot--code-action-params) (eglot--update-hints-1) (eglot--semtok-request): Use eglot-range-region. (eglot--after-change): Add comment. eglot-range-region is an idea by Lua Viana Reis <me@lua.blog.br>
This commit is contained in:
parent
78a73acf61
commit
d81bb68ed0
1 changed files with 30 additions and 24 deletions
|
|
@ -1259,13 +1259,18 @@ TRUENAMEP indicated PATH is already a truename."
|
||||||
eglot--uri-path-allowed-chars)))))
|
eglot--uri-path-allowed-chars)))))
|
||||||
|
|
||||||
(defun eglot-range-region (range &optional markers)
|
(defun eglot-range-region (range &optional markers)
|
||||||
"Return a cons (BEG . END) of positions representing LSP RANGE.
|
"Convert LSP \"Range\" RANGE to cons (BEG . END) of buffer positions.
|
||||||
If optional MARKERS, make markers instead."
|
If optional MARKERS, make markers instead."
|
||||||
(let* ((st (plist-get range :start))
|
(let* ((st (plist-get range :start))
|
||||||
(beg (eglot--lsp-position-to-point st markers))
|
(beg (eglot--lsp-position-to-point st markers))
|
||||||
(end (eglot--lsp-position-to-point (plist-get range :end) markers)))
|
(end (eglot--lsp-position-to-point (plist-get range :end) markers)))
|
||||||
(cons beg end)))
|
(cons beg end)))
|
||||||
|
|
||||||
|
(defun eglot-region-range (from to)
|
||||||
|
"Convert FROM and TO buffer positions to an LSP \"Range\"."
|
||||||
|
(list :start (eglot--pos-to-lsp-position from)
|
||||||
|
:end (eglot--pos-to-lsp-position to)))
|
||||||
|
|
||||||
(defun eglot-server-capable (&rest feats)
|
(defun eglot-server-capable (&rest feats)
|
||||||
"Determine if current server is capable of FEATS."
|
"Determine if current server is capable of FEATS."
|
||||||
(unless (cl-some (lambda (feat)
|
(unless (cl-some (lambda (feat)
|
||||||
|
|
@ -2051,21 +2056,19 @@ encoding and Eglot will set this variable automatically.")
|
||||||
(defun eglot--lsp-position-to-point (pos-plist &optional marker)
|
(defun eglot--lsp-position-to-point (pos-plist &optional marker)
|
||||||
"Convert LSP position POS-PLIST to Emacs point.
|
"Convert LSP position POS-PLIST to Emacs point.
|
||||||
If optional MARKER, return a marker instead"
|
If optional MARKER, return a marker instead"
|
||||||
(save-excursion
|
(eglot--widening
|
||||||
(save-restriction
|
(goto-char (point-min))
|
||||||
(widen)
|
(forward-line (min most-positive-fixnum
|
||||||
(goto-char (point-min))
|
(plist-get pos-plist :line)))
|
||||||
(forward-line (min most-positive-fixnum
|
(unless (eobp) ;; if line was excessive leave point at eob
|
||||||
(plist-get pos-plist :line)))
|
(let ((col (plist-get pos-plist :character)))
|
||||||
(unless (eobp) ;; if line was excessive leave point at eob
|
(unless (wholenump col)
|
||||||
(let ((col (plist-get pos-plist :character)))
|
(eglot--warn
|
||||||
(unless (wholenump col)
|
"Caution: LSP server sent invalid character position %s. Using 0 instead."
|
||||||
(eglot--warn
|
col)
|
||||||
"Caution: LSP server sent invalid character position %s. Using 0 instead."
|
(setq col 0))
|
||||||
col)
|
(funcall eglot-move-to-linepos-function col)))
|
||||||
(setq col 0))
|
(if marker (copy-marker (point-marker)) (point))))
|
||||||
(funcall eglot-move-to-linepos-function col)))
|
|
||||||
(if marker (copy-marker (point-marker)) (point)))))
|
|
||||||
|
|
||||||
|
|
||||||
;;; More helpers
|
;;; More helpers
|
||||||
|
|
@ -3023,6 +3026,13 @@ Records BEG, END and PRE-CHANGE-LENGTH locally."
|
||||||
`(,lsp-beg ,lsp-end ,pre-change-length
|
`(,lsp-beg ,lsp-end ,pre-change-length
|
||||||
,(buffer-substring-no-properties beg end)))))
|
,(buffer-substring-no-properties beg end)))))
|
||||||
(_ (setf eglot--recent-changes :emacs-messup)))
|
(_ (setf eglot--recent-changes :emacs-messup)))
|
||||||
|
;; JT@2025-11-14: Not 100% sure an idle timer is right to coalesce
|
||||||
|
;; multiple edits into a single didChange notification. It allows,
|
||||||
|
;; for example, user to modify the buffer in one place, spend
|
||||||
|
;; arbitrary time in quick successive movement and edit again. Only
|
||||||
|
;; after the idle delay will the change be sent. A regular timer
|
||||||
|
;; would be simpler would notify the server to start processing
|
||||||
|
;; changes sooner.
|
||||||
(when eglot--change-idle-timer (cancel-timer eglot--change-idle-timer))
|
(when eglot--change-idle-timer (cancel-timer eglot--change-idle-timer))
|
||||||
(let ((buf (current-buffer)))
|
(let ((buf (current-buffer)))
|
||||||
(setq eglot--change-idle-timer
|
(setq eglot--change-idle-timer
|
||||||
|
|
@ -3455,8 +3465,7 @@ for which LSP on-type-formatting should be requested."
|
||||||
((and beg end)
|
((and beg end)
|
||||||
`(:textDocument/rangeFormatting
|
`(:textDocument/rangeFormatting
|
||||||
:documentRangeFormattingProvider
|
:documentRangeFormattingProvider
|
||||||
(:range ,(list :start (eglot--pos-to-lsp-position beg)
|
(:range ,(eglot-region-range beg end))))
|
||||||
:end (eglot--pos-to-lsp-position end)))))
|
|
||||||
(t
|
(t
|
||||||
'(:textDocument/formatting :documentFormattingProvider nil)))))
|
'(:textDocument/formatting :documentFormattingProvider nil)))))
|
||||||
(eglot-server-capable-or-lose cap)
|
(eglot-server-capable-or-lose cap)
|
||||||
|
|
@ -4095,8 +4104,7 @@ edit proposed by the server."
|
||||||
(cl-defun eglot--code-action-params (&key (beg (point)) (end beg)
|
(cl-defun eglot--code-action-params (&key (beg (point)) (end beg)
|
||||||
only triggerKind)
|
only triggerKind)
|
||||||
(list :textDocument (eglot--TextDocumentIdentifier)
|
(list :textDocument (eglot--TextDocumentIdentifier)
|
||||||
:range (list :start (eglot--pos-to-lsp-position beg)
|
:range (eglot-region-range beg end)
|
||||||
:end (eglot--pos-to-lsp-position end))
|
|
||||||
:context
|
:context
|
||||||
`(:diagnostics
|
`(:diagnostics
|
||||||
[,@(mapcar #'eglot--diag-to-lsp-diag
|
[,@(mapcar #'eglot--diag-to-lsp-diag
|
||||||
|
|
@ -4531,8 +4539,7 @@ If NOERROR, return predicate, else erroring function."
|
||||||
(eglot--current-server-or-lose)
|
(eglot--current-server-or-lose)
|
||||||
:textDocument/inlayHint
|
:textDocument/inlayHint
|
||||||
(list :textDocument (eglot--TextDocumentIdentifier)
|
(list :textDocument (eglot--TextDocumentIdentifier)
|
||||||
:range (list :start (eglot--pos-to-lsp-position from)
|
:range (eglot-region-range from to))
|
||||||
:end (eglot--pos-to-lsp-position to)))
|
|
||||||
:success-fn (lambda (hints)
|
:success-fn (lambda (hints)
|
||||||
(eglot--when-live-buffer buf
|
(eglot--when-live-buffer buf
|
||||||
(eglot--widening
|
(eglot--widening
|
||||||
|
|
@ -4714,8 +4721,7 @@ If NOERROR, return predicate, else erroring function."
|
||||||
((eglot-server-capable :semanticTokensProvider :range)
|
((eglot-server-capable :semanticTokensProvider :range)
|
||||||
(req :textDocument/semanticTokens/range beg end
|
(req :textDocument/semanticTokens/range beg end
|
||||||
(list :textDocument (eglot--TextDocumentIdentifier)
|
(list :textDocument (eglot--TextDocumentIdentifier)
|
||||||
:range (list :start (eglot--pos-to-lsp-position beg)
|
:range (eglot-region-range beg end))
|
||||||
:end (eglot--pos-to-lsp-position end)))
|
|
||||||
#'identity))
|
#'identity))
|
||||||
(t
|
(t
|
||||||
(req :textDocument/semanticTokens/full (point-min) (point-max)
|
(req :textDocument/semanticTokens/full (point-min) (point-max)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue