Skip to content

Commit

Permalink
further racer-debug work
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Nov 4, 2016
1 parent fdf6f2e commit 24870a7
Showing 1 changed file with 59 additions and 31 deletions.
90 changes: 59 additions & 31 deletions racer.el
Original file line number Diff line number Diff line change
Expand Up @@ -100,35 +100,53 @@ If nil, we will query $CARGO_HOME at runtime."
(let ((root (locate-dominating-file (or buffer-file-name default-directory) "Cargo.toml")))
(and root (file-truename root))))

;; Variables used by `racer-debug'.
(defvar racer--last-directory nil)
(defvar racer--last-src-path nil)
(defvar racer--last-cargo-home nil)
(defvar racer--last-command nil)
(defvar racer--last-args nil)
(defvar racer--prev-state nil)

(defun racer-debug ()
"Open a buffer with debug information necessary for filing a bug report."
"Open a buffer describing the last racer command run.
Helps users find configuration issues, or file bugs on
racer or racer.el."
(interactive)
(let ((buf (get-buffer-create "*racer-debug*"))
(inhibit-read-only t))
(with-current-buffer buf
(erase-buffer)
(setq buffer-read-only t)
(insert "Configuration:\n")
(insert "default-directory: " racer--last-directory "\n")
(insert "RUST_SRC_PATH: " racer--last-src-path "\n")
(insert "CARGO_HOME: " racer--last-cargo-home "\n\n")
(insert "Command run:\n")
(insert "$ " racer-cmd " " racer--last-command " " (s-join " " racer--last-args))
(insert "\n\nPlease report bugs ")
(insert (racer--url-button "on GitHub" "https://github.com/racer-rust/emacs-racer/issues/new")
"."))
(let* ((process-environment
(plist-get racer--prev-state :process-environment))
(rust-src-path-used
(--first (s-prefix-p "RUST_SRC_PATH=" it) process-environment))
(cargo-home-used
(--first (s-prefix-p "CARGO_HOME=" it) process-environment))
(stdout (plist-get racer--prev-state :stdout))
(stderr (plist-get racer--prev-state :stderr)))
(insert
"The last racer command was:\n\n"
(format "$ cd %s\n"
(plist-get racer--prev-state :default-directory))
(format "$ export %s\n" cargo-home-used)
(format "$ export %s\n" rust-src-path-used)
(format "$ %s %s\n\n"
(plist-get racer--prev-state :program)
(s-join " " (plist-get racer--prev-state :args)))
(format "This command terminated with exit code %s.\n\n"
(plist-get racer--prev-state :exit-code))
(if (s-blank? stdout)
"No output on stdout.\n\n"
(format "stdout:\n\n%s\n\n" (s-trim-right stdout)))
(if (s-blank? stderr)
"No output on stderr.\n\n"
(format "stderr:\n\n%s\n\n" (s-trim-right stderr)))
"Please report bugs "
(racer--url-button "on GitHub" "https://github.com/racer-rust/emacs-racer/issues/new")
".")))
(switch-to-buffer buf)
(goto-char (point-min))))

(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 @@ -138,14 +156,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)))
;; Save variables for `racer-debug'.
(setq racer--last-directory default-directory)
(setq racer--last-src-path rust-src-path)
(setq racer--last-cargo-home cargo-home)
(setq racer--last-command command)
(setq racer--last-args args)
;; Invoke racer.
(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 Down Expand Up @@ -176,17 +192,29 @@ Return a list (exit-code stdout stderr)."
nil args))
(setq stdout (buffer-string)))
(setq stderr (racer--slurp tmp-file-for-stderr))
(setq racer--prev-state
(list
:program program
:args args
:exit-code exit-code
:stdout stdout
:stderr stderr
:default-directory default-directory
:process-environment process-environment))
(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

0 comments on commit 24870a7

Please sign in to comment.