diff --git a/README.md b/README.md index 46bcb1ff5..11e877766 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ The following options are currently available. | Option | Type | Default value | What it Does | | --- | --- | --- | --- | | `enable_snippets` | `bool` | `true` | Enables snippet completions when the client also supports them | +| `enable_argument_placeholders` | `bool` | `true` | Whether to enable function argument placeholder completions | | `enable_ast_check_diagnostics` | `bool` | `true` | Whether to enable ast-check diagnostics | | `enable_autofix` | `bool` | `true` | Whether to automatically fix errors on save. Currently supports adding and removing discards. | | `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 ## Quick Thanks :) -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! +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! [![OpenCollective Backers](https://opencollective.com/zigtools/backers.svg?width=890&limit=1000)](https://opencollective.com/zigtools#category-CONTRIBUTE) diff --git a/schema.json b/schema.json index 554c6f036..31e559297 100644 --- a/schema.json +++ b/schema.json @@ -9,6 +9,11 @@ "type": "boolean", "default": "true" }, + "enable_argument_placeholders": { + "description": "Whether to enable function argument placeholder completions", + "type": "boolean", + "default": "true" + }, "enable_ast_check_diagnostics": { "description": "Whether to enable ast-check diagnostics", "type": "boolean", diff --git a/src/Config.zig b/src/Config.zig index 21496923e..27d1fe36f 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -7,6 +7,9 @@ /// Enables snippet completions when the client also supports them enable_snippets: bool = true, +/// Whether to enable function argument placeholder completions +enable_argument_placeholders: bool = true, + /// Whether to enable ast-check diagnostics enable_ast_check_diagnostics: bool = true, diff --git a/src/config_gen/config.json b/src/config_gen/config.json index 8d796e019..e0c3317e0 100644 --- a/src/config_gen/config.json +++ b/src/config_gen/config.json @@ -6,6 +6,12 @@ "type": "bool", "default": "true" }, + { + "name": "enable_argument_placeholders", + "description": "Whether to enable function argument placeholder completions", + "type": "bool", + "default": "true" + }, { "name": "enable_ast_check_diagnostics", "description": "Whether to enable ast-check diagnostics", @@ -168,4 +174,4 @@ "default": "null" } ] -} \ No newline at end of file +} diff --git a/src/features/completions.zig b/src/features/completions.zig index d0eac34d3..97c688b02 100644 --- a/src/features/completions.zig +++ b/src/features/completions.zig @@ -195,11 +195,33 @@ fn nodeToCompletion( const func = tree.fullFnProto(&buf, node).?; if (func.name_token) |name_token| { const use_snippets = server.config.enable_snippets and server.client_capabilities.supports_snippets; - const insert_text = if (use_snippets) blk: { - const skip_self_param = !(parent_is_type_val orelse true) and - try analyser.hasSelfParam(handle, func); - break :blk try Analyser.getFunctionSnippet(arena, tree, func, skip_self_param); - } else tree.tokenSlice(func.name_token.?); + + const insert_text = blk: { + const func_name = tree.tokenSlice(func.name_token.?); + + if (!use_snippets) break :blk func_name; + + const skip_self_param = !(parent_is_type_val orelse true) and try analyser.hasSelfParam(handle, func); + + const use_placeholders = server.config.enable_argument_placeholders; + if (use_placeholders) break :blk try Analyser.getFunctionSnippet(arena, tree, func, skip_self_param); + + switch (func.ast.params.len) { + // No arguments, leave cursor at the end + 0 => break :blk try std.fmt.allocPrint(arena, "{s}()", .{func_name}), + 1 => { + if (skip_self_param) { + // The one argument is a self parameter, leave cursor at the end + break :blk try std.fmt.allocPrint(arena, "{s}()", .{func_name}); + } + + // Non-self parameter, leave the cursor in the parentheses + break :blk try std.fmt.allocPrint(arena, "{s}(${{1:}})", .{func_name}); + }, + // Atleast one non-self parameter, leave the cursor in the parentheses + else => break :blk try std.fmt.allocPrint(arena, "{s}(${{1:}})", .{func_name}), + } + }; const is_type_function = Analyser.isTypeFunction(handle.tree, func);