Make ein:pytools-jump-to-source-command more robust

This commit is contained in:
Takafumi Arakaki 2012-06-03 17:50:13 +02:00
parent 853a9ec50e
commit 05a437b1ad
2 changed files with 20 additions and 12 deletions

View file

@ -54,19 +54,25 @@
kernel kernel
(format "__import__('ein').find_source('%s')" object) (format "__import__('ein').find_source('%s')" object)
(lambda (string object) (lambda (string object)
(let* ((filename-lineno (split-string string "\n")) (if (string-match "^WARNING: .*" string)
(filename (car filename-lineno)) (ein:log 'info "Jumping to the source of %s...Not found" object)
(lineno (string-to-number (cadr filename-lineno)))) (let* ((filename-lineno (split-string string "\n"))
(unless (equal filename "") (filename (car filename-lineno))
(find-file-other-window filename) (lineno (string-to-number (cadr filename-lineno))))
(goto-char (point-min)) (unless (equal filename "")
(forward-line (1- lineno)) (find-file-other-window filename)
(ein:log 'info "Jumping to the source of %s...Done" object)))) (goto-char (point-min))
(forward-line (1- lineno))
(ein:log 'info "Jumping to the source of %s...Done" object)))))
(list object))) (list object)))
(defun ein:pytools-jump-to-source-command () (defun ein:pytools-jump-to-source-command ()
(interactive) (interactive)
(ein:pytools-jump-to-source (ein:pytools-get-kernel) (ein:object-at-point))) (let ((kernel (ein:pytools-get-kernel))
(object (ein:object-at-point)))
(assert (ein:kernel-ready-p kernel) nil "Kernel is not ready.")
(assert object nil "Object at point not found.")
(ein:pytools-jump-to-source kernel object)))
(provide 'ein-pytools) (provide 'ein-pytools)

8
ein.py
View file

@ -25,6 +25,8 @@ def find_source(name):
"""Given an object as string, `name`, print its place in source code.""" """Given an object as string, `name`, print its place in source code."""
from IPython.core.interactiveshell import InteractiveShell from IPython.core.interactiveshell import InteractiveShell
inst = InteractiveShell.instance() inst = InteractiveShell.instance()
(filename, lineno, use_temp) = inst._find_edit_target(name, {}, []) ret = inst._find_edit_target(name, {}, [])
print filename if ret:
print lineno (filename, lineno, use_temp) = ret
print filename
print lineno