From: Dmitry Gutov Date: Mon, 4 May 2015 23:44:20 +0000 (+0300) Subject: Insert, highlight and align line numbers in xref output X-Git-Tag: emacs-25.0.90~2214 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=9fa69f6b14a3f13217ddbafbc120d5f8fafaca50;p=emacs.git Insert, highlight and align line numbers in xref output * lisp/progmodes/etags.el (xref-location-line): Specialize for xref-etags-location. * lisp/progmodes/xref.el (xref-location-line): New generic method. (xref-file-location): Add reader for the line slot. (xref--location-at-point): Skip to the `xref-location' property. (xref--collect-reference): Drop the line number from description. (xref--insert-xrefs): Insert, highlight and align line numbers. --- diff --git a/lisp/progmodes/etags.el b/lisp/progmodes/etags.el index 4e923aac197..6acafdbaba0 100644 --- a/lisp/progmodes/etags.el +++ b/lisp/progmodes/etags.el @@ -2143,6 +2143,10 @@ for \\[find-tag] (which see)." (etags-goto-tag-location tag-info) (point-marker))))) +(cl-defmethod xref-location-line ((l xref-etags-location)) + (with-slots (tag-info) l + (nth 1 tag-info))) + (provide 'etags) diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el index ae0fbb82617..657657c687a 100644 --- a/lisp/progmodes/xref.el +++ b/lisp/progmodes/xref.el @@ -73,13 +73,17 @@ "Return a string used to group a set of locations. This is typically the filename.") +(cl-defgeneric xref-location-line (_location) + "Return the line number corresponding to the location." + nil) + ;;;; Commonly needed location classes are defined here: ;; FIXME: might be useful to have an optional "hint" i.e. a string to ;; search for in case the line number is sightly out of date. (defclass xref-file-location (xref-location) ((file :type string :initarg :file) - (line :type fixnum :initarg :line) + (line :type fixnum :initarg :line :reader xref-location-line) (column :type fixnum :initarg :column)) :documentation "A file location is a file/line/column triple. Line numbers start from 1 and columns from 0.") @@ -435,9 +439,10 @@ Used for temporary buffers.") (xref-show-location-at-point)) (defun xref--location-at-point () - (save-excursion - (back-to-indentation) - (get-text-property (point) 'xref-location))) + (let ((pos (next-single-char-property-change (line-beginning-position) + 'xref-location + nil (line-end-position)))) + (and pos (get-text-property pos 'xref-location)))) (defvar-local xref--window nil "ACTION argument to call `display-buffer' with.") @@ -531,11 +536,24 @@ XREF-ALIST is of the form ((GROUP . (XREF ...)) ...). Where GROUP is a string for decoration purposes and XREF is an `xref--xref' object." (require 'compile) ;; For the compilation-info face. - (cl-loop for ((group . xrefs) . more1) on xref-alist do + (cl-loop for ((group . xrefs) . more1) on xref-alist + for max-line-width = + (cl-loop for xref in xrefs + maximize (let ((line (xref-location-line + (oref xref :location)))) + (length (and line (format "%d" line))))) + for line-format = (and max-line-width + (format "%%%dd: " max-line-width)) + do (xref--insert-propertized '(face compilation-info) group "\n") (cl-loop for (xref . more2) on xrefs do - (insert " ") (with-slots (description location) xref + (let ((line (xref-location-line location))) + (if line + (xref--insert-propertized + '(face compilation-line-number) + (format line-format line)) + (insert " "))) (xref--insert-propertized (list 'xref-location location ;; 'face 'font-lock-keyword-face @@ -729,12 +747,9 @@ tools are used, and when." (regexp-quote name)) (line-end-position) t) (goto-char (match-beginning 0)) - (xref-make (format - "%d: %s" - line - (buffer-substring - (line-beginning-position) - (line-end-position))) + (xref-make (buffer-substring + (line-beginning-position) + (line-end-position)) (xref-make-file-location file line (current-column))))))))