File: magithub-completion.el

package info (click to toggle)
magithub 0.1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,920 kB
  • sloc: lisp: 3,684; makefile: 82
file content (102 lines) | stat: -rw-r--r-- 4,256 bytes parent folder | download | duplicates (2)
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
;;; magithub-completion.el --- Completion using info provided by magithub  -*- lexical-binding: t; -*-

;; Copyright (C) 2018  Steve Purcell

;; Author: Steve Purcell <steve@sanityinc.com>
;; Keywords: convenience

;; 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:

;; Provides `completion-at-point' functions which complete issue
;; numbers etc when they are entered in commit messages.

;; Extended information is attached to completions so that `company'
;; can access it via the standard `company-capf' backend.

;;; Code:


(require 'magithub-settings)
(require 'magithub-issue)


;;;###autoload
(defun magithub-completion-complete-issues ()
  "A `completion-at-point' function which completes \"#123\" issue references.
Add this to `completion-at-point-functions' in buffers where you
want this to be available."
  (when (magithub-enabled-p)
    (when (looking-back "#\\([0-9]*\\)" (- (point) 10))
      (let ((start (match-beginning 1))
            (end (match-end 0))
            (prefix (match-string 1))
            completions)
        (dolist (i (magithub--issue-list))
          (let-alist i
            (let ((n (number-to-string .number)))
              (when (string-prefix-p prefix n)
                (push (propertize n :issue i) completions)))))
        (list start end (sort completions #'string<)
              :exclusive 'no
              :company-docsig (lambda (c)
                                (let-alist (get-text-property 0 :issue c)
                                  .title))
              :annotation-function (lambda (c)
                                     (let-alist (get-text-property 0 :issue c)
                                       .title))
              :company-doc-buffer (lambda (c)
                                    (save-window-excursion
                                      (magithub-issue-visit
				       (get-text-property 0 :issue c)))))))))

;;;###autoload
(defun magithub-completion-complete-users ()
  "A `completion-at-point' function which completes \"@user\" user references.
Add this to `completion-at-point-functions' in buffers where you
want this to be available.  The user list is currently simply the
list of all users who created issues or pull requests."
  (when (magithub-enabled-p)
    (when (looking-back "@\\([_-A-Za-z0-9]*\\)" (- (point) 30))
      (let ((start (match-beginning 1))
            (end (match-end 0))
            (prefix (match-string 1))
            completions)
        (dolist (i (magithub--issue-list))
          (let-alist i
            (when (string-prefix-p prefix .user.login)
              (let ((candidate (copy-sequence .user.login))
                    (association (and .author_association
                                      (not (string= "NONE" .author_association))
                                      .author_association)))
                (push (propertize candidate :user .user :association association)
                      completions)))))
        (list start end (sort (cl-remove-duplicates completions :test #'string=)
			      #'string<)
              :exclusive 'no
              :company-docsig (lambda (c) (get-text-property 0 :association c))
              :annotation-function (lambda (c) (get-text-property 0 :association c)))))))

;;;###autoload
(defun magithub-completion-enable ()
  "Enable completion of info from magithub in the current buffer."
  (make-local-variable 'completion-at-point-functions)
  (dolist (f '(magithub-completion-complete-issues
	       magithub-completion-complete-users))
    (add-to-list 'completion-at-point-functions f)))


(provide 'magithub-completion)
;;; magithub-completion.el ends here