diff --git a/src/analysis.zig b/src/analysis.zig index d9c8118..4c60e08 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -111,7 +111,7 @@ pub fn getFunctionSnippet( func: ast.full.FnProto, skip_self_param: bool, ) ![]const u8 { - const name_index = func.name_token orelse unreachable; + const name_index = func.name_token.?; var buffer = std.ArrayList(u8).init(allocator); try buffer.ensureCapacity(128); @@ -2601,7 +2601,7 @@ fn makeScopeInternal( if (isContainer(tree, node_idx)) { var buf: [2]ast.Node.Index = undefined; - const ast_decls = declMembers(tree,node_idx, &buf); + const ast_decls = declMembers(tree, node_idx, &buf); (try scopes.addOne(allocator)).* = .{ .range = nodeSourceRange(tree, node_idx), @@ -2638,10 +2638,10 @@ fn makeScopeInternal( (try error_completions.addOne(allocator)).* = .{ .label = name, .kind = .Constant, - .documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| - .{ .kind = .Markdown, .value = docs } - else - null, + .documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| .{ + .kind = .Markdown, + .value = docs, + } else null, }; } @@ -2676,7 +2676,9 @@ fn makeScopeInternal( if (container_decl) |container| { const kind = token_tags[container.ast.main_token]; - if (empty_field and (kind == .keyword_struct or (kind == .keyword_union and container.ast.arg == 0))) { + if (empty_field and + (kind == .keyword_struct or (kind == .keyword_union and container.ast.arg == 0))) + { continue; } @@ -2684,10 +2686,10 @@ fn makeScopeInternal( (try enum_completions.addOne(allocator)).* = .{ .label = name, .kind = .Constant, - .documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| - .{ .kind = .Markdown, .value = docs } - else - null, + .documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| .{ + .kind = .Markdown, + .value = docs, + } else null, }; } } @@ -2926,20 +2928,17 @@ fn makeScopeInternal( std.debug.assert(token_tags[name_token] == .identifier); const name = tree.tokenSlice(name_token); - try scope.decls.putNoClobber(name, if (is_for) - .{ - .array_payload = .{ - .identifier = name_token, - .array_expr = while_node.ast.cond_expr, - }, - } - else - .{ - .pointer_payload = .{ - .name = name_token, - .condition = while_node.ast.cond_expr, - }, - }); + try scope.decls.putNoClobber(name, if (is_for) .{ + .array_payload = .{ + .identifier = name_token, + .array_expr = while_node.ast.cond_expr, + }, + } else .{ + .pointer_payload = .{ + .name = name_token, + .condition = while_node.ast.cond_expr, + }, + }); // for loop with index as well if (token_tags[name_token + 1] == .comma) { diff --git a/src/main.zig b/src/main.zig index 685b25b..99cdcdd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -297,10 +297,14 @@ fn typeToCompletion( try list.append(.{ .label = "len", .kind = .Field, + .insertText = "len", + .insertTextFormat = .PlainText, }); try list.append(.{ .label = "ptr", .kind = .Field, + .insertText = "ptr", + .insertTextFormat = .PlainText, }); } }, @@ -310,6 +314,8 @@ fn typeToCompletion( try list.append(.{ .label = "*", .kind = .Operator, + .insertText = "*", + .insertTextFormat = .PlainText, }); } try nodeToCompletion( @@ -425,7 +431,7 @@ fn nodeToCompletion( } else false; break :blk try analysis.getFunctionSnippet(&arena.allocator, tree, func, skip_self_param); - } else ""; + } else tree.tokenSlice(func.name_token.?); const is_type_function = analysis.isTypeFunction(handle.tree, func); @@ -462,6 +468,8 @@ fn nodeToCompletion( .kind = if (is_const) .Constant else .Variable, .documentation = doc, .detail = analysis.getVariableSignature(tree, var_decl), + .insertText = tree.tokenSlice(var_decl.ast.mut_token + 1), + .insertTextFormat = .PlainText, }); }, .container_field, @@ -474,6 +482,8 @@ fn nodeToCompletion( .kind = .Field, .documentation = doc, .detail = analysis.getContainerFieldSignature(handle.tree, field), + .insertText = tree.tokenSlice(field.ast.name_token), + .insertTextFormat = .PlainText, }); }, .array_type, @@ -482,6 +492,8 @@ fn nodeToCompletion( try list.append(.{ .label = "len", .kind = .Field, + .insertText = "len", + .insertTextFormat = .PlainText, }); }, .ptr_type, @@ -496,11 +508,23 @@ fn nodeToCompletion( try list.append(.{ .label = "*", .kind = .Operator, + .insertText = "*", + .insertTextFormat = .PlainText, }); }, .Slice => { - try list.append(.{ .label = "ptr", .kind = .Field }); - try list.append(.{ .label = "len", .kind = .Field }); + try list.append(.{ + .label = "ptr", + .kind = .Field, + .insertText = "ptr", + .insertTextFormat = .PlainText, + }); + try list.append(.{ + .label = "len", + .kind = .Field, + .insertText = "len", + .insertTextFormat = .PlainText, + }); return; }, } @@ -515,6 +539,8 @@ fn nodeToCompletion( try list.append(.{ .label = "?", .kind = .Operator, + .insertText = "?", + .insertTextFormat = .PlainText, }); } return; @@ -523,6 +549,8 @@ fn nodeToCompletion( try list.append(.{ .label = "len", .kind = .Field, + .insertText = "len", + .insertTextFormat = .PlainText, }); }, else => if (analysis.nodeToString(tree, node)) |string| { @@ -531,6 +559,8 @@ fn nodeToCompletion( .kind = .Field, .documentation = doc, .detail = tree.getNodeSource(node), + .insertText = string, + .insertTextFormat = .PlainText, }); }, } @@ -963,36 +993,48 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: analysis.Decl .kind = .Constant, .documentation = doc, .detail = tree.source[offsets.tokenLocation(tree, first_token).start..offsets.tokenLocation(tree, last_token).end], + .insertText = tree.tokenSlice(param.name_token.?), + .insertTextFormat = .PlainText, }); }, .pointer_payload => |payload| { try context.completions.append(.{ .label = tree.tokenSlice(payload.name), .kind = .Variable, + .insertText = tree.tokenSlice(payload.name), + .insertTextFormat = .PlainText, }); }, .array_payload => |payload| { try context.completions.append(.{ .label = tree.tokenSlice(payload.identifier), .kind = .Variable, + .insertText = tree.tokenSlice(payload.identifier), + .insertTextFormat = .PlainText, }); }, .array_index => |payload| { try context.completions.append(.{ .label = tree.tokenSlice(payload), .kind = .Variable, + .insertText = tree.tokenSlice(payload), + .insertTextFormat = .PlainText, }); }, .switch_payload => |payload| { try context.completions.append(.{ .label = tree.tokenSlice(payload.node), .kind = .Variable, + .insertText = tree.tokenSlice(payload.node), + .insertTextFormat = .PlainText, }); }, .label_decl => |label_decl| { try context.completions.append(.{ .label = tree.tokenSlice(label_decl), .kind = .Variable, + .insertText = tree.tokenSlice(label_decl), + .insertTextFormat = .PlainText, }); }, } @@ -1044,15 +1086,15 @@ fn completeBuiltin(arena: *std.heap.ArenaAllocator, id: types.RequestId, config: } else { insert_text = builtin.name; } - builtin_completions.?[idx].insertText = + builtin_completions.?[idx].insertText = if (config.include_at_in_builtins) - insert_text - else - insert_text[1..]; + insert_text + else + insert_text[1..]; } truncateCompletions(builtin_completions.?, config.max_detail_length); } - + try send(arena, types.Response{ .id = id, .result = .{ @@ -1115,9 +1157,9 @@ fn completeFieldAccess( fn completeError( arena: *std.heap.ArenaAllocator, - id: types.RequestId, + id: types.RequestId, handle: *DocumentStore.Handle, - config: Config + config: Config, ) !void { const completions = try document_store.errorCompletionItems(arena, handle); truncateCompletions(completions, config.max_detail_length); @@ -1135,13 +1177,13 @@ fn completeError( fn completeDot( arena: *std.heap.ArenaAllocator, - id: types.RequestId, + id: types.RequestId, handle: *DocumentStore.Handle, - config: Config + config: Config, ) !void { var completions = try document_store.enumCompletionItems(arena, handle); truncateCompletions(completions, config.max_detail_length); - + try send(arena, types.Response{ .id = id, .result = .{