From ef94ff26bf25fe8f1122cee250ee6065c918f493 Mon Sep 17 00:00:00 2001 From: masicn Date: Tue, 6 Aug 2024 19:22:26 +0200 Subject: [PATCH] cargo: improve and unify function regexp --- rustic-cargo.el | 2 +- rustic-interaction.el | 11 +- test/rustic-cargo-test.el | 206 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+), 3 deletions(-) diff --git a/rustic-cargo.el b/rustic-cargo.el index d259356..c3b9b83 100644 --- a/rustic-cargo.el +++ b/rustic-cargo.el @@ -227,7 +227,7 @@ If ARG is not nil, use value as argument and store it in (defconst rustic-cargo-mod-regexp "^\s*mod\s+\\([[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*\\)\s*{") (defconst rustic-cargo-fn-regexp - "^\s*\\(?:async\s+\\)?\s*fn\s+\\([^(]+\\)\s*(") + (concat rustic-func-item-beg-re "\\([^(<]+\\)\\s-*\\(?:<\\s-*.+\\s-*>\\s-*\\)?(")) (defun rustic-cargo--get-test-target() "Return either a full fn name or a mod name, whatever is closer to the point." diff --git a/rustic-interaction.el b/rustic-interaction.el index ee6cc9a..b926bfd 100644 --- a/rustic-interaction.el +++ b/rustic-interaction.el @@ -31,8 +31,15 @@ visiting a project." ;; that we added an extra regexp for funcs. (defvar rustic-func-item-beg-re - (concat "\\s-*\\(?:priv\\|pub\\)?\\s-*\\(?:async\\)?\\s-*" - (regexp-opt '("fn"))) + (concat "^" + "\\(?:\\s-*" + (regexp-opt '("priv" "pub")) "\\s-*" + "\\(?:(\\s-*\\(?:in\\s-+\\)?" (regexp-opt '("crate" "self" "super")) "\\s-*)\\)?" + "\\)?" + "\\(?:\\s-*" (regexp-opt '("async" "const")) "\\s-+\\)?" + "\\(?:\\s-*unsafe\\s-+\\)?" + "\\(?:\\s-*extern\\(?:\\s-+\"C\"\\)?\\s-+\\)?" + "\\s-*fn\\s-+") "Start of a rust function.") (defun rustic-beginning-of-function () diff --git a/test/rustic-cargo-test.el b/test/rustic-cargo-test.el index 640be8c..8d3cd82 100644 --- a/test/rustic-cargo-test.el +++ b/test/rustic-cargo-test.el @@ -90,6 +90,212 @@ fn test1() { (setq rustic-test-arguments "") (kill-buffer buf))) +(ert-deftest rustic-test-cargo-current-test-pub () + (let* ((string "#[test] +fn test1() { +} +#[test] +pub fn test2() { +}") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 4) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-test-pub-in-crate () + (let* ((string "#[test] +fn test1() { +} +#[test] +pub(in crate) fn test2() { +}") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 4) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-test-pub-self () + (let* ((string "#[test] +fn test1() { +} +#[test] + pub( self) fn test2() { +}") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 4) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-test-pub-super () + (let* ((string "#[test] + pub (super ) fn test1() { + } + #[test] + fn test2() { + }") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 1) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-test-const () + (let* ((string "#[test] + const fn test1() { + } + #[test] + fn test2() { + }") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 1) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-test-unsafe () + (let* ((string "#[test] + unsafe fn test1() { + } + #[test] + fn test2() { + }") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 1) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-test-extern () + (let* ((string "#[test] + extern \"C\" fn test1() { + } + #[test] + fn test2() { + }") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 1) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-test-diamond () + (let* ((string "#[test] +fn test1() { +} +#[test] + fn test2<'a>() { +}") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 4) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + +(ert-deftest rustic-test-cargo-current-combined () + (let* ((string "#[test] +fn test1() { +} +pub(in self) const unsafe extern fn test2 () { +}") + (default-directory (rustic-test-count-error-helper string)) + (buf (get-buffer-create "test-current-test"))) + (with-current-buffer buf + (insert string) + (goto-char (point-min)) + (forward-line 4) + (let* ((proc (rustic-cargo-current-test)) + (proc-buf (process-buffer proc))) + (rustic-test--wait-till-finished rustic-test-buffer-name) + (with-current-buffer proc-buf + (should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max)))) + (should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max))))) + (kill-buffer proc-buf))) + (setq rustic-test-arguments "") + (kill-buffer buf))) + (ert-deftest rustic-test-cargo-current-test-no-test-found () ;; test with use #46 (let* ((string "mod tests {