+
+
+
1707│(defun elpy-goto-definition ()
+1708│ "Go to the definition of the symbol at point, if found."
+1709│ (interactive)
+1710│ (let ((location (elpy-rpc-get-definition)))
+1711│ (if location
+1712│ (elpy-goto-location (car location) (cadr location))
+1713│ (error "No definition found"))))
+
+
+
+
+
2721│(defun elpy-rpc--get-rpc-buffer ()
+2722│ "Return the RPC buffer associated with the current buffer,
+2723│creating one if necessary."
+2724│ (when (not (elpy-rpc--process-buffer-p elpy-rpc--buffer))
+2725│ (setq elpy-rpc--buffer
+2726│ (or (elpy-rpc--find-buffer (elpy-library-root)
+2727│ elpy-rpc-python-command)
+2728│ (elpy-rpc--open (elpy-library-root)
+2729│ elpy-rpc-python-command))))
+
+
+
+
+
1226│(defun elpy-library-root ()
+1227│ "Return the root of the Python package chain of the current buffer.
+1228│
+1229│That is, if you have /foo/package/module.py, it will return /foo,
+1230│so that import package.module will pick up module.py."
+1231│ (locate-dominating-file default-directory
+1232│ (lambda (dir)
+1233│ (not (file-exists-p
+1234│ (format "%s/__init__.py"
+1235│ dir))))))
+
+
+
+最後終於找到了elpy-library-root這個函數。
+代碼都快讀完了都沒有發現啥問題,不知道讀到這裏的聰明的你有沒有看出啥來。
+
+
+
+
27│ def __init__(self, project_root):
+28│ self.project_root = project_root
+29│ self.completions = {}
+30│ sys.path.append(project_root)
+
+
+
+甚至在這裏把項目的路徑根都保存出來啦。
+發現一個問題,出現的這個路徑是有問題的,總是我項目的父路徑,爲什麼呢?
+
+
+
+
27│ def __init__(self, project_root):
+28│ self.project_root = project_root
+29│ self.completions = {}
+30│ sys.path.append(project_root)
+
+
+
+甚至在這裏把項目的路徑根都保存出來啦。
+發現一個問題,出現的這個路徑是有問題的,總是我項目的父路徑,爲什麼呢?
+重點來了。
+
+
+
+
(locate-dominating-file default-directory
+ (lambda (dir)
+ (not (file-exists-p
+ (format "%s/__init__.py"
+ dir))))))
+
+
+
+這個方法是用來幹嘛的?看半天沒看明白,後來才明白,是根據第二個參數向上級目錄遍歷,
+直到找到滿足條件的目錄,然後把他的父目錄返回,就是我項目的父目錄。
+我們看到這個匿名函數是幹嘛的?
+是檢查該目錄下有沒有"init.py"文件,思考一下,作者爲啥這麼寫呢。
+其實想想也挺簡單的,檢查這是不是一個包嘛,如果是包,就默認去找上級沒有"init.py".
+
+