From dacbc6a69a5bd2ea7b4f5680d58659ec8ab4369b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 27 Dec 2021 21:33:24 +0300 Subject: [PATCH] move the rest of ssr parsing to fragments --- crates/ide_ssr/src/fragments.rs | 14 ++++++++++++ crates/ide_ssr/src/parsing.rs | 40 +++++++++------------------------ 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/crates/ide_ssr/src/fragments.rs b/crates/ide_ssr/src/fragments.rs index 4c768a970b1..dab214b4074 100644 --- a/crates/ide_ssr/src/fragments.rs +++ b/crates/ide_ssr/src/fragments.rs @@ -36,6 +36,20 @@ pub(crate) fn item(s: &str) -> Result { Ok(node.syntax().clone_subtree()) } +pub(crate) fn pat(s: &str) -> Result { + let template = "const _: () = {let {} = ();};"; + 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::Pat::cast).ok_or(())?; + if node.to_string() != s { + return Err(()); + } + Ok(node.syntax().clone_subtree()) +} + pub(crate) fn expr(s: &str) -> Result { let template = "const _: () = {};"; let input = template.replace("{}", s); diff --git a/crates/ide_ssr/src/parsing.rs b/crates/ide_ssr/src/parsing.rs index d70823403e6..aaaee576b52 100644 --- a/crates/ide_ssr/src/parsing.rs +++ b/crates/ide_ssr/src/parsing.rs @@ -4,12 +4,12 @@ //! placeholders, which start with `$`. For replacement templates, this is the final form. For //! search patterns, we go further and parse the pattern as each kind of thing that we can match. //! e.g. expressions, type references etc. +use rustc_hash::{FxHashMap, FxHashSet}; +use std::{fmt::Display, str::FromStr}; +use syntax::{SmolStr, SyntaxKind, SyntaxNode, T}; use crate::errors::bail; use crate::{fragments, SsrError, SsrPattern, SsrRule}; -use rustc_hash::{FxHashMap, FxHashSet}; -use std::{fmt::Display, str::FromStr}; -use syntax::{ast, AstNode, SmolStr, SyntaxKind, SyntaxNode, T}; #[derive(Debug)] pub(crate) struct ParsedRule { @@ -75,14 +75,14 @@ fn new( let raw_template_stmt = raw_template.map(fragments::stmt); if let raw_template_expr @ Some(Ok(_)) = raw_template.map(fragments::expr) { - builder.try_add2(fragments::expr(&raw_pattern), raw_template_expr); + builder.try_add(fragments::expr(&raw_pattern), raw_template_expr); } else { - builder.try_add2(fragments::expr(&raw_pattern), raw_template_stmt.clone()); + builder.try_add(fragments::expr(&raw_pattern), raw_template_stmt.clone()); } - builder.try_add2(fragments::ty(&raw_pattern), raw_template.map(fragments::ty)); - builder.try_add2(fragments::item(&raw_pattern), raw_template.map(fragments::item)); - builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse)); - builder.try_add2(fragments::stmt(&raw_pattern), raw_template_stmt); + builder.try_add(fragments::ty(&raw_pattern), raw_template.map(fragments::ty)); + builder.try_add(fragments::item(&raw_pattern), raw_template.map(fragments::item)); + builder.try_add(fragments::pat(&raw_pattern), raw_template.map(fragments::pat)); + builder.try_add(fragments::stmt(&raw_pattern), raw_template_stmt); builder.build() } } @@ -93,27 +93,7 @@ struct RuleBuilder { } impl RuleBuilder { - fn try_add( - &mut self, - pattern: Result, - template: Option>, - ) { - match (pattern, template) { - (Ok(pattern), Some(Ok(template))) => self.rules.push(ParsedRule { - placeholders_by_stand_in: self.placeholders_by_stand_in.clone(), - pattern: pattern.syntax().clone(), - template: Some(template.syntax().clone()), - }), - (Ok(pattern), None) => self.rules.push(ParsedRule { - placeholders_by_stand_in: self.placeholders_by_stand_in.clone(), - pattern: pattern.syntax().clone(), - template: None, - }), - _ => {} - } - } - - fn try_add2( + fn try_add( &mut self, pattern: Result, template: Option>,