Skip to content

Commit

Permalink
make argument placeholders optional (#1317)
Browse files Browse the repository at this point in the history
* 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 <leecannon@leecannon.xyz>
  • Loading branch information
jiacai2050 and leecannon authored Jul 16, 2023
1 parent 8225cba commit 73033c3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
8 changes: 7 additions & 1 deletion src/config_gen/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -168,4 +174,4 @@
"default": "null"
}
]
}
}
32 changes: 27 additions & 5 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 73033c3

Please sign in to comment.