From 364d795d22e7a080be793379e30cfdd2036840cf Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Sat, 21 Aug 2021 16:48:19 +0200 Subject: [PATCH] fix: some improvements for telescope.symbols if run in insert mode (#1117) - keep insert if run in insert mode - also look in `.local/share/nvim/telescope/symbols/*.json` for symbols can be overriden with `symbol_path` --- doc/telescope.txt | 14 ++++++++++++-- lua/telescope/actions/init.lua | 14 ++++++++++++-- lua/telescope/builtin/init.lua | 8 +++++++- lua/telescope/builtin/internal.lua | 22 ++++++++++++++++++++-- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/doc/telescope.txt b/doc/telescope.txt index 20b3984..b7862b6 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -595,13 +595,23 @@ builtin.planets({opts}) *builtin.planets()* builtin.symbols({opts}) *builtin.symbols()* - Lists symbols inside of data/telescope-sources/*.json found in your runtime - path. Check README for more info + Lists symbols inside of `data/telescope-sources/*.json` found in your + runtime path or found in `stdpath("data")/telescope/symbols/*.json`. The + second path can be customized. We provide a couple of default symbols which + can be found in https://github.com/nvim-telescope/telescope-symbols.nvim. + This repos README also provides more information about the format in which + the symbols have to be. Parameters: ~ {opts} (table) options to pass to the picker + Fields: ~ + {symbol_path} (string) specify the second path. Default: + `stdpath("data")/telescope/symbols/*.json` + {sources} (table) specify a table of sources you want to load + this time + builtin.commands({opts}) *builtin.commands()* Lists available plugin/user commands and runs them on `` diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 43315cc..7f82823 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -373,9 +373,19 @@ actions.run_builtin = function(prompt_bufnr) end actions.insert_symbol = function(prompt_bufnr) - local selection = action_state.get_selected_entry() + local symbol = action_state.get_selected_entry().value[1] actions.close(prompt_bufnr) - vim.api.nvim_put({ selection.value[1] }, "", true, true) + vim.api.nvim_put({ symbol }, "", true, true) +end + +actions.insert_symbol_i = function(prompt_bufnr) + local symbol = action_state.get_selected_entry().value[1] + actions._close(prompt_bufnr, true) + local cursor = vim.api.nvim_win_get_cursor(0) + vim.api.nvim_buf_set_text(0, cursor[1] - 1, cursor[2], cursor[1] - 1, cursor[2], { symbol }) + vim.schedule(function() + vim.api.nvim_win_set_cursor(0, { cursor[1], cursor[2] + #symbol }) + end) end -- TODO: Think about how to do this. diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 70c4302..9dc29af 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -202,8 +202,14 @@ builtin.builtin = require("telescope.builtin.internal").builtin ---@param opts table: options to pass to the picker builtin.planets = require("telescope.builtin.internal").planets ---- Lists symbols inside of data/telescope-sources/*.json found in your runtime path. Check README for more info +--- Lists symbols inside of `data/telescope-sources/*.json` found in your runtime path +--- or found in `stdpath("data")/telescope/symbols/*.json`. The second path can be customized. +--- We provide a couple of default symbols which can be found in +--- https://github.com/nvim-telescope/telescope-symbols.nvim. This repos README also provides more +--- information about the format in which the symbols have to be. ---@param opts table: options to pass to the picker +---@field symbol_path string: specify the second path. Default: `stdpath("data")/telescope/symbols/*.json` +---@field sources table: specify a table of sources you want to load this time builtin.symbols = require("telescope.builtin.internal").symbols --- Lists available plugin/user commands and runs them on `` diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 5355d5f..226373b 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -113,8 +113,22 @@ internal.planets = function(opts) end internal.symbols = function(opts) + local initial_mode = vim.fn.mode() local files = vim.api.nvim_get_runtime_file("data/telescope-sources/*.json", true) - if table.getn(files) == 0 then + local data_path = (function() + if not opts.symbol_path then + return Path:new { vim.fn.stdpath "data", "telescope", "symbols" } + else + return Path:new { opts.symbol_path } + end + end)() + if data_path:exists() then + for _, v in ipairs(require("plenary.scandir").scan_dir(data_path:absolute(), { search_pattern = "%.json$" })) do + table.insert(files, v) + end + end + + if #files == 0 then print( "No sources found! Check out https://github.com/nvim-telescope/telescope-symbols.nvim " .. "for some prebuild symbols or how to create you own symbol source." @@ -157,7 +171,11 @@ internal.symbols = function(opts) }, sorter = conf.generic_sorter(opts), attach_mappings = function(_) - actions.select_default:replace(actions.insert_symbol) + if initial_mode == "i" then + actions.select_default:replace(actions.insert_symbol_i) + else + actions.select_default:replace(actions.insert_symbol) + end return true end, }):find()