summaryrefslogtreecommitdiff
path: root/fnl/conf/pkgs/cmp.fnl
blob: 99fa55d5ec023ecb20457e332d1204406f963bee (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
112
113
114
(local cmp (require :cmp))
(local compare (require :cmp.config.compare))
(local always-first [:write :edit :split :quit])

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

(fn string-startswith-anyof? [str start-list]
  (fn iter [[item & rest]]
    (if (not item) false
        (string-startswith? str item) true
        (iter rest)))

  (iter start-list))

(fn string-startswith-upper? [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 ":"
                       {:matching {:disallow_partial_fuzzy_matching true
                                   :disallow_prefix_unmatching true}
                        :sources (cmp.config.sources [{:name :path}]
                                                     [{:name :cmdline
                                                       :keyword_length 1}])
                        :preselect cmp.PreselectMode.Item
                        :sorting {:priority_weight 2
                                  :comparators [(fn [e1 e2]
                                                  (fn iter [[item & rest]]
                                                    (if (or (not rest)
                                                            (not item))
                                                        false
                                                        (= e1.completion_item.label
                                                           item)
                                                        true
                                                        (iter rest)))

                                                  (iter always-first))
                                                compare.offset
                                                compare.exact
                                                compare.score
                                                compare.locality
                                                compare.kind
                                                compare.length
                                                compare.order]}
                        :mapping (cmp.mapping.preset.cmdline {:<CR> {:c (fn [fallback]
                                                                          (if (not (cmp.confirm {:behavior (enum :ConfirmBehavior
                                                                                                                 :Replace)
                                                                                                 :select true}))
                                                                              (fallback)
                                                                              (vim.schedule fallback)))}})})))

; {: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)})))
;     disallow_fuzzy_matching = false,
;     disallow_partial_matching = false,
;     disallow_prefix_unmatching = false,)))
(cmp-setup cmp true)