ein-jedi.el 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; ein-jedi.el --- ein Jedi
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-jedi.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-jedi.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-jedi.el.
  15. ;; If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;;; Code:
  19. (require 'jedi nil t)
  20. (require 'ein-ac)
  21. (require 'ein-completer)
  22. (eval-when-compile (require 'ein-connect))
  23. (defvar ein:jedi-dot-complete-sources
  24. '(ac-source-jedi-direct ac-source-ein-direct))
  25. (defun ein:jedi--completer-complete ()
  26. (let ((d (deferred:new #'identity))
  27. (kernel (ein:get-kernel)))
  28. (if (ein:kernel-live-p kernel)
  29. (ein:completer-complete
  30. kernel
  31. :callbacks
  32. (list :complete_reply
  33. (cons (lambda (d &rest args) (deferred:callback-post d args))
  34. d)))
  35. ;; Pass "no match" result when kernel the request was not sent:
  36. (deferred:callback-post d (list nil nil)))
  37. d))
  38. ;;;###autoload
  39. (defun* ein:jedi-complete (&key (expand ac-expand-on-auto-complete))
  40. "Run completion using candidates calculated by EIN and Jedi."
  41. (interactive)
  42. (lexical-let ((expand expand))
  43. (deferred:$
  44. (deferred:parallel ; or `deferred:earlier' is better?
  45. (jedi:complete-request)
  46. (ein:jedi--completer-complete))
  47. (deferred:nextc it
  48. (lambda (replies)
  49. (destructuring-bind
  50. (_ ; ignore `jedi:complete-request' what returns.
  51. ((&key matched_text matches &allow-other-keys) ; :complete_reply
  52. _)) ; ignore metadata
  53. replies
  54. (ein:ac-prepare-completion matches)
  55. (let ((ac-expand-on-auto-complete expand))
  56. (ac-start))))))))
  57. ;; Why `ac-start'? See: `jedi:complete'.
  58. ;;;###autoload
  59. (defun ein:jedi-dot-complete ()
  60. "Insert \".\" and run `ein:jedi-complete'."
  61. (interactive)
  62. (insert ".")
  63. (unless (ac-cursor-on-diable-face-p)
  64. (ein:jedi-complete :expand nil)))
  65. (defun ein:jedi-complete-on-dot-install (map)
  66. (ein:complete-on-dot-install map #'ein:jedi-dot-complete))
  67. ;;;###autoload
  68. (defun ein:jedi-setup ()
  69. "Setup auto-completion using EIN and Jedi.el_ together.
  70. Jedi.el_ is a Python auto-completion library for Emacs.
  71. To use EIN and Jedi together, add the following in your Emacs setup.::
  72. (add-hook 'ein:connect-mode-hook 'ein:jedi-setup)
  73. .. _Jedi.el: https://github.com/tkf/emacs-jedi"
  74. (let ((map ein:connect-mode-map))
  75. (define-key map "\C-c\C-i" 'ein:jedi-complete)
  76. (ein:jedi-complete-on-dot-install map)))
  77. (provide 'ein-jedi)
  78. ;;; ein-jedi.el ends here