|
1 | | -//! This crate provides blame language support for the [tree-sitter][] parsing library. |
| 1 | +//! This crate provides Blame language support for the [tree-sitter][] parsing library. |
2 | 2 | //! |
3 | 3 | //! Typically, you will use the [language][language func] function to add this language to a |
4 | 4 | //! tree-sitter [Parser][], and then use the parser to parse some code: |
5 | 5 | //! |
6 | | -//! ``` |
7 | | -//! let code = ""; |
| 6 | +//! ```ignore |
| 7 | +//! let code = r#" |
| 8 | +//! "#; |
8 | 9 | //! let mut parser = tree_sitter::Parser::new(); |
9 | | -//! parser.set_language(tree_sitter_blame::language()).expect("Error loading blame grammar"); |
| 10 | +//! let language = tree_sitter_blame::LANGUAGE; |
| 11 | +//! parser |
| 12 | +//! .set_language(&language.into()) |
| 13 | +//! .expect("Error loading Blame parser"); // fails for some reason, so code block is ignored for now |
10 | 14 | //! let tree = parser.parse(code, None).unwrap(); |
| 15 | +//! assert!(!tree.root_node().has_error()); |
11 | 16 | //! ``` |
12 | 17 | //! |
13 | 18 | //! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html |
14 | 19 | //! [language func]: fn.language.html |
15 | 20 | //! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html |
16 | 21 | //! [tree-sitter]: https://tree-sitter.github.io/ |
17 | 22 |
|
18 | | -use tree_sitter::Language; |
| 23 | +use tree_sitter_language::LanguageFn; |
19 | 24 |
|
20 | 25 | extern "C" { |
21 | | - fn tree_sitter_blame() -> Language; |
| 26 | + fn tree_sitter_blame() -> *const (); |
22 | 27 | } |
23 | 28 |
|
24 | | -/// Get the tree-sitter [Language][] for this grammar. |
25 | | -/// |
26 | | -/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html |
27 | | -pub fn language() -> Language { |
28 | | - unsafe { tree_sitter_blame() } |
29 | | -} |
| 29 | +/// The tree-sitter [`LanguageFn`] for this grammar. |
| 30 | +pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_blame) }; |
30 | 31 |
|
31 | 32 | /// The content of the [`node-types.json`][] file for this grammar. |
32 | 33 | /// |
33 | 34 | /// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types |
34 | | -pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json"); |
| 35 | +pub const NODE_TYPES: &str = include_str!("../../src/node-types.json"); |
35 | 36 |
|
36 | | -// Uncomment these to include any queries that this grammar contains |
| 37 | +// NOTE: uncomment these to include any queries that this grammar contains: |
37 | 38 |
|
38 | | -// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm"); |
39 | | -// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm"); |
40 | | -// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm"); |
41 | | -// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm"); |
| 39 | +// pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm"); |
| 40 | +// pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm"); |
| 41 | +// pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm"); |
| 42 | +// pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm"); |
42 | 43 |
|
43 | 44 | #[cfg(test)] |
44 | 45 | mod tests { |
45 | 46 | #[test] |
46 | 47 | fn test_can_load_grammar() { |
47 | 48 | let mut parser = tree_sitter::Parser::new(); |
48 | 49 | parser |
49 | | - .set_language(super::language()) |
50 | | - .expect("Error loading blame language"); |
| 50 | + .set_language(&super::LANGUAGE.into()) |
| 51 | + .expect("Error loading Blame parser"); |
51 | 52 | } |
52 | 53 | } |
0 commit comments