fix(rust): update README example

This commit is contained in:
Jisuk Byun 2024-04-19 02:25:42 +09:00 committed by GitHub
parent 01bf431261
commit 4cd23ff6b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,7 +12,7 @@ Rust bindings to the [Tree-sitter][] parsing library.
First, create a parser:
```rust
use tree_sitter::{Parser, Language};
use tree_sitter::{InputEdit, Language, Parser, Point};
let mut parser = Parser::new();
```
@ -28,21 +28,21 @@ Then, add a language as a dependency:
```toml
[dependencies]
tree-sitter = "0.21.0"
tree-sitter-rust = "0.20.4"
tree-sitter = "0.22"
tree-sitter-rust = "0.21"
```
To then use a language, you assign them to the parser.
```rust
parser.set_language(tree_sitter_rust::language()).expect("Error loading Rust grammar");
parser.set_language(&tree_sitter_rust::language()).expect("Error loading Rust grammar");
```
Now you can parse source code:
```rust
let source_code = "fn test() {}";
let tree = parser.parse(source_code, None).unwrap();
let mut tree = parser.parse(source_code, None).unwrap();
let root_node = tree.root_node();
assert_eq!(root_node.kind(), "source_file");
@ -56,7 +56,7 @@ Once you have a syntax tree, you can update it when your source code changes.
Passing in the previous edited tree makes `parse` run much more quickly:
```rust
let new_source_code = "fn test(a: u32) {}"
let new_source_code = "fn test(a: u32) {}";
tree.edit(&InputEdit {
start_byte: 8,
@ -85,7 +85,7 @@ let lines = &[
// Parse the source code using a custom callback. The callback is called
// with both a byte offset and a row/column offset.
let tree = parser.parse_with(&mut |_byte: u32, position: Point| -> &[u8] {
let tree = parser.parse_with(&mut |_byte: usize, position: Point| -> &[u8] {
let row = position.row as usize;
let column = position.column as usize;
if row < lines.len() {