summaryrefslogtreecommitdiff
path: root/vim/.config/nvim/lua/packed.lua
blob: 28a21ccc70b50b653d8d68c732ebecab40ce10ac (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
115
116
117
118
119
120
121
122
123
124
125
126
127
--
-- Packed: a single file, somewhat compatible Packer replacement
-- Written by grace
--

local function resolve_src(s)
  if s:match("^https?://") then
    return s
  end
  if s:match("^[%w%-]+/[%w%-_.]+$") then
    return "https://github.com/" .. s
  end
  return s
end

local function normalize_plugin(p)
  if type(p) == "string" then
    return {
      src = resolve_src(p),
      name = p:match(".*/(.*)"),
    }
  end

  if type(p[1]) == "string" then
    p.src = resolve_src(p[1])
    p[1] = nil
  end

  if p.src then
    p.src = resolve_src(p.src)
  end

  if not p.name and p.as then
    p.name = p.as
  end

  if not p.version then
    if p.commit then
      p.version = p.commit
    elseif p.tag then
      p.version = p.tag
    elseif p.branch then
      p.version = p.branch
    end
  end



  local unsupported = {
    'disable', 'installer', 'updater', 'after', 'rtp',
    'opt', 'bufread', 'lock', 'run', 'requires', 'rocks', 'setup'
  }

  for _, field in ipairs(unsupported) do
    if p[field] then
      vim.notify("Packed does not support `" .. field .. "`: " .. (p.name or p.src), vim.log.levels.WARN)
      vim.api.nvim_echo({ { "Press Enter to continue...", "MoreMsg" } }, false, {})
      vim.fn.getchar()
    end
  end

  if p.cmd or p.ft or p.keys or p.event or p.fn or p.cond or p.module or p.module_pattern then
    vim.notify("Packed does not support lazy loading: " .. (p.name or p.src), vim.log.levels.WARN)
    vim.api.nvim_echo({ { "Press Enter to continue...", "MoreMsg" } }, false, {})
    vim.fn.getchar()
  end



  return p
end

local function setup_plugins(plugs)
  local plugins = {}

  if type(plugs) == "function" then
    local function use(p)
      table.insert(plugins, p)
    end

    plugs(use)
  else
    plugins = plugs
  end
  local specs = {}

  for _, p in ipairs(plugins) do
    p = normalize_plugin(p)

    local spec = vim.deepcopy(p)
    spec.config = nil

    table.insert(specs, spec)
  end

  vim.pack.add(specs, { confirm = true, load = true })


  for _, p in ipairs(plugins) do
    if type(p) ~= "string" and p.config then
      local ok = pcall(p.config)
      if not ok then
        vim.notify("Config failed: " .. (p.name or p.src), vim.log.levels.ERROR)
        vim.api.nvim_echo({ { "Press Enter to continue...", "MoreMsg" } }, false, {})
        vim.fn.getchar()
      end
    end
  end


  local unused = vim.iter(vim.pack.get())
      :filter(function(p)
        return not p.active
      end)
      :map(function(p)
        return p.spec.name
      end)
      :totable()

  vim.pack.del(unused)
end


local r = {}
r.setup = setup_plugins

return r