Skip to content

Commit 00e6180

Browse files
committed
completion: handle cursor placement
Correctly handles cursor placement after a completion when placeholders are disabled but snippets are not.
1 parent 415703c commit 00e6180

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/features/completions.zig

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,33 @@ fn nodeToCompletion(
195195
const func = tree.fullFnProto(&buf, node).?;
196196
if (func.name_token) |name_token| {
197197
const use_snippets = server.config.enable_snippets and server.client_capabilities.supports_snippets;
198-
const use_placeholders = server.config.enable_argument_placeholders and server.client_capabilities.supports_snippets;
199-
const insert_text = if (use_snippets) blk: {
200-
if (use_placeholders) {
201-
const skip_self_param = !(parent_is_type_val orelse true) and
202-
try analyser.hasSelfParam(handle, func);
203-
break :blk try Analyser.getFunctionSnippet(arena, tree, func, skip_self_param);
204-
} else {
205-
break :blk try std.fmt.allocPrint(arena, "{s}()", .{tree.tokenSlice(func.name_token.?)});
198+
199+
const insert_text = blk: {
200+
const func_name = tree.tokenSlice(func.name_token.?);
201+
202+
if (!use_snippets) break :blk func_name;
203+
204+
const skip_self_param = !(parent_is_type_val orelse true) and try analyser.hasSelfParam(handle, func);
205+
206+
const use_placeholders = server.config.enable_argument_placeholders;
207+
if (use_placeholders) break :blk try Analyser.getFunctionSnippet(arena, tree, func, skip_self_param);
208+
209+
switch (func.ast.params.len) {
210+
// No arguments, leave cursor at the end
211+
0 => break :blk try std.fmt.allocPrint(arena, "{s}()", .{func_name}),
212+
1 => {
213+
if (skip_self_param) {
214+
// The one argument is a self parameter, leave cursor at the end
215+
break :blk try std.fmt.allocPrint(arena, "{s}()", .{func_name});
216+
}
217+
218+
// Non-self parameter, leave the cursor in the parentheses
219+
break :blk try std.fmt.allocPrint(arena, "{s}(${{1:}})", .{func_name});
220+
},
221+
// Atleast one non-self parameter, leave the cursor in the parentheses
222+
else => break :blk try std.fmt.allocPrint(arena, "{s}(${{1:}})", .{func_name}),
206223
}
207-
} else tree.tokenSlice(func.name_token.?);
224+
};
208225

209226
const is_type_function = Analyser.isTypeFunction(handle.tree, func);
210227

0 commit comments

Comments
 (0)