]> git.lizzy.rs Git - rust.git/commitdiff
Remove possible multiline details in completions
authorLukas Wirth <lukastw97@gmail.com>
Wed, 24 Nov 2021 15:01:33 +0000 (16:01 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Wed, 24 Nov 2021 15:08:11 +0000 (16:08 +0100)
crates/ide_completion/src/completions/qualified_path.rs
crates/ide_completion/src/completions/snippet.rs
crates/ide_completion/src/render.rs
crates/ide_completion/src/render/enum_variant.rs
crates/ide_completion/src/render/function.rs
crates/ide_completion/src/snippet.rs
crates/ide_completion/src/tests/expression.rs
crates/ide_completion/src/tests/pattern.rs
crates/ide_completion/src/tests/type_pos.rs
crates/syntax/src/display.rs

index b5bf6b551462b3fa246f6d5234bc4441c6c0d318..a2662d2932bd999c3bcf7cdd8b06ddf4cd8e753b 100644 (file)
@@ -260,8 +260,8 @@ fn foo() { let _ = lib::S::$0 }
 "#,
             expect![[r#"
                 fn public_method() fn()
-                ct PUBLIC_CONST    pub const PUBLIC_CONST: u32 = 1;
-                ta PublicType      pub type PublicType = u32;
+                ct PUBLIC_CONST    pub const PUBLIC_CONST: u32;
+                ta PublicType      pub type PublicType;
             "#]],
         );
     }
@@ -392,10 +392,10 @@ fn subfunc() {
             expect![[r#"
                 ta SubTy (as Sub)        type SubTy;
                 ta Ty (as Super)         type Ty;
-                ct CONST (as Super)      const CONST: u8 = 0;
+                ct CONST (as Super)      const CONST: u8;
                 fn func() (as Super)     fn()
                 me method(…) (as Super)  fn(&self)
-                ct C2 (as Sub)           const C2: () = ();
+                ct C2 (as Sub)           const C2: ();
                 fn subfunc() (as Sub)    fn()
                 me submethod(…) (as Sub) fn(&self)
             "#]],
@@ -626,7 +626,7 @@ pub fn func(self) {}
 }
 "#,
             expect![[r#"
-                ct MAX     pub const MAX: Self = 255;
+                ct MAX     pub const MAX: Self;
                 me func(…) fn(self)
             "#]],
         );
index 12bccfae11d975283e75224fd7019e0e0f690705..02f711f51d866664cd992c5e36c032e5d2b64616 100644 (file)
@@ -115,7 +115,7 @@ fn add_custom_completions(
             for import in imports.into_iter() {
                 builder.add_import(import);
             }
-            builder.detail(snip.description.as_deref().unwrap_or_default());
+            builder.set_detail(snip.description.clone());
             builder.add_to(acc);
         },
     );
index a1d4eb998fa6e239f84a2a467a2bc669243c2d17..d4f35b5dcd1ee7d022551f46f670ca4c5b9299ea 100644 (file)
@@ -433,7 +433,7 @@ fn main() { Foo::Fo$0 }
                         kind: SymbolKind(
                             Variant,
                         ),
-                        detail: "{ x: i32, y: i32 }",
+                        detail: "{x: i32, y: i32}",
                     },
                 ]
             "#]],
index 728e0e2a174bbf5cca014b81aa1f592f7acf048c..26cb73d09c9ef380aa2e0e137ef99c3068db5089 100644 (file)
@@ -1,10 +1,10 @@
 //! Renderer for `enum` variants.
 
-use std::iter;
+use std::{iter, mem};
 
 use hir::{HasAttrs, HirDisplay};
 use ide_db::SymbolKind;
-use itertools::Itertools;
+use stdx::format_to;
 
 use crate::{
     item::{CompletionItem, ImportEdit},
@@ -105,18 +105,31 @@ fn detail(&self) -> String {
             .into_iter()
             .map(|field| (field.name(self.ctx.db()), field.ty(self.ctx.db())));
 
+        let mut b = String::new();
+        let mut first_run = true;
         match self.variant_kind {
-            hir::StructKind::Tuple | hir::StructKind::Unit => format!(
-                "({})",
-                detail_types.map(|(_, t)| t.display(self.ctx.db()).to_string()).format(", ")
-            ),
-            hir::StructKind::Record => format!(
-                "{{ {} }}",
-                detail_types
-                    .map(|(n, t)| format!("{}: {}", n, t.display(self.ctx.db()).to_string()))
-                    .format(", ")
-            ),
+            hir::StructKind::Tuple | hir::StructKind::Unit => {
+                format_to!(b, "(");
+                for (_, t) in detail_types {
+                    if !mem::take(&mut first_run) {
+                        format_to!(b, ", ");
+                    }
+                    format_to!(b, "{}", t.display(self.ctx.db()));
+                }
+                format_to!(b, ")");
+            }
+            hir::StructKind::Record => {
+                format_to!(b, "{{");
+                for (n, t) in detail_types {
+                    if !mem::take(&mut first_run) {
+                        format_to!(b, ", ");
+                    }
+                    format_to!(b, "{}: {}", n, t.display(self.ctx.db()));
+                }
+                format_to!(b, "}}");
+            }
         }
+        b
     }
 }
 
index f598b414a73b4238b11c9974bec2a5d1698b0083..86dfbf7fc8f9da931d269aa745580a773cd31cae 100644 (file)
@@ -4,6 +4,7 @@
 use hir::{AsAssocItem, HasSource, HirDisplay};
 use ide_db::SymbolKind;
 use itertools::Itertools;
+use stdx::format_to;
 use syntax::ast;
 
 use crate::{
@@ -122,14 +123,11 @@ fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
 
     fn detail(&self) -> String {
         let ret_ty = self.func.ret_type(self.ctx.db());
-        let ret = if ret_ty.is_unit() {
-            // Omit the return type if it is the unit type
-            String::new()
-        } else {
-            format!(" {}", self.ty_display())
-        };
-
-        format!("fn({}){}", self.params_display(), ret)
+        let mut detail = format!("fn({})", self.params_display());
+        if !ret_ty.is_unit() {
+            format_to!(detail, " -> {}", ret_ty.display(self.ctx.db()));
+        }
+        detail
     }
 
     fn params_display(&self) -> String {
@@ -153,12 +151,6 @@ fn params_display(&self) -> String {
         }
     }
 
-    fn ty_display(&self) -> String {
-        let ret_ty = self.func.ret_type(self.ctx.db());
-
-        format!("-> {}", ret_ty.display(self.ctx.db()))
-    }
-
     fn params(&self) -> Params {
         let ast_params = match self.ast_node.param_list() {
             Some(it) => it,
index bcaa1ded8f6107b374fe837578297f88f950d2ee..1a3f42b8398e99a487bbd1b1ef6ed6df61a53ba9 100644 (file)
@@ -176,6 +176,9 @@ fn validate_snippet(
         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))
 }
index a63480873447ac2fb03540f730e62e99516ebb23..eba2a5e1efda9f60aa5c8bd14f8453e604f0ab5a 100644 (file)
@@ -544,11 +544,11 @@ fn func() {
 "#,
         expect![[r#"
             ev TupleV(…)   (u32)
-            ev RecordV     { field: u32 }
+            ev RecordV     {field: u32}
             ev UnitV       ()
-            ct ASSOC_CONST const ASSOC_CONST: () = ();
+            ct ASSOC_CONST const ASSOC_CONST: ();
             fn assoc_fn()  fn()
-            ta AssocType   type AssocType = ();
+            ta AssocType   type AssocType;
         "#]],
     );
 }
index 6dd1b6699881e6016d822ac9a3473af2b75fafa2..81c45c64cc1d24333b9e99efa2a2a9545f5dd42f 100644 (file)
@@ -286,11 +286,11 @@ fn func() {
 "#,
         expect![[r#"
             ev TupleV(…)   (u32)
-            ev RecordV     { field: u32 }
+            ev RecordV     {field: u32}
             ev UnitV       ()
-            ct ASSOC_CONST const ASSOC_CONST: () = ();
+            ct ASSOC_CONST const ASSOC_CONST: ();
             fn assoc_fn()  fn()
-            ta AssocType   type AssocType = ();
+            ta AssocType   type AssocType;
         "#]],
     );
 }
index a03f1e7ff8d6007b027d16cc7246844074fea482..a76f97f3da3f611ab9ea4b0269a7bb5c9889644c 100644 (file)
@@ -199,7 +199,7 @@ fn assoc_fn() {}
 fn func(_: Enum::$0) {}
 "#,
         expect![[r#"
-            ta AssocType type AssocType = ();
+            ta AssocType type AssocType;
         "#]],
     );
 }
index 95e37944cc4d14ff0faf9d28d35d49c8eae02626..e2115cbd2b0244a0633bf33dd2ecdc53bf5a984d 100644 (file)
@@ -1,10 +1,6 @@
-//! This module contains utilities for turning SyntaxNodes and HIR types
-//! into types that may be used to render in a UI.
+//! This module contains utilities for rendering syntax nodes into a string representing their signature.
 
-use crate::{
-    ast::{self, AstNode, HasAttrs, HasGenericParams, HasName},
-    SyntaxKind::{ATTR, COMMENT},
-};
+use crate::ast::{self, HasAttrs, HasGenericParams, HasName};
 
 use ast::HasVisibility;
 use stdx::format_to;
@@ -55,25 +51,39 @@ pub fn function_declaration(node: &ast::Fn) -> String {
 }
 
 pub fn const_label(node: &ast::Const) -> String {
-    let label: String = node
-        .syntax()
-        .children_with_tokens()
-        .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
-        .map(|node| node.to_string())
-        .collect();
-
-    label.trim().to_owned()
+    let mut s = String::new();
+    if let Some(vis) = node.visibility() {
+        format_to!(s, "{} ", vis);
+    }
+    format_to!(s, "const ");
+    if let Some(name) = node.name() {
+        format_to!(s, "{}", name);
+    } else {
+        format_to!(s, "?");
+    }
+    format_to!(s, ": ");
+    if let Some(ty) = node.ty() {
+        format_to!(s, "{}", ty);
+    } else {
+        format_to!(s, "?");
+    }
+    format_to!(s, ";");
+    s
 }
 
 pub fn type_label(node: &ast::TypeAlias) -> String {
-    let label: String = node
-        .syntax()
-        .children_with_tokens()
-        .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
-        .map(|node| node.to_string())
-        .collect();
-
-    label.trim().to_owned()
+    let mut s = String::new();
+    if let Some(vis) = node.visibility() {
+        format_to!(s, "{} ", vis);
+    }
+    format_to!(s, "type ");
+    if let Some(name) = node.name() {
+        format_to!(s, "{}", name);
+    } else {
+        format_to!(s, "?");
+    }
+    format_to!(s, ";");
+    s
 }
 
 pub fn macro_label(node: &ast::Macro) -> String {