From 6cd3836bd711ae37a979805322a28ea0e2c2ce55 Mon Sep 17 00:00:00 2001 From: Dinesh Salunke Date: Fri, 3 Jan 2025 20:09:04 +0530 Subject: [PATCH] refactor: add tnf package --- lazy-lock.json | 9 +-- .../lua/telescope/_extensions/tnf.lua | 79 ++++++++++++++++++ lua/config/keymaps.lua | 2 + lua/plugins/telescope.lua | 80 ++++++++++--------- lua/plugins/tnf.lua | 5 ++ 5 files changed, 133 insertions(+), 42 deletions(-) create mode 100644 lua/_plugins/tnf.nvim/lua/telescope/_extensions/tnf.lua create mode 100644 lua/plugins/tnf.lua diff --git a/lazy-lock.json b/lazy-lock.json index 136cc59..3fd088e 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -10,8 +10,7 @@ "cmp-nvim-lua": { "branch": "main", "commit": "f12408bdb54c39c23e67cab726264c10db33ada8" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" }, - "conform.nvim": { "branch": "master", "commit": "9180320205d250429f0f80e073326c674e2a7149" }, - "copilot.lua": { "branch": "master", "commit": "886ee73b6d464b2b3e3e6a7ff55ce87feac423a9" }, + "conform.nvim": { "branch": "master", "commit": "990d37017e193fe40bdabf058e598e76f5e7d929" }, "dressing.nvim": { "branch": "master", "commit": "3a45525bb182730fe462325c99395529308f431e" }, "flash.nvim": { "branch": "main", "commit": "34c7be146a91fec3555c33fe89c7d643f6ef5cf1" }, "gitsigns.nvim": { "branch": "main", "commit": "5f808b5e4fef30bd8aca1b803b4e555da07fc412" }, @@ -35,13 +34,13 @@ "nui.nvim": { "branch": "main", "commit": "53e907ffe5eedebdca1cd503b00aa8692068ca46" }, "nvim-cmp": { "branch": "main", "commit": "b555203ce4bd7ff6192e759af3362f9d217e8c89" }, "nvim-colorizer.lua": { "branch": "master", "commit": "a065833f35a3a7cc3ef137ac88b5381da2ba302e" }, - "nvim-lspconfig": { "branch": "master", "commit": "8b15a1a597a59f4f5306fad9adfe99454feab743" }, - "nvim-notify": { "branch": "master", "commit": "c3797193536711b5d8983975791c4b11dc35ab3a" }, + "nvim-lspconfig": { "branch": "master", "commit": "a8ef5e6e497b3ebeaaf35b939c07c211563b2e05" }, + "nvim-notify": { "branch": "master", "commit": "bd9cd51f9ef2f6326fc2bc9931d0718c1794e247" }, "nvim-spectre": { "branch": "master", "commit": "08be31c104df3b4b049607694ebb2b6ced4f928b" }, "nvim-treesitter": { "branch": "master", "commit": "b6492f41c46d9c3bc3550ecfb10c3f261e73cba6" }, "nvim-treesitter-textobjects": { "branch": "master", "commit": "ad8f0a472148c3e0ae9851e26a722ee4e29b1595" }, "nvim-ts-autotag": { "branch": "main", "commit": "1cca23c9da708047922d3895a71032bc0449c52d" }, - "nvim-web-devicons": { "branch": "master", "commit": "4adeeaa7a32d46cf3b5833341358c797304f950a" }, + "nvim-web-devicons": { "branch": "master", "commit": "5740b7382429d20b6ed0bbdb0694185af9507d44" }, "playground": { "branch": "master", "commit": "ba48c6a62a280eefb7c85725b0915e021a1a0749" }, "plenary.nvim": { "branch": "master", "commit": "2d9b06177a975543726ce5c73fca176cedbffe9d" }, "smart-splits.nvim": { "branch": "master", "commit": "d2da32b76b89126609f4142bd55279c8723cb050" }, diff --git a/lua/_plugins/tnf.nvim/lua/telescope/_extensions/tnf.lua b/lua/_plugins/tnf.nvim/lua/telescope/_extensions/tnf.lua new file mode 100644 index 0000000..ff67692 --- /dev/null +++ b/lua/_plugins/tnf.nvim/lua/telescope/_extensions/tnf.lua @@ -0,0 +1,79 @@ +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") +local Path = require("plenary.path") +local built_in = require("telescope.builtin") + +local os_sep = Path.path.sep + +local is_dir = function(path) + if Path.is_path(path) then + return path:is_dir() + end + return string.sub(path, -1, -1) == os_sep +end + +local create_file = function(file) + if not file then + return + end + if file == "" then + vim.notify( + string.format("[file_browser.%s] %s", "actions.create", "Please enter a valid file or folder name!"), + vim.log.levels.WARN, + { + title = "telescope-file-browser.nvim", + } + ) + return + end + file = Path:new(file) + if file:exists() then + vim.notify( + string.format("[file_browser.%s] %s", "actions.create", "Selection already exists!"), + vim.log.levels.WARN, + { + title = "telescope-file-browser.nvim", + } + ) + return + end + + if not is_dir(file.filename) then + file:touch({ parents = true }) + else + Path:new(file.filename:sub(1, -2)):mkdir({ parents = true, mode = 493 }) -- 493 => decimal for mode 0755 + end + + return file +end +local get_dropdown = require("telescope.themes").get_dropdown +local tnf = function() + built_in.find_files(get_dropdown({ + previewer = false, + find_command = { "find", ".", "-type", "d", "-not", "-path", "*/node_modules/*" }, + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local current_picker = action_state.get_current_picker(prompt_bufnr) + local entry = action_state.get_selected_entry() + if entry then + local base_dir = action_state.get_selected_entry().value .. os_sep + current_picker:reset_prompt(base_dir, true) + else + local file_path = current_picker:_get_prompt() + actions.close(prompt_bufnr) + local file = create_file(file_path) + if file then + vim.cmd.edit(file:absolute()) + end + end + end) + return true + end, + })) +end + +return require("telescope").register_extension({ + exports = { + tnf = tnf, + }, +}) diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 24fd81c..1c62527 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -71,3 +71,5 @@ map("n", "[", "tabprevious", { desc = "Previous Tab" }) -- map("t", "", [[]], { desc = "Escape on terminal" }) map("n", "", "ggG", { desc = "Select all" }) + +map("n", "nf", "Telescope tnf", { desc = "Telescope new file" }) diff --git a/lua/plugins/telescope.lua b/lua/plugins/telescope.lua index 6420e22..aa31504 100644 --- a/lua/plugins/telescope.lua +++ b/lua/plugins/telescope.lua @@ -104,44 +104,50 @@ return { desc = "[ L ]ist [ D ]ef[ i ]nitions", }, }, - opts = { - defaults = { - prompt_prefix = "", - selection_caret = "", - sorting_strategy = "ascending", - path_display = { - "smart", - }, - layout_config = { - horizontal = { - prompt_position = "top", - width = 0.75, - height = 0.65, - preview_width = 0.6, + config = function(_, opts) + local telescope = require("telescope") + + telescope.setup({ + defaults = { + prompt_prefix = "", + selection_caret = "", + sorting_strategy = "ascending", + path_display = { + "smart", + }, + layout_config = { + horizontal = { + prompt_position = "top", + width = 0.75, + height = 0.65, + preview_width = 0.6, + }, + }, + mappings = { + i = { + [""] = function(...) + require("telescope.actions").move_selection_previous(...) + end, + [""] = function(...) + require("telescope.actions").move_selection_next(...) + end, + [""] = function(...) + require("trouble.providers.telescope").open_with_trouble(...) + end, + }, + n = { + q = function(...) + require("telescope.actions").close(...) + end, + [""] = function(...) + require("trouble.providers.telescope").open_with_trouble(...) + end, + }, }, }, - mappings = { - i = { - [""] = function(...) - require("telescope.actions").move_selection_previous(...) - end, - [""] = function(...) - require("telescope.actions").move_selection_next(...) - end, - [""] = function(...) - require("trouble.providers.telescope").open_with_trouble(...) - end, - }, - n = { - q = function(...) - require("telescope.actions").close(...) - end, - [""] = function(...) - require("trouble.providers.telescope").open_with_trouble(...) - end, - }, - }, - }, - }, + }) + + telescope.load_extension("tnf") + end, }, } diff --git a/lua/plugins/tnf.lua b/lua/plugins/tnf.lua new file mode 100644 index 0000000..03d6820 --- /dev/null +++ b/lua/plugins/tnf.lua @@ -0,0 +1,5 @@ +return { + "thob/tnf.nvim", + dependencies = { "nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim" }, + dir = "~/.config/nvim/lua/_plugins/tnf.nvim", +}