From 04a3402b5af1965d9a146e96c691c05d5e3496af Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 15 Mar 2024 00:51:59 +0800 Subject: [PATCH] feat(telescope): make `SPC f r` use `Telescope neomru` --- autoload/SpaceVim/layers/telescope.vim | 2 +- config/plugins/telescope.nvim-0.1.2.vim | 1 + config/plugins/telescope.nvim-0.1.5.vim | 1 + lua/telescope/_extensions/neomru.lua | 57 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lua/telescope/_extensions/neomru.lua diff --git a/autoload/SpaceVim/layers/telescope.vim b/autoload/SpaceVim/layers/telescope.vim index b20452956..e76f88578 100644 --- a/autoload/SpaceVim/layers/telescope.vim +++ b/autoload/SpaceVim/layers/telescope.vim @@ -103,7 +103,7 @@ function! SpaceVim#layers#telescope#config() abort \ 1) let lnum = expand('') + s:lnum - 1 - call SpaceVim#mapping#space#def('nnoremap', ['f', 'r'], 'Telescope oldfiles', + call SpaceVim#mapping#space#def('nnoremap', ['f', 'r'], 'Telescope neomru', \ ['open-recent-file', \ [ \ 'SPC f r is to open recent file list', diff --git a/config/plugins/telescope.nvim-0.1.2.vim b/config/plugins/telescope.nvim-0.1.2.vim index cc85afd7b..67bdf154d 100644 --- a/config/plugins/telescope.nvim-0.1.2.vim +++ b/config/plugins/telescope.nvim-0.1.2.vim @@ -4,6 +4,7 @@ lua require('telescope').load_extension('project') lua require('telescope').load_extension('scriptnames') lua require('telescope').load_extension('neoyank') lua require('telescope').load_extension('task') +lua require('telescope').load_extension('neomru') if SpaceVim#layers#isLoaded('zettelkasten') lua require('telescope').load_extension('zettelkasten_template') endif diff --git a/config/plugins/telescope.nvim-0.1.5.vim b/config/plugins/telescope.nvim-0.1.5.vim index 4f8a4d122..34de184de 100644 --- a/config/plugins/telescope.nvim-0.1.5.vim +++ b/config/plugins/telescope.nvim-0.1.5.vim @@ -4,6 +4,7 @@ lua require('telescope').load_extension('project') lua require('telescope').load_extension('scriptnames') lua require('telescope').load_extension('neoyank') lua require('telescope').load_extension('task') +lua require('telescope').load_extension('neomru') if SpaceVim#layers#isLoaded('zettelkasten') lua require('telescope').load_extension('zettelkasten_template') endif diff --git a/lua/telescope/_extensions/neomru.lua b/lua/telescope/_extensions/neomru.lua new file mode 100644 index 000000000..526b23e9b --- /dev/null +++ b/lua/telescope/_extensions/neomru.lua @@ -0,0 +1,57 @@ +--============================================================================= +-- neomru.lua --- telescope neomru extension +-- Copyright (c) 2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= + +local action_state = require("telescope.actions.state") +local actions = require("telescope.actions") +local conf = require("telescope.config").values +local entry_display = require("telescope.pickers.entry_display") +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local sp_file = require('spacevim.api').import('file') + +local function prepare_output_table() + local lines = {} + local oldfiles = vim.fn['neomru#_gather_file_candidates']() + + for _, f in ipairs(oldfiles) do + table.insert(lines, sp_file.unify_path(f, ':.')) + end + return lines +end + +local function show_script_names(opts) + opts = opts or {} + pickers.new(opts, { + prompt_title = "Recent Files", + finder = finders.new_table { + results = prepare_output_table() + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + -- print(vim.inspect(selection)) + -- vim.cmd("e " + entry.value) + vim.cmd('e ' .. entry.value) + end) + return true + end, + }):find() +end + +local function run() + show_script_names() +end + +return require("telescope").register_extension({ + exports = { + -- Default when to argument is given, i.e. :Telescope scriptnames + neomru = run, + }, +})