Skip to content

Commit

Permalink
feat(rust): Scope attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 15, 2024
1 parent a910e82 commit b9c03a3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,7 @@ Language scopes:
- uses: Use statements (paths only; excl. `use`/`as`/`*`)
- strings: Strings (regular, raw, byte; includes interpolation parts in
format strings!)
- attribute: Attributes like `#[attr]`

--rust-query <TREE-SITTER-QUERY>
Scope Rust code using a custom tree-sitter query.
Expand Down
3 changes: 3 additions & 0 deletions src/scoping/langs/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum PreparedRustQuery {
/// There is currently no support for an 'interpolation' type node in
/// tree-sitter-rust (like there is in TypeScript and Python, for example).
Strings,
/// Attributes like `#[attr]`.
Attribute,
}

impl From<PreparedRustQuery> for TSQuery {
Expand Down Expand Up @@ -65,6 +67,7 @@ impl From<PreparedRustQuery> for TSQuery {
"
}
PreparedRustQuery::Strings => "(string_content) @string",
PreparedRustQuery::Attribute => "(attribute) @attribute",
},
)
.expect("Prepared queries to be valid")
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ impl InScopeLinePart {
include_str!("rust/base.rs"),
Rust::new(CodeQuery::Prepared(PreparedRustQuery::DocComments)),
)]
#[case(
"base.rs_attribute",
include_str!("rust/base.rs"),
Rust::new(CodeQuery::Prepared(PreparedRustQuery::Attribute)),
)]
#[case(
"base.tf_variable-block",
include_str!("hcl/base.tf"),
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/rust/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,8 @@ This is a raw string with no special treatment for \n
control_flow();
TestEnum::match_statement(TestEnum::VariantOne);
}

#[cfg(feature = "some-feature")]
#[allow(clippy::single_match_else)]
#[cfg(doc)]
fn function_with_attributes() {}
19 changes: 19 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 12
l: "#[macro_use]\n"
m: " ^^^^^^^^^ "
- n: 181
l: "#[tokio::main]\n"
m: " ^^^^^^^^^^^ "
- n: 237
l: "#[cfg(feature = \"some-feature\")]\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 238
l: "#[allow(clippy::single_match_else)]\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 239
l: "#[cfg(doc)]\n"
m: " ^^^^^^^^ "
3 changes: 3 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_strings.snap
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,6 @@ expression: inscope_parts
- n: 230
l: " println!(\"{}\", square(5));\n"
m: " ^^ "
- n: 237
l: "#[cfg(feature = \"some-feature\")]\n"
m: " ^^^^^^^^^^^^ "

0 comments on commit b9c03a3

Please sign in to comment.