Skip to content

Commit d344d89

Browse files
jiacai2050leecannon
authored andcommitted
make argument placeholders optional (#1317)
* support argument placeholder * default to true * fix review * completion: handle cursor placement Correctly handles cursor placement after a completion when placeholders are disabled but snippets are not. --------- Co-authored-by: Lee Cannon <[email protected]>
1 parent 9b8986f commit d344d89

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The following options are currently available.
6363
| Option | Type | Default value | What it Does |
6464
| --- | --- | --- | --- |
6565
| `enable_snippets` | `bool` | `true` | Enables snippet completions when the client also supports them |
66+
| `enable_argument_placeholders` | `bool` | `true` | Whether to enable function argument placeholder completions |
6667
| `enable_ast_check_diagnostics` | `bool` | `true` | Whether to enable ast-check diagnostics |
6768
| `enable_autofix` | `bool` | `true` | Whether to automatically fix errors on save. Currently supports adding and removing discards. |
6869
| `enable_import_embedfile_argument_completions` | `bool` | `true` | Whether to enable import/embedFile argument completions |
@@ -149,7 +150,7 @@ You can use zls as a library! [Check out this demo repo](https://github.com/zigt
149150

150151
## Quick Thanks :)
151152

152-
We'd like to take a second to thank all our awesome [contributors](https://github.com/zigtools/zls/graphs/contributors) and donators/backers/sponsors; if you have time or money to spare, consider partaking in either of these options - they help keep zls awesome for everyone!
153+
We'd like to take a second to thank all our awesome [contributors](https://github.com/zigtools/zls/graphs/contributors) and donators/backers/sponsors; if you have time or money to spare, consider partaking in either of these options - they help keep zls awesome for everyone!
153154

154155
[![OpenCollective Backers](https://opencollective.com/zigtools/backers.svg?width=890&limit=1000)](https://opencollective.com/zigtools#category-CONTRIBUTE)
155156

schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"type": "boolean",
1010
"default": "true"
1111
},
12+
"enable_argument_placeholders": {
13+
"description": "Whether to enable function argument placeholder completions",
14+
"type": "boolean",
15+
"default": "true"
16+
},
1217
"enable_ast_check_diagnostics": {
1318
"description": "Whether to enable ast-check diagnostics",
1419
"type": "boolean",

src/Config.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
/// Enables snippet completions when the client also supports them
88
enable_snippets: bool = true,
99

10+
/// Whether to enable function argument placeholder completions
11+
enable_argument_placeholders: bool = true,
12+
1013
/// Whether to enable ast-check diagnostics
1114
enable_ast_check_diagnostics: bool = true,
1215

src/config_gen/config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
"type": "bool",
77
"default": "true"
88
},
9+
{
10+
"name": "enable_argument_placeholders",
11+
"description": "Whether to enable function argument placeholder completions",
12+
"type": "bool",
13+
"default": "true"
14+
},
915
{
1016
"name": "enable_ast_check_diagnostics",
1117
"description": "Whether to enable ast-check diagnostics",
@@ -168,4 +174,4 @@
168174
"default": "null"
169175
}
170176
]
171-
}
177+
}

src/features/completions.zig

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +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 insert_text = if (use_snippets) blk: {
199-
const skip_self_param = !(parent_is_type_val orelse true) and
200-
try analyser.hasSelfParam(handle, func);
201-
break :blk try Analyser.getFunctionSnippet(arena, tree, func, skip_self_param);
202-
} else 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}),
223+
}
224+
};
203225

204226
const is_type_function = Analyser.isTypeFunction(handle.tree, func);
205227

0 commit comments

Comments
 (0)