Merge branch 'master' into label_details_support

This commit is contained in:
Auguste Rame 2022-07-07 03:15:05 -04:00 committed by GitHub
commit acb339ab95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 10 deletions

View File

@ -190,6 +190,8 @@ fn loadPackages(context: LoadPackagesContext) !void {
}; };
} }
} }
} else {
return error.RunFailed;
} }
}, },
else => return error.RunFailed, else => return error.RunFailed,
@ -268,7 +270,7 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: [:0]u8) anyerror!*Ha
.cache_root = self.zig_cache_root, .cache_root = self.zig_cache_root,
.global_cache_root = self.zig_global_cache_root, .global_cache_root = self.zig_global_cache_root,
}) catch |err| { }) catch |err| {
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err }); log.warn("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err });
}; };
try self.build_files.append(self.allocator, build_file); try self.build_files.append(self.allocator, build_file);
@ -537,7 +539,7 @@ pub fn applySave(self: *DocumentStore, handle: *Handle) !void {
.cache_root = self.zig_cache_root, .cache_root = self.zig_cache_root,
.global_cache_root = self.zig_global_cache_root, .global_cache_root = self.zig_global_cache_root,
}) catch |err| { }) catch |err| {
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err }); log.warn("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err });
}; };
} }
} }

View File

@ -1081,7 +1081,7 @@ fn completeLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index
.orig_handle = handle, .orig_handle = handle,
}; };
try analysis.iterateLabels(handle, pos_index, declToCompletion, context); try analysis.iterateLabels(handle, pos_index, declToCompletion, context);
sortCompletionItems(completions.items, config); sortCompletionItems(completions.items, config, arena.allocator());
truncateCompletions(completions.items, config.max_detail_length); truncateCompletions(completions.items, config.max_detail_length);
try send(arena, types.Response{ try send(arena, types.Response{
@ -1154,7 +1154,7 @@ fn completeGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_inde
.orig_handle = handle, .orig_handle = handle,
}; };
try analysis.iterateSymbolsGlobal(&document_store, arena, handle, pos_index, declToCompletion, context); try analysis.iterateSymbolsGlobal(&document_store, arena, handle, pos_index, declToCompletion, context);
sortCompletionItems(completions.items, config); sortCompletionItems(completions.items, config, arena.allocator());
truncateCompletions(completions.items, config.max_detail_length); truncateCompletions(completions.items, config.max_detail_length);
if (client_capabilities.label_details_support) { if (client_capabilities.label_details_support) {
@ -1188,7 +1188,7 @@ fn completeFieldAccess(arena: *std.heap.ArenaAllocator, id: types.RequestId, han
if (try analysis.getFieldAccessType(&document_store, arena, handle, position.absolute_index, &tokenizer)) |result| { if (try analysis.getFieldAccessType(&document_store, arena, handle, position.absolute_index, &tokenizer)) |result| {
held_range.release(); held_range.release();
try typeToCompletion(arena, &completions, result, handle, config); try typeToCompletion(arena, &completions, result, handle, config);
sortCompletionItems(completions.items, config); sortCompletionItems(completions.items, config, arena.allocator());
truncateCompletions(completions.items, config.max_detail_length); truncateCompletions(completions.items, config.max_detail_length);
if (client_capabilities.label_details_support) { if (client_capabilities.label_details_support) {
for (completions.items) |*item| { for (completions.items) |*item| {
@ -1397,7 +1397,10 @@ fn completeError(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *
} }
fn kindToSortScore(kind: types.CompletionItem.Kind) [] const u8 { fn kindToSortScore(kind: types.CompletionItem.Kind) [] const u8 {
return switch (kind) { return switch (kind)
{
.Constant => "1_",
.Variable => "2_", .Variable => "2_",
.Field => "3_", .Field => "3_",
.Function => "4_", .Function => "4_",
@ -1415,10 +1418,16 @@ fn kindToSortScore(kind: types.CompletionItem.Kind) [] const u8 {
}; };
} }
fn sortCompletionItems(completions: []types.CompletionItem, _: *const Config) void { fn sortCompletionItems(completions: []types.CompletionItem, _: *const Config, alloc: std.mem.Allocator) void {
// TODO: config for sorting rule? // TODO: config for sorting rule?
for (completions) |*c| { for (completions) |*c| {
c.sortText = kindToSortScore(c.kind); c.sortText = kindToSortScore(c.kind);
if (alloc.alloc(u8, 2 + c.label.len)) |it| {
std.mem.copy(u8, it, c.sortText.?);
std.mem.copy(u8, it[2 .. ], c.label);
c.sortText = it;
} else |_| {}
} }
} }
@ -1427,7 +1436,7 @@ fn completeDot(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *Do
defer tracy_zone.end(); defer tracy_zone.end();
var completions = try document_store.enumCompletionItems(arena, handle); var completions = try document_store.enumCompletionItems(arena, handle);
sortCompletionItems(completions, config); sortCompletionItems(completions, config, arena.allocator());
truncateCompletions(completions, config.max_detail_length); truncateCompletions(completions, config.max_detail_length);
try send(arena, types.Response{ try send(arena, types.Response{
@ -2094,6 +2103,8 @@ pub fn main() anyerror!void {
if (!args_it.skip()) @panic("Could not find self argument"); if (!args_it.skip()) @panic("Could not find self argument");
var config_path: ?[]const u8 = null; var config_path: ?[]const u8 = null;
defer if (config_path) |path| allocator.free(path);
var next_arg_config_path = false; var next_arg_config_path = false;
while (args_it.next()) |arg| { while (args_it.next()) |arg| {
if (next_arg_config_path) { if (next_arg_config_path) {
@ -2133,13 +2144,14 @@ pub fn main() anyerror!void {
config_read: { config_read: {
if (config_path) |path| { if (config_path) |path| {
defer allocator.free(path);
if (loadConfigFile(path)) |conf| { if (loadConfigFile(path)) |conf| {
config = conf; config = conf;
break :config_read; break :config_read;
} }
std.debug.print("Could not open configuration file '{s}'\n", .{path}); std.debug.print("Could not open configuration file '{s}'\n", .{path});
std.debug.print("Falling back to a lookup in the local and global configuration folders\n", .{}); std.debug.print("Falling back to a lookup in the local and global configuration folders\n", .{});
allocator.free(path);
config_path = null;
} }
if (try known_folders.getPath(allocator, .local_configuration)) |path| { if (try known_folders.getPath(allocator, .local_configuration)) |path| {
config_path = path; config_path = path;

View File

@ -261,6 +261,7 @@ pub const CompletionItem = struct {
pub const CompletionItemLabelDetails = struct { pub const CompletionItemLabelDetails = struct {
detail: ?string, detail: ?string,
description: ?string, description: ?string,
sortText: ?string = null,
}; };
pub const DocumentSymbol = struct { pub const DocumentSymbol = struct {

View File

@ -186,7 +186,7 @@ test "Request completion with no trailing whitespace" {
try server.request("textDocument/completion", try server.request("textDocument/completion",
\\{"textDocument":{"uri":"file:///test.zig"}, "position":{"line":1,"character":1}} \\{"textDocument":{"uri":"file:///test.zig"}, "position":{"line":1,"character":1}}
, ,
\\{"isIncomplete":false,"items":[{"label":"std","kind":21,"textEdit":null,"filterText":null,"insertText":"std","insertTextFormat":1,"detail":"const std = @import(\"std\")","documentation":null}]} \\{"isIncomplete":false,"items":[{"label":"std","kind":21,"textEdit":null,"filterText":null,"insertText":"std","insertTextFormat":1,"detail":"const std = @import(\"std\")","documentation":null,"sortText":"1_std"}]}
); );
} }