inlay_hints: implement ability to remove redundant hints (#690)

This commit is contained in:
Vesim 2022-09-29 20:36:29 +02:00 committed by GitHub
parent 9f2ea75777
commit 32ce19f9a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 5 deletions

View File

@ -52,6 +52,14 @@ inlay_hints_show_builtin: bool = true,
/// don't show inlay hints for single argument calls /// don't show inlay hints for single argument calls
inlay_hints_exclude_single_argument: bool = true, inlay_hints_exclude_single_argument: bool = true,
/// don't show inlay hints when parameter name matches the identifier
/// for example: `foo: foo`
inlay_hints_hide_redundant_param_names: bool = false,
/// don't show inlay hints when parameter names matches the last
/// for example: `foo: bar.foo`, `foo: &foo`
inlay_hints_hide_redundant_param_names_last_token: bool = false,
/// Whether to enable `*` and `?` operators in completion lists /// Whether to enable `*` and `?` operators in completion lists
operator_completions: bool = true, operator_completions: bool = true,
@ -69,8 +77,6 @@ skip_std_references: bool = false,
builtin_path: ?[]const u8 = null, builtin_path: ?[]const u8 = null,
pub fn loadFromFile(allocator: std.mem.Allocator, file_path: []const u8) ?Config { pub fn loadFromFile(allocator: std.mem.Allocator, file_path: []const u8) ?Config {
@setEvalBranchQuota(5000);
const tracy_zone = tracy.trace(@src()); const tracy_zone = tracy.trace(@src());
defer tracy_zone.end(); defer tracy_zone.end();
@ -84,7 +90,7 @@ pub fn loadFromFile(allocator: std.mem.Allocator, file_path: []const u8) ?Config
const file_buf = file.readToEndAlloc(allocator, 0x1000000) catch return null; const file_buf = file.readToEndAlloc(allocator, 0x1000000) catch return null;
defer allocator.free(file_buf); defer allocator.free(file_buf);
@setEvalBranchQuota(3000); @setEvalBranchQuota(7000);
const parse_options = std.json.ParseOptions{ .allocator = allocator, .ignore_unknown_fields = true }; const parse_options = std.json.ParseOptions{ .allocator = allocator, .ignore_unknown_fields = true };
// TODO: Better errors? Doesn't seem like std.json can provide us positions or context. // TODO: Better errors? Doesn't seem like std.json can provide us positions or context.
var config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), parse_options) catch |err| { var config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), parse_options) catch |err| {

View File

@ -99,8 +99,24 @@ fn writeCallHint(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *Doc
} }
while (ast.nextFnParam(&it)) |param| : (i += 1) { while (ast.nextFnParam(&it)) |param| : (i += 1) {
const name_token = param.name_token orelse continue;
if (i >= call.ast.params.len) break; if (i >= call.ast.params.len) break;
const name_token = param.name_token orelse continue;
const name = decl_tree.tokenSlice(name_token);
if (builder.config.inlay_hints_hide_redundant_param_names or builder.config.inlay_hints_hide_redundant_param_names_last_token) {
const last_param_token = tree.lastToken(call.ast.params[i]);
const param_name = tree.tokenSlice(last_param_token);
if (std.mem.eql(u8, param_name, name)) {
if (tree.firstToken(call.ast.params[i]) == last_param_token) {
if (builder.config.inlay_hints_hide_redundant_param_names)
continue;
} else {
if (builder.config.inlay_hints_hide_redundant_param_names_last_token)
continue;
}
}
}
const token_tags = decl_tree.tokens.items(.tag); const token_tags = decl_tree.tokens.items(.tag);
@ -114,7 +130,7 @@ fn writeCallHint(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *Doc
try builder.appendParameterHint( try builder.appendParameterHint(
offsets.tokenToPosition(tree, tree.firstToken(call.ast.params[i]), builder.encoding), offsets.tokenToPosition(tree, tree.firstToken(call.ast.params[i]), builder.encoding),
decl_tree.tokenSlice(name_token), name,
tooltip, tooltip,
no_alias, no_alias,
comp_time, comp_time,

View File

@ -300,6 +300,8 @@ pub const Configuration = struct {
enable_inlay_hints: ?bool, enable_inlay_hints: ?bool,
inlay_hints_show_builtin: ?bool, inlay_hints_show_builtin: ?bool,
inlay_hints_exclude_single_argument: ?bool, inlay_hints_exclude_single_argument: ?bool,
inlay_hints_hide_redundant_param_names: ?bool,
inlay_hints_hide_redundant_param_names_last_token: ?bool,
operator_completions: ?bool, operator_completions: ?bool,
include_at_in_builtins: ?bool, include_at_in_builtins: ?bool,
max_detail_length: ?usize, max_detail_length: ?usize,