feat: first (mostly) working version of goto_definition command

Tested only with zls and clangd so far.
This commit is contained in:
CJ van den Berg 2024-04-05 22:52:47 +02:00
parent 7e17f713e7
commit fc15c8bda6
6 changed files with 186 additions and 56 deletions

View file

@ -3,6 +3,7 @@ const tp = @import("thespian");
const cbor = @import("cbor");
const log = @import("log");
const tracy = @import("tracy");
const FileType = @import("syntax").FileType;
const Project = @import("Project.zig");
@ -57,11 +58,12 @@ pub fn query_recent_files(max: usize, query: []const u8) tp.result {
return (try get()).pid.send(.{ "query_recent_files", project, max, query });
}
pub fn did_open(file_path: []const u8, file_type: []const u8, version: usize, text: []const u8) tp.result {
pub fn did_open(file_path: []const u8, file_type: *const FileType, version: usize, text: []const u8) tp.result {
const project = tp.env.get().str("project");
if (project.len == 0)
return tp.exit("No project");
return (try get()).pid.send(.{ "did_open", project, file_path, file_type, version, @intFromPtr(text.ptr), text.len });
const text_ptr: usize = if (text.len > 0) @intFromPtr(text.ptr) else 0;
return (try get()).pid.send(.{ "did_open", project, file_path, file_type.name, file_type.language_server, version, text_ptr, text.len });
}
pub fn goto_definition(file_path: []const u8, row: usize, col: usize) tp.result {
@ -119,6 +121,7 @@ const Process = struct {
var path: []const u8 = undefined;
var query: []const u8 = undefined;
var file_type: []const u8 = undefined;
var language_server: []const u8 = undefined;
var high: i64 = 0;
var low: i64 = 0;
var max: usize = 0;
@ -144,15 +147,16 @@ const Process = struct {
std.time.milliTimestamp() - project.open_time,
});
} else if (try m.match(.{ "open", tp.extract(&project_directory) })) {
self.open(project_directory) catch |e| return from.send_raw(tp.exit_message(e));
self.open(project_directory) catch |e| return from.forward_error(e);
} else if (try m.match(.{ "request_recent_files", tp.extract(&project_directory), tp.extract(&max) })) {
self.request_recent_files(from, project_directory, max) catch |e| return from.send_raw(tp.exit_message(e));
self.request_recent_files(from, project_directory, max) catch |e| return from.forward_error(e);
} else if (try m.match(.{ "query_recent_files", tp.extract(&project_directory), tp.extract(&max), tp.extract(&query) })) {
self.query_recent_files(from, project_directory, max, query) catch |e| return from.send_raw(tp.exit_message(e));
} else if (try m.match(.{ "did_open", tp.extract(&project_directory), tp.extract(&path), tp.extract(&file_type), tp.extract(&version), tp.extract(&text_ptr), tp.extract(&text_len) })) {
self.did_open(from, project_directory, path, file_type, version, @as([*]const u8, @ptrFromInt(text_ptr))[0..text_len]) catch |e| return from.send_raw(tp.exit_message(e));
self.query_recent_files(from, project_directory, max, query) catch |e| return from.forward_error(e);
} else if (try m.match(.{ "did_open", tp.extract(&project_directory), tp.extract(&path), tp.extract(&file_type), tp.extract_cbor(&language_server), tp.extract(&version), tp.extract(&text_ptr), tp.extract(&text_len) })) {
const text = if (text_len > 0) @as([*]const u8, @ptrFromInt(text_ptr))[0..text_len] else "";
self.did_open(project_directory, path, file_type, language_server, version, text) catch |e| return from.forward_error(e);
} else if (try m.match(.{ "goto_definition", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) {
self.goto_definition(from, project_directory, path, row, col) catch |e| return from.send_raw(tp.exit_message(e));
self.goto_definition(from, project_directory, path, row, col) catch |e| return from.forward_error(e);
} else if (try m.match(.{"shutdown"})) {
if (self.walker) |pid| pid.send(.{"stop"}) catch {};
try from.send(.{ "project_manager", "shutdown" });
@ -189,18 +193,18 @@ const Process = struct {
// self.logger.print("queried: {s} for {s} match {d} in {d} ms", .{ project_directory, query, matched, std.time.milliTimestamp() - start_time });
}
fn did_open(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, file_type: []const u8, version: usize, text: []const u8) tp.result {
fn did_open(self: *Process, project_directory: []const u8, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) tp.result {
const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".did_open" });
defer frame.deinit();
const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project");
return project.did_open(from, file_path, file_type, version, text);
return project.did_open(file_path, file_type, language_server, version, text);
}
fn goto_definition(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, row: usize, col: usize) tp.result {
const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".goto_definition" });
defer frame.deinit();
const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project");
return project.goto_definition(from, file_path, row, col);
return project.goto_definition(from, file_path, row, col) catch |e| tp.exit_error(e);
}
};