Skip to content

Commit 854231c

Browse files
Fix snippets of aliased functions
1 parent 73033c3 commit 854231c

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

src/analysis.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,24 +152,26 @@ fn fmtSnippetPlaceholder(bytes: []const u8) SnippetPlaceholderFormatter {
152152
}
153153

154154
/// Creates snippet insert text for a function. Caller owns returned memory.
155-
pub fn getFunctionSnippet(allocator: std.mem.Allocator, tree: Ast, func: Ast.full.FnProto, skip_self_param: bool) ![]const u8 {
156-
const name_index = func.name_token.?;
155+
pub fn getFunctionSnippet(
156+
allocator: std.mem.Allocator,
157+
name: []const u8,
158+
iterator: *Ast.full.FnProto.Iterator,
159+
) ![]const u8 {
160+
const tree = iterator.tree.*;
157161

158162
var buffer = std.ArrayListUnmanaged(u8){};
159163
try buffer.ensureTotalCapacity(allocator, 128);
160164

161165
var buf_stream = buffer.writer(allocator);
162166

163-
try buf_stream.writeAll(tree.tokenSlice(name_index));
167+
try buf_stream.writeAll(name);
164168
try buf_stream.writeByte('(');
165169

166170
const token_tags = tree.tokens.items(.tag);
167171

168-
var it = func.iterate(&tree);
169172
var i: usize = 0;
170-
while (ast.nextFnParam(&it)) |param| : (i += 1) {
171-
if (skip_self_param and i == 0) continue;
172-
if (i != @intFromBool(skip_self_param))
173+
while (ast.nextFnParam(iterator)) |param| : (i += 1) {
174+
if (i != 0)
173175
try buf_stream.writeAll(", ${")
174176
else
175177
try buf_stream.writeAll("${");

src/features/completions.zig

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ fn typeToCompletion(
6868
.{ .node = n, .handle = type_handle.handle },
6969
field_access.unwrapped,
7070
orig_handle,
71+
null,
72+
null,
7173
type_handle.type.is_type_val,
7274
null,
7375
either_descriptor,
@@ -123,6 +125,8 @@ fn nodeToCompletion(
123125
node_handle: Analyser.NodeWithHandle,
124126
unwrapped: ?Analyser.TypeWithHandle,
125127
orig_handle: *const DocumentStore.Handle,
128+
orig_name: ?[]const u8,
129+
orig_doc: ?[]const u8,
126130
is_type_val: bool,
127131
parent_is_type_val: ?bool,
128132
either_descriptor: ?[]const u8,
@@ -137,11 +141,19 @@ fn nodeToCompletion(
137141
const datas = tree.nodes.items(.data);
138142
const token_tags = tree.tokens.items(.tag);
139143

140-
var doc_comments = try Analyser.getDocComments(arena, handle.tree, node);
141-
if (doc_comments == null) {
142-
if (try analyser.resolveVarDeclAlias(node_handle)) |result| {
143-
doc_comments = try result.docComments(arena);
144-
}
144+
var doc_comments = orig_doc orelse (try Analyser.getDocComments(arena, handle.tree, node));
145+
if (try analyser.resolveVarDeclAlias(node_handle)) |result| {
146+
const context = DeclToCompletionContext{
147+
.server = server,
148+
.analyser = analyser,
149+
.arena = arena,
150+
.completions = list,
151+
.orig_handle = orig_handle,
152+
.orig_name = Analyser.getDeclName(tree, node),
153+
.orig_doc = doc_comments,
154+
.either_descriptor = either_descriptor,
155+
};
156+
return try declToCompletion(context, result);
145157
}
146158

147159
const doc = try completionDoc(
@@ -194,17 +206,20 @@ fn nodeToCompletion(
194206
var buf: [1]Ast.Node.Index = undefined;
195207
const func = tree.fullFnProto(&buf, node).?;
196208
if (func.name_token) |name_token| {
209+
const func_name = orig_name orelse tree.tokenSlice(name_token);
197210
const use_snippets = server.config.enable_snippets and server.client_capabilities.supports_snippets;
198211

199212
const insert_text = blk: {
200-
const func_name = tree.tokenSlice(func.name_token.?);
201-
202213
if (!use_snippets) break :blk func_name;
203214

204215
const skip_self_param = !(parent_is_type_val orelse true) and try analyser.hasSelfParam(handle, func);
205216

206217
const use_placeholders = server.config.enable_argument_placeholders;
207-
if (use_placeholders) break :blk try Analyser.getFunctionSnippet(arena, tree, func, skip_self_param);
218+
if (use_placeholders) {
219+
var it = func.iterate(&tree);
220+
if (skip_self_param) _ = ast.nextFnParam(&it);
221+
break :blk try Analyser.getFunctionSnippet(arena, func_name, &it);
222+
}
208223

209224
switch (func.ast.params.len) {
210225
// No arguments, leave cursor at the end
@@ -226,7 +241,7 @@ fn nodeToCompletion(
226241
const is_type_function = Analyser.isTypeFunction(handle.tree, func);
227242

228243
try list.append(arena, .{
229-
.label = handle.tree.tokenSlice(name_token),
244+
.label = func_name,
230245
.kind = if (is_type_function) .Struct else .Function,
231246
.documentation = doc,
232247
.detail = Analyser.getFunctionSignature(handle.tree, func),
@@ -241,14 +256,15 @@ fn nodeToCompletion(
241256
.simple_var_decl,
242257
=> {
243258
const var_decl = tree.fullVarDecl(node).?;
259+
const name = orig_name orelse tree.tokenSlice(var_decl.ast.mut_token + 1);
244260
const is_const = token_tags[var_decl.ast.mut_token] == .keyword_const;
245261

246262
try list.append(arena, .{
247-
.label = handle.tree.tokenSlice(var_decl.ast.mut_token + 1),
263+
.label = name,
248264
.kind = if (is_const) .Constant else .Variable,
249265
.documentation = doc,
250266
.detail = try Analyser.getVariableSignature(arena, tree, var_decl),
251-
.insertText = tree.tokenSlice(var_decl.ast.mut_token + 1),
267+
.insertText = name,
252268
.insertTextFormat = .PlainText,
253269
});
254270
},
@@ -257,12 +273,13 @@ fn nodeToCompletion(
257273
.container_field_init,
258274
=> {
259275
const field = tree.fullContainerField(node).?;
276+
const name = tree.tokenSlice(field.ast.main_token);
260277
try list.append(arena, .{
261-
.label = handle.tree.tokenSlice(field.ast.main_token),
278+
.label = name,
262279
.kind = if (field.ast.tuple_like) .EnumMember else .Field,
263280
.documentation = doc,
264281
.detail = Analyser.getContainerFieldSignature(handle.tree, field),
265-
.insertText = tree.tokenSlice(field.ast.main_token),
282+
.insertText = name,
266283
.insertTextFormat = .PlainText,
267284
});
268285
},
@@ -357,6 +374,8 @@ const DeclToCompletionContext = struct {
357374
arena: std.mem.Allocator,
358375
completions: *std.ArrayListUnmanaged(types.CompletionItem),
359376
orig_handle: *const DocumentStore.Handle,
377+
orig_name: ?[]const u8 = null,
378+
orig_doc: ?[]const u8 = null,
360379
parent_is_type_val: ?bool = null,
361380
either_descriptor: ?[]const u8 = null,
362381
};
@@ -393,12 +412,15 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: Analyser.Decl
393412
.{ .node = node, .handle = decl_handle.handle },
394413
null,
395414
context.orig_handle,
415+
context.orig_name,
416+
context.orig_doc,
396417
false,
397418
context.parent_is_type_val,
398419
context.either_descriptor,
399420
),
400421
.param_payload => |pay| {
401422
const param = pay.param;
423+
const name = tree.tokenSlice(param.name_token.?);
402424
const doc = try completionDoc(
403425
context.server,
404426
context.arena,
@@ -407,11 +429,11 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: Analyser.Decl
407429
);
408430

409431
try context.completions.append(context.arena, .{
410-
.label = tree.tokenSlice(param.name_token.?),
432+
.label = name,
411433
.kind = .Constant,
412434
.documentation = doc,
413435
.detail = ast.paramSlice(tree, param),
414-
.insertText = tree.tokenSlice(param.name_token.?),
436+
.insertText = name,
415437
.insertTextFormat = .PlainText,
416438
});
417439
},
@@ -792,11 +814,12 @@ pub fn collectContainerFields(
792814
const container_decl = Ast.fullContainerDecl(container.handle.tree, &buffer, node) orelse return;
793815
for (container_decl.ast.members) |member| {
794816
const field = container.handle.tree.fullContainerField(member) orelse continue;
817+
const name = container.handle.tree.tokenSlice(field.ast.main_token);
795818
try completions.append(arena, .{
796-
.label = container.handle.tree.tokenSlice(field.ast.main_token),
819+
.label = name,
797820
.kind = if (field.ast.tuple_like) .EnumMember else .Field,
798821
.detail = Analyser.getContainerFieldSignature(container.handle.tree, field),
799-
.insertText = container.handle.tree.tokenSlice(field.ast.main_token),
822+
.insertText = name,
800823
.insertTextFormat = .PlainText,
801824
});
802825
}

0 commit comments

Comments
 (0)