# List item

> Add the shared item type for bullet and ordered lists.

`list_item()` registers entries for bullet and ordered lists. Each item starts with a paragraph and can contain more blocks, including nested lists.

## Try it

[Try the list-item editor example](https://gpdoc.dev/docs/plugins/structure/list-item/#list-item-example)

## Install

```sh
cargo add gpdoc-plugin-list-item
```

## Example

Add `gpdoc-plugin-bullet-list` to the [Quickstart app](/docs/quickstart/#configure-the-editor) to render these items inside a bullet list.

```rust
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_paragraph::paragraph;
use gpdoc_plugin_bullet_list::bullet_list;
use gpdoc_plugin_list_item::{list_item, list_item_body};
use gpui::{div, prelude::*};


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(bullet_list())
    .plugin(list_item().with_view(item_view))
    .build()
    .expect("configure editor");


fn item_view() -> ViewPlugin {
    list_item_body(|row, _, _| div().flex().child(row.marker).child(row.content))
}
```

Type `-` followed by a space to start a bullet list. Press **Enter** after the item text to create the next row.

## Compose a list item

The plugin passes each row its immediate parent list, zero-based index, marker, and editable body. Nested lists start counting again at their own first item.

### Anatomy

```text
list_item_body(...)
└── div()            Item row
    ├── marker       Bullet or number for this item
    └── row.content  Editable body, including nested lists
```

### Choose the marker for each list

Replace `item_view` in the example above.

```rust
fn item_view() -> ViewPlugin {
    list_item_body(|row, _, _| {
        let marker = if row.start.is_some() { row.marker } else { "–".into() };
        div().flex().child(marker).child(row.content)
    })
}
```

Bullet items now display a dash. Ordered items keep the number calculated from their parent list.

## Split an item at the caret

```rust
use gpdoc::{DocPos, Editor, Node, Selection};


let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "DraftReview")])?;
editor.run(gpdoc_plugin_bullet_list::Toggle)?;
editor.select(Selection::caret(DocPos(8)))?;
editor.run(gpdoc_plugin_list_item::Split)?;


let items = editor.document().iter_nodes().filter(|(_, node)| node.kind == "listItem");
assert_eq!(items.count(), 2);
```

The first text character starts at document position 3, inside the list, item, and paragraph. Position 8 is just after `Draft`. The command leaves the caret at the start of `Review` in the new sibling item.

## Settings

Register `gpdoc_plugin_list_item::list_item()` with a paragraph and a list container. It takes no options. Each `listItem` accepts a leading paragraph followed by zero or more blocks.

## Commands

### `splitListItem`

Split the item at the caret inside a paragraph. For a selection in a single paragraph, remove the selected text before splitting. An item containing only an empty paragraph lifts one level.

```rust
editor.run(gpdoc_plugin_list_item::Split)?;
```

### `sinkListItem`

With the selection inside one item, nest it under its previous sibling.

```rust
editor.run(gpdoc_plugin_list_item::Sink)?;
```

### `liftListItem`

With the selection inside one item, lift it one level. From a top-level list, this moves the item’s blocks into the surrounding document.

```rust
editor.run(gpdoc_plugin_list_item::Lift)?;
```

## Keyboard shortcuts

| Shortcut        | Command         |
| --------------- | --------------- |
| **Enter**       | `splitListItem` |
| **Tab**         | `sinkListItem`  |
| **Shift + Tab** | `liftListItem`  |

For Backspace and Delete at item boundaries, add [List keyboard shortcuts](/docs/plugins/structure/list-keymap/).

## Custom list plugins

`list_container<S>(p: &mut PluginSpec<S>, kind: &'static str, attrs: AttrSpecs)` registers a container that accepts one or more list items. To wrap selected paragraphs, register your own command and call `wrap_list` in its handler.

### Command helpers

Use the helpers from `gpdoc_plugin_list_item` when your own commands should edit lists the same way as the built-in plugins. The five command helpers below return `Result<Option<Transaction>, Error>`. A successful `Some(transaction)` describes the edit. `None` means the command does not apply at the current selection.

| Function and parameters                                                            | Result                                                                                                               |
| ---------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `wrap_list(editor: CommandContext<'_>, kind: &str, item_kind: &str, args: &Attrs)` | Wrap selected paragraphs in the named container and item types, using `args` as the new container’s attributes.      |
| `toggle_list(editor: CommandContext<'_>, kind: &str, item: &str, attrs: &Attrs)`   | Wrap paragraphs, lift matching items, or convert the containing list to the requested type if the schema accepts it. |
| `split_item(editor: CommandContext<'_>, item_kind: &str)`                          | Split the paragraph and its enclosing item at a text selection in that paragraph.                                    |
| `sink_item(editor: CommandContext<'_>, item_kind: &str)`                           | Nest the selected item under its previous sibling in a list of the same type.                                        |
| `lift_item(editor: CommandContext<'_>, item_kind: &str)`                           | Lift the selected item one level, retaining lists around any remaining siblings.                                     |

### Find the selected item

To inspect the selection, use `selected_item<'a>(editor: CommandContext<'a>, item_kind: &str) -> Option<(Located<'a>, Located<'a>)>`. The pair contains the enclosing list and the item, in that order. Both ends of a text selection must lie in the same item.

### Match a typed marker

To build an input rule like the ordered-list marker, use the following signature.

```rust
pub fn list_input_rule(
  list: &'static str,
  item: &'static str,
  pattern: &str,
  attrs: impl Fn(&str) -> Option<(Attrs, Attrs)> + Send + Sync + 'static,
) -> impl Fn(gpdoc_plugin::core::InputRuleContext<'_>) -> Result<Option<Vec<Edit>>, Error>
```

Pass a valid regular expression matching the marker from the start of the paragraph through the caret. The callback receives its first capture, or an empty string if there is no capture. Return the container and item attributes in that order to replace the typed marker with a list, or `None` to leave the text as typed.

## Source code

[list-item/src/lib.rs](/source/plugins/list-item/src/lib.rs.txt)