From df6b762b317fa11202e1a55671842fe13909de75 Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Thu, 10 Dec 2020 09:22:46 +0100 Subject: [PATCH] Feat: symbols json picker (#303) --- README.md | 1 + lua/telescope/actions/init.lua | 6 ++++ lua/telescope/builtin/init.lua | 1 + lua/telescope/builtin/internal.lua | 49 ++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/README.md b/README.md index ba49d6e..39afcd3 100644 --- a/README.md +++ b/README.md @@ -391,6 +391,7 @@ Built-in function ready to be bound to any key you like :smile:. | `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checkouts it out on enter. | | `builtin.git_branches` | Lists all branches with log preview and checkout action. | | `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multiselection still WIP) | +| `builtin.symbols` | Lists symbols inside a file `data/telescope-sources/*.json` found in your rtp. More info and symbol sources can be found [here](https://github.com/nvim-telescope/telescope-symbols.nvim) | | .................................. | Your next awesome finder function here :D | #### Built-in Previewers diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index aaeffab..b0dcc26 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -230,6 +230,12 @@ actions.run_builtin = function(prompt_bufnr) require('telescope.builtin')[entry.text]() end +actions.insert_symbol = function(prompt_bufnr) + local selection = actions.get_selected_entry() + actions.close(prompt_bufnr) + vim.api.nvim_put({selection.value[1]}, '', true, true) +end + -- TODO: Think about how to do this. actions.insert_value = function(prompt_bufnr) local entry = actions.get_selected_entry(prompt_bufnr) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index c9dd1fd..14e99a6 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -36,6 +36,7 @@ builtin.git_status = require('telescope.builtin.git').status builtin.builtin = require('telescope.builtin.internal').builtin builtin.planets = require('telescope.builtin.internal').planets +builtin.symbols = require('telescope.builtin.internal').symbols builtin.commands = require('telescope.builtin.internal').commands builtin.quickfix = require('telescope.builtin.internal').quickfix builtin.loclist = require('telescope.builtin.internal').loclist diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 0b2b626..00868bb 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -86,6 +86,55 @@ internal.planets = function(opts) }:find() end +internal.symbols = function(opts) + local files = vim.api.nvim_get_runtime_file('data/telescope-sources/*.json', true) + if table.getn(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.") + return + end + + local sources = {} + if opts.sources then + for _, v in ipairs(files) do + for _, s in ipairs(opts.sources) do + if v:find(s) then + table.insert(sources, v) + end + end + end + else + sources = files + end + + local results = {} + for _, source in ipairs(sources) do + local data = vim.fn.json_decode(path.read_file(source)) + for _, entry in ipairs(data) do + table.insert(results, entry) + end + end + + pickers.new(opts, { + prompt_title = 'Symbols', + finder = finders.new_table { + results = results, + entry_maker = function(entry) + return { + value = entry, + ordinal = entry[1] .. ' ' .. entry[2], + display = entry[1] .. ' ' .. entry[2], + } + end + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(_) + actions.goto_file_selection_edit:replace(actions.insert_symbol) + return true + end + }):find() +end + internal.commands = function(opts) pickers.new(opts, { prompt_title = 'Commands',