Skip to content

Commit 6734c54

Browse files
committed
rewrite uri logic to always perform normalization
1 parent 178861c commit 6734c54

29 files changed

+963
-524
lines changed

src/DiagnosticsCollection.zig

Lines changed: 27 additions & 27 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
}
@@ -212,7 +212,7 @@ fn collectUrisFromErrorBundle(
212212
allocator: std.mem.Allocator,
213213
error_bundle: std.zig.ErrorBundle,
214214
src_base_path: ?[]const u8,
215-
uri_set: *std.StringArrayHashMapUnmanaged(void),
215+
uri_set: *Uri.ArrayHashMap(void),
216216
) error{OutOfMemory}!void {
217217
if (error_bundle.errorMessageCount() == 0) return;
218218
for (error_bundle.getMessages()) |msg_index| {
@@ -224,20 +224,20 @@ fn collectUrisFromErrorBundle(
224224
try uri_set.ensureUnusedCapacity(allocator, 1);
225225
const uri = try pathToUri(allocator, src_base_path, src_path) orelse continue;
226226
if (uri_set.fetchPutAssumeCapacity(uri, {})) |_| {
227-
allocator.free(uri);
227+
uri.deinit(allocator);
228228
}
229229
}
230230
}
231231

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

240-
return try URI.fromPath(allocator, absolute_src_path);
240+
return try .fromPath(allocator, absolute_src_path);
241241
}
242242

243243
pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator.Error || std.posix.WriteError)!void {
@@ -252,8 +252,8 @@ pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator
252252
defer collection.mutex.unlock();
253253

254254
const entry = collection.outdated_files.pop() orelse break;
255-
defer collection.allocator.free(entry.key);
256-
const document_uri = entry.key;
255+
defer entry.key.deinit(collection.allocator);
256+
const document_uri: Uri = entry.key;
257257

258258
_ = arena_allocator.reset(.retain_capacity);
259259

@@ -263,7 +263,7 @@ pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator
263263
const notification: lsp.TypedJsonRPCNotification(lsp.types.PublishDiagnosticsParams) = .{
264264
.method = "textDocument/publishDiagnostics",
265265
.params = .{
266-
.uri = document_uri,
266+
.uri = document_uri.raw,
267267
.diagnostics = diagnostics.items,
268268
},
269269
};
@@ -279,7 +279,7 @@ pub fn publishDiagnostics(collection: *DiagnosticsCollection) (std.mem.Allocator
279279

280280
fn collectLspDiagnosticsForDocument(
281281
collection: *DiagnosticsCollection,
282-
document_uri: []const u8,
282+
document_uri: Uri,
283283
offset_encoding: offsets.Encoding,
284284
arena: std.mem.Allocator,
285285
diagnostics: *std.ArrayList(lsp.types.Diagnostic),
@@ -316,7 +316,7 @@ pub const collectLspDiagnosticsForDocumentTesting = if (@import("builtin").is_te
316316
fn convertErrorBundleToLSPDiangostics(
317317
eb: std.zig.ErrorBundle,
318318
error_bundle_src_base_path: ?[]const u8,
319-
document_uri: []const u8,
319+
document_uri: Uri,
320320
offset_encoding: offsets.Encoding,
321321
arena: std.mem.Allocator,
322322
diagnostics: *std.ArrayList(lsp.types.Diagnostic),
@@ -331,8 +331,8 @@ fn convertErrorBundleToLSPDiangostics(
331331
const src_path = eb.nullTerminatedString(src_loc.src_path);
332332

333333
if (!is_single_document) {
334-
const uri = try pathToUri(arena, error_bundle_src_base_path, src_path) orelse continue;
335-
if (!std.mem.eql(u8, document_uri, uri)) continue;
334+
const src_uri = try pathToUri(arena, error_bundle_src_base_path, src_path) orelse continue;
335+
if (!document_uri.eql(src_uri)) continue;
336336
}
337337

338338
const src_range = errorBundleSourceLocationToRange(eb, src_loc, offset_encoding);
@@ -348,14 +348,14 @@ fn convertErrorBundleToLSPDiangostics(
348348
const note_src_path = eb.nullTerminatedString(note_src_loc.src_path);
349349
const note_src_range = errorBundleSourceLocationToRange(eb, note_src_loc, offset_encoding);
350350

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

356356
lsp_note.* = .{
357357
.location = .{
358-
.uri = note_uri,
358+
.uri = note_uri.raw,
359359
.range = note_src_range,
360360
},
361361
.message = eb.nullTerminatedString(eb_note.msg),
@@ -473,13 +473,13 @@ test DiagnosticsCollection {
473473
var eb3 = try createTestingErrorBundle(&.{.{ .message = "As" }});
474474
defer eb3.deinit(std.testing.allocator);
475475

476-
const uri = try URI.fromPath(std.testing.allocator, testing_src_path);
477-
defer std.testing.allocator.free(uri);
476+
const uri: Uri = try .fromPath(std.testing.allocator, testing_src_path);
477+
defer uri.deinit(std.testing.allocator);
478478

479479
{
480480
try collection.pushErrorBundle(.parse, 1, null, eb1);
481481
try std.testing.expectEqual(1, collection.outdated_files.count());
482-
try std.testing.expectEqualStrings(uri, collection.outdated_files.keys()[0]);
482+
try std.testing.expect(uri.eql(collection.outdated_files.keys()[0]));
483483

484484
var diagnostics: std.ArrayList(lsp.types.Diagnostic) = .empty;
485485
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);

0 commit comments

Comments
 (0)