Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify formatting of label details #1746

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const ClientCapabilities = struct {
hover_supports_md: bool = false,
signature_help_supports_md: bool = false,
completion_doc_supports_md: bool = false,
label_details_support: bool = false,
supports_configuration: bool = false,
supports_workspace_did_change_configuration_dynamic_registration: bool = false,
supports_textDocument_definition_linkSupport: bool = false,
Expand Down Expand Up @@ -432,7 +431,6 @@ fn initializeHandler(server: *Server, _: std.mem.Allocator, request: types.Initi
}
if (textDocument.completion) |completion| {
if (completion.completionItem) |completionItem| {
server.client_capabilities.label_details_support = completionItem.labelDetailsSupport orelse false;
server.client_capabilities.supports_snippets = completionItem.snippetSupport orelse false;
if (completionItem.documentationFormat) |documentation_format| {
for (documentation_format) |format| {
Expand Down
8 changes: 4 additions & 4 deletions src/analyser/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn dotCompletions(
try completions.append(arena, .{
.label = "len",
.kind = .Field,
.detail = try std.fmt.allocPrint(arena, "const len: usize = {d}", .{array_info.len}),
.detail = try std.fmt.allocPrint(arena, "usize = {d}", .{array_info.len}),
});
},
.struct_type => |struct_index| {
Expand Down Expand Up @@ -250,14 +250,14 @@ test "dotCompletions - array types" {
.{
.label = "len",
.kind = .Field,
.detail = "const len: usize = 3",
.detail = "usize = 3",
},
});
try testCompletion(&ip, try ip.getUnknown(gpa, @"[1]u8"), &.{
.{
.label = "len",
.kind = .Field,
.detail = "const len: usize = 1",
.detail = "usize = 1",
},
});
}
Expand Down Expand Up @@ -343,7 +343,7 @@ test "dotCompletions - single pointer indirection" {
.{
.label = "len",
.kind = .Field,
.detail = "const len: usize = 1",
.detail = "usize = 1",
},
});
try testCompletion(&ip, try ip.getUnknown(gpa, @"**[1]u32"), &.{});
Expand Down
107 changes: 64 additions & 43 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,9 @@

/// Gets a function's keyword, name, arguments and return value.
pub fn getFunctionSignature(tree: Ast, func: Ast.full.FnProto) []const u8 {
const start = offsets.tokenToLoc(tree, func.ast.fn_token);

const end = if (func.ast.return_type != 0)
offsets.tokenToLoc(tree, ast.lastToken(tree, func.ast.return_type))
else
start;
return tree.source[start.start..end.end];
const first_token = func.ast.fn_token;
const last_token = if (func.ast.return_type != 0) ast.lastToken(tree, func.ast.return_type) else first_token;
return offsets.tokensToSlice(tree, first_token, last_token);
}

fn formatSnippetPlaceholder(
Expand Down Expand Up @@ -289,56 +285,81 @@
return deref_type.eql(expected);
}

pub fn getVariableSignature(allocator: std.mem.Allocator, tree: Ast, var_decl: Ast.full.VarDecl) error{OutOfMemory}![]const u8 {
pub fn getVariableSignature(
allocator: std.mem.Allocator,
tree: Ast,
var_decl: Ast.full.VarDecl,
include_name: bool,
) error{OutOfMemory}![]const u8 {
const node_tags = tree.nodes.items(.tag);

const start_token = var_decl.ast.mut_token;
const end_token = blk: {
const init_node = var_decl.ast.init_node;
if (init_node == 0)
break :blk start_token + 1;
const start_token = if (include_name)
var_decl.ast.mut_token
else if (var_decl.ast.type_node != 0)
tree.firstToken(var_decl.ast.type_node)
else
tree.firstToken(var_decl.ast.init_node);

const init_node = var_decl.ast.init_node;
if (var_decl.ast.init_node == 0) {
if (var_decl.ast.type_node == 0) return "";

Check warning on line 305 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L305

Added line #L305 was not covered by tests
return offsets.tokensToSlice(tree, start_token, ast.lastToken(tree, var_decl.ast.type_node));
}

if (node_tags[init_node] == .merge_error_sets)
const end_token = switch (node_tags[init_node]) {
.merge_error_sets => {
return try std.fmt.allocPrint(allocator, "{s} error", .{
offsets.tokensToSlice(tree, start_token, tree.firstToken(init_node) - 1),
});
},
.error_set_decl => tree.firstToken(init_node),

Check warning on line 315 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L315

Added line #L315 was not covered by tests
.container_decl,
.container_decl_trailing,
.container_decl_arg,
.container_decl_arg_trailing,
.container_decl_two,
.container_decl_two_trailing,
.tagged_union,
.tagged_union_trailing,
.tagged_union_enum_tag,
.tagged_union_enum_tag_trailing,
.tagged_union_two,
.tagged_union_two_trailing,
=> blk: {
var buf: [2]Ast.Node.Index = undefined;
const container_decl = tree.fullContainerDecl(&buf, init_node).?;

if (node_tags[init_node] == .error_set_decl)
break :blk tree.firstToken(init_node);

var buf: [2]Ast.Node.Index = undefined;
const container_decl = tree.fullContainerDecl(&buf, init_node) orelse
break :blk ast.lastToken(tree, init_node);

var token = container_decl.ast.main_token;
var offset: Ast.TokenIndex = 0;
var token = container_decl.ast.main_token;
var offset: Ast.TokenIndex = 0;

// Tagged union: union(enum)
if (container_decl.ast.enum_token) |enum_token| {
token = enum_token;
offset += 1;
}
// Tagged union: union(enum)
if (container_decl.ast.enum_token) |enum_token| {
token = enum_token;
offset += 1;

Check warning on line 338 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L337-L338

Added lines #L337 - L338 were not covered by tests
}

// Backing integer: struct(u32), union(enum(u32))
// Tagged union: union(ComplexTypeTag)
if (container_decl.ast.arg != 0) {
token = ast.lastToken(tree, container_decl.ast.arg);
offset += 1;
}
// Backing integer: struct(u32), union(enum(u32))
// Tagged union: union(ComplexTypeTag)
if (container_decl.ast.arg != 0) {
token = ast.lastToken(tree, container_decl.ast.arg);
offset += 1;
}

break :blk token + offset;
break :blk token + offset;
},
else => ast.lastToken(tree, init_node),
};

return offsets.tokensToSlice(tree, start_token, end_token);
}

pub fn getContainerFieldSignature(tree: Ast, field: Ast.full.ContainerField) []const u8 {
if (field.ast.value_expr == 0 and field.ast.type_expr == 0 and field.ast.align_expr == 0) {
return ""; // TODO display the container's type
}
const start = offsets.tokenToIndex(tree, field.ast.main_token);
const end_node = if (field.ast.value_expr != 0) field.ast.value_expr else field.ast.type_expr;
const end = offsets.tokenToLoc(tree, ast.lastToken(tree, end_node)).end;
return tree.source[start..end];
pub fn getContainerFieldSignature(tree: Ast, field: Ast.full.ContainerField) ?[]const u8 {
if (field.ast.type_expr == 0) return null;
const end_node = if (field.ast.value_expr != 0) field.ast.value_expr else if (field.ast.align_expr != 0) field.ast.align_expr else field.ast.type_expr;

const first_token = tree.firstToken(field.ast.type_expr);
const last_token = ast.lastToken(tree, end_node);
return offsets.tokensToSlice(tree, first_token, last_token);
}

/// The node is the meta-type `type`
Expand Down
Loading
Loading