forked from kostafey/ejc-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejc-company.el
91 lines (76 loc) · 3.18 KB
/
ejc-company.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
;;; ejc-company.el -- SQL completitions at point by company-mode (the part of ejc-sql).
;;; Copyright © 2020 - Kostafey <[email protected]>
;;; 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 2, 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, write to the Free Software Foundation,
;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
;;; Commentary:
;; `ejc-company' is a `company' completion backend for `ejc-sql'.
;; To use it, add `ejc-company-backend' to `company-backends':
;; (requre 'ejc-company)
;; (push 'ejc-company-backend company-backends)
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'company)
(require 'ejc-completion-common)
(defun ejc-company-make-candidate (candidate)
(let ((text (car candidate))
(meta (cadr candidate)))
(propertize text 'meta meta)))
(defun ejc-company-add-meta (meta candidates)
(-map (lambda (k) (list k meta))
candidates))
(defun ejc-company-candidates (prefix)
(let* ((prefix-1 (ejc-get-prefix-word))
(prefix-2 (save-excursion
(search-backward "." nil t)
(ejc-get-prefix-word)))
(res))
(dolist (item
(cl-remove-if-not
(lambda (c) (string-prefix-p prefix (car c) t))
(append
(ejc-append-without-duplicates
(ejc-company-add-meta
"ansi sql" (ejc-get-ansi-sql-words))
(ejc-company-add-meta
"keyword" (ejc-get-keywords))
'car :right)
(ejc-company-add-meta
"owner" (ejc-owners-candidates))
(ejc-company-add-meta
"table" (ejc-tables-candidates))
(ejc-company-add-meta
"view" (ejc-views-candidates))
(if (not prefix-1)
(ejc-company-add-meta
"package" (ejc-packages-candidates)))
(ejc-company-add-meta
"column" (ejc-colomns-candidates)))))
(push (ejc-company-make-candidate item) res))
res))
(defun ejc-company-annotation (candidate)
(format " %s" (get-text-property 0 'meta candidate)))
(defun ejc-company-doc-buffer (candidate)
(company-doc-buffer (ac-ejc-documentation candidate)))
(defun ejc-company-backend (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'ejc-company-backend))
(prefix (and (bound-and-true-p ejc-sql-mode)
(company-grab-symbol)))
(candidates (ejc-company-candidates arg))
(doc-buffer (ejc-company-doc-buffer arg))
(annotation (ejc-company-annotation arg))))
(provide 'ejc-company)
;;; ejc-company.el ends here