-
Notifications
You must be signed in to change notification settings - Fork 1
/
dragonruby.el
63 lines (56 loc) · 2.39 KB
/
dragonruby.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
(require 'cl-lib)
(require 'subr-x)
(require 'browse-url)
(require 'request)
;; browse-url-url-encode-chars
(setq company-backends (delete 'company-dragonruby company-backends))
(setq company-backends (delete 'company-dragonruby company-backends))
(setq company-backends (delete 'company-dragonruby company-backends))
(setq company-backends (delete 'company-dragonruby company-backends))
(defun company-dragonruby--buffer-text (buffer)
(with-current-buffer buffer
(save-restriction (widen)
(buffer-substring-no-properties (point-min) (point-max)))))
(defun amir-httpbin ()
(interactive)
(request "http://httpbin.org/put"
:type "PUT"
:data (json-encode '(("key" . "value") ("key2" . "value2")))
:headers '(("Content-Type" . "application/json"))
:parser 'json-read
:success (cl-function
(lambda (&key response &allow-other-keys)
(message "I sent: %S" (assoc-default 'json data))))) )
(defun company-dragonruby--get-autocomplete ()
(interactive)
(let* ((text (company-dragonruby--buffer-text (current-buffer)))
(index (point))
(char-at-index-for-text (string (aref text (- index 2)))))
(if (string= char-at-index-for-text ".")
(request "http://localhost:9001/dragon/autocomplete/"
:type "POST"
:data (json-encode `(("index" . ,index) ("text" . ,text)))
:parser 'buffer-string
:error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)))
:sync t
:success
(cl-function (lambda (&key data &allow-other-keys)
(when data
(setq company-dragonruby--autocomplete-result (format "%s" data))))))
(setq company-dragonruby--autocomplete-result ""))))
(defun company-dragonruby--find-candidates (prefix)
(let ((res '()))
(company-dragonruby--get-autocomplete)
(dolist (item (split-string company-dragonruby--autocomplete-result "\n" t))
(when (string-prefix-p prefix item)
(push (propertize item) res)))
res))
(defun company-dragonruby (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-dragonruby))
(prefix (and (eq major-mode 'ruby-mode)
(company-grab-symbol-cons "\\.\\|->" 2)))
(candidates (company-dragonruby--find-candidates arg))))
(add-to-list 'company-backends 'company-dragonruby)