summaryrefslogtreecommitdiff
path: root/fnl/conf/pkgs/cmp.fnl
blob: 33653b8bf7de7f80997e7259b98186f989839fd0 (plain)
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
103
104
105
106
107
108
109
110
111
(local cmp (require :cmp))
(local compare (require :cmp.config.compare))

(local once false)

(local string-startswith
       (fn [str start]
         (= start (string.sub str 1 (string.len start)))))

(local string-startswith-anyof (fn [str start-list]
                                 (var ret false)
                                 (each [_ start (ipairs start-list)]
                                   (set ret (string-startswith str start)))
                                 ret))

(local string-startswith-upper
       (fn [str]
         (local first-char (string.sub str 1 1))
         (= first-char (string.upper first-char))))

(fn has-words-before []
  (local [line col] (vim.api.nvim_win_get_cursor 0))
  (local [word & rest] (vim.api.nvim_buf_get_lines 0 (- line 1) line true))
  (local before (word:sub col col))
  (local is_string (before:match "%s"))
  (and (not= col 0) (= is_string nil)))

(fn enum [types key]
  (. (. cmp types) key))

(fn cmp-setup [cmp autocomplete]
  (let [luasnip (require :luasnip)
        snip (fn [args]
               (luasnip.lsp_expand (. args :body)))]
    (local cfg
           {:experimental {:ghost_text true}
            :snippet {:expand snip}
            :preselect cmp.PreselectMode.None
            :mapping {:<Tab> (cmp.mapping (fn [fallback]
                                            (if (cmp.visible)
                                                (cmp.select_next_item)
                                                (luasnip.expand_or_jumpable)
                                                (luasnip.expand_or_jump)
                                                (has-words-before)
                                                (cmp.complete)
                                                (fallback))
                                            [:i :s]))
                      :<S-Tab> (cmp.mapping (fn [fallback]
                                              (if (cmp.visible)
                                                  (cmp.select_prev_item)
                                                  (luasnip.jumpable -1)
                                                  (luasnip.jump -1)
                                                  (fallback))
                                              [:i :s]))
                      :<C-b> (cmp.mapping.scroll_docs -4)
                      :<C-f> (cmp.mapping.scroll_docs 4)
                      :<C-A> (cmp.mapping.complete)
                      :<CR> (cmp.mapping.confirm {:behavior (enum :ConfirmBehavior
                                                                  :Replace)
                                                  :select true})}
            :sources (cmp.config.sources [{:name :nvim_lsp
                                           {:name :path} {:name :luasnip}}])})
    (if (not autocomplete) (tset cfg :completion {:autocomplete false}))
    ;; (print (vim.inspect cfg))
    (cmp.setup cfg)
    (cmp.setup.cmdline ["/" "?"]
                       {:sources (cmp.config.sources [{:name :buffer}])
                        :experimental {:ghost_text true}
                        :mapping (cmp.mapping.preset.cmdline)})
    (cmp.setup.cmdline ":"
                       {:sources (cmp.config.sources [{:name :path}]
                                                     [{:name :cmdline_history
                                                       :keyword_pattern "^[ABCDEFHIJKLMNOPQRSTUVWXYZ].*"
                                                       :entry_filter (fn [entry
                                                                          ctx]
                                                                       (if (string-startswith-upper entry.completion_item.label)
                                                                           true
                                                                           false))
                                                       :max_item_count 1}
                                                      {:name :cmdline
                                                       :keyword_length 2
                                                       :entry_filter (fn [entry
                                                                          ctx]
                                                                       (if (string-startswith entry.completion_item.label
                                                                                              :w)
                                                                           false
                                                                           true))}])
                        :experimental {:ghost_text true}
                        :preselect cmp.PreselectMode.Item
                        ; :completion {:keyword_length 2}
                        :sorting {:priority_weight 2
                                  :comparators [compare.order
                                                compare.exact
                                                compare.length]}
                        ; compare.offset
                        ; compare.scopes
                        ; compare.length
                        ; compare.recently_used
                        ; compare.exact]}
                        ;compare.score]}
                        ; compare.locality]}
                        ; compare.kind]}
                        ; compare.sort_text
                        :mapping (cmp.mapping.preset.cmdline {:<CR> {:c (fn [fallback]
                                                                          (if (not (cmp.confirm {:behavior (enum :ConfirmBehavior
                                                                                                                 :Replace)
                                                                                                 :select true}))
                                                                              (fallback)
                                                                              (vim.schedule fallback)))}})})))

(cmp-setup cmp true)