Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Techatrix <[email protected]>
  • Loading branch information
llogick and Techatrix committed Feb 19, 2024
1 parent 6d1713d commit 2e31eaa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The following options are currently available.
| `semantic_tokens` | `enum` | `.full` | Set level of semantic tokens. Partial only includes information that requires semantic analysis. |
| `enable_inlay_hints` | `bool` | `true` | Enables inlay hint support when the client also supports it |
| `inlay_hints_show_variable_type_hints` | `bool` | `true` | Enable inlay hints for variable types |
| `inlay_hints_show_anon_literal_field_type` | `bool` | `true` | Enable inlay hints for fields in union and struct inits |
| `inlay_hints_show_struct_literal_field_type` | `bool` | `true` | Enable inlay hints for fields in struct and union literals |
| `inlay_hints_show_parameter_name` | `bool` | `true` | Enable inlay hints for parameter names |
| `inlay_hints_show_builtin` | `bool` | `true` | Enable inlay hints for builtin functions |
| `inlay_hints_exclude_single_argument` | `bool` | `true` | Don't show inlay hints for single argument calls |
Expand Down
4 changes: 2 additions & 2 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"type": "boolean",
"default": true
},
"inlay_hints_show_anon_literal_field_type": {
"description": "Enable inlay hints for fields in union and struct inits",
"inlay_hints_show_struct_literal_field_type": {
"description": "Enable inlay hints for fields in struct and union literals",
"type": "boolean",
"default": true
},
Expand Down
4 changes: 2 additions & 2 deletions src/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ enable_inlay_hints: bool = true,
/// Enable inlay hints for variable types
inlay_hints_show_variable_type_hints: bool = true,

/// Enable inlay hints for fields in union and struct inits
inlay_hints_show_anon_literal_field_type: bool = true,
/// Enable inlay hints for fields in struct and union literals
inlay_hints_show_struct_literal_field_type: bool = true,

/// Enable inlay hints for parameter names
inlay_hints_show_parameter_name: bool = true,
Expand Down
4 changes: 2 additions & 2 deletions src/config_gen/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"default": true
},
{
"name": "inlay_hints_show_anon_literal_field_type",
"description": "Enable inlay hints for fields in union and struct inits",
"name": "inlay_hints_show_struct_literal_field_type",
"description": "Enable inlay hints for fields in struct and union literals",
"type": "bool",
"default": true
},
Expand Down
17 changes: 7 additions & 10 deletions src/features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -471,24 +471,21 @@ fn writeNodeInlayHint(
.struct_init,
.struct_init_comma,
=> {
if (!builder.config.inlay_hints_show_anon_literal_field_type) return;
if (!builder.config.inlay_hints_show_struct_literal_field_type) return;
var buffer: [2]Ast.Node.Index = undefined;
const struct_init = tree.fullStructInit(&buffer, node) orelse return;
const struct_init = tree.fullStructInit(&buffer, node).?;
for (struct_init.ast.fields) |value_node| { // the node of `value` in `.name = value`
const name_token = tree.firstToken(value_node) - 2; // math our way two token indexes back to get the `name`
const name_loc = offsets.tokenToLoc(tree, name_token);
const name = offsets.locToSlice(tree.source, name_loc);
const decl = (try builder.analyser.getSymbolEnumLiteral(builder.arena, builder.handle, name_loc.start, name)) orelse return;
if (decl.decl != .ast_node) return;
const field = decl.handle.tree.fullContainerField(decl.decl.ast_node) orelse return;
if (field.ast.type_expr == 0) return;
const decl = (try builder.analyser.getSymbolEnumLiteral(builder.arena, builder.handle, name_loc.start, name)) orelse continue;
const ty = try decl.resolveType(builder.analyser) orelse continue;
const type_str: []const u8 = try std.fmt.allocPrint(builder.arena, "{}", .{ty.fmt(builder.analyser)});
if (type_str.len == 0) continue;
try appendTypeHintString(
builder,
name_token,
try reduceTypeWhitespace(
offsets.nodeToSlice(decl.handle.tree, field.ast.type_expr),
builder.arena,
),
try reduceTypeWhitespace(type_str, builder.arena),
);
}
},
Expand Down

0 comments on commit 2e31eaa

Please sign in to comment.