branch: elpa/doc-show-inline commit 4ab5c6c88c5911038a6504389d5408a3b8c9325e Author: Campbell Barton <ideasma...@gmail.com> Commit: Campbell Barton <ideasma...@gmail.com>
Add eglot support --- changelog.rst | 3 +++ doc-show-inline.el | 56 ++++++++++++++++++++++++++++++++++++------------------ readme.rst | 5 +++-- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/changelog.rst b/changelog.rst index 8f98699421..d13f06cd97 100644 --- a/changelog.rst +++ b/changelog.rst @@ -3,6 +3,9 @@ Change Log ########## +- 2022-11-20 + - Fix use with ``eglot``. + - 2022-04-19 - Fix use of marker-position on an integer. diff --git a/doc-show-inline.el b/doc-show-inline.el index 2fc0ea149e..a1b9d89e79 100644 --- a/doc-show-inline.el +++ b/doc-show-inline.el @@ -8,7 +8,7 @@ ;; URL: https://codeberg.org/ideasman42/emacs-doc-show-inline ;; Keywords: convenience ;; Version: 0.1 -;; Package-Requires: ((emacs "26.2")) +;; Package-Requires: ((emacs "27.1")) ;;; Commentary: @@ -109,7 +109,12 @@ This hook is called instead of the mode hooks such as: `c-mode-hook' and `after-change-major-mode-hook'.") (defvar-local doc-show-inline-locations 'doc-show-inline-locations-default - "Scans the current buffer for locations that `xref' should look-up.") + "Scans the current buffer for locations that `xref' should look-up. +The result of each item must be a (symbol . position) cons cell, +where the symbol is a string used for the look-up and +the position is it's beginning in the buffer. + +Note that this function may move the POINT without using `save-excursion'.") (defvar-local doc-show-inline-extract-doc 'doc-show-inline-extract-doc-default "Function to extract the doc-string given the destination buffer. @@ -136,7 +141,7 @@ When unset, the :filter property from `doc-show-inline-mode-defaults' is used.") ;; Allow disabling for debugging. (defconst doc-show-inline--use-lookup-cache t) ;; Always set when the mode is active: -;; - key: the symbol as a string (from `xref-backend-identifier-at-point'), +;; - key: the symbol as a string (from `xref-backend-identifier-at-point' or from `imenu'), ;; - value: the syntax-highlighted string to display (from `doc-show-inline--doc-from-xref'). (defvar-local doc-show-inline--lookup-cache nil) @@ -437,7 +442,11 @@ the point should not be moved by this function." (pair nil) (mark nil) (imstack nil) - (result nil)) + (result nil) + + ;; As the results differ between back-ends, some custom handling is needed. + (xref-backend (xref-find-backend))) + ;; Elements of alist are either ("name" . marker), or ;; ("submenu" ("name" . marker) ... ). The list can be ;; Arbitrarily nested. @@ -462,8 +471,28 @@ the point should not be moved by this function." (marker-position mark)) (t ;; Integer. mark)))) + (unless (or (and pos-beg (<= pos pos-beg)) (and pos-end (>= pos pos-end))) - (push pos result)))))) + (goto-char pos) + (let ((sym nil)) + (cond + ((eq xref-backend 'eglot) + ;; EGLOT mode has some differences. + ;; - `xref-backend-identifier-at-point' isn't functional. + ;; - The point is at the beginning of the line. + ;; For this reason, it's necessary to search for `sym' & set the + ;; position to this. + (setq sym (car pair)) + (unless (looking-at-p (regexp-quote sym)) + ;; In most cases limiting by `line-end-position' is sufficient. + (save-match-data + (when (search-forward sym pos-end t) + (setq pos (- (point) (length sym))))))) + (t + ;; This works for `xref-lsp'. + (setq sym (xref-backend-identifier-at-point xref-backend)))) + + (push (cons sym pos) result))))))) (t (setq alist (car imstack)) (setq imstack (cdr imstack))))) @@ -690,28 +719,17 @@ XREF-BACKEND is the back-end used to find this symbol." buf)) (while points - (let ((pos (pop points))) + (pcase-let ((`(,sym . ,pos) (pop points))) (cond ((null (doc-show-inline--pos-in-overlays pos overlays-in-view)) (doc-show-inline--log-info "symbol \"%s\" in %S at point %d is not in the overlay list" - (save-excursion - (goto-char pos) - (xref-backend-identifier-at-point xref-backend)) + sym (current-buffer) pos)) (t (goto-char pos) - (let ((sym (xref-backend-identifier-at-point xref-backend))) - (cond - ((null sym) - (doc-show-inline--log-fail - "can't find symbol in %S at point %d using `xref-backend' %S" - (current-buffer) - pos - xref-backend)) - (t - (doc-show-inline--idle-handle-pos pos sym xref-backend))))))))) + (doc-show-inline--idle-handle-pos pos sym xref-backend)))))) ;; Close any buffers loaded only for the purpose of extracting text. (mapc 'kill-buffer temporary-buffers))))) diff --git a/readme.rst b/readme.rst index da32c6fb71..ff68de1999 100644 --- a/readme.rst +++ b/readme.rst @@ -116,7 +116,8 @@ These functions can overridden to adjust behavior. so by default they are ignored to avoid unnecessary overhead. ``doc-show-inline-locations`` (symbol ``'doc-show-inline-locations-default``) - A function that returns a list of points that contain symbols from the current buffer. + A function that returns a list of ``(symbol . point)`` cons cells, + containing symbols and their starting positions in th current buffer. By default ``imenu`` is used for this purpose. @@ -134,7 +135,7 @@ Details - This package only directly depends on ``imenu`` and ``xref``, so any languages that store function definitions in external files will work. - While there are no direct dependencies on ``lsp-mode``, - this has only been tested using ``lsp-mode`` with both ``ccls`` and ``clangd`` backends. + this has been tested using ``lsp-mode`` and ``eglot`` with both ``ccls`` and ``clangd`` backends. - Documentation overlays are added while idle. - Any comment before the function is considered it's doc-string, *(unless it's the trailing comment of a non-blank line).*