mirror of
https://github.com/github/codeql.git
synced 2026-06-30 00:55:29 +02:00
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>
This commit is contained in:
@@ -882,7 +882,6 @@ fn emit_extras_in(visitor: &mut Visitor, node: Node<'_>) {
|
||||
}
|
||||
|
||||
fn traverse_yeast(tree: &yeast::Ast, visitor: &mut Visitor) {
|
||||
use yeast::Cursor;
|
||||
let mut cursor = tree.walk();
|
||||
visitor.enter_node(cursor.node());
|
||||
let mut recurse = true;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
pub trait Cursor<'a, T, N, F> {
|
||||
fn node(&self) -> &'a N;
|
||||
fn field_id(&self) -> Option<F>;
|
||||
fn field_name(&self) -> Option<&'static str>;
|
||||
fn goto_first_child(&mut self) -> bool;
|
||||
fn goto_next_sibling(&mut self) -> bool;
|
||||
fn goto_parent(&mut self) -> bool;
|
||||
}
|
||||
@@ -7,7 +7,6 @@ use serde_json::{json, Value};
|
||||
|
||||
pub mod build;
|
||||
pub mod captures;
|
||||
pub mod cursor;
|
||||
pub mod dump;
|
||||
pub mod node_types_yaml;
|
||||
pub mod query;
|
||||
@@ -19,7 +18,6 @@ mod visitor;
|
||||
pub use yeast_macros::{query, rule, tree, trees};
|
||||
|
||||
use captures::Captures;
|
||||
pub use cursor::Cursor;
|
||||
use query::QueryNode;
|
||||
|
||||
/// Node id: an index into the [`Ast`] arena. A newtype around `usize`
|
||||
@@ -174,6 +172,36 @@ impl<'a> AstCursor<'a> {
|
||||
self.node_id
|
||||
}
|
||||
|
||||
pub fn node(&self) -> &'a Node {
|
||||
&self.ast.nodes[self.node_id.0]
|
||||
}
|
||||
|
||||
pub fn field_id(&self) -> Option<FieldId> {
|
||||
let (_, children) = self.parents.last()?;
|
||||
children.current_field()
|
||||
}
|
||||
|
||||
pub fn field_name(&self) -> Option<&'static str> {
|
||||
if self.field_id() == Some(CHILD_FIELD) {
|
||||
None
|
||||
} else {
|
||||
self.field_id()
|
||||
.and_then(|id| self.ast.field_name_for_id(id))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn goto_first_child(&mut self) -> bool {
|
||||
self.goto_first_child_opt().is_some()
|
||||
}
|
||||
|
||||
pub fn goto_next_sibling(&mut self) -> bool {
|
||||
self.goto_next_sibling_opt().is_some()
|
||||
}
|
||||
|
||||
pub fn goto_parent(&mut self) -> bool {
|
||||
self.goto_parent_opt().is_some()
|
||||
}
|
||||
|
||||
fn goto_next_sibling_opt(&mut self) -> Option<()> {
|
||||
self.node_id = self.parents.last_mut()?.1.next()?;
|
||||
Some(())
|
||||
@@ -194,37 +222,6 @@ impl<'a> AstCursor<'a> {
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
impl<'a> Cursor<'a, Ast, Node, FieldId> for AstCursor<'a> {
|
||||
fn node(&self) -> &'a Node {
|
||||
&self.ast.nodes[self.node_id.0]
|
||||
}
|
||||
|
||||
fn field_id(&self) -> Option<FieldId> {
|
||||
let (_, children) = self.parents.last()?;
|
||||
children.current_field()
|
||||
}
|
||||
|
||||
fn field_name(&self) -> Option<&'static str> {
|
||||
if self.field_id() == Some(CHILD_FIELD) {
|
||||
None
|
||||
} else {
|
||||
self.field_id()
|
||||
.and_then(|id| self.ast.field_name_for_id(id))
|
||||
}
|
||||
}
|
||||
|
||||
fn goto_first_child(&mut self) -> bool {
|
||||
self.goto_first_child_opt().is_some()
|
||||
}
|
||||
|
||||
fn goto_next_sibling(&mut self) -> bool {
|
||||
self.goto_next_sibling_opt().is_some()
|
||||
}
|
||||
|
||||
fn goto_parent(&mut self) -> bool {
|
||||
self.goto_parent_opt().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over the child Ids of a node.
|
||||
#[derive(Debug)]
|
||||
|
||||
Reference in New Issue
Block a user