Skip to content

Instantly share code, notes, and snippets.

@andelf
Last active October 4, 2015 03:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andelf/b4703902778accfd6ced to your computer and use it in GitHub Desktop.
Save andelf/b4703902778accfd6ced to your computer and use it in GitHub Desktop.
Racer Auto-Complete Mode
;; FileName : racer-autocomplete.el
;; Author : ShuYu Wang <andelf@gmail.com>
;; Created : Wed May 20 10:08:03 2015 by ShuYu Wang
;; Copyright : Feather Workshop (c) 2015
;; Description : racer auto-complete source
;; Time-stamp: <2015-05-20 10:08:18 andelf>
;;; copy this to your load-path and add following to your ~/.emacs
;; (setq racer-cmd "/your/path/to/bin/racer")
;; (setq rust-srcpath "/your/path/to/rust/src")
;; (require 'racer-autocomplete)
;; (add-hook 'rust-mode-hook
;; #'(lambda ()
;; (add-to-list 'ac-sources 'ac-source-racer)
;; ))
(defvar racer-cmd "/Users/233/github/racer/target/release/racer")
(defvar rust-srcpath "/Users/233/github/rust/src/")
;;; rust-mode no longer requires cl, so am putting it here for now (this file uses 'case')
(require 'cl)
(defvar racer-file-name)
(defvar racer-tmp-file-name)
(defvar racer-line-number)
(defvar racer-column-number)
(defvar racer-completion-results)
(defvar racer-start-pos)
(defvar racer-end-pos)
(defun racer-get-line-number ()
; for some reason if the current-column is 0, then the linenumber is off by 1
(if (= (current-column) 0)
(1+ (count-lines 1 (point)))
(count-lines 1 (point))))
(defun racer--write-tmp-file (tmp-file-name)
"Write the racer temporary file to `TMP-FILE-NAME'."
(push-mark)
(setq racer-file-name (buffer-file-name))
(setq racer-tmp-file-name tmp-file-name)
(setq racer-line-number (racer-get-line-number))
(setq racer-column-number (current-column))
(setq racer-completion-results `())
(write-region nil nil tmp-file-name nil 0))
(defun racer--tempfilename ()
(concat temporary-file-directory (file-name-nondirectory (buffer-file-name)) ".racertmp"))
(defun racer--candidates ()
(setenv "RUST_SRC_PATH" rust-srcpath)
(let ((tmpfilename (concat (buffer-file-name) ".racertmp")))
(racer--write-tmp-file tmpfilename)
(unwind-protect
(progn
(let ((completion-results (list))
(lines (process-lines racer-cmd
"complete"
(number-to-string (count-lines 1 (point)))
(number-to-string (current-column))
tmpfilename)))
(dolist (line lines)
(when (string-match "^MATCH \\([^,]+\\),\\(.+\\)$" line)
(let ((completion (match-string 1 line)))
(push completion completion-results))))
completion-results))
(delete-file tmpfilename))))
(defun racer--prefix ()
(setenv "RUST_SRC_PATH" rust-srcpath)
(let ((tmpfilename (concat (buffer-file-name) ".racertmp")))
(racer--write-tmp-file tmpfilename)
(unwind-protect
(progn
(let ((lines (process-lines racer-cmd
"prefix"
(number-to-string (count-lines 1 (point)))
(number-to-string (current-column))
tmpfilename)))
(when (string-match "^PREFIX \\(.+\\),\\(.+\\),\\(.*\\)$" (nth 0 lines))
(+ (- (point) (current-column))
(string-to-number (match-string 1 (nth 0 lines)))))))
(delete-file tmpfilename))))
(ac-define-source racer
'((candidates . racer--candidates)
(prefix . racer--prefix)
(requires . 0)
(cache)
(symbol . "R")))
(provide 'racer-autocomplete)
@andelf
Copy link
Author

andelf commented May 20, 2015

Copyright (c) 2014 Phil Dawes
Copyright (c) 2015 andelf <andelf@gmail.com>

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment