]> git.eshelyaron.com Git - emacs.git/commitdiff
* progmodes/python.el: Completion code cleanups.
authorFabián Ezequiel Gallina <fgallina@gnu.org>
Sat, 2 Aug 2014 22:52:55 +0000 (19:52 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Sat, 2 Aug 2014 22:52:55 +0000 (19:52 -0300)
(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.

lisp/ChangeLog
lisp/progmodes/python.el

index a6d526b1c5120014a2cdc62cc2cf33b7a2230a50..8c7ac9c74234f3c0ff843f3bceef9394a7c8e52c 100644 (file)
@@ -1,3 +1,11 @@
+2014-08-02  Fabián Ezequiel Gallina  <fgallina@gnu.org>
+
+       * 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.
+
 2014-08-02  Alan Mackenzie  <acm@muc.de>
 
        Fix confusion in C++ file caused by comma in "= {1,2},".  Bug
index 7d7cd9de19eb53f1a2c669b6cb0de9a27b89a779..6ed912f8cfd06972e0ef25f37f1bb42089e0b170 100644 (file)
@@ -2872,21 +2872,15 @@ the full statement in the case of imports."
   :type 'string
   :group 'python)
 
-(defun python-shell-completion-get-completions (process line input)
-  "Do completion at point for PROCESS.
-LINE is used to detect the context on how to complete given INPUT."
+(defun python-shell-completion-get-completions (process import input)
+  "Do completion at point using PROCESS for IMPORT or INPUT.
+When IMPORT is non-nil takes precedence over INPUT for
+completion."
   (let* ((prompt
-          ;; Get last prompt of the inferior process buffer (this
-          ;; intentionally avoids using `comint-last-prompt' because
-          ;; of incompatibilities with Emacs 24.x).
           (with-current-buffer (process-buffer process)
-            (save-excursion
+            (let ((prompt-boundaries (python-util-comint-last-prompt)))
               (buffer-substring-no-properties
-               (- (point) (length line))
-               (progn
-                 (re-search-backward "^")
-                 (python-util-forward-comment)
-                 (point))))))
+               (car prompt-boundaries) (cdr prompt-boundaries)))))
          (completion-code
           ;; Check whether a prompt matches a pdb string, an import
           ;; 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."
                   python-shell--prompt-calculated-input-regexp prompt)
                  python-shell-completion-string-code)
                 (t nil)))
-         (input
-          (if (string-match
-               (python-rx (+ space) (or "from" "import") space)
-               line)
-              line
-            input)))
+         (subject (or import input)))
     (and completion-code
          (> (length input) 0)
          (with-current-buffer (process-buffer process)
            (let ((completions
                   (python-util-strip-string
                    (python-shell-send-string-no-output
-                    (format completion-code input) process))))
+                    (format completion-code subject) process))))
              (and (> (length completions) 2)
                   (split-string completions
                                 "^'\\|^\"\\|;\\|'$\\|\"$" t)))))))
@@ -2921,14 +2910,20 @@ LINE is used to detect the context on how to complete given INPUT."
 Optional argument PROCESS forces completions to be retrieved
 using that one instead of current buffer's process."
   (setq process (or process (get-buffer-process (current-buffer))))
-  (let* ((start
+  (let* ((last-prompt-end (cdr (python-util-comint-last-prompt)))
+         (import-statement
+          (when (string-match-p
+                 (rx (* space) word-start (or "from" "import") word-end space)
+                 (buffer-substring-no-properties last-prompt-end (point)))
+            (buffer-substring-no-properties last-prompt-end (point))))
+         (start
           (save-excursion
             (if (not (re-search-backward
                       (python-rx
                        (or whitespace open-paren close-paren string-delimiter))
-                      (cdr (python-util-comint-last-prompt))
+                      last-prompt-end
                       t 1))
-                (cdr (python-util-comint-last-prompt))
+                last-prompt-end
               (forward-char (length (match-string-no-properties 0)))
               (point))))
          (end (point)))
@@ -2936,8 +2931,7 @@ using that one instead of current buffer's process."
           (completion-table-dynamic
            (apply-partially
             #'python-shell-completion-get-completions
-            process (buffer-substring-no-properties
-                     (line-beginning-position) end))))))
+            process import-statement)))))
 
 (define-obsolete-function-alias
   'python-shell-completion-complete-at-point