aboutsummaryrefslogtreecommitdiffstats
path: root/lua/pi/telescope
diff options
context:
space:
mode:
authorElvis Claros Castro <elvis@claros.ar>2024-07-12 11:18:14 -0300
committerElvis Claros Castro <elvis@claros.ar>2024-07-12 11:18:14 -0300
commitea0f4bf8ffade657b1d45152dbc183d4679e75c4 (patch)
treecc1fb023bc308e019ddce33a4b0aed39f56805dc /lua/pi/telescope
parent1fd0af14d592d612c2aaab7717c04c8806ba3daa (diff)
downloadnvim-conf-ea0f4bf8ffade657b1d45152dbc183d4679e75c4.tar.gz
nvim-conf-ea0f4bf8ffade657b1d45152dbc183d4679e75c4.zip
Feat: PI
Diffstat (limited to 'lua/pi/telescope')
-rw-r--r--lua/pi/telescope/init.lua186
-rw-r--r--lua/pi/telescope/mappings.lua55
-rw-r--r--lua/pi/telescope/my_actions.lua170
-rw-r--r--lua/pi/telescope/setup.lua222
4 files changed, 633 insertions, 0 deletions
diff --git a/lua/pi/telescope/init.lua b/lua/pi/telescope/init.lua
new file mode 100644
index 0000000..7117a35
--- /dev/null
+++ b/lua/pi/telescope/init.lua
@@ -0,0 +1,186 @@
+local SHOULD_RELOAD_TELESCOPE = false
+
+local reloader = function()
+ if SHOULD_RELOAD_TELESCOPE then
+ Reload "plenary"
+ Reload "telescope"
+ Reload "pi.telescope.setup"
+ end
+end
+
+local fb_actions = require("telescope").extensions.file_browser.actions
+local actions = require "telescope.actions"
+local my_actions = require "pi.telescope.my_actions"
+
+local M = {}
+
+function M.project_files()
+ local opts = { show_untracked = true } -- define here if you want to define something
+ local ok = pcall(require("telescope.builtin").git_files, opts)
+ if not ok then
+ require("telescope.builtin").find_files(opts)
+ end
+end
+
+function M.branches()
+ require("telescope.builtin").git_branches {
+ attach_mappings = function(_, map)
+ map("i", "<c-j>", actions.git_create_branch)
+ map("n", "<c-j>", actions.git_create_branch)
+ return true
+ end,
+ }
+end
+
+function M.search_config()
+ require("telescope.builtin").git_files {
+ prompt_title = "< Dotfiles >",
+ cwd = "$HOME/.dotfiles",
+ show_untracked = true,
+ }
+end
+
+M.find_nvim_config = function()
+ require("telescope.builtin").find_files {
+ prompt_title = "< Neovim >",
+ cwd = "$HOME/.config/nvim/",
+ }
+end
+
+M.find_nvim_plugin = function()
+ require("telescope.builtin").find_files {
+ prompt_title = "< Plugins >",
+ cwd = "$HOME/.config/nvim/lua/plugins/",
+ attach_mappings = function(_, map)
+ map("i", "<C-y>", my_actions.create_plugin)
+ map("i", "<C-w>", my_actions.disable_plugin)
+ map("i", "<C-e>", my_actions.enable_plugin)
+
+ return true
+ end,
+ }
+end
+
+function M.grep_string()
+ vim.ui.input({ prompt = "Grep for > " }, function(input)
+ if input == nil then
+ return
+ end
+ require("telescope.builtin").grep_string { search = input }
+ end)
+end
+
+function M.grep_word()
+ require("telescope.builtin").grep_string { search = vim.fn.expand "<cword>" }
+end
+
+function M.find_symbol()
+ vim.ui.input({ prompt = "Symbol for > " }, function(input)
+ if input == nil then
+ return
+ end
+ require("telescope.builtin").lsp_workspace_symbols { query = input }
+ end)
+end
+
+function M.my_plugins()
+ if vim.fn.isdirectory(vim.fn.getenv "HOME" .. "/code/plugins/") == 0 then
+ vim.notify("Directory ~/code/plugins does not exists", vim.log.levels.WARN, { title = "Telescope Mappings" })
+ return
+ end
+ require("telescope.builtin").find_files {
+ cwd = "~/code/plugins/",
+ }
+end
+
+function M.scratchs()
+ require("telescope.builtin").find_files {
+ prompt_title = "Scratchs",
+ no_ignore = true,
+ cwd = "~/.config/nvim/lua/scratchs/",
+ attach_mappings = function(_, map)
+ map("i", "<c-t>", my_actions.create_scratch_file)
+
+ return true
+ end,
+ }
+end
+
+function M.projectionist()
+ return require("telescope").extensions.projectionist.projectionist()
+end
+
+function M.laravel_commands()
+ return require("telescope").extensions.laravel.laravel()
+end
+
+function M.api_specs()
+ if vim.fn.isdirectory "./api-specification" == 0 then
+ vim.notify("Directory api-specification does not exists", vim.log.levels.WARN, { title = "Telescope Mappings" })
+ return
+ end
+ require("telescope.builtin").git_files {
+ cwd = "./api-specification",
+ show_untracked = true,
+ }
+end
+
+function M.worktree()
+ return require("telescope").extensions.git_worktree.git_worktrees()
+end
+
+function M.worktree_create()
+ return require("telescope").extensions.git_worktree.create_git_worktree()
+end
+
+function M.refactor()
+ return require("telescope").extensions.refactoring.refactors()
+end
+
+function M.gateway()
+ return require("pi.php.gateway").graphql_definitions {
+ layout_config = {
+ horizontal = {
+ preview_width = function(_, cols, _)
+ return math.floor(cols * 0.7)
+ end,
+ },
+ },
+ }
+end
+
+function M.file_browser_relative()
+ return M.file_browser { path = "%:p:h" }
+end
+
+function M.file_browser(opts)
+ opts = opts or {}
+
+ opts = {
+ path = opts.path,
+ sorting_strategy = "ascending",
+ scroll_strategy = "cycle",
+ layout_config = {
+ prompt_position = "top",
+ },
+
+ attach_mappings = function(_, map)
+ map("i", "<c-y>", fb_actions.create_from_prompt)
+
+ return true
+ end,
+ }
+
+ return require("telescope").extensions.file_browser.file_browser(opts)
+end
+
+return setmetatable({}, {
+ __index = function(_, k)
+ reloader()
+ if M[k] then
+ return M[k]
+ else
+ return require("telescope.builtin")[k]
+ end
+ end,
+})
diff --git a/lua/pi/telescope/mappings.lua b/lua/pi/telescope/mappings.lua
new file mode 100644
index 0000000..a927023
--- /dev/null
+++ b/lua/pi/telescope/mappings.lua
@@ -0,0 +1,55 @@
+local map_tele = function(key, f, opts)
+ local default = {
+ mode = "n",
+ options = {},
+ buffer = nil,
+ desc = nil,
+ }
+ opts = opts or {}
+ opts = vim.tbl_deep_extend("force", default, opts or {})
+
+ local rhs = function()
+ R("pi.telescope")[f](opts.options)
+ end
+
+ local map_options = {
+ remap = false,
+ silent = true,
+ }
+ if opts.buffer then
+ map_options.buffer = opts.buffer
+ end
+ if opts.desc then
+ map_options.desc = "Telescope: " .. opts.desc
+ end
+
+ vim.keymap.set(opts.mode, key, rhs, map_options)
+end
+
+-- not shure what this line does
+vim.api.nvim_set_keymap(
+ "c",
+ "<c-r><c-r>",
+ "<Plug>(TelescopeFuzzyCommandSearch)",
+ { noremap = false, nowait = true, desc = "In comand look for previous commands" }
+)
+
+map_tele("<leader>pp", "project_files", { desc = "Open Project files" })
+map_tele("<leader>gc", "branches", { desc = "Git Branches selector" })
+map_tele("<leader>pw", "grep_word", { desc = "Grep word under cursor" })
+map_tele("<leader>ps", "grep_string", { desc = "Grep provided input string" })
+map_tele("<leader>rc", "search_config", { desc = "Search Files in .dotfiles directory" })
+map_tele("<leader>rn", "find_nvim_config", { desc = "Search Files in nvim config directory" })
+map_tele("<leader>rp", "find_nvim_plugin", { desc = "Search Neovim plugins" })
+map_tele("<leader>rs", "scratchs", { desc = "Search Neovim Scratchs files" })
+map_tele("<leader>ph", "help_tags", { desc = "Neovim Help" })
+map_tele("<leader>pe", "buffers", { desc = "Open buffers" })
+map_tele("<leader>pr", "treesitter", { desc = "Treesitter buffer elements" })
+map_tele("<leader>bb", "file_browser_relative", { desc = "Current directory" })
+map_tele("<leader>bp", "file_browser", { desc = "Project directory" })
+map_tele("<leader>gs", "git_status", { desc = "Git status" })
+map_tele("<leader>ve", "diagnostics", { desc = "Diagnostic of the buffer" })
+map_tele("<leader>po", "api_specs", { desc = "Openapi directory" })
+map_tele("<leader>pm", "gateway", { desc = "Graphql mutations and queries" })
+
+return map_tele
diff --git a/lua/pi/telescope/my_actions.lua b/lua/pi/telescope/my_actions.lua
new file mode 100644
index 0000000..2afb8aa
--- /dev/null
+++ b/lua/pi/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",
+ "pi_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", "pi_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",
+ "pi_plugin_has_enable_property",
+ [[
+(return_statement
+ (expression_list
+ (table_constructor
+ (field
+ name: (identifier) @enabled (#eq? @enabled "enabled")
+ )
+ )
+ )
+ )
+ ]]
+ )
+
+ local query = vim.treesitter.query.get("lua", "pi_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",
+ "pi_plugin_return_table_constructor",
+ [[
+(return_statement
+ (expression_list
+ (table_constructor) @table_constructor
+ )
+ )
+ ]]
+ )
+
+ local query = vim.treesitter.query.get("lua", "pi_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/pi/telescope/setup.lua b/lua/pi/telescope/setup.lua
new file mode 100644
index 0000000..0e368c7
--- /dev/null
+++ b/lua/pi/telescope/setup.lua
@@ -0,0 +1,222 @@
+local actions = require "telescope.actions"
+local action_state = require "telescope.actions.state"
+local action_layout = require "telescope.actions.layout"
+
+local set_prompt_to_entry_value = function(prompt_bufnr)
+ local entry = action_state.get_selected_entry()
+ if not entry or not type(entry) == "table" then
+ return
+ end
+
+ action_state.get_current_picker(prompt_bufnr):reset_prompt(entry.ordinal)
+end
+
+local filetype_hook = function(filepath, bufnr, opts)
+ -- Here for example you can say: if ft == "xyz" then this_regex_highlighing else nothing end
+ --
+ local is_image = function(filepath)
+ local image_extensions = { "png", "jpg" } -- Supported image formats
+ local split_path = vim.split(filepath:lower(), ".", { plain = true })
+ local extension = split_path[#split_path]
+ return vim.tbl_contains(image_extensions, extension)
+ end
+ if not is_image(filepath) then
+ return true
+ end
+ local ui = require "image.ui"
+ local options = require "image.options"
+ local dimensions = require "image.dimensions"
+ local api = require "image.api"
+ local config = require "image.config"
+ local user_opts = {
+ render = {
+ foreground_color = true,
+ background_color = true,
+ },
+ }
+ local global_opts = vim.tbl_deep_extend("force", config.DEFAULT_OPTS, user_opts)
+
+ local ascii_width, ascii_height, horizontal_padding, vertical_padding, img_width, img_height =
+ dimensions.calculate_ascii_width_height(opts.winid, filepath, global_opts)
+ options.set_options_before_render(bufnr)
+ ui.buf_clear(bufnr)
+ local label = ui.create_label(
+ filepath,
+ ascii_width,
+ horizontal_padding,
+ global_opts.render.show_image_dimensions,
+ img_width,
+ img_height
+ )
+
+ api.get_ascii_data_sync(filepath, ascii_width, ascii_height, global_opts, function(ascii_data)
+ ui.buf_insert_data_with_padding(bufnr, ascii_data, horizontal_padding, vertical_padding, label, global_opts)
+
+ options.set_options_after_render(bufnr)
+ end)
+ return false
+end
+
+require("telescope").setup {
+ defaults = {
+ preview = {
+ filetype_hook = filetype_hook,
+ },
+ prompt_prefix = "❯ ",
+ selection_caret = "❯ ",
+
+ winblend = 0,
+
+ layout_strategy = "horizontal",
+ layout_config = {
+ width = 0.95,
+ height = 0.85,
+ -- preview_cutoff = 120,
+ prompt_position = "bottom",
+
+ horizontal = {
+ preview_width = function(_, cols, _)
+ if cols > 200 then
+ return math.floor(cols * 0.5)
+ else
+ return math.floor(cols * 0.7)
+ end
+ end,
+ },
+
+ vertical = {
+ width = 0.9,
+ height = 0.95,
+ preview_height = 0.5,
+ },
+
+ flex = {
+ horizontal = {
+ preview_width = 0.9,
+ },
+ },
+ },
+
+ selection_strategy = "reset",
+ sorting_strategy = "descending",
+ scroll_strategy = "cycle",
+ color_devicons = true,
+
+ mappings = {
+ i = {
+ -- ["<C-x>"] = false,
+ -- ["<C-s>"] = actions.select_horizontal,
+ ["<C-n>"] = "move_selection_next",
+
+ ["<C-y>"] = set_prompt_to_entry_value,
+
+ -- These are new :)
+ ["<M-p>"] = action_layout.toggle_preview,
+ ["<M-m>"] = action_layout.toggle_mirror,
+ -- ["<M-p>"] = action_layout.toggle_prompt_position,
+
+ -- ["<M-m>"] = actions.master_stack,
+
+ -- ["<C-q>"] = actions.send_to_qflist + actions.open_qflist,
+ -- ["<M-q>"] = actions.send_selected_to_qflist + actions.open_qflist,
+
+ -- This is nicer when used with smart-history plugin.
+ ["<C-k>"] = actions.cycle_history_next,
+ ["<C-j>"] = actions.cycle_history_prev,
+
+ -- ["<c-space>"] = function(prompt_bufnr)
+ -- local opts = {
+ -- callback = actions.toggle_selection,
+ -- loop_callback = actions.send_selected_to_qflist,
+ -- }
+ -- require("telescope").extensions.hop._hop_loop(prompt_bufnr, opts)
+ -- end,
+
+ -- ["<C-w>"] = function()
+ -- vim.api.nvim_input("<c-s-w>")
+ -- end,
+ },
+ },
+
+ -- borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
+ -- file_ignore_patterns = nil,
+
+ file_previewer = require("telescope.previewers").vim_buffer_cat.new,
+ grep_previewer = require("telescope.previewers").vim_buffer_vimgrep.new,
+ qflist_previewer = require("telescope.previewers").vim_buffer_qflist.new,
+
+ history = {
+ path = "~/.local/share/nvim/databases/telescope_history.sqlite3",
+ limit = 100,
+ },
+ -- buffer_previewer_maker = new_maker,
+ },
+
+ pickers = {
+ fd = {
+ mappings = {
+ n = {
+ ["kj"] = "close",
+ },
+ },
+ },
+
+ git_branches = {
+ mappings = {
+ i = {
+ ["<C-a>"] = false,
+ },
+ },
+ },
+ },
+
+ extensions = {
+ fzy_native = {
+ override_generic_sorter = true,
+ override_file_sorter = true,
+ },
+
+ fzf_writer = {
+ use_highlighter = false,
+ minimum_grep_characters = 6,
+ },
+
+ hop = {
+ -- keys define your hop keys in order; defaults to roughly lower- and uppercased home row
+ keys = { "a", "s", "d", "f", "g", "h", "j", "k", "l", ";" }, -- ... and more
+
+ -- Highlight groups to link to signs and lines; the below configuration refers to demo
+ -- sign_hl typically only defines foreground to possibly be combined with line_hl
+ sign_hl = { "WarningMsg", "Title" },
+
+ -- optional, typically a table of two highlight groups that are alternated between
+ line_hl = { "CursorLine", "Normal" },
+
+ -- options specific to `hop_loop`
+ -- true temporarily disables Telescope selection highlighting
+ clear_selection_hl = false,
+ -- highlight hopped to entry with telescope selection highlight
+ -- note: mutually exclusive with `clear_selection_hl`
+ trace_entry = true,
+ -- jump to entry where hoop loop was started from
+ reset_selection = true,
+ },
+
+ ["ui-select"] = {
+ require("telescope.themes").get_dropdown {
+ borderchars = {
+ prompt = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
+ results = { "─", "│", "─", "│", "╭", "╮", "┤", "├" },
+ preview = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
+ },
+ width = 0.8,
+ previewer = false,
+ },
+ },
+ },
+}
+
+require("telescope").load_extension "notify"
+require("telescope").load_extension "file_browser"
+require("telescope").load_extension "ui-select"
+require("telescope").load_extension "fzf"