aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/telescope/actions.lua
blob: abd3381694b6c492f3b4f36d055c3fba9fce8f32 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
local M = {}

local action_state = require "telescope.actions.state"
local actions = require "telescope.actions"

M.create_plugin = function(prompt_bufnr)
  local new_plugin = action_state.get_current_line()
  actions.close(prompt_bufnr)
  vim.cmd(string.format("edit ~/.config/nvim/lua/plugins/%s.lua", new_plugin))
end

local get_disable_stm_node = function(bufnr, tree)
  vim.treesitter.set_query(
    "lua",
    "alpha_plugin_is_disabled",
    [[
(return_statement
  (expression_list
    (table_constructor
      (field
        name: (identifier) @enabled (#eq? @enabled "enabled")
        value: (false) @is_false
        )
      )
    )
  )
        ]]
  )

  local query = vim.treesitter.get_query("lua", "alpha_plugin_is_disabled")

  for id, node in query:iter_captures(tree:root(), bufnr) do
    if query.captures[id] == "enabled" then
      return node
    end
  end

  return nil
end

local get_enabled_node = function(bufnr, tree)
  vim.treesitter.set_query(
    "lua",
    "alpha_plugin_has_enable_property",
    [[
(return_statement
  (expression_list
    (table_constructor
      (field
        name: (identifier) @enabled (#eq? @enabled "enabled")
        )
      )
    )
  )
        ]]
  )

  local query = vim.treesitter.get_query("lua", "alpha_plugin_has_enable_property")

  for id, node in query:iter_captures(tree:root(), bufnr) do
    if query.captures[id] == "enabled" then
      return node
    end
  end

  return nil
end

local get_return_node = function(bufnr, tree)
  vim.treesitter.set_query(
    "lua",
    "alpha_plugin_return_table_constructor",
    [[
(return_statement
  (expression_list
    (table_constructor) @table_constructor
    )
  )
        ]]
  )

  local query = vim.treesitter.get_query("lua", "alpha_plugin_return_table_constructor")

  for id, node in query:iter_captures(tree:root(), bufnr) do
    if query.captures[id] == "table_constructor" then
      return node
    end
  end

  return nil
end

M.disable_plugin = function(prompt_bufnr)
  local entry = action_state.get_selected_entry()
  local uri = vim.uri_from_fname(entry.cwd .. entry[1])
  local bufnr = vim.uri_to_bufnr(uri)
  vim.fn.bufload(bufnr)

  local parsers = require "nvim-treesitter.parsers"
  local parser = parsers.get_parser(bufnr)
  local tree = parser:parse()[1]

  local disabled_stm_node = get_disable_stm_node(bufnr, tree)
  if disabled_stm_node ~= nil then
    vim.notify("Plugin is already disable", vim.log.levels.WARN, {})
    return
  end

  local start_line
  local end_line

  local enable_node = get_enabled_node(bufnr, tree)
  if enable_node == nil then
    -- node does not exists need to assume line
    local return_node = get_return_node(bufnr, tree)

    if return_node == nil then
      vim.notify("Can determine how to diable the plugin", vim.log.levels.ERROR, {})
      return
    end

    start_line = return_node:start() + 2
    end_line = start_line
  else
    start_line = enable_node:start()
    end_line = start_line + 1
  end

  vim.api.nvim_buf_set_lines(bufnr, start_line, end_line, false, { "  enabled = false," })
  vim.notify("Plugin " .. entry[1] .. " Disabled", vim.log.levels.INFO, {})
  actions.close(prompt_bufnr)
  vim.api.nvim_buf_call(bufnr, function()
    vim.cmd "write"
  end)
end

M.enable_plugin = function(prompt_bufnr)
  local entry = action_state.get_selected_entry()
  local uri = vim.uri_from_fname(entry.cwd .. entry[1])
  local bufnr = vim.uri_to_bufnr(uri)
  vim.fn.bufload(bufnr)

  local parsers = require "nvim-treesitter.parsers"
  local parser = parsers.get_parser(bufnr)

  local tree = parser:parse()[1]

  local disabled_stm_node = get_disable_stm_node(bufnr, tree)
  if disabled_stm_node == nil then
    vim.notify("Plugin is already Enable", vim.log.levels.WARN, {})
    return
  end

  local enable_line = disabled_stm_node:start()
  vim.api.nvim_buf_set_lines(bufnr, enable_line, enable_line + 1, false, {})
  vim.notify("Plugin " .. entry[1] .. " Enabled", vim.log.levels.INFO, {})
  actions.close(prompt_bufnr)
  vim.api.nvim_buf_call(bufnr, function()
    vim.cmd "write"
  end)
end

return M