From 88db4f086e9248a79832f284d7d6d4117ae32519 Mon Sep 17 00:00:00 2001 From: Ariel D'Alessandro Date: Sat, 6 May 2023 17:00:32 +0200 Subject: Rename my actions to avoid warning on the require --- lua/alpha/globals.lua | 4 +- lua/alpha/telescope/actions.lua | 170 ------------------------------------- lua/alpha/telescope/init.lua | 10 +-- lua/alpha/telescope/my_actions.lua | 170 +++++++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 177 deletions(-) delete mode 100644 lua/alpha/telescope/actions.lua create mode 100644 lua/alpha/telescope/my_actions.lua (limited to 'lua/alpha') diff --git a/lua/alpha/globals.lua b/lua/alpha/globals.lua index b575c0c..e445e72 100644 --- a/lua/alpha/globals.lua +++ b/lua/alpha/globals.lua @@ -13,11 +13,11 @@ dump = function(...) return unpack(args) end -RELOAD = function(...) +Reload = function(...) return require("plenary.reload").reload_module(...) end R = function(name) - RELOAD(name) + Reload(name) return require(name) end diff --git a/lua/alpha/telescope/actions.lua b/lua/alpha/telescope/actions.lua deleted file mode 100644 index b0e9f10..0000000 --- a/lua/alpha/telescope/actions.lua +++ /dev/null @@ -1,170 +0,0 @@ -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.query.set( - "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.query.get("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.query.set( - "lua", - "alpha_plugin_has_enable_property", - [[ -(return_statement - (expression_list - (table_constructor - (field - name: (identifier) @enabled (#eq? @enabled "enabled") - ) - ) - ) - ) - ]] - ) - - local query = vim.treesitter.query.get("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.query.set( - "lua", - "alpha_plugin_return_table_constructor", - [[ -(return_statement - (expression_list - (table_constructor) @table_constructor - ) - ) - ]] - ) - - local query = vim.treesitter.query.get("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 - -M.create_scratch_file = function(prompt_bufnr) - local new_scratch = action_state.get_current_line() - actions.close(prompt_bufnr) - vim.cmd(string.format("edit ~/.config/nvim/lua/scratchs/%s.lua", new_scratch)) -end - -return M diff --git a/lua/alpha/telescope/init.lua b/lua/alpha/telescope/init.lua index 38cd9be..5d9f5ca 100644 --- a/lua/alpha/telescope/init.lua +++ b/lua/alpha/telescope/init.lua @@ -1,16 +1,16 @@ -SHOULD_RELOAD_TELESCOPE = true +local SHOULD_RELOAD_TELESCOPE = true local reloader = function() if SHOULD_RELOAD_TELESCOPE then - RELOAD "plenary" - RELOAD "telescope" - RELOAD "alpha.telescope.setup" + Reload "plenary" + Reload "telescope" + Reload "alpha.telescope.setup" end end local fb_actions = require("telescope").extensions.file_browser.actions local actions = require "telescope.actions" -local my_actions = require "alpha.telescope.actions" +local my_actions = require "alpha.telescope.my_actions" local M = {} diff --git a/lua/alpha/telescope/my_actions.lua b/lua/alpha/telescope/my_actions.lua new file mode 100644 index 0000000..b0e9f10 --- /dev/null +++ b/lua/alpha/telescope/my_actions.lua @@ -0,0 +1,170 @@ +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.query.set( + "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.query.get("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.query.set( + "lua", + "alpha_plugin_has_enable_property", + [[ +(return_statement + (expression_list + (table_constructor + (field + name: (identifier) @enabled (#eq? @enabled "enabled") + ) + ) + ) + ) + ]] + ) + + local query = vim.treesitter.query.get("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.query.set( + "lua", + "alpha_plugin_return_table_constructor", + [[ +(return_statement + (expression_list + (table_constructor) @table_constructor + ) + ) + ]] + ) + + local query = vim.treesitter.query.get("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 + +M.create_scratch_file = function(prompt_bufnr) + local new_scratch = action_state.get_current_line() + actions.close(prompt_bufnr) + vim.cmd(string.format("edit ~/.config/nvim/lua/scratchs/%s.lua", new_scratch)) +end + +return M -- cgit v1.2.3