|
13 | 13 | # Copyright, 2022, by Motonori Iwamuro. |
14 | 14 | # Copyright, 2023-2025, by Charlie Savage. |
15 | 15 |
|
| 16 | +require "set" |
16 | 17 | require_relative "lib/cursor" |
17 | 18 | require_relative "lib/code_completion" |
18 | 19 |
|
@@ -477,20 +478,21 @@ def ancestors_by_kind(*kinds) |
477 | 478 | # Find child cursors by kind. |
478 | 479 | # @parameter recurse [Boolean | Nil] Whether to recurse into children. |
479 | 480 | # @parameter kinds [Array(Symbol)] The cursor kinds to search for. |
480 | | - # @returns [Array(Cursor)] Array of matching cursors. |
| 481 | + # @yields {|cursor| ...} Each matching cursor if a block is given. |
| 482 | + # @returns [Enumerator] If no block is given. |
481 | 483 | # @raises [RuntimeError] If recurse parameter is not nil or boolean. |
482 | | - def find_by_kind(recurse, *kinds) |
| 484 | + def find_by_kind(recurse, *kinds, &block) |
483 | 485 | unless (recurse == nil || recurse == true || recurse == false) |
484 | 486 | raise("Recurse parameter must be nil or a boolean value. Value was: #{recurse}") |
485 | 487 | end |
486 | 488 |
|
487 | | - result = Array.new |
| 489 | + return enum_for(__method__, recurse, *kinds) unless block_given? |
| 490 | + |
| 491 | + kinds_set = kinds.to_set |
| 492 | + |
488 | 493 | self.each(recurse) do |child, parent| |
489 | | - if kinds.include?(child.kind) |
490 | | - result << child |
491 | | - end |
| 494 | + yield child if kinds_set.include?(child.kind) |
492 | 495 | end |
493 | | - result |
494 | 496 | end |
495 | 497 |
|
496 | 498 | # Find all references to this cursor in a file. |
|
0 commit comments