Skip to content

Commit ea96c37

Browse files
committed
compress
1 parent 660cf34 commit ea96c37

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

crates/ide_ssr/src/fragments.rs

+18-44
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,19 @@
99
use syntax::{ast, AstNode, SyntaxNode};
1010

1111
pub(crate) fn ty(s: &str) -> Result<SyntaxNode, ()> {
12-
let template = "type T = {};";
13-
let input = template.replace("{}", s);
14-
let parse = syntax::SourceFile::parse(&input);
15-
if !parse.errors().is_empty() {
16-
return Err(());
17-
}
18-
let node = parse.tree().syntax().descendants().find_map(ast::Type::cast).ok_or(())?;
19-
if node.to_string() != s {
20-
return Err(());
21-
}
22-
Ok(node.syntax().clone_subtree())
12+
fragment::<ast::Type>("type T = {};", s)
2313
}
2414

2515
pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
26-
let template = "{}";
27-
let input = template.replace("{}", s);
28-
let parse = syntax::SourceFile::parse(&input);
29-
if !parse.errors().is_empty() {
30-
return Err(());
31-
}
32-
let node = parse.tree().syntax().descendants().find_map(ast::Item::cast).ok_or(())?;
33-
if node.to_string() != s {
34-
return Err(());
35-
}
36-
Ok(node.syntax().clone_subtree())
16+
fragment::<ast::Item>("{}", s)
3717
}
3818

3919
pub(crate) fn pat(s: &str) -> Result<SyntaxNode, ()> {
40-
let template = "const _: () = {let {} = ();};";
41-
let input = template.replace("{}", s);
42-
let parse = syntax::SourceFile::parse(&input);
43-
if !parse.errors().is_empty() {
44-
return Err(());
45-
}
46-
let node = parse.tree().syntax().descendants().find_map(ast::Pat::cast).ok_or(())?;
47-
if node.to_string() != s {
48-
return Err(());
49-
}
50-
Ok(node.syntax().clone_subtree())
20+
fragment::<ast::Pat>("const _: () = {let {} = ();};", s)
5121
}
5222

5323
pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
54-
let template = "const _: () = {};";
55-
let input = template.replace("{}", s);
56-
let parse = syntax::SourceFile::parse(&input);
57-
if !parse.errors().is_empty() {
58-
return Err(());
59-
}
60-
let node = parse.tree().syntax().descendants().find_map(ast::Expr::cast).ok_or(())?;
61-
if node.to_string() != s {
62-
return Err(());
63-
}
64-
Ok(node.syntax().clone_subtree())
24+
fragment::<ast::Expr>("const _: () = {};", s)
6525
}
6626

6727
pub(crate) fn stmt(s: &str) -> Result<SyntaxNode, ()> {
@@ -82,3 +42,17 @@ pub(crate) fn stmt(s: &str) -> Result<SyntaxNode, ()> {
8242
}
8343
Ok(node.syntax().clone_subtree())
8444
}
45+
46+
fn fragment<T: AstNode>(template: &str, s: &str) -> Result<SyntaxNode, ()> {
47+
let s = s.trim();
48+
let input = template.replace("{}", s);
49+
let parse = syntax::SourceFile::parse(&input);
50+
if !parse.errors().is_empty() {
51+
return Err(());
52+
}
53+
let node = parse.tree().syntax().descendants().find_map(T::cast).ok_or(())?;
54+
if node.syntax().text() != s {
55+
return Err(());
56+
}
57+
Ok(node.syntax().clone_subtree())
58+
}

0 commit comments

Comments
 (0)