From a8dcb89a81a316cbd32f938ca2677767b2b3606d Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Sat, 24 Sep 2022 21:26:55 +0200 Subject: [PATCH] refactor getting the first & last parameter token into a function --- src/Server.zig | 14 ++++---------- src/ast.zig | 11 +++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Server.zig b/src/Server.zig index 57fffb1..833e1ef 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -724,11 +724,8 @@ fn hoverSymbol( doc_str = try analysis.collectDocComments(server.arena.allocator(), handle.tree, doc_comments, hover_kind, false); } - const first_token = param.first_doc_comment orelse - param.comptime_noalias orelse - param.name_token orelse - tree.firstToken(param.type_expr); // extern fn - const last_token = param.anytype_ellipsis3 orelse tree.lastToken(param.type_expr); + const first_token = ast.paramFirstToken(tree, param); + const last_token = ast.paramLastToken(tree, param); const start = offsets.tokenToIndex(tree, first_token); const end = offsets.tokenToLoc(tree, last_token).end; @@ -1052,11 +1049,8 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: analysis.Decl else null; - const first_token = param.first_doc_comment orelse - param.comptime_noalias orelse - param.name_token orelse - tree.firstToken(param.type_expr); - const last_token = param.anytype_ellipsis3 orelse tree.lastToken(param.type_expr); + const first_token = ast.paramFirstToken(tree, param); + const last_token = ast.paramLastToken(tree, param); try context.completions.append(allocator, .{ .label = tree.tokenSlice(param.name_token.?), diff --git a/src/ast.zig b/src/ast.zig index 13c55c2..29d433a 100644 --- a/src/ast.zig +++ b/src/ast.zig @@ -896,6 +896,17 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex { }; } +pub fn paramFirstToken(tree: Ast, param: Ast.full.FnProto.Param) Ast.TokenIndex { + return param.first_doc_comment orelse + param.comptime_noalias orelse + param.name_token orelse + tree.firstToken(param.type_expr); +} + +pub fn paramLastToken(tree: Ast, param: Ast.full.FnProto.Param) Ast.TokenIndex { + return param.anytype_ellipsis3 orelse tree.lastToken(param.type_expr); +} + pub fn containerField(tree: Ast, node: Ast.Node.Index) ?Ast.full.ContainerField { return switch (tree.nodes.items(.tag)[node]) { .container_field => tree.containerField(node),