aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2014-08-02 19:52:55 -0300
committerFabián Ezequiel Gallina2014-08-02 19:52:55 -0300
commit01c600f6d6c8aea12497782494b4fef584495213 (patch)
tree495b09a1bda92e4f20278f431779fe8fff04d38c /lisp/progmodes/python.el
parent88e3bc88c74c6293fd3e3d229da893385fdfad7d (diff)
downloademacs-01c600f6d6c8aea12497782494b4fef584495213.tar.gz
emacs-01c600f6d6c8aea12497782494b4fef584495213.zip
* progmodes/python.el: Completion code cleanups.
(python-shell-completion-get-completions): Detect and send import statements directly to completion function. (python-shell-completion-at-point): Simplify prompt calculation and import vs input completion logic.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el42
1 files changed, 18 insertions, 24 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 7d7cd9de19e..6ed912f8cfd 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2872,21 +2872,15 @@ the full statement in the case of imports."
2872 :type 'string 2872 :type 'string
2873 :group 'python) 2873 :group 'python)
2874 2874
2875(defun python-shell-completion-get-completions (process line input) 2875(defun python-shell-completion-get-completions (process import input)
2876 "Do completion at point for PROCESS. 2876 "Do completion at point using PROCESS for IMPORT or INPUT.
2877LINE is used to detect the context on how to complete given INPUT." 2877When IMPORT is non-nil takes precedence over INPUT for
2878completion."
2878 (let* ((prompt 2879 (let* ((prompt
2879 ;; Get last prompt of the inferior process buffer (this
2880 ;; intentionally avoids using `comint-last-prompt' because
2881 ;; of incompatibilities with Emacs 24.x).
2882 (with-current-buffer (process-buffer process) 2880 (with-current-buffer (process-buffer process)
2883 (save-excursion 2881 (let ((prompt-boundaries (python-util-comint-last-prompt)))
2884 (buffer-substring-no-properties 2882 (buffer-substring-no-properties
2885 (- (point) (length line)) 2883 (car prompt-boundaries) (cdr prompt-boundaries)))))
2886 (progn
2887 (re-search-backward "^")
2888 (python-util-forward-comment)
2889 (point))))))
2890 (completion-code 2884 (completion-code
2891 ;; Check whether a prompt matches a pdb string, an import 2885 ;; Check whether a prompt matches a pdb string, an import
2892 ;; statement or just the standard prompt and use the 2886 ;; statement or just the standard prompt and use the
@@ -2899,19 +2893,14 @@ LINE is used to detect the context on how to complete given INPUT."
2899 python-shell--prompt-calculated-input-regexp prompt) 2893 python-shell--prompt-calculated-input-regexp prompt)
2900 python-shell-completion-string-code) 2894 python-shell-completion-string-code)
2901 (t nil))) 2895 (t nil)))
2902 (input 2896 (subject (or import input)))
2903 (if (string-match
2904 (python-rx (+ space) (or "from" "import") space)
2905 line)
2906 line
2907 input)))
2908 (and completion-code 2897 (and completion-code
2909 (> (length input) 0) 2898 (> (length input) 0)
2910 (with-current-buffer (process-buffer process) 2899 (with-current-buffer (process-buffer process)
2911 (let ((completions 2900 (let ((completions
2912 (python-util-strip-string 2901 (python-util-strip-string
2913 (python-shell-send-string-no-output 2902 (python-shell-send-string-no-output
2914 (format completion-code input) process)))) 2903 (format completion-code subject) process))))
2915 (and (> (length completions) 2) 2904 (and (> (length completions) 2)
2916 (split-string completions 2905 (split-string completions
2917 "^'\\|^\"\\|;\\|'$\\|\"$" t))))))) 2906 "^'\\|^\"\\|;\\|'$\\|\"$" t)))))))
@@ -2921,14 +2910,20 @@ LINE is used to detect the context on how to complete given INPUT."
2921Optional argument PROCESS forces completions to be retrieved 2910Optional argument PROCESS forces completions to be retrieved
2922using that one instead of current buffer's process." 2911using that one instead of current buffer's process."
2923 (setq process (or process (get-buffer-process (current-buffer)))) 2912 (setq process (or process (get-buffer-process (current-buffer))))
2924 (let* ((start 2913 (let* ((last-prompt-end (cdr (python-util-comint-last-prompt)))
2914 (import-statement
2915 (when (string-match-p
2916 (rx (* space) word-start (or "from" "import") word-end space)
2917 (buffer-substring-no-properties last-prompt-end (point)))
2918 (buffer-substring-no-properties last-prompt-end (point))))
2919 (start
2925 (save-excursion 2920 (save-excursion
2926 (if (not (re-search-backward 2921 (if (not (re-search-backward
2927 (python-rx 2922 (python-rx
2928 (or whitespace open-paren close-paren string-delimiter)) 2923 (or whitespace open-paren close-paren string-delimiter))
2929 (cdr (python-util-comint-last-prompt)) 2924 last-prompt-end
2930 t 1)) 2925 t 1))
2931 (cdr (python-util-comint-last-prompt)) 2926 last-prompt-end
2932 (forward-char (length (match-string-no-properties 0))) 2927 (forward-char (length (match-string-no-properties 0)))
2933 (point)))) 2928 (point))))
2934 (end (point))) 2929 (end (point)))
@@ -2936,8 +2931,7 @@ using that one instead of current buffer's process."
2936 (completion-table-dynamic 2931 (completion-table-dynamic
2937 (apply-partially 2932 (apply-partially
2938 #'python-shell-completion-get-completions 2933 #'python-shell-completion-get-completions
2939 process (buffer-substring-no-properties 2934 process import-statement)))))
2940 (line-beginning-position) end))))))
2941 2935
2942(define-obsolete-function-alias 2936(define-obsolete-function-alias
2943 'python-shell-completion-complete-at-point 2937 'python-shell-completion-complete-at-point