Skip to content

Commit

Permalink
Set the compilation-error-regexp-alist in a hook (#126)
Browse files Browse the repository at this point in the history
Some modes, for instance verilog-mode, adds to the
compilation-error-regexp-alist and friends which could interfere with
rg-mode regexps. Since rg-mode isn't a generic compilation mode we
want to avoid that.
This solution defer setting these regexps to the last run
compilation-mode-hook to be able to override whatever happens in other
hooks.
  • Loading branch information
dajva committed Nov 13, 2021
1 parent fa7293d commit 47bda7e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
27 changes: 18 additions & 9 deletions rg-result.el
Expand Up @@ -507,6 +507,19 @@ This function is called from `compilation-filter-hook'."
(when (re-search-backward "^File: \\(.*\\)$" (point-min) t)
(list (match-string 1))))))

(defun rg-set-compilation-error-regexps ()
"Set the compilation mode regexps for errors for rg-mode buffers."
(set (make-local-variable 'compilation-error-regexp-alist)
'(rg-group-with-column
rg-nogroup-with-column
rg-group-no-column
rg-nogroup-no-column))
(set (make-local-variable 'compilation-error-regexp-alist-alist)
(list (cons 'rg-nogroup-no-column (list rg-file-line-pattern-nogroup 1 2))
(cons 'rg-nogroup-with-column (list rg-file-line-column-pattern-nogroup 1 2 3))
(cons 'rg-group-with-column (list (rg-file-line-column-pattern-group) 'rg-match-grouped-filename 1 2))
(cons 'rg-group-no-column (list (rg-file-line-pattern-group) 'rg-match-grouped-filename 1)))))

(define-compilation-mode rg-mode "rg"
"Major mode for `rg' search results.
Commands:
Expand All @@ -528,14 +541,6 @@ Commands:
(set (make-local-variable 'compilation-line-face) 'rg-line-number-face)
(set (make-local-variable 'compilation-column-face) 'rg-column-number-face)

(set (make-local-variable 'compilation-error-regexp-alist)
'(rg-group-with-column rg-nogroup-with-column rg-group-no-column rg-nogroup-no-column))
(set (make-local-variable 'compilation-error-regexp-alist-alist)
(list (cons 'rg-nogroup-no-column (list rg-file-line-pattern-nogroup 1 2))
(cons 'rg-nogroup-with-column (list rg-file-line-column-pattern-nogroup 1 2 3))
(cons 'rg-group-with-column (list (rg-file-line-column-pattern-group) 'rg-match-grouped-filename 1 2))
(cons 'rg-group-no-column (list (rg-file-line-pattern-group) 'rg-match-grouped-filename 1))))

;; compilation-directory-matcher can't be nil, so we set it to a regexp that
;; can never match.
(set (make-local-variable 'compilation-directory-matcher) '("\\`a\\`"))
Expand All @@ -545,7 +550,11 @@ Commands:
(set (make-local-variable 'compilation-error-screen-columns) nil)
(unless rg-search-history
(setq rg-search-history (rg-history-create)))
(set (make-local-variable 'compilation-filter-hook) '(rg-filter)))
(set (make-local-variable 'compilation-filter-hook) '(rg-filter))
;; Set compilation error regexps as the last compilation-mode-hook to be
;; able to override these if they were set by other hooks.
(add-hook 'compilation-mode-hook
'rg-set-compilation-error-regexps 90 'local))

(defun rg-maybe-show-header ()
"Recreate header if enabled."
Expand Down
29 changes: 29 additions & 0 deletions test/rg.el-test.el
Expand Up @@ -974,6 +974,35 @@ and ungrouped otherwise."
(should (equal (expand-file-name called-dir) project-dir))
(should (eq called-literal nil))))


(defun rg-only-rg-regexps-p ()
(and (seq-every-p
(lambda (elem) (eq elem t))
(mapcar (lambda (regexp)
(s-starts-with-p "rg-" (symbol-name regexp)))
compilation-error-regexp-alist))
(seq-every-p
(lambda (elem) (eq elem t))
(mapcar (lambda (pair)
(s-starts-with-p "rg-" (symbol-name (car pair))))
compilation-error-regexp-alist-alist))))

(ert-deftest rg-integration/compilation-error-regexp ()
"Test that compilation-error-regexp-alist is only using rg regexps."
(with-temp-buffer
(rg-mode)
(should (rg-only-rg-regexps-p)))
(with-temp-buffer
(setq compilation-error-regexp-alist '(foo-bar))
(rg-mode)
(should (rg-only-rg-regexps-p)))
(with-temp-buffer
(add-hook 'compilation-mode-hook
(lambda ()
(setq compilation-error-regexp-alist '(foo-bar))))
(rg-mode)
(should (rg-only-rg-regexps-p))))

(provide 'rg.el-test)

;;; rg.el-test.el ends here

0 comments on commit 47bda7e

Please sign in to comment.