Add textobjects queries (#20924)

Co-Authored-By: Max <max@zed.dev>

Release Notes:

- vim: Added motions `[[`, `[]`, `]]`, `][` for navigating by section,
`[m`, `]m`, `[M`, `]M` for navigating by method, and `[*`, `]*`, `[/`,
`]/` for comments. These currently only work for languages built in to
Zed, as they are powered by new tree-sitter queries.
- vim: Added new text objects: `ic`, `ac` for inside/around classes,
`if`,`af` for functions/methods, and `g c` for comments. These currently
only work for languages built in to Zed, as they are powered by new
tree-sitter queries.

---------

Co-authored-by: Max <max@zed.dev>
This commit is contained in:
Conrad Irwin 2024-12-03 09:37:01 -08:00 committed by GitHub
parent c443307c19
commit 75c9dc179b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 1205 additions and 26 deletions

View file

@ -3441,6 +3441,30 @@ impl MultiBufferSnapshot {
})
}
pub fn excerpt_before(&self, id: ExcerptId) -> Option<MultiBufferExcerpt<'_>> {
let start_locator = self.excerpt_locator_for_id(id);
let mut cursor = self.excerpts.cursor::<Option<&Locator>>(&());
cursor.seek(&Some(start_locator), Bias::Left, &());
cursor.prev(&());
let excerpt = cursor.item()?;
Some(MultiBufferExcerpt {
excerpt,
excerpt_offset: 0,
})
}
pub fn excerpt_after(&self, id: ExcerptId) -> Option<MultiBufferExcerpt<'_>> {
let start_locator = self.excerpt_locator_for_id(id);
let mut cursor = self.excerpts.cursor::<Option<&Locator>>(&());
cursor.seek(&Some(start_locator), Bias::Left, &());
cursor.next(&());
let excerpt = cursor.item()?;
Some(MultiBufferExcerpt {
excerpt,
excerpt_offset: 0,
})
}
pub fn excerpt_boundaries_in_range<R, T>(
&self,
range: R,
@ -4689,6 +4713,26 @@ impl<'a> MultiBufferExcerpt<'a> {
}
}
pub fn id(&self) -> ExcerptId {
self.excerpt.id
}
pub fn start_anchor(&self) -> Anchor {
Anchor {
buffer_id: Some(self.excerpt.buffer_id),
excerpt_id: self.excerpt.id,
text_anchor: self.excerpt.range.context.start,
}
}
pub fn end_anchor(&self) -> Anchor {
Anchor {
buffer_id: Some(self.excerpt.buffer_id),
excerpt_id: self.excerpt.id,
text_anchor: self.excerpt.range.context.end,
}
}
pub fn buffer(&self) -> &'a BufferSnapshot {
&self.excerpt.buffer
}