mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 10:50:00 +00:00
Make the extraction of slint!
macro as part of the compiler core
This includes slint-viewer, slint-interpreter (when loading path, not string), slint-compiler. (This would also include internal things such as `import { Xxx } from "foo.rs"`, if we didn't check for .slint or .60 extension before) But that doesn't include anything that's not opening the source by path (so not the lsp which use its own representation coming from the editor, or varius tools like the updater and fmt which also open the files themselves)
This commit is contained in:
parent
1c033e7d72
commit
cb840660aa
5 changed files with 79 additions and 61 deletions
|
@ -408,3 +408,64 @@ fn test_locate_rust_macro() {
|
|||
);
|
||||
do_test("slint!(slint!(abc))slint!()", &["slint!(abc)", ""]);
|
||||
}
|
||||
|
||||
/// Given a Rust source file contents, return a string containing the contents of the first `slint!` macro
|
||||
///
|
||||
/// All the other bytes which are not newlines are replaced by space. This allow offsets in the resulting
|
||||
/// string to preserve line and column number.
|
||||
pub fn extract_rust_macro(rust_source: String) -> Option<String> {
|
||||
let core::ops::Range { start, end } = locate_slint_macro(&rust_source).next()?;
|
||||
let mut bytes = rust_source.into_bytes();
|
||||
for c in &mut bytes[..start] {
|
||||
if *c != b'\n' {
|
||||
*c = b' '
|
||||
}
|
||||
}
|
||||
for c in &mut bytes[end..] {
|
||||
if *c != b'\n' {
|
||||
*c = b' '
|
||||
}
|
||||
}
|
||||
Some(String::from_utf8(bytes).expect("We just added spaces"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_rust_macro() {
|
||||
assert_eq!(extract_rust_macro("\nslint{!{}}".into()), None);
|
||||
assert_eq!(
|
||||
extract_rust_macro(
|
||||
"abc\n€\nslint ! {x \" \\\" }🦀\" { () {}\n {} }xx =}- ;}\n xxx \n yyy {}\n".into(),
|
||||
),
|
||||
Some(
|
||||
" \n \n x \" \\\" }🦀\" { () {}\n {} }xx = \n \n \n".into(),
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
extract_rust_macro("xx\nabcd::slint!{abc{}efg".into()),
|
||||
Some(" \n abc{}efg".into())
|
||||
);
|
||||
assert_eq!(
|
||||
extract_rust_macro("slint!\nnot.\nslint!{\nunterminated\nxxx".into()),
|
||||
Some(" \n \n \nunterminated\nxxx".into())
|
||||
);
|
||||
assert_eq!(extract_rust_macro("foo\n/* slint! { hello }\n".into()), None);
|
||||
assert_eq!(extract_rust_macro("foo\n/* slint::slint! { hello }\n".into()), None);
|
||||
assert_eq!(
|
||||
extract_rust_macro("foo\n// slint! { hello }\nslint!{world}\na".into()),
|
||||
Some(" \n \n world \n ".into())
|
||||
);
|
||||
assert_eq!(extract_rust_macro("foo\n\" slint! { hello }\"\n".into()), None);
|
||||
assert_eq!(
|
||||
extract_rust_macro(
|
||||
"abc\n€\nslint ! (x /* \\\" )🦀*/ { () {}\n {} }xx =)- ;}\n xxx \n yyy {}\n".into(),
|
||||
),
|
||||
Some(
|
||||
" \n \n x /* \\\" )🦀*/ { () {}\n {} }xx = \n \n \n".into(),
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
extract_rust_macro("abc slint![x slint!() [{[]}] s] abc".into()),
|
||||
Some(" x slint!() [{[]}] s ".into()),
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue