Skip to content

Commit

Permalink
Define a convenience command for running shell commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Nov 4, 2016
1 parent c778303 commit 8b84a4d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 30 deletions.
48 changes: 40 additions & 8 deletions racer.el
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ If nil, we will query $CARGO_HOME at runtime."
(propertize text 'face 'racer-help-heading-face))

(defun racer--call (command &rest args)
"Call racer command COMMAND with args ARGS."
"Call racer command COMMAND with args ARGS.
Return stdout if COMMAND exits normally, otherwise show an
error."
(let ((rust-src-path (or racer-rust-src-path (getenv "RUST_SRC_PATH")))
(cargo-home (or racer-cargo-home (getenv "CARGO_HOME"))))
(when (null rust-src-path)
Expand All @@ -115,7 +117,12 @@ If nil, we will query $CARGO_HOME at runtime."
(format "RUST_SRC_PATH=%s" (expand-file-name rust-src-path))
(format "CARGO_HOME=%s" (expand-file-name cargo-home)))
process-environment)))
(apply #'process-lines racer-cmd command args))))
(-let [(exit-code stdout _stderr)
(racer--shell-command racer-cmd (cons command args))]
(unless (zerop exit-code)
(user-error "%s exited with %s. `M-x racer-debug' for more info"
racer-cmd exit-code))
stdout))))

(defmacro racer--with-temporary-file (path-sym &rest body)
"Create a temporary file, and bind its path to PATH-SYM.
Expand All @@ -126,15 +133,40 @@ Evaluate BODY, then delete the temporary file."
(progn ,@body)
(delete-file ,path-sym))))

(defun racer--slurp (file)
"Return the contents of FILE as a string."
(with-temp-buffer
(insert-file-contents-literally file)
(buffer-string)))

(defun racer--shell-command (program args)
"Execute PROGRAM with ARGS.
Return a list (exit-code stdout stderr)."
(racer--with-temporary-file tmp-file-for-stderr
(let (exit-code stdout stderr)
;; Create a temporary buffer for `call-process` to write stdout
;; into.
(with-temp-buffer
(setq exit-code
(apply #'call-process program nil
(list (current-buffer) tmp-file-for-stderr)
nil args))
(setq stdout (buffer-string)))
(setq stderr (racer--slurp tmp-file-for-stderr))
(list exit-code stdout stderr))))

(defun racer--call-at-point (command)
"Call racer command COMMAND at point of current buffer."
"Call racer command COMMAND at point of current buffer.
Return a list of all the lines returned by the command."
(racer--with-temporary-file tmp-file
(write-region nil nil tmp-file nil 'silent)
(racer--call command
(number-to-string (line-number-at-pos))
(number-to-string (racer--current-column))
(buffer-file-name)
tmp-file)))
(s-lines
(s-trim-right
(racer--call command
(number-to-string (line-number-at-pos))
(number-to-string (racer--current-column))
(buffer-file-name)
tmp-file)))))

(defun racer--read-rust-string (string)
"Convert STRING, a rust string literal, to an elisp string."
Expand Down
54 changes: 32 additions & 22 deletions test/racer-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ baz.")))
"Ensure we extract the correct name in `racer--describe-at-point'."
(cl-letf (((symbol-function 'racer--call)
(lambda (&rest _)
(list
"PREFIX 36,37,n"
"MATCH new;new();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"Constructs a new, empty `Vec<T>`.\""
"END"))))
(s-join
"\n"
(list
"PREFIX 36,37,n"
"MATCH new;new();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"Constructs a new, empty `Vec<T>`.\""
"END")))))
(should
(equal (plist-get (racer--describe-at-point "new") :name)
"new"))))
Expand All @@ -128,10 +130,12 @@ baz.")))
"If there's no docstring, racer--describe-at-point should use nil."
(cl-letf (((symbol-function 'racer--call)
(lambda (&rest _)
(list
"PREFIX 36,37,n"
"MATCH new;new();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END"))))
(s-join
"\n"
(list
"PREFIX 36,37,n"
"MATCH new;new();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END")))))
(should
(null (plist-get (racer--describe-at-point "new") :docstring)))))

Expand All @@ -141,12 +145,14 @@ baz.")))
Since we've moved point to the end of symbol, the other functions just happen to have the same prefix."
(cl-letf (((symbol-function 'racer--call)
(lambda (&rest _)
(list
"PREFIX 36,37,n"
"MATCH new_bar;new_bar();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"MATCH new;new();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"MATCH new_foo;new_foo();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END"))))
(s-join
"\n"
(list
"PREFIX 36,37,n"
"MATCH new_bar;new_bar();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"MATCH new;new();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"MATCH new_foo;new_foo();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END")))))
(should
(equal (plist-get (racer--describe-at-point "new") :name)
"new"))))
Expand All @@ -173,10 +179,12 @@ Since we've moved point to the end of symbol, the other functions just happen to
"Smoke test for `racer-describe'."
(cl-letf (((symbol-function 'racer--call)
(lambda (&rest _)
(list
"PREFIX 36,37,n"
"MATCH foo;foo();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END"))))
(s-join
"\n"
(list
"PREFIX 36,37,n"
"MATCH foo;foo();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END")))))
(with-temp-buffer
(rust-mode)
(insert "foo();")
Expand All @@ -192,10 +200,12 @@ Otherwise, if the point is at the start of the symbol, we don't find anything."
(cl-letf (((symbol-function 'racer--call)
(lambda (&rest _)
(setq point-during-call (point))
(list
"PREFIX 36,37,n"
"MATCH foo;foo();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END"))))
(s-join
"\n"
(list
"PREFIX 36,37,n"
"MATCH foo;foo();294;11;/home/user/src/rustc-1.10.0/src/libstd/../libcollections/vec.rs;Function;pub fn new() -> Vec<T>;\"\""
"END")))))
(with-temp-buffer
(rust-mode)
(insert "foo();")
Expand Down

0 comments on commit 8b84a4d

Please sign in to comment.