Skip to content

Add insert_at operation for inserting into a list at a specific index #24

Closed

Description

@nathanjmcdougall

Problem

There is currently no way to insert an item at a specific position in a YAML sequence while preserving comments on existing items. The only options are:

  1. �ppend — adds at the end only

eplace the entire list — loses all internal comments

For example, inserting a pre-commit repo at index 1 (between two existing repos) currently requires replacing the entire
epos list, which destroys any comments within the existing items.

Proposal

Add an \insert_at\ method to \Document\ (and \Editor):

\\python
doc.insert_at('repos', index=1, value={'repo': 'https://...', 'hooks': [...]})
\\

This would require a new \Op\ variant in the Rust layer (sibling to \�ppend):

\
ust
Op::InsertAt { index: usize, value: Value }
\\

The patcher would locate the byte position of the Nth item and insert the new content before it, matching the indentation of its siblings.

Use case

In usethis, pre-commit hooks have a defined ordering. When adding a new hook repo, it often needs to be inserted between existing repos rather than appended at the end. Without \insert_at, the workaround is a full list replacement via
eplace('repos', value=new_list), which loses all comments within the list.

Notes

  • Comments on existing items would be fully preserved since they are not touched.
  • The new item's indentation should match existing siblings (related to indentation inference in the patcher).

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions