[Python-mode] [PATCH] Improved pycomplete

Urs Fleisch urs.fleisch at gmail.com
Mon Jul 2 21:18:18 CEST 2012


Hi Andreas,

I have put the code for company and auto-complete into separate files.

Regards,
Urs

=== added file 'completion/auto-complete-pycomplete.el'
--- completion/auto-complete-pycomplete.el	1970-01-01 00:00:00 +0000
+++ completion/auto-complete-pycomplete.el	2012-07-02 19:14:02 +0000
@@ -0,0 +1,37 @@
+;;; auto-complete-pycomplete.el --- an auto-complete source for pycomplete.el
+
+;; Copyright (C) 2012  Urs Fleisch
+
+;; Author: Urs Fleisch <ufleisch at users.sourceforge.net>
+;; Keywords: languages, processes, python, oop
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; The ac-source can be enabled solely using
+;; (setq ac-sources '(ac-source-pycomplete))
+;; or before the other sources using
+;; (add-to-list 'ac-sources 'ac-source-pycomplete).
+
+;;; Code:
+
+(require 'pycomplete)
+(require 'auto-complete)
+
+(ac-define-source pycomplete
+  '((candidates . py-complete-completions)))
+
+(provide 'auto-complete-pycomplete)
+;;; auto-complete-pycomplete.el ends here

=== added file 'completion/company-pycomplete.el'
--- completion/company-pycomplete.el	1970-01-01 00:00:00 +0000
+++ completion/company-pycomplete.el	2012-07-02 19:10:20 +0000
@@ -0,0 +1,55 @@
+;;; company-pycomplete.el --- a company-mode completion back-end for pycomplete.el
+
+;; Copyright (C) 2012  Urs Fleisch
+
+;; Author: Urs Fleisch <ufleisch at users.sourceforge.net>
+;; Keywords: languages, processes, python, oop
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; The backend can be enabled using
+;; (setq company-backends '((company-pycomplete)))
+
+;;; Code:
+
+(require 'pycomplete)
+(require 'company)
+
+(defun company-pycomplete--grab-symbol ()
+  ;; stolen from company-pysmell--grab-symbol
+  (let ((symbol (company-grab-symbol)))
+    (when symbol
+      (cons symbol
+            (save-excursion
+              (let ((pos (point)))
+                (goto-char (- (point) (length symbol)))
+                (while (eq (char-before) ?.)
+                  (goto-char (1- (point)))
+                  (skip-syntax-backward "w_"))
+                (- pos (point))))))))
+
+(defun company-pycomplete (command &optional arg &rest ignored)
+  "A `company-mode' completion back-end for pycomplete."
+  (interactive (list 'interactive))
+  (case command
+    ('interactive (company-begin-backend 'company-pycomplete))
+    ('prefix (and (derived-mode-p 'python-mode)
+                  (not (company-in-string-or-comment))
+                  (company-pycomplete--grab-symbol)))
+    ('candidates (py-complete-completions))))
+
+(provide 'company-pycomplete)
+;;; company-pycomplete.el ends here

=== modified file 'completion/pycomplete.el'
--- completion/pycomplete.el	2012-07-01 13:05:48 +0000
+++ completion/pycomplete.el	2012-07-02 18:43:06 +0000
@@ -78,4 +78,10 @@
              (display-completion-list completions))
            (message "Making completion list...%s" "done")))))
 
+(defun py-complete-completions ()
+  "get possible completions for current statement"
+  (pycomplete-get-all-completions
+   (py-symbol-near-point)
+   (py-find-global-imports)))
+
 (provide 'pycomplete)



More information about the Python-mode mailing list