Detect self arguments and skip it when generating snippets

This commit is contained in:
Alexandros Naskos 2020-06-06 02:44:43 +03:00
parent 5a8e062118
commit 73b58abe51
2 changed files with 20 additions and 4 deletions

View File

@ -76,7 +76,7 @@ pub fn getFunctionSignature(tree: *ast.Tree, func: *ast.Node.FnProto) []const u8
}
/// Gets a function snippet insert text
pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func: *ast.Node.FnProto) ![]const u8 {
pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func: *ast.Node.FnProto, skip_self_param: bool) ![]const u8 {
const name_tok = func.name_token orelse unreachable;
var buffer = std.ArrayList(u8).init(allocator);
@ -88,6 +88,7 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
var buf_stream = buffer.outStream();
for (func.paramsConst()) |param, param_num| {
if (skip_self_param and param_num == 0) continue;
if (param_num != 0) try buffer.appendSlice(", ${") else try buffer.appendSlice("${");
try buf_stream.print("{}:", .{param_num + 1});

View File

@ -247,9 +247,24 @@ fn nodeToCompletion(
const func = node.cast(std.zig.ast.Node.FnProto).?;
if (func.name_token) |name_token| {
const use_snippets = config.enable_snippets and client_capabilities.supports_snippets;
const insert_text = if (use_snippets)
try analysis.getFunctionSnippet(list.allocator, analysis_ctx.tree(), func)
else
const insert_text = if (use_snippets) blk: {
const skip_self_param = if (func.params_len > 0) param_check: {
var child_analysis_ctx = try analysis_ctx.clone();
break :param_check switch (func.paramsConst()[0].param_type) {
.type_expr => |type_node| if (analysis_ctx.in_container == analysis.resolveTypeOfNode(&child_analysis_ctx, type_node))
true
else if (type_node.cast(std.zig.ast.Node.PrefixOp)) |prefix_op|
prefix_op.op == .PtrType and analysis_ctx.in_container == analysis.resolveTypeOfNode(&child_analysis_ctx, prefix_op.rhs)
else
false,
else => false,
};
} else
false;
break :blk try analysis.getFunctionSnippet(list.allocator, analysis_ctx.tree(), func, skip_self_param);
} else
null;
const is_type_function = analysis.isTypeFunction(analysis_ctx.tree(), func);