From b9c03a3484c5bd4eaaf27cbc03c75afefcb65ab7 Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Thu, 15 Aug 2024 16:15:56 +0200 Subject: [PATCH] feat(rust): Scope attributes --- README.md | 1 + src/scoping/langs/rust.rs | 3 +++ tests/langs/mod.rs | 5 +++++ tests/langs/rust/base.rs | 5 +++++ .../r#mod__langs__base.rs_attribute.snap | 19 +++++++++++++++++++ .../r#mod__langs__base.rs_strings.snap | 3 +++ 6 files changed, 36 insertions(+) create mode 100644 tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap diff --git a/README.md b/README.md index 55eb158c..7958f3ac 100644 --- a/README.md +++ b/README.md @@ -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 Scope Rust code using a custom tree-sitter query. diff --git a/src/scoping/langs/rust.rs b/src/scoping/langs/rust.rs index b6e7f3b1..8f683a6a 100644 --- a/src/scoping/langs/rust.rs +++ b/src/scoping/langs/rust.rs @@ -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 for TSQuery { @@ -65,6 +67,7 @@ impl From for TSQuery { " } PreparedRustQuery::Strings => "(string_content) @string", + PreparedRustQuery::Attribute => "(attribute) @attribute", }, ) .expect("Prepared queries to be valid") diff --git a/tests/langs/mod.rs b/tests/langs/mod.rs index ec615bfc..efef6af2 100644 --- a/tests/langs/mod.rs +++ b/tests/langs/mod.rs @@ -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"), diff --git a/tests/langs/rust/base.rs b/tests/langs/rust/base.rs index cf52312b..5a59feaa 100644 --- a/tests/langs/rust/base.rs +++ b/tests/langs/rust/base.rs @@ -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() {} diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap b/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap new file mode 100644 index 00000000..37c1a086 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap @@ -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: " ^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap b/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap index b5d6ca6c..8e22aa73 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap @@ -125,3 +125,6 @@ expression: inscope_parts - n: 230 l: " println!(\"{}\", square(5));\n" m: " ^^ " +- n: 237 + l: "#[cfg(feature = \"some-feature\")]\n" + m: " ^^^^^^^^^^^^ "