Skip to content

Commit

Permalink
Merge pull request #15 from runiq/export-setup-function
Browse files Browse the repository at this point in the history
Register setup function
  • Loading branch information
jvgrootveld authored Sep 12, 2022
2 parents 9fd15a4 + 9e81d07 commit 856af0d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 48 deletions.
133 changes: 85 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
["<C-s>"] = {
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
["<C-q>"] = { 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 `<cr>`)_.
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 `<cr>`)_.
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", "<leader>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
},
["<C-s>"] = {
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
},
["<C-s>"] = {
before_action = function(selection) print("before C-s") end,
action = function(selection)
vim.cmd("edit " .. selection.path)
end
},
["<C-q>"] = { action = z_utils.create_basic_command("split") },
},
},
["<C-q>"] = { action = z_utils.create_basic_command("split") },
}
},
})

-- Load the extension
t.load_extension('zoxide')

-- Add a mapping
vim.keymap.set("n", "<leader>cd", t.extensions.zoxide.list)
```

### Default config
## Default config

```lua
{
Expand Down Expand Up @@ -113,18 +161,7 @@ require("telescope._extensions.zoxide.config").setup({
}
```

## Example config:

```lua
vim.api.nvim_set_keymap(
"n",
"<leader>cd",
":lua require'telescope'.extensions.zoxide.list{}<CR>",
{noremap = true, silent = true}
)
```

## Default mappings:
## Default mappings

| Action | Description | Command executed |
| ------- | ---------------------------------------------------- | ------------------------------------------------ |
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/_extensions/zoxide.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 856af0d

Please sign in to comment.