forked from wernerandrew/jedi-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jedi-starter.el
192 lines (152 loc) · 7.07 KB
/
jedi-starter.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
;; Package setup
(add-to-list 'load-path "~/.emacs.d") ; to find Emacs 23 package.el
(require 'package)
(package-initialize)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(defvar local-packages '(projectile auto-complete epc jedi))
(defun uninstalled-packages (packages)
(delq nil
(mapcar (lambda (p) (if (package-installed-p p nil) nil p)) packages)))
;; This delightful bit adapted from:
;; http://batsov.com/articles/2012/02/19/package-management-in-emacs-the-good-the-bad-and-the-ugly/
(let ((need-to-install (uninstalled-packages local-packages)))
(when need-to-install
(progn
(package-refresh-contents)
(dolist (p need-to-install)
(package-install p)))))
;; Global Jedi config vars
(defvar jedi-config:use-system-python nil
"Will use system python and active environment for Jedi server.
May be necessary for some GUI environments (e.g., Mac OS X)")
(defvar jedi-config:with-virtualenv nil
"Set to non-nil to point to a particular virtualenv.")
(defvar jedi-config:vcs-root-sentinel ".git")
(defvar jedi-config:python-module-sentinel "__init__.py")
;; Helper functions
;; Small helper to scrape text from shell output
(defun get-shell-output (cmd)
(replace-regexp-in-string "[ \t\n]*$" "" (shell-command-to-string cmd)))
;; Ensure that PATH is taken from shell
;; Necessary on some environments without virtualenv
;; Taken from: http://stackoverflow.com/questions/8606954/path-and-exec-path-set-but-emacs-does-not-find-executable
(defun set-exec-path-from-shell-PATH ()
"Set up Emacs' `exec-path' and PATH environment variable to match that used by the user's shell."
(interactive)
(let ((path-from-shell (get-shell-output "$SHELL --login -i -c 'echo $PATH'")))
(setenv "PATH" path-from-shell)
(setq exec-path (split-string path-from-shell path-separator))))
;; Package specific initialization
(add-hook
'after-init-hook
'(lambda ()
;; Looks like you need Emacs 24 for projectile
(unless (< emacs-major-version 24)
(require 'projectile)
(projectile-global-mode))
;; Auto-complete
(require 'auto-complete-config)
(ac-config-default)
;; Uncomment next line if you like the menu right away
;; (setq ac-show-menu-immediately-on-auto-complete t)
;; Can also express in terms of ac-delay var, e.g.:
;; (setq ac-auto-show-menu (* ac-delay 2))
;; Jedi
(require 'jedi)
;; (Many) config helpers follow
;; Alternative methods of finding the current project root
;; Method 1: basic
(defun get-project-root (buf repo-file &optional init-file)
"Just uses the vc-find-root function to figure out the project root.
Won't always work for some directory layouts."
(let* ((buf-dir (expand-file-name (file-name-directory (buffer-file-name buf))))
(project-root (vc-find-root buf-dir repo-file)))
(if project-root
(expand-file-name project-root)
nil)))
;; Method 2: slightly more robust
(defun get-project-root-with-file (buf repo-file &optional init-file)
"Guesses that the python root is the less 'deep' of either:
-- the root directory of the repository, or
-- the directory before the first directory after the root
having the init-file file (e.g., '__init__.py'."
;; make list of directories from root, removing empty
(defun make-dir-list (path)
(delq nil (mapcar (lambda (x) (and (not (string= x "")) x))
(split-string path "/"))))
;; convert a list of directories to a path starting at "/"
(defun dir-list-to-path (dirs)
(mapconcat 'identity (cons "" dirs) "/"))
;; a little something to try to find the "best" root directory
(defun try-find-best-root (base-dir buffer-dir current)
(cond
(base-dir ;; traverse until we reach the base
(try-find-best-root (cdr base-dir) (cdr buffer-dir)
(append current (list (car buffer-dir)))))
(buffer-dir ;; try until we hit the current directory
(let* ((next-dir (append current (list (car buffer-dir))))
(file-file (concat (dir-list-to-path next-dir) "/" init-file)))
(if (file-exists-p file-file)
(dir-list-to-path current)
(try-find-best-root nil (cdr buffer-dir) next-dir))))
(t nil)))
(let* ((buffer-dir (expand-file-name (file-name-directory (buffer-file-name buf))))
(vc-root-dir (vc-find-root buffer-dir repo-file)))
(if (and init-file vc-root-dir)
(try-find-best-root
(make-dir-list (expand-file-name vc-root-dir))
(make-dir-list buffer-dir)
'())
vc-root-dir))) ;; default to vc root if init file not given
;; Set this variable to find project root
(defvar jedi-config:find-root-function 'get-project-root-with-file)
(defun current-buffer-project-root ()
(funcall jedi-config:find-root-function
(current-buffer)
jedi-config:vcs-root-sentinel
jedi-config:python-module-sentinel))
(defun jedi-config:setup-server-args ()
;; little helper macro for building the arglist
(defmacro add-args (arg-list arg-name arg-value)
`(setq ,arg-list (append ,arg-list (list ,arg-name ,arg-value))))
;; and now define the args
(let ((project-root (current-buffer-project-root)))
(make-local-variable 'jedi:server-args)
(when project-root
(message (format "Adding system path: %s" project-root))
(add-args jedi:server-args "--sys-path" project-root))
(when jedi-config:with-virtualenv
(message (format "Adding virtualenv: %s" jedi-config:with-virtualenv))
(add-args jedi:server-args "--virtual-env" jedi-config:with-virtualenv))))
;; Use system python
(defun jedi-config:set-python-executable ()
(set-exec-path-from-shell-PATH)
(make-local-variable 'jedi:server-command)
(set 'jedi:server-command
(list (executable-find "python") ;; may need help if running from GUI
(cadr default-jedi-server-command))))
;; Now hook everything up
;; Hook up to autocomplete
(add-to-list 'ac-sources 'ac-source-jedi-direct)
;; Enable Jedi setup on mode start
(add-hook 'python-mode-hook 'jedi:setup)
;; Buffer-specific server options
(add-hook 'python-mode-hook
'jedi-config:setup-server-args)
(when jedi-config:use-system-python
(add-hook 'python-mode-hook
'jedi-config:set-python-executable))
;; And custom keybindings
(defun jedi-config:setup-keys ()
(local-set-key (kbd "M-.") 'jedi:goto-definition)
(local-set-key (kbd "M-,") 'jedi:goto-definition-pop-marker)
(local-set-key (kbd "M-?") 'jedi:show-doc)
(local-set-key (kbd "M-/") 'jedi:get-in-function-call))
;; Don't let tooltip show up automatically
(setq jedi:get-in-function-call-delay 10000000)
;; Start completion at method dot
(setq jedi:complete-on-dot t)
;; Use custom keybinds
(add-hook 'python-mode-hook 'jedi-config:setup-keys)
))