-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Rust language (comments, doc comments, strings)
- Loading branch information
Showing
14 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use super::{CodeQuery, Language, LanguageScoper, TSLanguage, TSQuery}; | ||
use crate::scoping::{ROScopes, Scoper}; | ||
use clap::ValueEnum; | ||
use std::{fmt::Debug, str::FromStr}; | ||
use tree_sitter::QueryError; | ||
|
||
/// The Rust language. | ||
pub type Rust = Language<RustQuery>; | ||
/// A query for Rust. | ||
pub type RustQuery = CodeQuery<CustomRustQuery, PremadeRustQuery>; | ||
|
||
/// Premade tree-sitter queries for Rust. | ||
#[derive(Debug, Clone, Copy, ValueEnum)] | ||
pub enum PremadeRustQuery { | ||
/// Comments (line and block styles; excluding doc comments; comment chars incl.). | ||
Comments, | ||
/// Doc comments (comment chars included). | ||
DocComments, | ||
/// Strings (regular, raw, byte; includes interpolation parts in format strings!). | ||
/// | ||
/// There is currently no support for an 'interpolation' type node in | ||
/// tree-sitter-rust (like there is in TypeScript and Python, for example). | ||
Strings, | ||
} | ||
|
||
impl From<PremadeRustQuery> for TSQuery { | ||
fn from(value: PremadeRustQuery) -> Self { | ||
TSQuery::new( | ||
Rust::lang(), | ||
match value { | ||
PremadeRustQuery::Comments => { | ||
r#" | ||
[ | ||
(line_comment)+ @line | ||
(block_comment) | ||
(#not-match? @line "^///") | ||
] | ||
@comment | ||
"# | ||
} | ||
PremadeRustQuery::DocComments => { | ||
r#" | ||
( | ||
(line_comment)+ @line | ||
(#match? @line "^///") | ||
) | ||
"# | ||
} | ||
PremadeRustQuery::Strings => { | ||
r#" | ||
[ | ||
(string_literal) | ||
(raw_string_literal) | ||
] | ||
@string | ||
"# | ||
} | ||
}, | ||
) | ||
.expect("Premade queries to be valid") | ||
} | ||
} | ||
|
||
/// A custom tree-sitter query for Rust. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct CustomRustQuery(String); | ||
|
||
impl FromStr for CustomRustQuery { | ||
type Err = QueryError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match TSQuery::new(Rust::lang(), s) { | ||
Ok(_) => Ok(Self(s.to_string())), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} | ||
|
||
impl From<CustomRustQuery> for TSQuery { | ||
fn from(value: CustomRustQuery) -> Self { | ||
TSQuery::new(Rust::lang(), &value.0) | ||
.expect("Valid query, as object cannot be constructed otherwise") | ||
} | ||
} | ||
|
||
impl Scoper for Rust { | ||
fn scope<'viewee>(&self, input: &'viewee str) -> ROScopes<'viewee> { | ||
ROScopes::from_raw_ranges(input, Self::scope_via_query(&mut self.query(), input)) | ||
} | ||
} | ||
|
||
impl LanguageScoper for Rust { | ||
fn lang() -> TSLanguage { | ||
tree_sitter_rust::language() | ||
} | ||
|
||
fn query(&self) -> TSQuery { | ||
self.query.clone().into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod csharp; | ||
mod go; | ||
mod python; | ||
mod rust; | ||
mod typescript; | ||
|
||
use std::{fs::read_to_string, path::Path}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// __T__A doc comment. | ||
/// | ||
/// More context.__T__ | ||
/// | ||
/// ## Some section | ||
/// | ||
/// Some context.__T__ | ||
pub fn doc__T__comment(left__T__: u8, right: u8) { | ||
// This is a normal__T__ comment. | ||
// Another line__T__ for the comment. | ||
let _ = left__T__ + right; | ||
|
||
/* | ||
This is a__T__ block comment. | ||
It also has multiple lines.__T__ | ||
*/ | ||
let _ = left__T__ - right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// __T__A doc comment. | ||
/// | ||
/// More context.__T__ | ||
/// | ||
/// ## Some section | ||
/// | ||
/// Some context.__T__ | ||
pub fn doc__T__comment(left__T__: u8, right: u8) { | ||
// This is a normal__T__ comment. | ||
// Another line__T__ for the comment. | ||
let _ = left__T__ + right; | ||
|
||
/* | ||
This is a__T__ block comment. | ||
It also has multiple lines.__T__ | ||
*/ | ||
let _ = left__T__ - right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
fn main() { | ||
let regular = "Hello,__T__ world!"; | ||
|
||
let raw = r#"This is__T__ a raw string: \n won't escape__T__"#; | ||
|
||
let byte_str = b"byte string__T__"; | ||
|
||
let byte_literal = br#"Byte string__T__ literal"#; | ||
|
||
let value = 10; | ||
let formatted = format!("Va__T__lue: __T__{}__T__", value); | ||
|
||
let value__T__ = 10; | ||
let formatted = format!("Va__T__lue: __T__{}__T__", value__T__); | ||
|
||
let value__T__ = 10; | ||
// CAUTION: This is nuked incorrectly; tree-sitter doesn't have an "interpolation" | ||
// node for Rust like has for typescript, for example. | ||
let formatted = format!("Va__T__lue: __T__{value__T__}__T__"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use rstest::rstest; | ||
use srgn::scoping::langs::rust::{PremadeRustQuery, Rust, RustQuery}; | ||
|
||
use super::{get_input_output, nuke_target}; | ||
|
||
#[rstest] | ||
#[case("comments.rs", RustQuery::Premade(PremadeRustQuery::Comments))] | ||
#[case("doc-comments.rs", RustQuery::Premade(PremadeRustQuery::DocComments))] | ||
#[case("strings.rs", RustQuery::Premade(PremadeRustQuery::Strings))] | ||
fn test_rust_nuke(#[case] file: &str, #[case] query: RustQuery) { | ||
let lang = Rust::new(query); | ||
|
||
let (input, output) = get_input_output("rust", file); | ||
let result = nuke_target(&input, &lang); | ||
|
||
assert_eq!(result, output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// __T__A doc comment. | ||
/// | ||
/// More context.__T__ | ||
/// | ||
/// ## Some section | ||
/// | ||
/// Some context.__T__ | ||
pub fn doc__T__comment(left__T__: u8, right: u8) { | ||
// This is a normal comment. | ||
// Another line for the comment. | ||
let _ = left__T__ + right; | ||
|
||
/* | ||
This is a block comment. | ||
It also has multiple lines. | ||
*/ | ||
let _ = left__T__ - right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// A doc comment. | ||
/// | ||
/// More context. | ||
/// | ||
/// ## Some section | ||
/// | ||
/// Some context. | ||
pub fn doc__T__comment(left__T__: u8, right: u8) { | ||
// This is a normal__T__ comment. | ||
// Another line__T__ for the comment. | ||
let _ = left__T__ + right; | ||
|
||
/* | ||
This is a__T__ block comment. | ||
It also has multiple lines.__T__ | ||
*/ | ||
let _ = left__T__ - right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
fn main() { | ||
let regular = "Hello, world!"; | ||
|
||
let raw = r#"This is a raw string: \n won't escape"#; | ||
|
||
let byte_str = b"byte string"; | ||
|
||
let byte_literal = br#"Byte string literal"#; | ||
|
||
let value = 10; | ||
let formatted = format!("Value: {}", value); | ||
|
||
let value__T__ = 10; | ||
let formatted = format!("Value: {}", value__T__); | ||
|
||
let value__T__ = 10; | ||
// CAUTION: This is nuked incorrectly; tree-sitter doesn't have an "interpolation" | ||
// node for Rust like has for typescript, for example. | ||
let formatted = format!("Value: {value}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters