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

Clarify a few things about plugins #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions docs/plugins/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@ Where:

## Usage {#usage}

A plugin has two usages:
Plugins generally can have three usages:

- [Utility plugin](#utility-plugin): Load them in your `init.lua`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think https://yazi-rs.github.io/docs/plugins/overview#sync-vs-async already included this, i.e. require-ing a plugin

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My confusion was that I thought that plugins can only either do actions on a keypress or as previewers/preloaders, with no other possibility of integration. How can we word this better?

- [Functional plugin](#functional-plugin): Bind the `plugin` command to a key in `keymap.toml`, and activate it by pressing the key.
- [Custom previewers, preloaders](/docs/configuration/yazi#plugin): Configure them as `previewers` or `preloaders` in your `[plugin]` of `yazi.toml` file.

### Utility plugin {#utility-plugin}

Such plugins can be loaded by `require`-ing them in your `init.lua`, for example:

```lua
-- ~/.config/yazi/init.lua
require("zoxide"):setup {
update_db = true,
}
```

These plugins will usually [subscribe to events](/docs/plugins/utils/#ps.sub) and work in the background.

### Functional plugin {#functional-plugin}

You can bind a `plugin` command to a specific key in your `keymap.toml` with:
Expand All @@ -69,9 +83,9 @@ return {

## Sync vs Async {#sync-vs-async}

The plugin system is designed with an async-first philosophy. Therefore, unless specifically specified, such as the `--sync` for the `plugin` command, all plugins run in an async context.
The plugin system is designed with an async-first philosophy. Therefore, unless specifically specified, such as the `--sync` for the `plugin` command, plugin code runs in an async context.

There is one exception - all `init.lua` are synchronous, which includes:
There is one exception - all top-level statements in `init.lua` files are synchronous, which includes:

- The `init.lua` for Yazi itself, i.e. `~/.config/yazi/init.lua`.
- The `init.lua` for each plugin, e.g. `~/.config/yazi/plugins/bar.yazi/init.lua`.
Expand Down Expand Up @@ -128,7 +142,7 @@ i = 2

### Async context {#async-context}

When a plugin is executed asynchronously, an isolated async context is created for it automatically.
When a plugin function is executed asynchronously (as is the default), an isolated async context is created for it automatically.

In this context, you can use all the async functions supported by Yazi, and it operates concurrently with the main thread, ensuring that the main thread is not blocked.

Expand Down Expand Up @@ -159,6 +173,10 @@ return {
}
```

`ya.sync` wraps a user-supplied function that operates in the sync context, and returns a function callable from an async context.
When the wrapped function is thus called from an async context, it will run the wrapped code inside the sync context,
propagating the return value to the caller back in the async context.

Note that `ya.sync()` call must be at the top level:

```lua
Expand Down