Skip to content

Commit fb42402

Browse files
tausbnCopilot
andcommitted
yeast: Delete the Cursor trait, inline its methods on AstCursor
The trait had a single implementor (`AstCursor`), three type parameters of which one (`T`) was never used in any method signature, and one external consumer that needed `use yeast::Cursor;` in scope just to call methods on the cursor. The abstraction was overhead without a second implementor to justify it. Move the six trait methods to an inherent `impl AstCursor` block; delete `shared/yeast/src/cursor.rs`, the `pub mod cursor;` and `pub use cursor::Cursor;` lines in `lib.rs`, and the `use yeast::Cursor;` in `tree-sitter-extractor`'s `traverse_yeast`. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bda8e7d commit fb42402

3 files changed

Lines changed: 26 additions & 38 deletions

File tree

shared/tree-sitter-extractor/src/extractor/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,6 @@ fn emit_extras_in(visitor: &mut Visitor, node: Node<'_>) {
882882
}
883883

884884
fn traverse_yeast(tree: &yeast::Ast, visitor: &mut Visitor) {
885-
use yeast::Cursor;
886885
let mut cursor = tree.walk();
887886
visitor.enter_node(cursor.node());
888887
let mut recurse = true;

shared/yeast/src/cursor.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

shared/yeast/src/lib.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use serde_json::{json, Value};
77

88
pub mod build;
99
pub mod captures;
10-
pub mod cursor;
1110
pub mod dump;
1211
pub mod node_types_yaml;
1312
pub mod query;
@@ -19,7 +18,6 @@ mod visitor;
1918
pub use yeast_macros::{query, rule, tree, trees};
2019

2120
use captures::Captures;
22-
pub use cursor::Cursor;
2321
use query::QueryNode;
2422

2523
/// Node id: an index into the [`Ast`] arena. A newtype around `usize`
@@ -174,37 +172,16 @@ impl<'a> AstCursor<'a> {
174172
self.node_id
175173
}
176174

177-
fn goto_next_sibling_opt(&mut self) -> Option<()> {
178-
self.node_id = self.parents.last_mut()?.1.next()?;
179-
Some(())
180-
}
181-
182-
fn goto_first_child_opt(&mut self) -> Option<()> {
183-
let parent_id = self.node_id;
184-
let parent = self.ast.get_node(parent_id)?;
185-
let mut children = ChildrenIter::new(parent);
186-
let first_child = children.next()?;
187-
self.node_id = first_child;
188-
self.parents.push((parent_id, children));
189-
Some(())
190-
}
191-
192-
fn goto_parent_opt(&mut self) -> Option<()> {
193-
self.node_id = self.parents.pop()?.0;
194-
Some(())
195-
}
196-
}
197-
impl<'a> Cursor<'a, Ast, Node, FieldId> for AstCursor<'a> {
198-
fn node(&self) -> &'a Node {
175+
pub fn node(&self) -> &'a Node {
199176
&self.ast.nodes[self.node_id.0]
200177
}
201178

202-
fn field_id(&self) -> Option<FieldId> {
179+
pub fn field_id(&self) -> Option<FieldId> {
203180
let (_, children) = self.parents.last()?;
204181
children.current_field()
205182
}
206183

207-
fn field_name(&self) -> Option<&'static str> {
184+
pub fn field_name(&self) -> Option<&'static str> {
208185
if self.field_id() == Some(CHILD_FIELD) {
209186
None
210187
} else {
@@ -213,17 +190,37 @@ impl<'a> Cursor<'a, Ast, Node, FieldId> for AstCursor<'a> {
213190
}
214191
}
215192

216-
fn goto_first_child(&mut self) -> bool {
193+
pub fn goto_first_child(&mut self) -> bool {
217194
self.goto_first_child_opt().is_some()
218195
}
219196

220-
fn goto_next_sibling(&mut self) -> bool {
197+
pub fn goto_next_sibling(&mut self) -> bool {
221198
self.goto_next_sibling_opt().is_some()
222199
}
223200

224-
fn goto_parent(&mut self) -> bool {
201+
pub fn goto_parent(&mut self) -> bool {
225202
self.goto_parent_opt().is_some()
226203
}
204+
205+
fn goto_next_sibling_opt(&mut self) -> Option<()> {
206+
self.node_id = self.parents.last_mut()?.1.next()?;
207+
Some(())
208+
}
209+
210+
fn goto_first_child_opt(&mut self) -> Option<()> {
211+
let parent_id = self.node_id;
212+
let parent = self.ast.get_node(parent_id)?;
213+
let mut children = ChildrenIter::new(parent);
214+
let first_child = children.next()?;
215+
self.node_id = first_child;
216+
self.parents.push((parent_id, children));
217+
Some(())
218+
}
219+
220+
fn goto_parent_opt(&mut self) -> Option<()> {
221+
self.node_id = self.parents.pop()?.0;
222+
Some(())
223+
}
227224
}
228225

229226
/// An iterator over the child Ids of a node.

0 commit comments

Comments
 (0)