Repository: rust-analyzer/rust-analyzer
Pull request #11689 (2022-03-12 13:12:24)

minor: Pad type inlay hints if no colons are requested
bors r+


Patch:
diff --git a/crates/parser/src/grammar/generic_args.rs b/crates/parser/src/grammar/generic_args.rs
index 948873ae2d..1148c6c35c 100644
--- a/crates/parser/src/grammar/generic_args.rs
+++ b/crates/parser/src/grammar/generic_args.rs
@@ -72,24 +72,28 @@ fn lifetime_arg(p: &mut Parser) {
     m.complete(p, LIFETIME_ARG);
 }
 
-pub(super) fn const_arg_expr(p: &mut Parser) {
-    // The tests in here are really for `const_arg`, which wraps the content
-    // CONST_ARG.
+// test const_arg
+// type T = S<92>;
+pub(super) fn const_arg(p: &mut Parser) {
+    let m = p.start();
     match p.current() {
         // test const_arg_block
         // type T = S<{90 + 2}>;
         T!['{'] => {
             expressions::block_expr(p);
+            m.complete(p, CONST_ARG);
         }
         // test const_arg_literal
         // type T = S<"hello", 0xdeadbeef>;
         k if k.is_literal() => {
             expressions::literal(p);
+            m.complete(p, CONST_ARG);
         }
         // test const_arg_bool_literal
         // type T = S<true>;
         T![true] | T![false] => {
             expressions::literal(p);
+            m.complete(p, CONST_ARG);
         }
         // test const_arg_negative_number
         // type T = S<-92>;
@@ -98,24 +102,19 @@ pub(super) fn const_arg_expr(p: &mut Parser) {
             p.bump(T![-]);
             expressions::literal(p);
             lm.complete(p, PREFIX_EXPR);
+            m.complete(p, CONST_ARG);
         }
+        // test const_arg_path
+        // struct S<const N: u32 = u32::MAX>;
         _ => {
-            // This shouldn't be hit by `const_arg`
             let lm = p.start();
             paths::use_path(p);
             lm.complete(p, PATH_EXPR);
+            m.complete(p, CONST_ARG);
         }
     }
 }
 
-// test const_arg
-// type T = S<92>;
-pub(super) fn const_arg(p: &mut Parser) {
-    let m = p.start();
-    const_arg_expr(p);
-    m.complete(p, CONST_ARG);
-}
-
 fn type_arg(p: &mut Parser) {
     let m = p.start();
     types::type_(p);
diff --git a/crates/parser/src/grammar/generic_params.rs b/crates/parser/src/grammar/generic_params.rs
index fbd514078d..1009c67ffa 100644
--- a/crates/parser/src/grammar/generic_params.rs
+++ b/crates/parser/src/grammar/generic_params.rs
@@ -79,16 +79,10 @@ fn const_param(p: &mut Parser, m: Marker) {
     }
 
     if p.at(T![=]) {
-        // test const_param_default_literal
+        // test const_param_defaults
         // struct A<const N: i32 = -1>;
         p.bump(T![=]);
-
-        // test const_param_default_expression
-        // struct A<const N: i32 = { 1 }>;
-
-        // test const_param_default_path
-        // struct A<const N: i32 = i32::MAX>;
-        generic_args::const_arg_expr(p);
+        generic_args::const_arg(p);
     }
 
     m.complete(p, CONST_PARAM);
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index fd6602624f..2faef70072 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -437,7 +437,8 @@ pub(crate) fn inlay_hint(
         },
         tooltip: None,
         padding_left: Some(match inlay_hint.kind {
-            InlayKind::TypeHint | InlayKind::ParameterHint => false,
+            InlayKind::TypeHint => !render_colons,
+            InlayKind::ParameterHint => false,
             InlayKind::ChainingHint => true,
         }),
         padding_right: Some(match inlay_hint.kind {


Test patch:
diff --git a/crates/parser/test_data/parser/inline/ok/0200_const_param_default_literal.rast b/crates/parser/test_data/parser/inline/ok/0165_const_param_defaults.rast
similarity index 81%
rename from crates/parser/test_data/parser/inline/ok/0200_const_param_default_literal.rast
rename to crates/parser/test_data/parser/inline/ok/0165_const_param_defaults.rast
index 8e52313651..6de10353bf 100644
--- a/crates/parser/test_data/parser/inline/ok/0200_const_param_default_literal.rast
+++ b/crates/parser/test_data/parser/inline/ok/0165_const_param_defaults.rast
@@ -21,10 +21,11 @@ SOURCE_FILE
         WHITESPACE " "
         EQ "="
         WHITESPACE " "
-        PREFIX_EXPR
-          MINUS "-"
-          LITERAL
-            INT_NUMBER "1"
+        CONST_ARG
+          PREFIX_EXPR
+            MINUS "-"
+            LITERAL
+              INT_NUMBER "1"
       R_ANGLE ">"
     SEMICOLON ";"
   WHITESPACE "\n"
diff --git a/crates/parser/test_data/parser/inline/ok/0200_const_param_default_literal.rs b/crates/parser/test_data/parser/inline/ok/0165_const_param_defaults.rs
similarity index 100%
rename from crates/parser/test_data/parser/inline/ok/0200_const_param_default_literal.rs
rename to crates/parser/test_data/parser/inline/ok/0165_const_param_defaults.rs
diff --git a/crates/parser/test_data/parser/inline/ok/0188_const_param_default_path.rast b/crates/parser/test_data/parser/inline/ok/0188_const_arg_path.rast
similarity index 67%
rename from crates/parser/test_data/parser/inline/ok/0188_const_param_default_path.rast
rename to crates/parser/test_data/parser/inline/ok/0188_const_arg_path.rast
index 11002bf98d..440dfb06a3 100644
--- a/crates/parser/test_data/parser/inline/ok/0188_const_param_default_path.rast
+++ b/crates/parser/test_data/parser/inline/ok/0188_const_arg_path.rast
@@ -3,7 +3,7 @@ SOURCE_FILE
     STRUCT_KW "struct"
     WHITESPACE " "
     NAME
-      IDENT "A"
+      IDENT "S"
     GENERIC_PARAM_LIST
       L_ANGLE "<"
       CONST_PARAM
@@ -17,20 +17,21 @@ SOURCE_FILE
           PATH
             PATH_SEGMENT
               NAME_REF
-                IDENT "i32"
+                IDENT "u32"
         WHITESPACE " "
         EQ "="
         WHITESPACE " "
-        PATH_EXPR
-          PATH
+        CONST_ARG
+          PATH_EXPR
             PATH
+              PATH
+                PATH_SEGMENT
+                  NAME_REF
+                    IDENT "u32"
+              COLON2 "::"
               PATH_SEGMENT
                 NAME_REF
-                  IDENT "i32"
-            COLON2 "::"
-            PATH_SEGMENT
-              NAME_REF
-                IDENT "MAX"
+                  IDENT "MAX"
       R_ANGLE ">"
     SEMICOLON ";"
   WHITESPACE "\n"
diff --git a/crates/parser/test_data/parser/inline/ok/0188_const_arg_path.rs b/crates/parser/test_data/parser/inline/ok/0188_const_arg_path.rs
new file mode 100644
index 0000000000..ee075f3e50
--- /dev/null
+++ b/crates/parser/test_data/parser/inline/ok/0188_const_arg_path.rs
@@ -0,0 +1,1 @@
+struct S<const N: u32 = u32::MAX>;
diff --git a/crates/parser/test_data/parser/inline/ok/0188_const_param_default_path.rs b/crates/parser/test_data/parser/inline/ok/0188_const_param_default_path.rs
deleted file mode 100644
index f3da43ca06..0000000000
--- a/crates/parser/test_data/parser/inline/ok/0188_const_param_default_path.rs
+++ /dev/null
@@ -1,1 +0,0 @@
-struct A<const N: i32 = i32::MAX>;
diff --git a/crates/parser/test_data/parser/inline/ok/0199_const_param_default_expression.rast b/crates/parser/test_data/parser/inline/ok/0199_const_param_default_expression.rast
deleted file mode 100644
index 0607ff54fb..0000000000
--- a/crates/parser/test_data/parser/inline/ok/0199_const_param_default_expression.rast
+++ /dev/null
@@ -1,34 +0,0 @@
-SOURCE_FILE
-  STRUCT
-    STRUCT_KW "struct"
-    WHITESPACE " "
-    NAME
-      IDENT "A"
-    GENERIC_PARAM_LIST
-      L_ANGLE "<"
-      CONST_PARAM
-        CONST_KW "const"
-        WHITESPACE " "
-        NAME
-          IDENT "N"
-        COLON ":"
-        WHITESPACE " "
-        PATH_TYPE
-          PATH
-            PATH_SEGMENT
-              NAME_REF
-                IDENT "i32"
-        WHITESPACE " "
-        EQ "="
-        WHITESPACE " "
-        BLOCK_EXPR
-          STMT_LIST
-            L_CURLY "{"
-            WHITESPACE " "
-            LITERAL
-              INT_NUMBER "1"
-            WHITESPACE " "
-            R_CURLY "}"
-      R_ANGLE ">"
-    SEMICOLON ";"
-  WHITESPACE "\n"
diff --git a/crates/parser/test_data/parser/inline/ok/0199_const_param_default_expression.rs b/crates/parser/test_data/parser/inline/ok/0199_const_param_default_expression.rs
deleted file mode 100644
index 551bde0b00..0000000000
--- a/crates/parser/test_data/parser/inline/ok/0199_const_param_default_expression.rs
+++ /dev/null
@@ -1,1 +0,0 @@
-struct A<const N: i32 = { 1 }>;