From f0fefde4017fa077a018c4ed03a66b5c6970fb8a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 27 Dec 2021 20:33:33 +0300 Subject: [PATCH] remove Item::parse --- crates/ide_ssr/src/fragments.rs | 11 +++++++++++ crates/ide_ssr/src/parsing.rs | 2 +- crates/ide_ssr/src/replacing.rs | 8 +++++--- crates/parser/src/lib.rs | 2 -- crates/syntax/src/lib.rs | 7 ------- crates/syntax/src/tests.rs | 9 --------- 6 files changed, 17 insertions(+), 22 deletions(-) diff --git a/crates/ide_ssr/src/fragments.rs b/crates/ide_ssr/src/fragments.rs index 0abf9e4d98c..5eaad8b1d53 100644 --- a/crates/ide_ssr/src/fragments.rs +++ b/crates/ide_ssr/src/fragments.rs @@ -19,3 +19,14 @@ pub(crate) fn ty(s: &str) -> Result { let node = parse.tree().syntax().descendants().find_map(ast::Type::cast).ok_or(())?; Ok(node.syntax().clone()) } + +pub(crate) fn item(s: &str) -> Result { + let template = "{}"; + let input = template.replace("{}", s); + let parse = syntax::SourceFile::parse(&input); + if !parse.errors().is_empty() { + return Err(()); + } + let node = parse.tree().syntax().descendants().find_map(ast::Item::cast).ok_or(())?; + Ok(node.syntax().clone()) +} diff --git a/crates/ide_ssr/src/parsing.rs b/crates/ide_ssr/src/parsing.rs index b32efe879a4..1d5633cfe00 100644 --- a/crates/ide_ssr/src/parsing.rs +++ b/crates/ide_ssr/src/parsing.rs @@ -80,7 +80,7 @@ impl ParsedRule { builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_stmt.clone()); } builder.try_add2(fragments::ty(&raw_pattern), raw_template.map(fragments::ty)); - builder.try_add(ast::Item::parse(&raw_pattern), raw_template.map(ast::Item::parse)); + builder.try_add2(fragments::item(&raw_pattern), raw_template.map(fragments::item)); builder.try_add(ast::Path::parse(&raw_pattern), raw_template.map(ast::Path::parse)); builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse)); builder.try_add(ast::Stmt::parse(&raw_pattern), raw_template_stmt); diff --git a/crates/ide_ssr/src/replacing.rs b/crates/ide_ssr/src/replacing.rs index 9265af7c13a..1c4cb6bc770 100644 --- a/crates/ide_ssr/src/replacing.rs +++ b/crates/ide_ssr/src/replacing.rs @@ -1,5 +1,6 @@ //! Code for applying replacement templates for matches that have previously been found. +use crate::fragments; use crate::{resolving::ResolvedRule, Match, SsrMatches}; use itertools::Itertools; use rustc_hash::{FxHashMap, FxHashSet}; @@ -228,9 +229,10 @@ fn parse_as_kind(code: &str, kind: SyntaxKind) -> Option { if let Ok(expr) = ast::Expr::parse(code) { return Some(expr.syntax().clone()); } - } else if ast::Item::can_cast(kind) { - if let Ok(item) = ast::Item::parse(code) { - return Some(item.syntax().clone()); + } + if ast::Item::can_cast(kind) { + if let Ok(item) = fragments::item(code) { + return Some(item); } } None diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index fb5280d1977..da62590ab66 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -142,7 +142,6 @@ pub enum ParserEntryPoint { Expr, StatementOptionalSemi, Pattern, - Item, Attr, } @@ -164,7 +163,6 @@ pub fn parse(inp: &Input, entry_point: ParserEntryPoint) -> Output { ParserEntryPoint::Path => grammar::entry::prefix::path, ParserEntryPoint::Expr => grammar::entry::prefix::expr, ParserEntryPoint::Pattern => grammar::entry::prefix::pat, - ParserEntryPoint::Item => grammar::entry::prefix::item, ParserEntryPoint::StatementOptionalSemi => grammar::entry_points::stmt_optional_semi, ParserEntryPoint::Attr => grammar::entry_points::attr, }; diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index b82df661624..a495e4aff17 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -194,13 +194,6 @@ impl ast::Expr { } } -impl ast::Item { - /// Returns `text`, parsed as an item, but only if it has no errors. - pub fn parse(text: &str) -> Result { - parsing::parse_text_as(text, parser::ParserEntryPoint::Item) - } -} - impl ast::Attr { /// Returns `text`, parsed as an attribute, but only if it has no errors. pub fn parse(text: &str) -> Result { diff --git a/crates/syntax/src/tests.rs b/crates/syntax/src/tests.rs index a632d0e6309..a1f35aab680 100644 --- a/crates/syntax/src/tests.rs +++ b/crates/syntax/src/tests.rs @@ -86,15 +86,6 @@ fn pattern_parser_tests() { ); } -#[test] -fn item_parser_tests() { - fragment_parser_dir_test( - &["parser/fragments/item/ok"], - &["parser/fragments/item/err"], - crate::ast::Item::parse, - ); -} - #[test] fn stmt_parser_tests() { fragment_parser_dir_test(