From 9e81d074d5dfb2ca10c024173aa7377693f32d1e Mon Sep 17 00:00:00 2001 From: runiq Date: Wed, 7 Sep 2022 02:21:35 +0200 Subject: [PATCH] Register setup function This way, telescope will call the plugin setup automatically upon loading the extension. The config can be set via telescope.setup(), as described in https://github.com/nvim-telescope/telescope.nvim#telescope-setup-structure. This should make configuration of this extension easier by bringing it in line with telescope's standard config structure. --- README.md | 133 +++++++++++++++++---------- lua/telescope/_extensions/zoxide.lua | 1 + 2 files changed, 86 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index c149532..b8b2d65 100644 --- a/README.md +++ b/README.md @@ -15,69 +15,117 @@ Plug 'nvim-telescope/telescope.nvim' Plug 'jvgrootveld/telescope-zoxide' ``` -## Setup +## Configuration -You can setup the extension by adding the following to your config: +You can add, extend and update Telescope Zoxide config by using [Telescope's default configuration mechanism for extensions](https://github.com/nvim-telescope/telescope.nvim#telescope-setup-structure). +An example config: ```lua -require'telescope'.load_extension('zoxide') +-- Useful for easily creating commands +local z_utils = require("telescope._extensions.zoxide.utils") + +require('telescope').setup{ + -- (other Telescope configuration...) + extensions = { + zoxide = { + prompt_title = "[ Walking on the shoulders of TJ ]", + mappings = { + default = { + after_action = function(selection) + print("Update to (" .. selection.z_score .. ") " .. selection.path) + end + }, + [""] = { + before_action = function(selection) print("before C-s") end, + action = function(selection) + vim.cmd("edit " .. selection.path) + end + }, + -- Opens the selected entry in a new split + [""] = { action = z_utils.create_basic_command("split") }, + }, + } + } +} ``` -## Available functions: +You can add new mappings and extend default mappings. +_(Note: The mapping with the key 'default' is the mapping invoked on pressing ``)_. +Every keymapping must have an `action` function and supports the optional functions `before_action` and `after_action`. -### List +Tip: If the action is a telescope picker, you should also set `keepinsert = true` to open it in insert mode. Else you can't directly type into the next telescope picker. -With Telescope command +All action functions are called with the current `selection` object as parameter which contains the selected path and Zoxide score. -```vim -:Telescope zoxide list -``` +Tip: Make use of the supplied `z_utils.create_basic_command` helper function to easily invoke a vim command for the selected path. -In Lua +## Loading the extension + +You can then load the extension by adding the following after your call to telescope's own `setup()` function: ```lua -require'telescope'.extensions.zoxide.list{} +require("telescope").load_extension('zoxide') ``` -## Overridable config +Loading the extension will allow you to use the following functionality: -You can add, extend and update Telescope Zoxide config by calling the setup function after loading the plugin. +### List -You can add new mappings and extend default mappings. -_(Note: The mapping with the key 'default' is the mapping invoked on pressing ``)_. -Every keymapping must have an `action` function and supports the optional functions `before_action` and `after_action`. +With Telescope command: -Tip: If the action is a telescope picker, you should also set `keepinsert = true` to open it in insert mode. Else you can't directly type into the next telescope picker. +```vim +:Telescope zoxide list +``` -All action functions are called with the current `selection` object as parameter which contains the selected path and Zoxide score. +In Lua: -Tip: Make use of the supplied `z_utils.create_basic_command` helper function to easily invoke a vim command for the selected path. +```lua +require("telescope").extensions.zoxide.list({picker_opts}) +``` + +You can also bind the function to a key: -### Example setup +```lua +vim.keymap.set("n", "cd", require("telescope").extensions.zoxide.list) +``` + +## Full example ```lua +local t = require("telescope") local z_utils = require("telescope._extensions.zoxide.utils") -require("telescope._extensions.zoxide.config").setup({ - prompt_title = "[ Walking on the shoulders of TJ ]", - mappings = { - default = { - after_action = function(selection) - print("Update to (" .. selection.z_score .. ") " .. selection.path) - end - }, - [""] = { - before_action = function(selection) print("before C-s") end, - action = function(selection) - vim.cmd("edit " .. selection.path) - end +-- Configure the extension +t.setup({ + extensions = { + zoxide = { + prompt_title = "[ Walking on the shoulders of TJ ]", + mappings = { + default = { + after_action = function(selection) + print("Update to (" .. selection.z_score .. ") " .. selection.path) + end + }, + [""] = { + before_action = function(selection) print("before C-s") end, + action = function(selection) + vim.cmd("edit " .. selection.path) + end + }, + [""] = { action = z_utils.create_basic_command("split") }, + }, }, - [""] = { action = z_utils.create_basic_command("split") }, - } + }, }) + +-- Load the extension +t.load_extension('zoxide') + +-- Add a mapping +vim.keymap.set("n", "cd", t.extensions.zoxide.list) ``` -### Default config +## Default config ```lua { @@ -113,18 +161,7 @@ require("telescope._extensions.zoxide.config").setup({ } ``` -## Example config: - -```lua -vim.api.nvim_set_keymap( - "n", - "cd", - ":lua require'telescope'.extensions.zoxide.list{}", - {noremap = true, silent = true} -) -``` - -## Default mappings: +## Default mappings | Action | Description | Command executed | | ------- | ---------------------------------------------------- | ------------------------------------------------ | diff --git a/lua/telescope/_extensions/zoxide.lua b/lua/telescope/_extensions/zoxide.lua index d8b5b91..e660c80 100644 --- a/lua/telescope/_extensions/zoxide.lua +++ b/lua/telescope/_extensions/zoxide.lua @@ -4,6 +4,7 @@ if not has_telescope then end return telescope.register_extension { + setup = require("telescope._extensions.zoxide.config").setup, exports = { list = require("telescope._extensions.zoxide.list") }