]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/snippet.rs
Merge #11393
[rust.git] / crates / ide_completion / src / snippet.rs
index bcaa1ded8f6107b374fe837578297f88f950d2ee..3a4f713333baae7cc079eefdb3f77102f52e58f5 100644 (file)
 // These placeholders take the form of `$number` or `${number:placeholder_text}` which can be traversed as tabstop in ascending order starting from 1,
 // with `$0` being a special case that always comes last.
 //
-// There is also a special placeholder, `${receiver}`, which will be replaced by the receiver expression for postfix snippets, or nothing in case of normal snippets.
-// It does not act as a tabstop.
+// There is also a special placeholder, `${receiver}`, which will be replaced by the receiver expression for postfix snippets, or a `$0` tabstop in case of normal snippets.
+// This replacement for normal snippets allows you to reuse a snippet for both post- and prefix in a single definition.
+//
+// For the VSCode editor, rust-analyzer also ships with a small set of defaults which can be removed
+// by overwriting the settings object mentioned above, the defaults are:
+// [source,json]
+// ----
+// {
+//     "Arc::new": {
+//         "postfix": "arc",
+//         "body": "Arc::new(${receiver})",
+//         "requires": "std::sync::Arc",
+//         "description": "Put the expression into an `Arc`",
+//         "scope": "expr"
+//     },
+//     "Rc::new": {
+//         "postfix": "rc",
+//         "body": "Rc::new(${receiver})",
+//         "requires": "std::rc::Rc",
+//         "description": "Put the expression into an `Rc`",
+//         "scope": "expr"
+//     },
+//     "Box::pin": {
+//         "postfix": "pinbox",
+//         "body": "Box::pin(${receiver})",
+//         "requires": "std::boxed::Box",
+//         "description": "Put the expression into a pinned `Box`",
+//         "scope": "expr"
+//     },
+//     "Ok": {
+//         "postfix": "ok",
+//         "body": "Ok(${receiver})",
+//         "description": "Wrap the expression in a `Result::Ok`",
+//         "scope": "expr"
+//     },
+//     "Err": {
+//         "postfix": "err",
+//         "body": "Err(${receiver})",
+//         "description": "Wrap the expression in a `Result::Err`",
+//         "scope": "expr"
+//     },
+//     "Some": {
+//         "postfix": "some",
+//         "body": "Some(${receiver})",
+//         "description": "Wrap the expression in an `Option::Some`",
+//         "scope": "expr"
+//     }
+// }
+// ----
+
 use ide_db::helpers::{import_assets::LocatedImport, insert_use::ImportScope};
 use itertools::Itertools;
 use syntax::{ast, AstNode, GreenNode, SyntaxNode};
@@ -117,7 +165,7 @@ pub(crate) fn imports(
     }
 
     pub fn snippet(&self) -> String {
-        self.snippet.replace("${receiver}", "")
+        self.snippet.replace("${receiver}", "$0")
     }
 
     pub fn postfix_snippet(&self, receiver: &str) -> String {
@@ -137,11 +185,8 @@ fn import_edits(
             hir::PathResolution::Def(def) => def.into(),
             _ => return None,
         };
-        let path = ctx.scope.module()?.find_use_path_prefixed(
-            ctx.db,
-            item,
-            ctx.config.insert_use.prefix_kind,
-        )?;
+        let path =
+            ctx.module?.find_use_path_prefixed(ctx.db, item, ctx.config.insert_use.prefix_kind)?;
         Some((path.len() > 1).then(|| ImportEdit {
             import: LocatedImport::new(path.clone(), item, item, None),
             scope: import_scope.clone(),
@@ -164,18 +209,20 @@ fn validate_snippet(
 ) -> Option<(Box<[GreenNode]>, String, Option<Box<str>>)> {
     let mut imports = Vec::with_capacity(requires.len());
     for path in requires.iter() {
-        let path = ast::Path::parse(path).ok()?;
-        let valid_use_path = path.segments().all(|seg| {
-            matches!(seg.kind(), Some(ast::PathSegmentKind::Name(_)))
-                || seg.generic_arg_list().is_none()
-        });
-        if !valid_use_path {
+        let use_path = ast::SourceFile::parse(&format!("use {};", path))
+            .syntax_node()
+            .descendants()
+            .find_map(ast::Path::cast)?;
+        if use_path.syntax().text() != path.as_str() {
             return None;
         }
-        let green = path.syntax().green().into_owned();
+        let green = use_path.syntax().green().into_owned();
         imports.push(green);
     }
     let snippet = snippet.iter().join("\n");
-    let description = if description.is_empty() { None } else { Some(description.into()) };
+    let description = (!description.is_empty())
+        .then(|| description.split_once('\n').map_or(description, |(it, _)| it))
+        .map(ToOwned::to_owned)
+        .map(Into::into);
     Some((imports.into_boxed_slice(), snippet, description))
 }