Skip to content

Commit cfc8579

Browse files
committed
Documentation for search
1 parent bfa8c39 commit cfc8579

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ It is built with only standard library dependencies. It additionally ships with
1515
- [CLI](#cli)
1616
- [ast](#ast)
1717
- [check](#check)
18+
- [expr](#expr)
1819
- [format](#format)
1920
- [json](#json)
2021
- [match](#match)
@@ -26,6 +27,7 @@ It is built with only standard library dependencies. It additionally ships with
2627
- [SyntaxTree.read(filepath)](#syntaxtreereadfilepath)
2728
- [SyntaxTree.parse(source)](#syntaxtreeparsesource)
2829
- [SyntaxTree.format(source)](#syntaxtreeformatsource)
30+
- [SyntaxTree.search(source, query, &block)](#syntaxtreesearchsource-query-block)
2931
- [Nodes](#nodes)
3032
- [child_nodes](#child_nodes)
3133
- [Pattern matching](#pattern-matching)
@@ -129,6 +131,24 @@ To change the print width that you are checking against, specify the `--print-wi
129131
stree check --print-width=100 path/to/file.rb
130132
```
131133

134+
### expr
135+
136+
This command will output a Ruby case-match expression that would match correctly against the first expression of the input.
137+
138+
```sh
139+
stree expr path/to/file.rb
140+
```
141+
142+
For a file that contains `1 + 1`, you will receive:
143+
144+
```ruby
145+
SyntaxTree::Binary[
146+
left: SyntaxTree::Int[value: "1"],
147+
operator: :+,
148+
right: SyntaxTree::Int[value: "1"]
149+
]
150+
```
151+
132152
### format
133153

134154
This command will output the formatted version of each of the listed files. Importantly, it will not write that content back to the source files. It is meant to display the formatted version only.
@@ -312,6 +332,10 @@ This function takes an input string containing Ruby code and returns the syntax
312332

313333
This function takes an input string containing Ruby code, parses it into its underlying syntax tree, and formats it back out to a string. You can optionally pass a second argument to this method as well that is the maximum width to print. It defaults to `80`.
314334

335+
### SyntaxTree.search(source, query, &block)
336+
337+
This function takes an input string containing Ruby code, an input string containing a valid Ruby `in` clause expression that can be used to match against nodes in the tree (can be generated using `stree expr`, `stree match`, or `Node#construct_keys`), and a block. Each node that matches the given query will be yielded to the block. The block will receive the node as its only argument.
338+
315339
## Nodes
316340

317341
There are many different node types in the syntax tree. They are meant to be treated as immutable structs containing links to child nodes with minimal logic contained within their implementation. However, for the most part they all respond to a certain set of APIs, listed below.

lib/syntax_tree.rb

+6
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,10 @@ def self.read(filepath)
7575

7676
File.read(filepath, encoding: encoding)
7777
end
78+
79+
# Searches through the given source using the given pattern and yields each
80+
# node in the tree that matches the pattern to the given block.
81+
def self.search(source, query, &block)
82+
Search.new(Pattern.new(query).compile).scan(parse(source), &block)
83+
end
7884
end

0 commit comments

Comments
 (0)