Andersbakken / rtags

A client/server indexer for c/c++/objc[++] with integration for Emacs based on clang.
http://www.rtags.net
GNU General Public License v3.0
1.83k stars 252 forks source link

[Feature request] Easy integration with Eldoc #987

Open tuhdo opened 7 years ago

tuhdo commented 7 years ago

Since rtags can already display full symbol info at point, it is easy to add integration with Eldoc. Here is an Elisp snippet that I whipped out quickly to get it done:

  (defun rtags-eldoc-function ()
    (let* ((symbol (rtags-symbol-info-internal :location (or (rtags-target-declaration-first) (rtags-current-location)) :silent t))
           (symbol-text (cdr (assoc 'contents (rtags-get-file-contents :info symbol :maxlines (or 1 5))))))
      (when symbol
        (message symbol-text))))

(defun rtags-eldoc-mode ()
    (interactive)
    (setq-local eldoc-documentation-function #'rtags-eldoc-function)
    (eldoc-mode 1))

Just to show how easy it is to add Eldoc support.

I guess it could be made more polished e.g. type information is colorized with font-lock-type-face.

jwalt commented 7 years ago

Actually, eldoc-documentation-function is not supposed to message by itself, it should just return the string. As a result, this would be a minimal working example:

  (defun rtags-eldoc-function () (rtags-get-summary-text 1))

For syntax highlighting, you could use this helper function:

(defun fontify-string (str mode)
  "Return STR fontified according to MODE."
  (with-temp-buffer
        (insert str)
        (delay-mode-hooks (funcall mode))
        (font-lock-default-function mode)
        (font-lock-default-fontify-region
         (point-min) (point-max) nil)
        (buffer-string)))

and for a bit more polished eldoc, something like this:

  (defun rtags-eldoc-function ()
    (let ((summary (rtags-get-summary-text)))
      (and summary
           (fontify-string
            (replace-regexp-in-string
             "{[^}]*$" ""
             (mapconcat
              (lambda (str) (if (= 0 (length str)) "//" (string-trim str)))
              (split-string summary "\r?\n")
              " "))
            major-mode))))

(It joins all lines into one, converts brief docs into a trailing comments and strips incomplete function bodies)

zhihuiFan commented 6 years ago

I execute "rtags-symbol-type" function every time when I am interested in a variable. I hope I can see the type information automatically on message buffer. However I don't know how to do it:( Looks the above code is something for that, could anyone explain more how to make it happen? If you can explain how rtags-eldoc-function is called, that will be much better.