Skip to content
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
16 changes: 15 additions & 1 deletion src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,20 @@ fn kindToSortScore(kind: types.completion.Item.Kind) []const u8 {
};
}

fn itemSortScore(item: types.completion.Item) []const u8 {
// Completion items have two ways to mark deprecation; we need to check both.
const deprecated: bool = item.deprecated orelse if (item.tags) |tags|
std.mem.findScalar(types.completion.Item.Tag, tags, .Deprecated) != null
else
false;

if (deprecated) {
return "9";
} else {
return kindToSortScore(item.kind.?);
}
}

fn collectUsedMembersSet(builder: *Builder, likely: EnumLiteralContext.Likely, dot_token_index: Ast.TokenIndex) error{OutOfMemory}!std.BufSet {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
Expand Down Expand Up @@ -993,7 +1007,7 @@ pub fn completionAtIndex(
}

if (item.sortText == null) {
const score = kindToSortScore(item.kind.?);
const score = itemSortScore(item.*);
item.sortText = try std.fmt.allocPrint(arena, "{s}_{s}", .{ score, item.label });
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,30 @@ test "deprecated" {
});
}

test "deprecated sorting" {
try testCompletionWithOptions(
\\pub const Test = struct {
\\ pub const a = @compileError("Deprecated; some message");
\\ pub const b = true;
\\};
\\const foo = Test.<cursor>
, &.{
.{
.label = "b",
.kind = .Constant,
.deprecated = false,
},
.{
.label = "a",
.kind = .Constant,
.documentation = "Deprecated; some message",
.deprecated = true,
},
}, .{
.check_order = true,
});
}

test "declarations" {
try testCompletion(
\\const S = struct {
Expand Down Expand Up @@ -4407,6 +4431,7 @@ fn testCompletionWithOptions(
enable_argument_placeholders: bool = true,
enable_snippets: bool = true,
completion_label_details: bool = true,
check_order: bool = false,
},
) !void {
const cursor_idx = std.mem.find(u8, source, "<cursor>").?;
Expand Down Expand Up @@ -4583,6 +4608,26 @@ fn testCompletionWithOptions(
try error_builder.msgAtIndex("invalid completions\n{s}", test_uri.raw, cursor_idx, .err, .{buffer.items});
return error.MissingOrUnexpectedCompletions;
}

if (options.check_order) {
const Item = types.completion.Item;

const items: []Item = try allocator.dupe(Item, completion_list.items);
defer allocator.free(items);

std.mem.sort(Item, items, {}, struct {
fn sort(_: void, lhs: Item, rhs: Item) bool {
return std.mem.lessThan(u8, lhs.sortText.?, rhs.sortText.?);
}
}.sort);

for (0..expected_completions.len) |i| {
const expected_completion = expected_completions[i];
const actual_completion = items[i];

try std.testing.expectEqualStrings(expected_completion.label, actual_completion.label);
}
}
}

fn extractCompletionLabels(items: anytype) error{ DuplicateCompletionLabel, OutOfMemory }!std.StringArrayHashMapUnmanaged(void) {
Expand Down