-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdb-capf.el
102 lines (89 loc) · 3.61 KB
/
pdb-capf.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
;;; pdb-capf.el --- Completion-at-point function for python debugger -*- lexical-binding: t -*-
;; Copyright (C) 2020 Andrii Kolomoiets
;; Author: Andrii Kolomoiets <andreyk.mad@gmail.com>
;; Keywords: languages, abbrev, convenience
;; Package-Version: 1.0
;; Package-Requires: ((emacs "25.1"))
;; URL: https://github.com/muffinmad/emacs-pdb-capf
;; This file is NOT part of GNU Emacs.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; `completion-at-point' function that provides completions for current
;; pdb session.
;;
;; Add `pdb-capf' to `completion-at-point-functions' in buffer with
;; existing pdb session, e.g.:
;;
;; (add-hook 'completion-at-point-functions 'pdb-capf nil t)
;;
;;; Code:
(require 'rx)
(require 'python)
(defvar pdb-capf-script
(concat
"import rlcompleter;"
"__ECAP_N=locals().copy();__ECAP_N.update(**globals());"
"__ECAP_T='''%s''';"
"print(';'.join("
"getattr(rlcompleter.Completer(__ECAP_N), 'attr_matches' if '.' in __ECAP_T else 'global_matches')"
"(__ECAP_T)))")
"Python script to get completions.")
(defun pdb-capf--completions (string)
"Get completions for STRING from current pdb process."
(let* ((buffer (current-buffer))
(process (get-buffer-process buffer))
(comint-prompt-regexp (concat "^" python-shell-prompt-pdb-regexp)))
(with-temp-buffer
;; redirect output and send completion script
(comint-redirect-send-command-to-process
(format pdb-capf-script string)
(current-buffer) process nil t)
;; wait for command output
(with-current-buffer buffer
(while (null comint-redirect-completed)
(accept-process-output nil 0.1)))
(goto-char (point-min))
;; skip first line in case process echoes input
(unless (= (count-lines (point) (point-max)) 1)
(forward-line 1))
(let ((completions (buffer-substring-no-properties
(point)
(line-end-position))))
(unless (string-equal completions "")
(split-string completions ";"))))))
;;;###autoload
(defun pdb-capf ()
"Return completion table for current pdb session."
(when (and (process-live-p (get-buffer-process (current-buffer)))
(save-excursion
(forward-line 0)
(looking-at python-shell-prompt-pdb-regexp)))
(let* ((end (point))
(start (save-excursion
(comint-goto-process-mark)
(point)))
(start (save-excursion
(if (re-search-backward
(python-rx (or ":" whitespace open-paren close-paren
string-delimiter simple-operator))
start t)
(+ (point) (length (match-string-no-properties 0)))
start))))
(when (>= end start)
(list start
end
(completion-table-with-cache #'pdb-capf--completions)
:exclusive 'no)))))
(provide 'pdb-capf)
;;; pdb-capf.el ends here