首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使文件名/行号在Emacs gud缓冲区中可链接

使文件名/行号在Emacs gud缓冲区中可链接
EN

Stack Overflow用户
提问于 2010-01-19 02:35:03
回答 4查看 788关注 0票数 12

我通过gud buffer在Python中的测试用例上运行pdb。当我在测试用例中得到堆栈跟踪/失败时,它看起来是这样的:

代码语言:javascript
复制
FAIL: test_foo_function (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/testfoo.py", line 499, in test_foo_function
    self.assertEqual('foo', 'foo')

我希望能够将这行代码写成:

代码语言:javascript
复制
File "test/testfoo.py", line 499, in test_foo_function

单击并转到testfoo.py中的第499行。

(编辑) python-mode列表上的那些人把我带到了pdbtrack,我能够让它在那里工作。请参阅下面的答案...

EN

回答 4

Stack Overflow用户

发布于 2010-01-23 14:53:52

多亏了Gerard B的提示,我弄明白了。我用pdbtrack (shell)而不是纯pdb来做这件事,但我相信它应该在两者中都能工作。您需要启用编译shell次要模式。并在您的.emacs中包含以下代码:

代码语言:javascript
复制
;; if compilation-shell-minor-mode is on, then these regexes
;; will make errors linkable
(defun matt-add-global-compilation-errors (list)
  (dolist (x list)
    (add-to-list 'compilation-error-regexp-alist (car x))
    (setq compilation-error-regexp-alist-alist
      (cons x
            (assq-delete-all (car x)
                             compilation-error-regexp-alist-alist)))))

(matt-add-global-compilation-errors
 `(
   (matt-python ,(concat "^ *File \\(\"?\\)\\([^,\" \n    <>]+\\)\\1"
                    ", lines? \\([0-9]+\\)-?\\([0-9]+\\)?")
           2 (3 . 4) nil 2 2)
   (matt-pdb-stack ,(concat "^>?[[:space:]]*\\(\\([-_./a-zA-Z0-9 ]+\\)"
                       "(\\([0-9]+\\))\\)"
                       "[_a-zA-Z0-9]+()[[:space:]]*->")
              2 3 nil 0 1)
   (matt-python-unittest-err "^  File \"\\([-_./a-zA-Z0-9 ]+\\)\", line \\([0-9]+\\).*" 1 2)
   )
 )

(defun matt-set-local-compilation-errors (errors)
  "Set the buffer local compilation errors.

Ensures than any symbols given are defined in
compilation-error-regexp-alist-alist."
  (dolist (e errors)
     (when (symbolp e)
      (unless (assoc e compilation-error-regexp-alist-alist)
        (error (concat "Error %s is not listed in "
                       "compilation-error-regexp-alist-alist")
               e))))
  (set (make-local-variable 'compilation-error-regexp-alist)
       errors))

然后,您可以使用标准编译模式导航来压缩错误堆栈跟踪。

票数 5
EN

Stack Overflow用户

发布于 2010-01-19 07:45:55

我认为您想要定制的是compilation-parse-errors-filename-function,它是一个接受文件名的函数,并返回要显示的文件名的修改版本。这是一个缓冲区局部变量,所以您应该在每个将显示python错误的缓冲区中设置它(可能有一个合适的钩子可以使用,我没有安装python模式,所以无法查找它)。您将使用propertize返回输入文件名的一个版本,该文件名充当加载实际文件的超链接。属性在elisp手册中有很好的说明。

如果没有调用compilation parse- errors filename -function,那么您需要将一个列表添加到compilation-error-regexp-alist-alist (它确实显示为and and,这不是一个打字错误),它是一个模式名称列表,后面跟着用于匹配错误的正则表达式,并且匹配错误正则表达式中匹配的行号、文件名等信息的数字索引。

票数 2
EN

Stack Overflow用户

发布于 2010-01-19 17:14:59

补充Justin的答案:

我的slime配置中有以下内容,它应该从clojure堆栈跟踪跳转到文件和行。

不幸的是,我必须承认它目前对我并不起作用-函数无法找到正确的文件-但据我所知,这应该可以通过更改project-root的定义方式或通过更改文件系统上项目的结构来修复(我只是没有时间或倾向于研究它)。

不过,它确实提出了一个很好的观点,在大多数像这样的功能中,以通用和可移植的方式找出项目的根是有点棘手的。在本例中,我们依赖于src目录,但这可能不适合您的python项目。

因此,从Justin停止的地方开始,您应该能够从下面的函数中获得一些提示,并解析来自测试用例错误的文件名和行号,创建到行号的链接,并使用compilation-parse-errors-filename-functionpropertize使gud缓冲区中的行成为链接。

如果你让它工作了,请在你自己的问题中添加一个答案。我想很多人会发现它很有用。

代码语言:javascript
复制
  (defun slime-jump-to-trace (&optional on)
    "Jump to the file/line that the current stack trace line references.
    Only works with files in your project root's src/, not in dependencies."
    (interactive)
    (save-excursion
      (beginning-of-line)
      (search-forward-regexp "[0-9]: \\([^$(]+\\).*?\\([0-9]*\\))")
      (let ((line (string-to-number (match-string 2)))
            (ns-path (split-string (match-string 1) "\\."))
            (project-root (locate-dominating-file default-directory "src/")))

        (find-file (format "%s/src/%s.clj" project-root
                           (mapconcat 'identity ns-path "/")))
        (goto-line line))))

我还应该提到,我从web上的某个地方复制了这个函数,但我不记得URL了。它似乎来自Phil Hagelberg(技术)优秀的Emacs初学者工具包。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2088307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档