Skip to content

Commit b9b757e

Browse files
committed
Extract arguments() iterator from arguments_values()
1 parent fa9a13c commit b9b757e

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

crates/ark/src/lsp/traits/node.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ pub trait NodeExt: Sized {
9292
fn ancestors(&self) -> impl Iterator<Item = Self>;
9393
fn children_of(node: Self) -> impl Iterator<Item = Self>;
9494
fn next_siblings(&self) -> impl Iterator<Item = Self>;
95+
fn arguments(&self) -> impl Iterator<Item = (Option<Self>, Option<Self>)>;
9596
fn arguments_values(&self) -> impl Iterator<Item = Self>;
97+
fn arguments_names(&self) -> impl Iterator<Item = Self>;
9698
}
9799

98100
impl<'tree> NodeExt for Node<'tree> {
@@ -249,21 +251,37 @@ impl<'tree> NodeExt for Node<'tree> {
249251
})
250252
}
251253

252-
/// Takes a call node and iterates over the values of its arguments
253-
fn arguments_values(&self) -> impl Iterator<Item = Node<'tree>> {
254+
/// Iterator over argument names and values. Either of `name` and `value`
255+
/// may be absent, but not both.
256+
fn arguments(&self) -> impl Iterator<Item = (Option<Node<'tree>>, Option<Node<'tree>>)> {
254257
self.child_by_field_name("arguments")
255258
// Create iterator that unpacks Option with `flat_map()`
256259
.into_iter()
257260
.flat_map(Self::children_of)
258261
.filter_map(|node| {
259-
// This takes care of non-argument nodes like `(` and `)`
260-
if node.kind() == "argument" {
261-
node.child_by_field_name("value")
262-
} else {
263-
None
262+
if node.kind() != "argument" {
263+
return None;
264264
}
265+
266+
let name = node.child_by_field_name("name");
267+
let value = node.child_by_field_name("value");
268+
269+
// Likely not possible but just in case
270+
if value.is_none() && name.is_none() {
271+
return None;
272+
}
273+
274+
Some((name, value))
265275
})
266276
}
277+
278+
fn arguments_names(&self) -> impl Iterator<Item = Node<'tree>> {
279+
self.arguments().filter_map(|(name, _value)| name)
280+
}
281+
282+
fn arguments_values(&self) -> impl Iterator<Item = Node<'tree>> {
283+
self.arguments().filter_map(|(_name, value)| value)
284+
}
267285
}
268286

269287
/// First, recurse through children to find the smallest

0 commit comments

Comments
 (0)