Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow installing yazi plugins from a plugin monorepo #177

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ Yazi is highly customizable. It features its own plugin and event system,
themes, and keybindings. This section lists some of the plugins and themes that
I like.

- <https://github.com/DreamMaoMao/keyjump.yazi> allows jumping to a line by
typing a hint character, much like
- <https://github.com/redbeardymcgee/yazi-plugins/tree/main/keyjump.yazi> allows
jumping to a line by typing a hint character, much like
[hop.nvim](https://github.com/smoka7/hop.nvim)
- <https://github.com/Rolv-Apneseth/starship.yazi> is a port of the
[starship prompt](https://starship.rs) to yazi. It allows reusing the prompt
Expand All @@ -208,11 +208,6 @@ I like.
[my config](https://github.com/mikavilpas/dotfiles/commit/bb07515f69d219fd3435d222fcb2d80d27a25025#diff-973b37f40e024ca0f7e62f2569efce24ad550d0352adc8449168ac950af9eaf5R8)
for an example of using it

## 🍴 About my fork

I forked this from <https://github.com/DreamMaoMao/yazi.nvim> for my own use,
and also because I wanted to learn Neovim plugin development.

## Contributing

Please see [COMMUNITY.md](./COMMUNITY.md) for more information on the project!
29 changes: 16 additions & 13 deletions documentation/plugin-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ allows you to fully manage your yazi and neovim plugins from inside neovim.
## Getting started

In this example, we will install the yazi plugin
[DreamMaoMao/keyjump.yazi](https://github.com/DreamMaoMao/keyjump.yazi), which
adds a "jump-to-line" feature to yazi. We will also install a _flavor_ which
applies a color scheme to yazi.
[starship.yazi](https://github.com/Rolv-Apneseth/starship.yazi), which adds
support for the [starship](https://starship.rs/) shell prompt to yazi. We will
also install a _flavor_ which applies a color scheme to yazi.

In your yazi.nvim configuration, add a new lazy.nvim plugin specification for
`DreamMaoMao/keyjump.yazi`:
`Rolv-Apneseth/starship.yazi`:

```lua
-- this file is: /Users/mikavilpas/.config/nvim/lua/plugins/my-file-manager.lua
Expand All @@ -29,8 +29,7 @@ return {
},
},
{
-- example: include a plugin
"DreamMaoMao/keyjump.yazi",
"Rolv-Apneseth/starship.yazi",
lazy = true,
build = function(plugin)
require("yazi.plugin").build_plugin(plugin)
Expand All @@ -53,9 +52,9 @@ Next, run `:Lazy` in neovim to install the plugin and flavor.

Finally, make changes in your yazi configuration:

- add a keybinding to your `~/.config/yazi/keymap.toml` according to the
[instructions](https://github.com/DreamMaoMao/keyjump.yazi?tab=readme-ov-file#usage)
provided by the plugin author.
- (for plugins requiring keybindings) add a keybinding to your
`~/.config/yazi/keymap.toml` according to the instructions provided by the
plugin author.
- include the flavor in your `~/.config/yazi/theme.toml` according to the
[instructions](https://github.com/BennyOe/onedark.yazi?tab=readme-ov-file#%EF%B8%8F-usage)
provided by the flavor author.
Expand Down Expand Up @@ -123,12 +122,16 @@ plugin manager.
---@type LazyPlugin[]
return {
{
-- a yazi plugin which like flash.nvim in neovim,allow use key char to Precise selection
-- https://github.com/DreamMaoMao/keyjump.yazi
"DreamMaoMao/keyjump.yazi",
-- example: a yazi plugin monorepo which provides multiple plugins for
-- yazi. To use it, you need to specify the sub_dir for the plugin you want
-- to install.
"redbeardymcgee/yazi-plugins",
lazy = true,
build = function(plugin)
require("yazi.plugin").build_plugin(plugin)
-- This is a plugin like flash.nvim in neovim - it allows you to jump to
-- a line by typing the first few characters of the line.
-- https://github.com/redbeardymcgee/yazi-plugins
require("yazi.plugin").build_plugin(plugin, { sub_dir = "keyjump.yazi" })
end,
},
{
Expand Down
24 changes: 22 additions & 2 deletions lua/yazi/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ local M = {}
--- build = function(plugin)
--- -- this will be called by lazy.nvim after the plugin was updated
--- require("yazi.plugin").build_plugin(plugin)
---
--- -- if your plugin is not found at the root of the repository, you can
--- -- specify a subdirectory like this:
--- require("yazi.plugin").build_plugin({ sub_dir = "my-plugin.yazi" })
--- end,
--- }
--- ```
---
--- For more information, see the yazi.nvim documentation.
---
---@param plugin YaziLazyNvimSpec
---@param options? { yazi_dir: string }
---@param options? { yazi_dir: string, sub_dir?: string }
function M.build_plugin(plugin, options)
local yazi_dir = options and options.yazi_dir
or vim.fn.expand('~/.config/yazi')
Expand All @@ -39,11 +43,19 @@ function M.build_plugin(plugin, options)

local to = vim.fs.normalize(vim.fs.joinpath(yazi_plugins_dir, plugin.name))

if options and options.sub_dir then
plugin = {
name = plugin.name,
dir = vim.fs.joinpath(plugin.dir, options.sub_dir),
}
to = vim.fs.normalize(vim.fs.joinpath(yazi_plugins_dir, options.sub_dir))
end

return M.symlink(plugin, to)
end

---@param flavor YaziLazyNvimSpec
---@param options? { yazi_dir: string }
---@param options? { yazi_dir: string, sub_dir?: string }
function M.build_flavor(flavor, options)
local yazi_dir = options and options.yazi_dir
or vim.fn.expand('~/.config/yazi')
Expand All @@ -54,6 +66,14 @@ function M.build_flavor(flavor, options)

local to = vim.fs.normalize(vim.fs.joinpath(yazi_flavors_dir, flavor.name))

if options and options.sub_dir then
flavor = {
name = flavor.name,
dir = vim.fs.joinpath(flavor.dir, options.sub_dir),
}
to = vim.fs.normalize(vim.fs.joinpath(yazi_flavors_dir, options.sub_dir))
end

return M.symlink(flavor, to)
end

Expand Down
45 changes: 45 additions & 0 deletions spec/yazi/plugin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ describe('installing a plugin', function()
assert.is_equal(result.error, 'source directory does not exist')
assert.is_equal(result.from, plugin_dir)
end)

it('can install a plugin from a monorepo subdirectory', function()
local plugin_monorepo_dir = vim.fs.joinpath(base_dir, 'yazi-plugins')
local plugin_dir = vim.fs.joinpath(plugin_monorepo_dir, 'keyjump.yazi')
local yazi_dir = vim.fs.joinpath(base_dir, 'fake-yazi-dir')

vim.fn.mkdir(plugin_monorepo_dir)
vim.fn.mkdir(plugin_dir)
vim.fn.mkdir(yazi_dir)
vim.fn.mkdir(vim.fs.joinpath(yazi_dir, 'plugins'))

plugin.build_plugin({
dir = plugin_monorepo_dir,
name = 'yazi-plugins',
}, { yazi_dir = yazi_dir, sub_dir = 'keyjump.yazi' })

-- verify that the plugin was symlinked
local symlink =
vim.uv.fs_readlink(vim.fs.joinpath(yazi_dir, 'plugins', 'keyjump.yazi'))

assert.are.same(plugin_dir, symlink)
end)
end)

describe('installing a flavor', function()
Expand All @@ -73,6 +95,29 @@ describe('installing a plugin', function()

assert.are.same(flavor_dir, symlink)
end)

it('can install a flavor from a monorepo subdirectory', function()
local flavor_monorepo_dir = vim.fs.joinpath(base_dir, 'yazi-flavors')
local flavor_dir = vim.fs.joinpath(flavor_monorepo_dir, 'flavor123.yazi')
local yazi_dir = vim.fs.joinpath(base_dir, 'fake-yazi-dir')

vim.fn.mkdir(flavor_monorepo_dir)
vim.fn.mkdir(flavor_dir)
vim.fn.mkdir(yazi_dir)
vim.fn.mkdir(vim.fs.joinpath(yazi_dir, 'flavors'))

plugin.build_flavor({
dir = flavor_monorepo_dir,
name = 'yazi-flavors',
}, { yazi_dir = yazi_dir, sub_dir = 'flavor123.yazi' })

-- verify that the flavor was symlinked
local symlink = vim.uv.fs_readlink(
vim.fs.joinpath(yazi_dir, 'flavors', 'flavor123.yazi')
)

assert.are.same(flavor_dir, symlink)
end)
end)

describe('symlink', function()
Expand Down
Loading