-
Notifications
You must be signed in to change notification settings - Fork 8
/
helm-gitignore.el
102 lines (87 loc) · 3.39 KB
/
helm-gitignore.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
;;; helm-gitignore.el --- Generate .gitignore files with gitignore.io.
;;
;; Author: Juan Placencia
;; Version: 0.2.0
;; Keywords: helm gitignore gitignore.io
;; Homepage: https://github.com/jupl/helm-gitignore
;; Package-Requires: ((gitignore-mode "1.1.0") (helm "1.7.0") (request "0.1.0") (cl-lib "0.5"))
;;; Commentary:
;;
;; This package provides a configured helm to generate .gitignore files using
;; https://www.gitignore.io/.
;;
;;; Code:
(require 'gitignore-mode)
(require 'helm)
(require 'json)
(require 'request)
(require 'cl-lib)
(defvar helm-gitignore--api-url
"https://www.gitignore.io/api/%s"
"Url used to generate .gitignore file.")
(defvar helm-gitignore--list-url
"https://www.gitignore.io/dropdown/templates.json?term=%s"
"Url used to get list of templates in raw and human-friendly text.")
(defvar helm-gitignore--cache nil
"Cache that contains results of querying of candidates from gitignore.io.")
(defvar helm-gitignore--source
(helm-build-sync-source "gitignore.io"
:candidates 'helm-gitignore--candidates
:action 'helm-gitignore--action
:volatile t
:requires-pattern 1))
(defun helm-gitignore--candidates ()
"Query for gitignore templates."
(let ((delay 0.1))
(unless helm-gitignore--cache
(run-at-time delay nil 'helm-gitignore--query-candidates helm-pattern))
(and (sit-for delay)
(prog1 helm-gitignore--cache
(and helm-gitignore--cache
(setq helm-gitignore--cache nil))))))
(defun helm-gitignore--query-candidates (pattern)
"Query for gitignore templates using given PATTERN."
(request
(format helm-gitignore--list-url pattern)
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(setq helm-gitignore--cache
(mapcar 'helm-gitignore--format-result data))
(and helm-alive-p (helm-update))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys&rest _)
(message error-thrown)))))
(defun helm-gitignore--action (candidate)
"Generate .gitignore given at least a CANDIDATE and present to screen."
(let ((candidates (helm-marked-candidates)))
(unless candidates (setq candidates (list candidate)))
(request
(helm-gitignore--generate-url candidates)
:parser 'buffer-string
:success (cl-function
(lambda (&key data &allow-other-keys)
(when data
(with-current-buffer (get-buffer-create "*gitignore*")
(gitignore-mode)
(erase-buffer)
(insert data)
(goto-char (point-min))
(pop-to-buffer (current-buffer))))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys&rest _)
(message error-thrown))))))
(defun helm-gitignore--format-result (result)
"Pluck data from RESULT to create a Helm candidate."
(cons (assoc-default 'text result) (assoc-default 'id result)))
(defun helm-gitignore--generate-url (list)
"Create url from LIST to generate .gitignore using gitignore.io."
(format helm-gitignore--api-url (mapconcat 'identity list ",")))
;;;###autoload
(defun helm-gitignore ()
"Helm to generate .gitignore using gitignore.io."
(interactive)
(helm :sources 'helm-gitignore--source
:buffer "*helm-gitignore*"))
(provide 'helm-gitignore)
;;; helm-gitignore.el ends here