From ae3abd6e575940eb1221acf26c09e96352f052fa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 16:45:10 +0200 Subject: [PATCH] Rename ra_ssr -> ssr --- Cargo.lock | 32 +++++------ crates/ra_ide/Cargo.toml | 2 +- crates/ra_ide/src/lib.rs | 18 ++++--- crates/ra_ide/src/ssr.rs | 72 ------------------------- crates/rust-analyzer/Cargo.toml | 2 +- crates/rust-analyzer/src/bin/args.rs | 2 +- crates/rust-analyzer/src/cli.rs | 12 +++-- crates/rust-analyzer/src/cli/ssr.rs | 2 +- crates/{ra_ssr => ssr}/Cargo.toml | 11 ++-- crates/{ra_ssr => ssr}/src/errors.rs | 0 crates/{ra_ssr => ssr}/src/lib.rs | 52 ++++++++++++++++++ crates/{ra_ssr => ssr}/src/matching.rs | 0 crates/{ra_ssr => ssr}/src/nester.rs | 0 crates/{ra_ssr => ssr}/src/parsing.rs | 0 crates/{ra_ssr => ssr}/src/replacing.rs | 0 crates/{ra_ssr => ssr}/src/resolving.rs | 0 crates/{ra_ssr => ssr}/src/search.rs | 0 crates/{ra_ssr => ssr}/src/tests.rs | 0 18 files changed, 95 insertions(+), 110 deletions(-) delete mode 100644 crates/ra_ide/src/ssr.rs rename crates/{ra_ssr => ssr}/Cargo.toml (97%) rename crates/{ra_ssr => ssr}/src/errors.rs (100%) rename crates/{ra_ssr => ssr}/src/lib.rs (82%) rename crates/{ra_ssr => ssr}/src/matching.rs (100%) rename crates/{ra_ssr => ssr}/src/nester.rs (100%) rename crates/{ra_ssr => ssr}/src/parsing.rs (100%) rename crates/{ra_ssr => ssr}/src/replacing.rs (100%) rename crates/{ra_ssr => ssr}/src/resolving.rs (100%) rename crates/{ra_ssr => ssr}/src/search.rs (100%) rename crates/{ra_ssr => ssr}/src/tests.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 8704e43860..4d8b748392 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1135,28 +1135,14 @@ dependencies = [ "oorandom", "profile", "ra_assists", - "ra_ssr", "rustc-hash", + "ssr", "stdx", "syntax", "test_utils", "text_edit", ] -[[package]] -name = "ra_ssr" -version = "0.1.0" -dependencies = [ - "base_db", - "expect", - "hir", - "ide_db", - "rustc-hash", - "syntax", - "test_utils", - "text_edit", -] - [[package]] name = "rayon" version = "1.3.1" @@ -1254,11 +1240,11 @@ dependencies = [ "profile", "project_model", "ra_ide", - "ra_ssr", "rayon", "rustc-hash", "serde", "serde_json", + "ssr", "stdx", "syntax", "test_utils", @@ -1456,6 +1442,20 @@ dependencies = [ "serde", ] +[[package]] +name = "ssr" +version = "0.1.0" +dependencies = [ + "base_db", + "expect", + "hir", + "ide_db", + "rustc-hash", + "syntax", + "test_utils", + "text_edit", +] + [[package]] name = "stdx" version = "0.1.0" diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index e25aad6cfb..8519e9ccae 100644 --- a/crates/ra_ide/Cargo.toml +++ b/crates/ra_ide/Cargo.toml @@ -29,7 +29,7 @@ cfg = { path = "../cfg" } profile = { path = "../profile" } test_utils = { path = "../test_utils" } ra_assists = { path = "../ra_assists" } -ra_ssr = { path = "../ra_ssr" } +ssr = { path = "../ssr" } # ra_ide should depend only on the top-level `hir` package. if you need # something from some `hir_xxx` subpackage, reexport the API via `hir`. diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 66a234fff7..bbc9e4b8af 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -39,7 +39,6 @@ mod matching_brace; mod parent_module; mod references; mod runnables; -mod ssr; mod status; mod syntax_highlighting; mod syntax_tree; @@ -95,7 +94,7 @@ pub use ide_db::{ RootDatabase, }; pub use ra_assists::{Assist, AssistConfig, AssistId, AssistKind, ResolvedAssist}; -pub use ra_ssr::SsrError; +pub use ssr::SsrError; pub use text_edit::{Indel, TextEdit}; pub type Cancelable = Result; @@ -515,20 +514,23 @@ impl Analysis { &self, query: &str, parse_only: bool, - position: FilePosition, + resolve_context: FilePosition, selections: Vec, ) -> Cancelable> { self.with_db(|db| { - let edits = ssr::parse_search_replace(query, parse_only, db, position, selections)?; + let rule: ssr::SsrRule = query.parse()?; + let mut match_finder = ssr::MatchFinder::in_context(db, resolve_context, selections); + match_finder.add_rule(rule)?; + let edits = if parse_only { Vec::new() } else { match_finder.edits() }; Ok(SourceChange::from(edits)) }) } /// Performs an operation on that may be Canceled. - fn with_db T + std::panic::UnwindSafe, T>( - &self, - f: F, - ) -> Cancelable { + fn with_db(&self, f: F) -> Cancelable + where + F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe, + { self.db.catch_canceled(f) } } diff --git a/crates/ra_ide/src/ssr.rs b/crates/ra_ide/src/ssr.rs deleted file mode 100644 index a8a7041923..0000000000 --- a/crates/ra_ide/src/ssr.rs +++ /dev/null @@ -1,72 +0,0 @@ -use base_db::{FilePosition, FileRange}; -use ide_db::RootDatabase; - -use crate::SourceFileEdit; -use ra_ssr::{MatchFinder, SsrError, SsrRule}; - -// Feature: Structural Search and Replace -// -// Search and replace with named wildcards that will match any expression, type, path, pattern or item. -// The syntax for a structural search replace command is ` ==>> `. -// A `$` placeholder in the search pattern will match any AST node and `$` will reference it in the replacement. -// Within a macro call, a placeholder will match up until whatever token follows the placeholder. -// -// All paths in both the search pattern and the replacement template must resolve in the context -// in which this command is invoked. Paths in the search pattern will then match the code if they -// resolve to the same item, even if they're written differently. For example if we invoke the -// command in the module `foo` with a pattern of `Bar`, then code in the parent module that refers -// to `foo::Bar` will match. -// -// Paths in the replacement template will be rendered appropriately for the context in which the -// replacement occurs. For example if our replacement template is `foo::Bar` and we match some -// code in the `foo` module, we'll insert just `Bar`. -// -// Inherent method calls should generally be written in UFCS form. e.g. `foo::Bar::baz($s, $a)` will -// match `$s.baz($a)`, provided the method call `baz` resolves to the method `foo::Bar::baz`. -// -// The scope of the search / replace will be restricted to the current selection if any, otherwise -// it will apply to the whole workspace. -// -// Placeholders may be given constraints by writing them as `${::...}`. -// -// Supported constraints: -// -// |=== -// | Constraint | Restricts placeholder -// -// | kind(literal) | Is a literal (e.g. `42` or `"forty two"`) -// | not(a) | Negates the constraint `a` -// |=== -// -// Available via the command `rust-analyzer.ssr`. -// -// ```rust -// // Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)] -// -// // BEFORE -// String::from(foo(y + 5, z)) -// -// // AFTER -// String::from((y + 5).foo(z)) -// ``` -// -// |=== -// | Editor | Action Name -// -// | VS Code | **Rust Analyzer: Structural Search Replace** -// |=== -pub fn parse_search_replace( - rule: &str, - parse_only: bool, - db: &RootDatabase, - resolve_context: FilePosition, - selections: Vec, -) -> Result, SsrError> { - let rule: SsrRule = rule.parse()?; - let mut match_finder = MatchFinder::in_context(db, resolve_context, selections); - match_finder.add_rule(rule)?; - if parse_only { - return Ok(Vec::new()); - } - Ok(match_finder.edits()) -} diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index da8c09c09a..749cf648c6 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -48,7 +48,7 @@ toolchain = { path = "../toolchain" } # This should only be used in CLI base_db = { path = "../base_db" } ide_db = { path = "../ide_db" } -ra_ssr = { path = "../ra_ssr" } +ssr = { path = "../ssr" } hir = { path = "../hir" } hir_def = { path = "../hir_def" } hir_ty = { path = "../hir_ty" } diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index d3081e88bd..0bc92431a9 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -7,8 +7,8 @@ use std::{env, fmt::Write, path::PathBuf}; use anyhow::{bail, Result}; use pico_args::Arguments; -use ra_ssr::{SsrPattern, SsrRule}; use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity}; +use ssr::{SsrPattern, SsrRule}; use vfs::AbsPathBuf; pub(crate) struct Args { diff --git a/crates/rust-analyzer/src/cli.rs b/crates/rust-analyzer/src/cli.rs index 420abaccb9..b237a94d11 100644 --- a/crates/rust-analyzer/src/cli.rs +++ b/crates/rust-analyzer/src/cli.rs @@ -13,11 +13,13 @@ use anyhow::Result; use ra_ide::Analysis; use syntax::{AstNode, SourceFile}; -pub use analysis_bench::{BenchCmd, BenchWhat, Position}; -pub use analysis_stats::AnalysisStatsCmd; -pub use diagnostics::diagnostics; -pub use load_cargo::load_cargo; -pub use ssr::{apply_ssr_rules, search_for_patterns}; +pub use self::{ + analysis_bench::{BenchCmd, BenchWhat, Position}, + analysis_stats::AnalysisStatsCmd, + diagnostics::diagnostics, + load_cargo::load_cargo, + ssr::{apply_ssr_rules, search_for_patterns}, +}; #[derive(Clone, Copy)] pub enum Verbosity { diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs index 1357a93e16..c11e109437 100644 --- a/crates/rust-analyzer/src/cli/ssr.rs +++ b/crates/rust-analyzer/src/cli/ssr.rs @@ -1,7 +1,7 @@ //! Applies structured search replace rules from the command line. use crate::cli::{load_cargo::load_cargo, Result}; -use ra_ssr::{MatchFinder, SsrPattern, SsrRule}; +use ssr::{MatchFinder, SsrPattern, SsrRule}; pub fn apply_ssr_rules(rules: Vec) -> Result<()> { use base_db::SourceDatabaseExt; diff --git a/crates/ra_ssr/Cargo.toml b/crates/ssr/Cargo.toml similarity index 97% rename from crates/ra_ssr/Cargo.toml rename to crates/ssr/Cargo.toml index 4d22a8a982..cd05eeecc8 100644 --- a/crates/ra_ssr/Cargo.toml +++ b/crates/ssr/Cargo.toml @@ -1,22 +1,23 @@ [package] -edition = "2018" -name = "ra_ssr" +name = "ssr" version = "0.1.0" -authors = ["rust-analyzer developers"] -license = "MIT OR Apache-2.0" description = "Structural search and replace of Rust code" +license = "MIT OR Apache-2.0" repository = "https://github.com/rust-analyzer/rust-analyzer" +authors = ["rust-analyzer developers"] +edition = "2018" [lib] doctest = false [dependencies] +rustc-hash = "1.1.0" + text_edit = { path = "../text_edit" } syntax = { path = "../syntax" } base_db = { path = "../base_db" } ide_db = { path = "../ide_db" } hir = { path = "../hir" } -rustc-hash = "1.1.0" test_utils = { path = "../test_utils" } [dev-dependencies] diff --git a/crates/ra_ssr/src/errors.rs b/crates/ssr/src/errors.rs similarity index 100% rename from crates/ra_ssr/src/errors.rs rename to crates/ssr/src/errors.rs diff --git a/crates/ra_ssr/src/lib.rs b/crates/ssr/src/lib.rs similarity index 82% rename from crates/ra_ssr/src/lib.rs rename to crates/ssr/src/lib.rs index b4e35107ed..292bd5b9a7 100644 --- a/crates/ra_ssr/src/lib.rs +++ b/crates/ssr/src/lib.rs @@ -3,6 +3,58 @@ //! Allows searching the AST for code that matches one or more patterns and then replacing that code //! based on a template. +// Feature: Structural Search and Replace +// +// Search and replace with named wildcards that will match any expression, type, path, pattern or item. +// The syntax for a structural search replace command is ` ==>> `. +// A `$` placeholder in the search pattern will match any AST node and `$` will reference it in the replacement. +// Within a macro call, a placeholder will match up until whatever token follows the placeholder. +// +// All paths in both the search pattern and the replacement template must resolve in the context +// in which this command is invoked. Paths in the search pattern will then match the code if they +// resolve to the same item, even if they're written differently. For example if we invoke the +// command in the module `foo` with a pattern of `Bar`, then code in the parent module that refers +// to `foo::Bar` will match. +// +// Paths in the replacement template will be rendered appropriately for the context in which the +// replacement occurs. For example if our replacement template is `foo::Bar` and we match some +// code in the `foo` module, we'll insert just `Bar`. +// +// Inherent method calls should generally be written in UFCS form. e.g. `foo::Bar::baz($s, $a)` will +// match `$s.baz($a)`, provided the method call `baz` resolves to the method `foo::Bar::baz`. +// +// The scope of the search / replace will be restricted to the current selection if any, otherwise +// it will apply to the whole workspace. +// +// Placeholders may be given constraints by writing them as `${::...}`. +// +// Supported constraints: +// +// |=== +// | Constraint | Restricts placeholder +// +// | kind(literal) | Is a literal (e.g. `42` or `"forty two"`) +// | not(a) | Negates the constraint `a` +// |=== +// +// Available via the command `rust-analyzer.ssr`. +// +// ```rust +// // Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)] +// +// // BEFORE +// String::from(foo(y + 5, z)) +// +// // AFTER +// String::from((y + 5).foo(z)) +// ``` +// +// |=== +// | Editor | Action Name +// +// | VS Code | **Rust Analyzer: Structural Search Replace** +// |=== + mod matching; mod nester; mod parsing; diff --git a/crates/ra_ssr/src/matching.rs b/crates/ssr/src/matching.rs similarity index 100% rename from crates/ra_ssr/src/matching.rs rename to crates/ssr/src/matching.rs diff --git a/crates/ra_ssr/src/nester.rs b/crates/ssr/src/nester.rs similarity index 100% rename from crates/ra_ssr/src/nester.rs rename to crates/ssr/src/nester.rs diff --git a/crates/ra_ssr/src/parsing.rs b/crates/ssr/src/parsing.rs similarity index 100% rename from crates/ra_ssr/src/parsing.rs rename to crates/ssr/src/parsing.rs diff --git a/crates/ra_ssr/src/replacing.rs b/crates/ssr/src/replacing.rs similarity index 100% rename from crates/ra_ssr/src/replacing.rs rename to crates/ssr/src/replacing.rs diff --git a/crates/ra_ssr/src/resolving.rs b/crates/ssr/src/resolving.rs similarity index 100% rename from crates/ra_ssr/src/resolving.rs rename to crates/ssr/src/resolving.rs diff --git a/crates/ra_ssr/src/search.rs b/crates/ssr/src/search.rs similarity index 100% rename from crates/ra_ssr/src/search.rs rename to crates/ssr/src/search.rs diff --git a/crates/ra_ssr/src/tests.rs b/crates/ssr/src/tests.rs similarity index 100% rename from crates/ra_ssr/src/tests.rs rename to crates/ssr/src/tests.rs