From b625eb763eca0b4a73c323a21a5cc82f9eae8caa Mon Sep 17 00:00:00 2001 From: ryuukk <44361234+ryuukk@users.noreply.github.com> Date: Wed, 22 Jun 2022 02:21:21 +0200 Subject: [PATCH] Sort completion items --- src/main.zig | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index a55e22b..b527c36 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1076,6 +1076,9 @@ fn completeLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index .orig_handle = handle, }; try analysis.iterateLabels(handle, pos_index, declToCompletion, context); + + + sortCompletionItems(completions.items, config); truncateCompletions(completions.items, config.max_detail_length); try send(arena, types.Response{ @@ -1148,6 +1151,8 @@ fn completeGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_inde .orig_handle = handle, }; try analysis.iterateSymbolsGlobal(&document_store, arena, handle, pos_index, declToCompletion, context); + + sortCompletionItems(completions.items, config); truncateCompletions(completions.items, config.max_detail_length); try send(arena, types.Response{ @@ -1175,6 +1180,8 @@ fn completeFieldAccess(arena: *std.heap.ArenaAllocator, id: types.RequestId, han if (try analysis.getFieldAccessType(&document_store, arena, handle, position.absolute_index, &tokenizer)) |result| { held_range.release(); try typeToCompletion(arena, &completions, result, handle, config); + + sortCompletionItems(completions.items, config); truncateCompletions(completions.items, config.max_detail_length); } @@ -1193,7 +1200,8 @@ fn completeError(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: * const tracy_zone = tracy.trace(@src()); defer tracy_zone.end(); - const completions = try document_store.errorCompletionItems(arena, handle); + var completions = try document_store.errorCompletionItems(arena, handle); + truncateCompletions(completions, config.max_detail_length); logger.debug("Completing error:", .{}); @@ -1208,11 +1216,39 @@ fn completeError(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: * }); } +fn kindToSortScore(kind: types.CompletionItem.Kind) [] const u8 { + return switch (kind) + { + .Variable => "2_", + .Field => "3_", + .Function => "4_", + + .Keyword, + .EnumMember => "5_", + + .Class, + .Interface, + .Struct, + // Union? + .TypeParameter => "6_", + + else => "9_" + }; +} + +fn sortCompletionItems(completions: []types.CompletionItem, config: *const Config) void { + // TODO: config for sorting rule? + for (completions) |*c| { + c.sortText = kindToSortScore(c.kind); + } +} + fn completeDot(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, config: *const Config) !void { const tracy_zone = tracy.trace(@src()); defer tracy_zone.end(); var completions = try document_store.enumCompletionItems(arena, handle); + sortCompletionItems(completions, config); truncateCompletions(completions, config.max_detail_length); try send(arena, types.Response{