summaryrefslogtreecommitdiff
path: root/lua/99/extensions/init.lua
blob: 6a7d6315de44c46f839f97cdf9f27e943a0393f0 (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
local Files = require("99.extensions.files")

--- @class _99.Extensions.Source
--- @field init_for_buffer fun(_99: _99.State): nil
--- @field init fun(_99: _99.State): nil
--- @field refresh_state fun(_99: _99.State): nil

--- @param completion _99.Completion | nil
--- @return _99.Extensions.Source | nil
local function get_source(completion)
  local source = completion and completion.source or "native"

  if source == "native" then
    local ok, native = pcall(require, "99.extensions.native")
    if not ok then
      vim.notify("[99] Failed to load native completions", vim.log.levels.ERROR)
      return
    end
    return native
  end

  if source == "cmp" then
    local ok, cmp = pcall(require, "99.extensions.cmp")
    if not ok then
      vim.notify(
        '[99] nvim-cmp is not installed. Install hrsh7th/nvim-cmp or use source = "blink" or "native"',
        vim.log.levels.WARN
      )
      return
    end
    return cmp
  end
  if source == "blink" then
    local ok, _ = pcall(require, "blink.compat")
    if not ok then
      vim.notify(
        "[99] blink.compat is required for blink source. Install: { 'saghen/blink.compat', version = '2.*' }",
        vim.log.levels.ERROR
      )
      return
    end
    local cmp_ok, cmp = pcall(require, "99.extensions.cmp")
    if not cmp_ok then
      vim.notify(
        "[99] 99 completion module failed to load",
        vim.log.levels.ERROR
      )
      return
    end
    return cmp
  end
end

return {
  --- @param _99 _99.State
  init = function(_99)
    local source = get_source(_99.completion)
    if not source then
      return
    end
    source.init(_99)
  end,

  capture_project_root = function()
    local cwd = vim.fn.getcwd()
    local git_root = vim.fs.root(cwd, ".git")
    Files.set_project_root(git_root or cwd)
  end,

  --- @param _99 _99.State
  setup_buffer = function(_99)
    local source = get_source(_99.completion)
    if not source then
      return
    end
    source.init_for_buffer(_99)
  end,

  --- @param _99 _99.State
  refresh = function(_99)
    local source = get_source(_99.completion)
    if not source then
      return
    end
    source.refresh_state(_99)
  end,
}