Skip to content

Commit 8b2754a

Browse files
committed
rewrite uri logic to always perform normalization
1 parent 182082d commit 8b2754a

29 files changed

+964
-531
lines changed

src/DiagnosticsCollection.zig

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const std = @import("std");
22
const lsp = @import("lsp");
33
const tracy = @import("tracy");
44
const offsets = @import("offsets.zig");
5-
const URI = @import("uri.zig");
5+
const Uri = @import("Uri.zig");
66

77
allocator: std.mem.Allocator,
88
mutex: std.Thread.Mutex = .{},
@@ -12,13 +12,13 @@ tag_set: std.AutoArrayHashMapUnmanaged(Tag, struct {
1212
/// Used to store diagnostics from `pushErrorBundle`
1313
error_bundle: std.zig.ErrorBundle = .empty,
1414
/// Used to store diagnostics from `pushSingleDocumentDiagnostics`
15-
diagnostics_set: std.StringArrayHashMapUnmanaged(struct {
15+
diagnostics_set: Uri.ArrayHashMap(struct {
1616
arena: std.heap.ArenaAllocator.State = .{},
1717
diagnostics: []lsp.types.Diagnostic = &.{},
1818
error_bundle: std.zig.ErrorBundle = .empty,
1919
}) = .empty,
2020
}) = .empty,
21-
outdated_files: std.StringArrayHashMapUnmanaged(void) = .empty,
21+
outdated_files: Uri.ArrayHashMap(void) = .empty,
2222
transport: ?*lsp.Transport = null,
2323
offset_encoding: offsets.Encoding = .@"utf-16",
2424

@@ -44,22 +44,22 @@ pub fn deinit(collection: *DiagnosticsCollection) void {
4444
entry.error_bundle.deinit(collection.allocator);
4545
if (entry.error_bundle_src_base_path) |src_path| collection.allocator.free(src_path);
4646
for (entry.diagnostics_set.keys(), entry.diagnostics_set.values()) |uri, *lsp_diagnostic| {
47-
collection.allocator.free(uri);
47+
uri.deinit(collection.allocator);
4848
lsp_diagnostic.arena.promote(collection.allocator).deinit();
4949
lsp_diagnostic.error_bundle.deinit(collection.allocator);
5050
}
5151
entry.diagnostics_set.deinit(collection.allocator);
5252
}
5353
collection.tag_set.deinit(collection.allocator);
54-
for (collection.outdated_files.keys()) |uri| collection.allocator.free(uri);
54+
for (collection.outdated_files.keys()) |uri| uri.deinit(collection.allocator);
5555
collection.outdated_files.deinit(collection.allocator);
5656
collection.* = undefined;
5757
}
5858

5959
pub fn pushSingleDocumentDiagnostics(
6060
collection: *DiagnosticsCollection,
6161
tag: Tag,
62-
document_uri: []const u8,
62+
document_uri: Uri,
6363
/// LSP and ErrorBundle will not override each other.
6464
///
6565
/// Takes ownership on success.
@@ -81,15 +81,15 @@ pub fn pushSingleDocumentDiagnostics(
8181

8282
{
8383
try collection.outdated_files.ensureUnusedCapacity(collection.allocator, 1);
84-
const duped_uri = try collection.allocator.dupe(u8, document_uri);
85-
if (collection.outdated_files.fetchPutAssumeCapacity(duped_uri, {})) |_| collection.allocator.free(duped_uri);
84+
const duped_uri = try document_uri.dupe(collection.allocator);
85+
if (collection.outdated_files.fetchPutAssumeCapacity(duped_uri, {})) |_| duped_uri.deinit(collection.allocator);
8686
}
8787

8888
try gop_tag.value_ptr.diagnostics_set.ensureUnusedCapacity(collection.allocator, 1);
89-
const duped_uri = try collection.allocator.dupe(u8, document_uri);
89+
const duped_uri = try document_uri.dupe(collection.allocator);
9090
const gop_file = gop_tag.value_ptr.diagnostics_set.getOrPutAssumeCapacity(duped_uri);
9191
if (gop_file.found_existing) {
92-
collection.allocator.free(duped_uri);
92+
duped_uri.deinit(collection.allocator);
9393
} else {
9494
gop_file.value_ptr.* = .{};
9595
}
@@ -214,7 +214,7 @@ fn collectUrisFromErrorBundle(
214214
allocator: std.mem.Allocator,
215215
error_bundle: std.zig.ErrorBundle,
216216
src_base_path: ?[]const u8,
217-
uri_set: *std.StringArrayHashMapUnmanaged(void),
217+
uri_set: *Uri.ArrayHashMap(void),
218218
) error{OutOfMemory}!void {
219219
if (error_bundle.errorMessageCount() == 0) return;
220220
for (error_bundle.getMessages()) |msg_index| {
@@ -226,20 +226,20 @@ fn collectUrisFromErrorBundle(
226226
try uri_set.ensureUnusedCapacity(allocator, 1);
227227
const uri = try pathToUri(allocator, src_base_path, src_path) orelse continue;
228228
if (uri_set.fetchPutAssumeCapacity(uri, {})) |_| {
229-
allocator.free(uri);
229+
uri.deinit(allocator);
230230
}
231231
}
232232
}
233233

234-
fn pathToUri(allocator: std.mem.Allocator, base_path: ?[]const u8, src_path: []const u8) error{OutOfMemory}!?[]const u8 {
234+
fn pathToUri(allocator: std.mem.Allocator, base_path: ?[]const u8, src_path: []const u8) error{OutOfMemory}!?Uri {
235235
if (std.fs.path.isAbsolute(src_path)) {
236-
return try URI.fromPath(allocator, src_path);
236+
return try .fromPath(allocator, src_path);
237237
}
238238
const base = base_path orelse return null;
239239
const absolute_src_path = try std.fs.path.join(allocator, &.{ base, src_path });
240240
defer allocator.free(absolute_src_path);
241241

242-
return try URI.fromPath(allocator, absolute_src_path);
242+
return try .fromPath(allocator, absolute_src_path);
243243
}
244244

245245
pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator.Error || std.posix.WriteError)!void {
@@ -254,8 +254,8 @@ pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator
254254
defer collection.mutex.unlock();
255255

256256
const entry = collection.outdated_files.pop() orelse break;
257-
defer collection.allocator.free(entry.key);
258-
const document_uri = entry.key;
257+
defer entry.key.deinit(collection.allocator);
258+
const document_uri: Uri = entry.key;
259259

260260
_ = arena_allocator.reset(.retain_capacity);
261261

@@ -265,7 +265,7 @@ pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator
265265
const notification: lsp.TypedJsonRPCNotification(lsp.types.PublishDiagnosticsParams) = .{
266266
.method = "textDocument/publishDiagnostics",
267267
.params = .{
268-
.uri = document_uri,
268+
.uri = document_uri.raw,
269269
.diagnostics = diagnostics.items,
270270
},
271271
};
@@ -281,7 +281,7 @@ pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator
281281

282282
fn collectLspDiagnosticsForDocument(
283283
collection: *DiagnosticsCollection,
284-
document_uri: []const u8,
284+
document_uri: Uri,
285285
offset_encoding: offsets.Encoding,
286286
arena: std.mem.Allocator,
287287
diagnostics: *std.ArrayList(lsp.types.Diagnostic),
@@ -318,7 +318,7 @@ pub const collectLspDiagnosticsForDocumentTesting = if (@import("builtin").is_te
318318
fn convertErrorBundleToLSPDiangostics(
319319
eb: std.zig.ErrorBundle,
320320
error_bundle_src_base_path: ?[]const u8,
321-
document_uri: []const u8,
321+
document_uri: Uri,
322322
offset_encoding: offsets.Encoding,
323323
arena: std.mem.Allocator,
324324
diagnostics: *std.ArrayList(lsp.types.Diagnostic),
@@ -333,8 +333,8 @@ fn convertErrorBundleToLSPDiangostics(
333333
const src_path = eb.nullTerminatedString(src_loc.src_path);
334334

335335
if (!is_single_document) {
336-
const uri = try pathToUri(arena, error_bundle_src_base_path, src_path) orelse continue;
337-
if (!std.mem.eql(u8, document_uri, uri)) continue;
336+
const src_uri = try pathToUri(arena, error_bundle_src_base_path, src_path) orelse continue;
337+
if (!document_uri.eql(src_uri)) continue;
338338
}
339339

340340
const src_range = errorBundleSourceLocationToRange(eb, src_loc, offset_encoding);
@@ -350,14 +350,14 @@ fn convertErrorBundleToLSPDiangostics(
350350
const note_src_path = eb.nullTerminatedString(note_src_loc.src_path);
351351
const note_src_range = errorBundleSourceLocationToRange(eb, note_src_loc, offset_encoding);
352352

353-
const note_uri = if (is_single_document)
353+
const note_uri: Uri = if (is_single_document)
354354
document_uri
355355
else
356356
try pathToUri(arena, error_bundle_src_base_path, note_src_path) orelse continue;
357357

358358
lsp_note.* = .{
359359
.location = .{
360-
.uri = note_uri,
360+
.uri = note_uri.raw,
361361
.range = note_src_range,
362362
},
363363
.message = eb.nullTerminatedString(eb_note.msg),
@@ -478,13 +478,13 @@ test DiagnosticsCollection {
478478
var eb3 = try createTestingErrorBundle(&.{.{ .message = "As" }}, "");
479479
defer eb3.deinit(std.testing.allocator);
480480

481-
const uri = try URI.fromPath(std.testing.allocator, testing_src_path);
482-
defer std.testing.allocator.free(uri);
481+
const uri: Uri = try .fromPath(std.testing.allocator, testing_src_path);
482+
defer uri.deinit(std.testing.allocator);
483483

484484
{
485485
try collection.pushErrorBundle(.parse, 1, null, eb1);
486486
try std.testing.expectEqual(1, collection.outdated_files.count());
487-
try std.testing.expectEqualStrings(uri, collection.outdated_files.keys()[0]);
487+
try std.testing.expect(uri.eql(collection.outdated_files.keys()[0]));
488488

489489
var diagnostics: std.ArrayList(lsp.types.Diagnostic) = .empty;
490490
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);
@@ -544,20 +544,20 @@ test "DiagnosticsCollection - compile_log_text" {
544544
var eb = try createTestingErrorBundle(&.{.{ .message = "found compile log statement" }}, "@as(comptime_int, 7)\n@as(comptime_int, 13)");
545545
defer eb.deinit(std.testing.allocator);
546546

547-
const uri = try URI.fromPath(std.testing.allocator, testing_src_path);
548-
defer std.testing.allocator.free(uri);
547+
const src_uri: Uri = try .fromPath(std.testing.allocator, testing_src_path);
548+
defer src_uri.deinit(std.testing.allocator);
549549

550550
try collection.pushErrorBundle(.parse, 1, null, eb);
551551
try std.testing.expectEqual(1, collection.outdated_files.count());
552-
try std.testing.expectEqualStrings(uri, collection.outdated_files.keys()[0]);
552+
try std.testing.expect(src_uri.eql(collection.outdated_files.keys()[0]));
553553

554554
var arena_allocator: std.heap.ArenaAllocator = .init(std.testing.allocator);
555555
defer arena_allocator.deinit();
556556

557557
const arena = arena_allocator.allocator();
558558

559-
var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .empty;
560-
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);
559+
var diagnostics: std.ArrayList(lsp.types.Diagnostic) = .empty;
560+
try collection.collectLspDiagnosticsForDocument(src_uri, .@"utf-8", arena, &diagnostics);
561561

562562
try std.testing.expectEqual(1, diagnostics.items.len);
563563
try std.testing.expectEqual(lsp.types.DiagnosticSeverity.Error, diagnostics.items[0].severity);

0 commit comments

Comments
 (0)