Skip to content

Commit 9e03890

Browse files
tarsiusbrotzeit
authored andcommitted
Create rust-compile.el from existing code
For the time being `require' the new library from "rust-mode.el". In the mid-term we should stop doing that, so that users can load it if and only if they want to do so.
1 parent 5585cf9 commit 9e03890

File tree

3 files changed

+86
-74
lines changed

3 files changed

+86
-74
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EMACS ?= emacs
1010
EMACS_ARGS ?=
1111

1212
ELS = rust-mode.el
13+
ELS += rust-compile.el
1314
ELCS = $(ELS:.el=.elc)
1415

1516
DEPS =

rust-compile.el

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
;;; rust-compile.el --- Compile facilities -*-lexical-binding: t-*-
2+
;;; Commentary:
3+
4+
;; This library teaches `compilation-mode' about "rustc" output.
5+
6+
;;; Code:
7+
8+
(require 'compile)
9+
10+
;;; _
11+
12+
(defvar rustc-compilation-location
13+
(let ((file "\\([^\n]+\\)")
14+
(start-line "\\([0-9]+\\)")
15+
(start-col "\\([0-9]+\\)"))
16+
(concat "\\(" file ":" start-line ":" start-col "\\)")))
17+
18+
(defvar rustc-compilation-regexps
19+
(let ((re (concat "^\\(?:error\\|\\(warning\\)\\|\\(note\\)\\)[^\0]+?--> "
20+
rustc-compilation-location)))
21+
(cons re '(4 5 6 (1 . 2) 3)))
22+
"Specifications for matching errors in rustc invocations.
23+
See `compilation-error-regexp-alist' for help on their format.")
24+
25+
(defvar rustc-colon-compilation-regexps
26+
(let ((re (concat "^ *::: " rustc-compilation-location)))
27+
(cons re '(2 3 4 0 1)))
28+
"Specifications for matching `:::` hints in rustc invocations.
29+
See `compilation-error-regexp-alist' for help on their format.")
30+
31+
(defvar rustc-refs-compilation-regexps
32+
(let ((re "^\\([0-9]+\\)[[:space:]]*|"))
33+
(cons re '(nil 1 nil 0 1)))
34+
"Specifications for matching code references in rustc invocations.
35+
See `compilation-error-regexp-alist' for help on their format.")
36+
37+
;; Match test run failures and panics during compilation as
38+
;; compilation warnings
39+
(defvar cargo-compilation-regexps
40+
'("^\\s-+thread '[^']+' panicked at \\('[^']+', \\([^:]+\\):\\([0-9]+\\)\\)"
41+
2 3 nil nil 1)
42+
"Specifications for matching panics in cargo test invocations.
43+
See `compilation-error-regexp-alist' for help on their format.")
44+
45+
(defun rustc-scroll-down-after-next-error ()
46+
"In the new style error messages, the regular expression
47+
matches on the file name (which appears after `-->`), but the
48+
start of the error appears a few lines earlier. This hook runs
49+
after `next-error' (\\[next-error]); it simply scrolls down a few lines in
50+
the compilation window until the top of the error is visible."
51+
(save-selected-window
52+
(when (eq major-mode 'rust-mode)
53+
(select-window (get-buffer-window next-error-last-buffer 'visible))
54+
(when (save-excursion
55+
(beginning-of-line)
56+
(looking-at " *-->"))
57+
(let ((start-of-error
58+
(save-excursion
59+
(beginning-of-line)
60+
(while (not (looking-at "^[a-z]+:\\|^[a-z]+\\[E[0-9]+\\]:"))
61+
(forward-line -1))
62+
(point))))
63+
(set-window-start (selected-window) start-of-error))))))
64+
65+
(eval-after-load 'compile
66+
'(progn
67+
(add-to-list 'compilation-error-regexp-alist-alist
68+
(cons 'rustc-refs rustc-refs-compilation-regexps))
69+
(add-to-list 'compilation-error-regexp-alist 'rustc-refs)
70+
(add-to-list 'compilation-error-regexp-alist-alist
71+
(cons 'rustc rustc-compilation-regexps))
72+
(add-to-list 'compilation-error-regexp-alist 'rustc)
73+
(add-to-list 'compilation-error-regexp-alist-alist
74+
(cons 'rustc-colon rustc-colon-compilation-regexps))
75+
(add-to-list 'compilation-error-regexp-alist 'rustc-colon)
76+
(add-to-list 'compilation-error-regexp-alist-alist
77+
(cons 'cargo cargo-compilation-regexps))
78+
(add-to-list 'compilation-error-regexp-alist 'cargo)
79+
(add-hook 'next-error-hook 'rustc-scroll-down-after-next-error)))
80+
81+
;;; _
82+
(provide 'rust-compile)
83+
;;; rust-compile.el ends here

rust-mode.el

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
;;; Code:
1616

17-
(eval-when-compile (require 'rx)
18-
(require 'compile)
19-
(require 'url-vars))
17+
(eval-when-compile (require 'rx))
18+
(eval-when-compile (require 'url-vars))
2019

2120
(require 'json)
2221
(require 'thingatpt)
@@ -1973,77 +1972,6 @@ Return the created process."
19731972
(or (rust--format-error-handler)
19741973
(message "rustfmt detected problems, see *rustfmt* for more."))))))
19751974

1976-
;;; Compilation
1977-
1978-
(defvar rustc-compilation-location
1979-
(let ((file "\\([^\n]+\\)")
1980-
(start-line "\\([0-9]+\\)")
1981-
(start-col "\\([0-9]+\\)"))
1982-
(concat "\\(" file ":" start-line ":" start-col "\\)")))
1983-
1984-
(defvar rustc-compilation-regexps
1985-
(let ((re (concat "^\\(?:error\\|\\(warning\\)\\|\\(note\\)\\)[^\0]+?--> "
1986-
rustc-compilation-location)))
1987-
(cons re '(4 5 6 (1 . 2) 3)))
1988-
"Specifications for matching errors in rustc invocations.
1989-
See `compilation-error-regexp-alist' for help on their format.")
1990-
1991-
(defvar rustc-colon-compilation-regexps
1992-
(let ((re (concat "^ *::: " rustc-compilation-location)))
1993-
(cons re '(2 3 4 0 1)))
1994-
"Specifications for matching `:::` hints in rustc invocations.
1995-
See `compilation-error-regexp-alist' for help on their format.")
1996-
1997-
(defvar rustc-refs-compilation-regexps
1998-
(let ((re "^\\([0-9]+\\)[[:space:]]*|"))
1999-
(cons re '(nil 1 nil 0 1)))
2000-
"Specifications for matching code references in rustc invocations.
2001-
See `compilation-error-regexp-alist' for help on their format.")
2002-
2003-
;; Match test run failures and panics during compilation as
2004-
;; compilation warnings
2005-
(defvar cargo-compilation-regexps
2006-
'("^\\s-+thread '[^']+' panicked at \\('[^']+', \\([^:]+\\):\\([0-9]+\\)\\)"
2007-
2 3 nil nil 1)
2008-
"Specifications for matching panics in cargo test invocations.
2009-
See `compilation-error-regexp-alist' for help on their format.")
2010-
2011-
(defun rustc-scroll-down-after-next-error ()
2012-
"In the new style error messages, the regular expression
2013-
matches on the file name (which appears after `-->`), but the
2014-
start of the error appears a few lines earlier. This hook runs
2015-
after `next-error' (\\[next-error]); it simply scrolls down a few lines in
2016-
the compilation window until the top of the error is visible."
2017-
(save-selected-window
2018-
(when (eq major-mode 'rust-mode)
2019-
(select-window (get-buffer-window next-error-last-buffer 'visible))
2020-
(when (save-excursion
2021-
(beginning-of-line)
2022-
(looking-at " *-->"))
2023-
(let ((start-of-error
2024-
(save-excursion
2025-
(beginning-of-line)
2026-
(while (not (looking-at "^[a-z]+:\\|^[a-z]+\\[E[0-9]+\\]:"))
2027-
(forward-line -1))
2028-
(point))))
2029-
(set-window-start (selected-window) start-of-error))))))
2030-
2031-
(eval-after-load 'compile
2032-
'(progn
2033-
(add-to-list 'compilation-error-regexp-alist-alist
2034-
(cons 'rustc-refs rustc-refs-compilation-regexps))
2035-
(add-to-list 'compilation-error-regexp-alist 'rustc-refs)
2036-
(add-to-list 'compilation-error-regexp-alist-alist
2037-
(cons 'rustc rustc-compilation-regexps))
2038-
(add-to-list 'compilation-error-regexp-alist 'rustc)
2039-
(add-to-list 'compilation-error-regexp-alist-alist
2040-
(cons 'rustc-colon rustc-colon-compilation-regexps))
2041-
(add-to-list 'compilation-error-regexp-alist 'rustc-colon)
2042-
(add-to-list 'compilation-error-regexp-alist-alist
2043-
(cons 'cargo cargo-compilation-regexps))
2044-
(add-to-list 'compilation-error-regexp-alist 'cargo)
2045-
(add-hook 'next-error-hook 'rustc-scroll-down-after-next-error)))
2046-
20471975
;;; Secondary Commands
20481976

20491977
(defun rust-playpen-region (begin end)

0 commit comments

Comments
 (0)