From 57c5066451946cf188201b2496e7a3a7c50b3acd Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 25 Nov 2025 17:12:57 +0100 Subject: [PATCH] fix: add support for non-authorative file URIs from LSPs (part 2) --- src/Project.zig | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index 967ec9e..ed3b05a 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -956,16 +956,20 @@ fn send_goto_request(self: *Self, from: tp.pid_ref, file_path: []const u8, row: }, handler) catch return error.LspFailed; } +fn file_uri_to_path(uri: []const u8, file_path_buf: []u8) error{InvalidTargetURI}![]const u8 { + return std.Uri.percentDecodeBackwards(file_path_buf, if (std.mem.eql(u8, uri[0..7], "file://")) + uri[7..] + else if (std.mem.eql(u8, uri[0..5], "file:")) + uri[5..] + else + return error.InvalidTargetURI); +} + fn navigate_to_location_link(from: tp.pid_ref, location_link: []const u8) (ClientError || InvalidMessageError || cbor.Error)!void { const location: LocationLink = try read_locationlink(location_link); if (location.targetUri == null or location.targetRange == null) return error.InvalidMessageField; var file_path_buf: [std.fs.max_path_bytes]u8 = undefined; - var file_path = std.Uri.percentDecodeBackwards(&file_path_buf, if (std.mem.eql(u8, location.targetUri.?[0..7], "file://")) - location.targetUri.?[7..] - else if (std.mem.eql(u8, location.targetUri.?[0..5], "file:")) - location.targetUri.?[5..] - else - return error.InvalidTargetURI); + var file_path = try file_uri_to_path(location.targetUri.?, &file_path_buf); if (builtin.os.tag == .windows) { if (file_path[0] == '/') file_path = file_path[1..]; for (file_path, 0..) |c, i| if (c == '/') { @@ -1087,9 +1091,8 @@ fn send_reference(tag: []const u8, to: tp.pid_ref, location_: []const u8, name: const allocator = std.heap.c_allocator; const location: LocationLink = try read_locationlink(location_); if (location.targetUri == null or location.targetRange == null) return error.InvalidMessageField; - if (!std.mem.eql(u8, location.targetUri.?[0..7], "file://")) return error.InvalidTargetURI; var file_path_buf: [std.fs.max_path_bytes]u8 = undefined; - var file_path = std.Uri.percentDecodeBackwards(&file_path_buf, location.targetUri.?[7..]); + var file_path = try file_uri_to_path(location.targetUri.?, &file_path_buf); if (builtin.os.tag == .windows) { if (file_path[0] == '/') file_path = file_path[1..]; for (file_path, 0..) |c, i| if (c == '/') { @@ -1332,9 +1335,8 @@ fn send_symbol_information(to: tp.pid_ref, file_path: []const u8, item: []const var fp = file_path; if (location) |location_| { if (location_.targetUri == null or location_.targetRange == null) return error.InvalidMessageField; - if (!std.mem.eql(u8, location_.targetUri.?[0..7], "file://")) return error.InvalidTargetURI; var file_path_buf: [std.fs.max_path_bytes]u8 = undefined; - var file_path_ = std.Uri.percentDecodeBackwards(&file_path_buf, location_.targetUri.?[7..]); + var file_path_ = try file_uri_to_path(location_.targetUri.?, &file_path_buf); if (builtin.os.tag == .windows) { if (file_path_[0] == '/') file_path_ = file_path_[1..]; for (file_path_, 0..) |c, i| if (c == '/') { @@ -1520,9 +1522,8 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row: try cbor.writeValue(w, "rename_symbol_item"); try cbor.writeArrayHeader(w, renames.items.len); for (renames.items) |rename| { - if (!std.mem.eql(u8, rename.uri[0..7], "file://")) return error.InvalidTargetURI; var file_path_buf: [std.fs.max_path_bytes]u8 = undefined; - var file_path_ = std.Uri.percentDecodeBackwards(&file_path_buf, rename.uri[7..]); + var file_path_ = try file_uri_to_path(rename.uri, &file_path_buf); if (builtin.os.tag == .windows) { if (file_path_[0] == '/') file_path_ = file_path_[1..]; for (file_path_, 0..) |c, i| if (c == '/') { @@ -1789,9 +1790,8 @@ pub fn publish_diagnostics(self: *Self, to: tp.pid_ref, params_cb: []const u8) ( } if (uri == null) return error.InvalidMessageField; - if (!std.mem.eql(u8, uri.?[0..7], "file://")) return error.InvalidTargetURI; var file_path_buf: [std.fs.max_path_bytes]u8 = undefined; - const file_path = std.Uri.percentDecodeBackwards(&file_path_buf, uri.?[7..]); + const file_path = try file_uri_to_path(uri.?, &file_path_buf); try self.send_clear_diagnostics(to, file_path);