Skip to content

Commit

Permalink
update sort score of completions item kinds
Browse files Browse the repository at this point in the history
This could still be improved but should still be better over the current behaviour.
Related #1792
  • Loading branch information
Techatrix committed Jul 15, 2024
1 parent 623e2f2 commit efccfb7
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -578,32 +578,27 @@ fn completeFieldAccess(builder: *Builder, loc: offsets.Loc) error{OutOfMemory}!v
try typeToCompletion(builder, ty);
}

fn kindToSortScore(kind: types.CompletionItemKind) ?[]const u8 {
fn kindToSortScore(kind: types.CompletionItemKind) []const u8 {
return switch (kind) {
.Module => "1_", // use for packages
.Folder => "2_",
.File => "3_",

.Constant => "1_",

.Variable => "2_",
.Field => "3_",
.Function, .Method => "4_",

.Keyword, .Snippet, .EnumMember => "5_",

.Class,
.Interface,
.Module => "1", // used for packages
.Folder => "2",
.File => "3",

.Operator => "1",
.Field, .EnumMember => "2",
.Method => "3",
.Function => "4",
.Text, // used for labels
.Constant,
.Variable,
.Struct,
.Enum,
// Union?
.TypeParameter,
=> "6_",
=> "5",
.Snippet => "6",
.Keyword => "7",

else => {
log.debug(@typeName(types.CompletionItemKind) ++ ".{s} has no sort score specified!", .{@tagName(kind)});
return null;
},
else => unreachable,
};
}

Expand Down Expand Up @@ -879,10 +874,8 @@ pub fn completionAtIndex(
}
}

// TODO: config for sorting rule?
const prefix = kindToSortScore(item.kind.?) orelse continue;

item.sortText = try std.fmt.allocPrint(arena, "{s}{s}", .{ prefix, item.label });
const score = kindToSortScore(item.kind.?);
item.sortText = try std.fmt.allocPrint(arena, "{s}_{s}", .{ score, item.label });
}

return .{ .isIncomplete = false, .items = completions };
Expand Down

0 comments on commit efccfb7

Please sign in to comment.