From b150965ed7994c679711bc807de301a12f5c7944 Mon Sep 17 00:00:00 2001 From: Mikhail Modin Date: Sun, 15 Mar 2020 21:23:18 +0000 Subject: Swtches to rust SSR query check --- crates/ra_ide/src/lib.rs | 3 ++- crates/ra_ide/src/ssr.rs | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index c60e86aea..40276d4fe 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -478,9 +478,10 @@ impl Analysis { pub fn structural_search_replace( &self, query: &str, + parse_only: bool, ) -> Cancelable> { self.with_db(|db| { - let edits = ssr::parse_search_replace(query, db)?; + let edits = ssr::parse_search_replace(query, parse_only, db)?; Ok(SourceChange::source_file_edits("ssr", edits)) }) } diff --git a/crates/ra_ide/src/ssr.rs b/crates/ra_ide/src/ssr.rs index c011a2e74..1c9710a5d 100644 --- a/crates/ra_ide/src/ssr.rs +++ b/crates/ra_ide/src/ssr.rs @@ -1,8 +1,10 @@ //! structural search replace use crate::source_change::SourceFileEdit; +use ra_db::{SourceDatabase, SourceDatabaseExt}; +use ra_ide_db::symbol_index::SymbolsDatabase; use ra_ide_db::RootDatabase; -use ra_syntax::ast::make::expr_from_text; +use ra_syntax::ast::make::try_expr_from_text; use ra_syntax::ast::{AstToken, Comment}; use ra_syntax::{AstNode, SyntaxElement, SyntaxNode}; use ra_text_edit::{TextEdit, TextEditBuilder}; @@ -10,9 +12,6 @@ use rustc_hash::FxHashMap; use std::collections::HashMap; use std::str::FromStr; -pub use ra_db::{SourceDatabase, SourceDatabaseExt}; -use ra_ide_db::symbol_index::SymbolsDatabase; - #[derive(Debug, PartialEq)] pub struct SsrError(String); @@ -26,14 +25,17 @@ impl std::error::Error for SsrError {} pub fn parse_search_replace( query: &str, + parse_only: bool, db: &RootDatabase, ) -> Result, SsrError> { let mut edits = vec![]; let query: SsrQuery = query.parse()?; + if parse_only { + return Ok(edits); + } for &root in db.local_roots().iter() { let sr = db.source_root(root); for file_id in sr.walk() { - dbg!(db.file_relative_path(file_id)); let matches = find(&query.pattern, db.parse(file_id).tree().syntax()); if !matches.matches.is_empty() { edits.push(SourceFileEdit { file_id, edit: replace(&matches, &query.template) }); @@ -106,7 +108,10 @@ impl FromStr for SsrQuery { template = replace_in_template(template, var, new_var); } - let template = expr_from_text(&template).syntax().clone(); + let template = try_expr_from_text(&template) + .ok_or(SsrError("Template is not an expression".into()))? + .syntax() + .clone(); let mut placeholders = FxHashMap::default(); traverse(&template, &mut |n| { @@ -118,7 +123,13 @@ impl FromStr for SsrQuery { } }); - let pattern = SsrPattern { pattern: expr_from_text(&pattern).syntax().clone(), vars }; + let pattern = SsrPattern { + pattern: try_expr_from_text(&pattern) + .ok_or(SsrError("Pattern is not an expression".into()))? + .syntax() + .clone(), + vars, + }; let template = SsrTemplate { template, placeholders }; Ok(SsrQuery { pattern, template }) } @@ -284,7 +295,6 @@ mod tests { assert_eq!(result.pattern.vars[0].0, "__search_pattern_a"); assert_eq!(result.pattern.vars[1].0, "__search_pattern_b"); assert_eq!(&result.template.template.text(), "bar(__search_pattern_b, __search_pattern_a)"); - dbg!(result.template.placeholders); } #[test] @@ -334,6 +344,16 @@ mod tests { ); } + #[test] + fn parser_invlid_pattern() { + assert_eq!(parse_error_text(" ==>> ()"), "Parse error: Pattern is not an expression"); + } + + #[test] + fn parser_invlid_template() { + assert_eq!(parse_error_text("() ==>> )"), "Parse error: Template is not an expression"); + } + #[test] fn parse_match_replace() { let query: SsrQuery = "foo($x:expr) ==>> bar($x)".parse().unwrap(); -- cgit v1.2.3