Skip to content

Commit c2ec2c5

Browse files
committed
fix(file-symbols): use end-line, stop assuming ht
When symbols span multiple lines, `cend` might be less than `cbeg`, so we must use the correct line of the symbol instead of assuming the symbol only lives on one line. Also, `consult-lsp--flatten-document-symbols` assumed that the return values from LSP requests was always a hash-table, so in the same vein as #30 we are changing the code to use lsp-mode-provided helpers. Closes: #33
1 parent 19606a0 commit c2ec2c5

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

consult-lsp.el

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
;; Author: Gerry Agbobada
66
;; Maintainer: Gerry Agbobada
77
;; Package-Requires: ((emacs "27.1") (lsp-mode "5.0") (consult "0.16") (f "0.20.0"))
8-
;; Version: 1.0-dev
8+
;; Version: 1.1
99
;; Homepage: https://github.com/gagbo/consult-lsp
1010

1111
;; Copyright (c) 2021 Gerry Agbobada and contributors
@@ -428,39 +428,42 @@ usable in the annotation-function."
428428
;;;; File symbols
429429

430430
(defun consult-lsp--flatten-document-symbols (to-flatten)
431-
"Helper function for flattening document symbols to a plain list."
431+
"Helper function for flattening document symbols TO-FLATTEN to a plain list."
432432
(cl-labels ((rec-helper
433433
(to-flatten accumulator)
434434
(dolist (table to-flatten)
435-
(when (hash-table-p table)
436-
(push table accumulator)
437-
(when-let ((children (gethash "children" table)))
438-
(setq accumulator (rec-helper
439-
(append children nil) ; convert children from vector to list
440-
accumulator))
441-
(remhash "children" table))))
435+
(push table accumulator)
436+
(when-let ((children (lsp-get table "children")))
437+
(setq accumulator (rec-helper
438+
(append children nil) ; convert children from vector to list
439+
accumulator))))
442440
accumulator))
443441
(nreverse (rec-helper to-flatten nil))))
444442

445443
(defun consult-lsp--file-symbols--transformer (symbol)
446444
"Default transformer to produce a completion candidate from SYMBOL."
447-
(let ((line (thread-first symbol
448-
(lsp:document-symbol-selection-range)
449-
(lsp:range-start)
450-
(lsp:position-line)
451-
(lsp-translate-line)))
445+
(let ((lbeg (thread-first symbol
446+
(lsp:document-symbol-selection-range)
447+
(lsp:range-start)
448+
(lsp:position-line)
449+
(lsp-translate-line)))
450+
(lend (thread-first symbol
451+
(lsp:document-symbol-selection-range)
452+
(lsp:range-end)
453+
(lsp:position-line)
454+
(lsp-translate-line)))
452455
(cbeg (thread-first symbol
453-
(lsp:document-symbol-selection-range)
454-
(lsp:range-start)
455-
(lsp:position-character)
456-
(lsp-translate-column)))
456+
(lsp:document-symbol-selection-range)
457+
(lsp:range-start)
458+
(lsp:position-character)
459+
(lsp-translate-column)))
457460
(cend (thread-first symbol
458-
(lsp:document-symbol-selection-range)
459-
(lsp:range-end)
460-
(lsp:position-character)
461-
(lsp-translate-column))))
462-
(let ((beg (lsp--line-character-to-point line cbeg))
463-
(end (lsp--line-character-to-point line cend))
461+
(lsp:document-symbol-selection-range)
462+
(lsp:range-end)
463+
(lsp:position-character)
464+
(lsp-translate-column))))
465+
(let ((beg (lsp--line-character-to-point lbeg cbeg))
466+
(end (lsp--line-character-to-point lend cend))
464467
(marker (make-marker)))
465468
(set-marker marker beg)
466469
;; Pre-condition to respect narrowing
@@ -483,7 +486,7 @@ usable in the annotation-function."
483486
(format " (%s)"
484487
symb-info-name))))
485488
marker
486-
(1+ line)
489+
(1+ lbeg)
487490
'consult--type (consult-lsp--symbols--kind-to-narrow symbol)
488491
'consult--name (lsp:symbol-information-name symbol)
489492
'consult--details (lsp:document-symbol-detail? symbol))))))

0 commit comments

Comments
 (0)