Skip to content

Define xref methods only for emacs versions >= 25 (possibly fixes xref support) #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 46 additions & 45 deletions racer.el
Original file line number Diff line number Diff line change
Expand Up @@ -196,51 +196,52 @@
;;;###autoload
(defun racer--xref-backend () 'racer)

(cl-defmethod xref-backend-identifier-at-point ((_backend (eql racer)))
(let ((tmp-file (make-temp-file "racer")))
(write-region nil nil tmp-file nil 'silent)
(propertize (symbol-name (symbol-at-point))
'line (line-number-at-pos)
'column (current-column)
'file-name (buffer-file-name)
'tmp-file tmp-file)))

(cl-defmethod xref-backend-definitions ((_backend (eql racer)) identifier)
(let ((tmp-file (plist-get (text-properties-at 0 identifier) 'tmp-file))
(completions (racer--xref-make-definitions identifier)))
(prog1 (if (listp completions)
completions
(list completions))
(delete-file tmp-file))))

(cl-defmethod xref-backend-references ((_backend (eql racer)) identifier)
nil)

(defun racer--xref-make-definitions (identifier)
(setenv "RUST_SRC_PATH" (expand-file-name racer-rust-src-path))
(let* ((properties (text-properties-at 0 identifier))
(line-num (number-to-string (plist-get properties 'line)))
(column-num (number-to-string (plist-get properties 'column)))
(file-name (plist-get properties 'file-name))
(tmp-file (plist-get properties 'tmp-file))
(output (apply #' process-lines "racer" (list
"find-definition"
line-num
column-num
file-name
tmp-file)))
(completions '())
(regexp "MATCH \\w+,\\([0-9]+\\),\\([0-9]+\\),\\(.+\\),.+,\\(.+\\)"))
(dolist (completion output completions)
(when (string-match regexp completion)
(setq completions (append completions
(xref-make
(match-string 4 completion) ;; name
(xref-make-file-location
(match-string 3 completion) ;; file name
(string-to-number (match-string 1 completion)) ;; line
(string-to-number (match-string 2 completion)) ;; column
))))))))
(when (>= emacs-major-version 25)
(cl-defmethod xref-backend-identifier-at-point ((_backend (eql racer)))
(let ((tmp-file (make-temp-file "racer")))
(write-region nil nil tmp-file nil 'silent)
(propertize (symbol-name (symbol-at-point))
'line (line-number-at-pos)
'column (current-column)
'file-name (buffer-file-name)
'tmp-file tmp-file)))

(cl-defmethod xref-backend-definitions ((_backend (eql racer)) identifier)
(let ((tmp-file (plist-get (text-properties-at 0 identifier) 'tmp-file))
(completions (racer--xref-make-definitions identifier)))
(prog1 (if (listp completions)
completions
(list completions))
(delete-file tmp-file))))

(cl-defmethod xref-backend-references ((_backend (eql racer)) identifier)
nil)

(defun racer--xref-make-definitions (identifier)
(setenv "RUST_SRC_PATH" (expand-file-name racer-rust-src-path))
(let* ((properties (text-properties-at 0 identifier))
(line-num (number-to-string (plist-get properties 'line)))
(column-num (number-to-string (plist-get properties 'column)))
(file-name (plist-get properties 'file-name))
(tmp-file (plist-get properties 'tmp-file))
(output (apply #' process-lines "racer" (list
"find-definition"
line-num
column-num
file-name
tmp-file)))
(completions '())
(regexp "MATCH \\w+,\\([0-9]+\\),\\([0-9]+\\),\\(.+\\),.+,\\(.+\\)"))
(dolist (completion output completions)
(when (string-match regexp completion)
(setq completions (append completions
(xref-make
(match-string 4 completion) ;; name
(xref-make-file-location
(match-string 3 completion) ;; file name
(string-to-number (match-string 1 completion)) ;; line
(string-to-number (match-string 2 completion)) ;; column
)))))))))


(defun racer--syntax-highlight (str)
Expand Down