emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/lua-mode 6d66aee 284/468: Redo handling of "stdin:N" in tr


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode 6d66aee 284/468: Redo handling of "stdin:N" in tracebacks
Date: Thu, 5 Aug 2021 04:58:54 -0400 (EDT)

branch: elpa/lua-mode
commit 6d66aee136e65a2b9df7e252dd41da3c4965b12f
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>

    Redo handling of "stdin:N" in tracebacks
    
    The regexp that disabled highlighting of "stdin:N" turned out to be
    overly magical (unstable, any change in it caused breakage).
    
    This change enables highlighting back and adds advices around
    compilation-mode functions to ensure minimum inconvenience: now any
    "stdin:N:" message at line L points to line L of REPL buffer.
---
 lua-mode.el           | 43 ++++++++++++++++++++++--
 test/inferior-test.el | 93 ++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 114 insertions(+), 22 deletions(-)

diff --git a/lua-mode.el b/lua-mode.el
index 843a727..66f07d2 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -302,11 +302,47 @@ If the latter is nil, the keymap translates into 
`lua-mode-map' verbatim.")
 (defcustom lua-traceback-line-re
   ;; This regexp skips prompt and meaningless "stdin:N:" prefix when looking
   ;; for actual file-line locations.
-  "^\\(?:[\t ]*\\|.*>[\t ]+\\)\\(?:stdin:[0-9]+:[\t 
]*\\)?\\(?:stdin:[0-9]+:\\|\\([^\n\t ]*\\):\\([0-9]+\\):\\)"
+  "^\\(?:[\t ]*\\|.*>[\t ]+\\)\\(?:[^\n\t ]+:[0-9]+:[\t ]*\\)*\\(?:\\([^\n\t 
]+\\):\\([0-9]+\\):\\)"
   "Regular expression that describes tracebacks and errors."
   :type 'regexp
   :group 'lua)
 
+(defvar lua--repl-buffer-p nil
+  "Buffer-local flag saying if this is a Lua REPL buffer.")
+(make-variable-buffer-local 'lua--repl-buffer-p)
+
+
+(defadvice compilation-find-file (around lua--repl-find-file
+                                         (marker filename directory &rest 
formats)
+                                         activate)
+  "Return Lua REPL buffer when looking for \"stdin\" file in it."
+  (if (and
+       lua--repl-buffer-p
+       (string-equal filename "stdin")
+       ;; NOTE: this doesn't traverse `compilation-search-path' when
+       ;; looking for filename.
+       (not (file-exists-p (expand-file-name
+                        filename
+                        (when directory (expand-file-name directory))))))
+      (setq ad-return-value (current-buffer))
+    ad-do-it))
+
+
+(defadvice compilation-goto-locus (around lua--repl-goto-locus
+                                          (msg mk end-mk)
+                                          activate)
+  "When message points to Lua REPL buffer, go to the message itself.
+Usually, stdin:XX line number points to nowhere."
+  (let ((errmsg-buf (marker-buffer msg))
+        (error-buf (marker-buffer mk)))
+    (if (and (with-current-buffer errmsg-buf lua--repl-buffer-p)
+             (eq error-buf errmsg-buf))
+        (progn
+          (compilation-set-window (display-buffer (marker-buffer msg)) msg)
+          (goto-char msg))
+      ad-do-it)))
+
+
 (defcustom lua-indent-string-contents nil
   "If non-nil, contents of multiline string will be indented.
 Otherwise leading amount of whitespace on each line is preserved."
@@ -1600,6 +1636,7 @@ When called interactively, switch to the process buffer."
 
     ;; enable error highlighting in stack traces
     (require 'compile)
+    (setq lua--repl-buffer-p t)
     (make-local-variable 'compilation-error-regexp-alist)
     (setq compilation-error-regexp-alist
           (cons (list lua-traceback-line-re 1 2)
@@ -1639,7 +1676,9 @@ When called interactively, switch to the process buffer."
   "Send STR plus a newline to Lua subprocess.
 
 If `lua-process' is nil or dead, start a new process first."
-  (comint-simple-send (lua-get-create-process) str))
+  (unless (string-equal (substring str -1) "\n")
+    (setq str (concat str "\n")))
+  (process-send-string (lua-get-create-process) str))
 
 (defun lua-send-current-line ()
   "Send current line to Lua subprocess, found in `lua-process'.
diff --git a/test/inferior-test.el b/test/inferior-test.el
index 8d3bd80..bf8468a 100644
--- a/test/inferior-test.el
+++ b/test/inferior-test.el
@@ -1,5 +1,7 @@
 ;; lua-mode tests for inferior process handling
 
+(require 'cl-lib)
+
 (load (concat (file-name-directory (or load-file-name (buffer-file-name)
                                        default-directory))
               "test-helper.el") nil 'nomessage 'nosuffix)
@@ -27,9 +29,8 @@
       ;; lua-process-buffer should be nil
       (lua-hide-process-buffer)
       (should (get-buffer-window cur-buf)))))
-         
+
 (ert-deftest lua-runtime-error-msg-is-fontified ()
-  :expected-result (if (eq 23 emacs-major-version) :failed :passed)
   (with-lua-buffer
    (insert "\
 function bar()
@@ -41,9 +42,9 @@ function foo()
 end
 ")
    (rename-buffer "test-send-runtime-error.lua" 'unique)
-   ;; By default, basic face for all error messages is 'underline, this is pain
-   ;; to filter out, let's change that to 'default.
-   (let ((compilation-message-face 'default))
+   ;; By default non-nil compilation-message-face is appended to
+   ;; compilation-error faces, let's simplify the checks.
+   (let ((compilation-message-face nil))
      (lua-send-buffer)
      (lua-send-string "foo()")
      ;; Make sure to wait enough to get all the output from the subprocess.
@@ -53,41 +54,93 @@ end
         (equal
          ;; (buffer-string)
          (get-buffer-line-faces)
-         '(nil ;; motd line
-           ("> " comint-highlight-prompt
-            "test-send-runtime-error.lua" compilation-error
+         '(nil ;; motd line (not highlighted)
+           nil ;; first prompt (also not highlighted)
+           ("test-send-runtime-error.lua" compilation-error
             "2" compilation-line-number) ;; error message
-           nil ;; stack traceback
-           nil ;; in function error
+           nil                           ;; stack traceback
+           nil                           ;; in function error
            ("test-send-runtime-error.lua" compilation-error
             "2" compilation-line-number) ;; in 'bar'
            ("test-send-runtime-error.lua" compilation-error
             "6" compilation-line-number) ;; in 'foo'
+           ("stdin" compilation-error "1" compilation-line-number)
            nil ;; in main chunk
-           nil
            nil)))))))
 
 
 (ert-deftest lua-syntax-error-msg-is-fontified ()
-  :expected-result (if (eq 23 emacs-major-version) :failed :passed)
   (with-lua-buffer
    (rename-buffer "test-send-syntax-error.lua")
    (insert "\
-foo = 1
-bar = 2
-
 function () end
 ")
-   (let ((compilation-message-face 'default))
+   ;; By default non-nil compilation-message-face is appended to
+   ;; compilation-error faces, let's simplify the checks.
+   (let ((compilation-message-face nil))
      (lua-send-buffer)
      (while (accept-process-output lua-process 0 200))
      (with-current-buffer lua-process-buffer
        (should
         (equal
          (get-buffer-line-faces)
-         '(nil
-           ("> " comint-highlight-prompt
+         '(nil ;; motd line, no highlight
+           nil ;; first prompt, also no highlight
+           (;; "stdin" is being highlighted here because compilation mode
+            ;; thinks it is some sort of "make: ..." message.  This doesn't
+            ;; happen in wildlife, because there's a default message face
+            ;; (underline) that prevents this.  In tests this is turned off,
+            ;; see `(compilation-message-face nil)' above, to simplify
+            ;; font-lock face checks.
+            "stdin" font-lock-function-name-face
             "test-send-syntax-error.lua" compilation-error
-            "4" compilation-line-number)
+            "1" compilation-line-number)
            ;; stacktrace with misc info, no font-lock
-           nil nil nil nil nil nil)))))))
+           nil nil
+           ("stdin" compilation-error "1" compilation-line-number)
+           ("stdin" compilation-error "1" compilation-line-number)
+           nil nil)))))))
+
+
+(require 'compile)
+(if (fboundp 'compilation--loc->file-struct)
+    (defun get-error-file (err-info)
+      (caar (compilation--loc->file-struct
+             (compilation--message->loc (compilation-next-error 0)))))
+  (defun get-error-file (err-info)
+    (caar (nth 2 (car err-info)))))
+
+(ert-deftest lua-repl-doesnt-ask-for-stdin-file ()
+  "Ensure REPL doesn't annoyingly ask to open file named \"stdin\"."
+  (let ((fname (make-temp-file "lua_mode_test" nil ".lua"))
+        buf)
+    (unwind-protect
+        (progn
+          (save-current-buffer
+            (setq buf (find-file fname))
+            (insert "function () end")
+            ;; Make sure the buffer can be killed cleanly
+            (set-buffer-modified-p nil)
+            (lua-send-buffer)
+            (while (accept-process-output lua-process 0 200))
+            (with-current-buffer lua-process-buffer
+              (font-lock-fontify-buffer))
+            (cl-letf
+                (((symbol-function 'read-file-name)
+                  (lambda (&rest args)
+                    (error "read-file-name must not be called"))))
+              (should (equal (next-error) nil))
+              (with-current-buffer lua-process-buffer
+                (should (equal
+                         fname
+                         (get-error-file (compilation-next-error 0)))))
+
+              (should (equal (next-error) nil))
+              (with-current-buffer lua-process-buffer
+                (should (equal
+                         "stdin"
+                         (get-error-file (compilation-next-error 0))))))))
+      (when buf
+        (kill-buffer buf))
+      (delete-file fname)
+      (kill-buffer "*lua*"))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]