From 29b0045bc7cf2b7bcf3fda291a79de00048556b1 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Fri, 8 May 2020 18:01:34 +0300 Subject: [PATCH 1/2] Detect type functions and require PascalCase, require camelCase for the rest --- src/analysis.zig | 4 ++++ src/main.zig | 62 +++++++++++++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index 2fee411..524a078 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -87,3 +87,7 @@ pub fn getVariableSignature(tree: *std.zig.ast.Tree, var_decl: *std.zig.ast.Node pub fn isCamelCase(name: []const u8) bool { return !std.ascii.isUpper(name[0]) and std.mem.indexOf(u8, name, "_") == null; } + +pub fn isPascalCase(name: []const u8) bool { + return std.ascii.isUpper(name[0]) and std.mem.indexOf(u8, name, "_") == null; +} diff --git a/src/main.zig b/src/main.zig index e594f28..db92495 100644 --- a/src/main.zig +++ b/src/main.zig @@ -119,6 +119,20 @@ fn cacheSane(document: *types.TextDocument) !void { document.sane_text = try std.mem.dupe(allocator, u8, document.text); } +// TODO: Is this correct or can we get a better end? +fn astLocationToRange(loc: std.zig.ast.Tree.Location) types.Range { + return .{ + .start = .{ + .line = @intCast(i64, loc.line), + .character = @intCast(i64, loc.column), + }, + .end = .{ + .line = @intCast(i64, loc.line), + .character = @intCast(i64, loc.column), + }, + }; +} + fn publishDiagnostics(document: *types.TextDocument) !void { const tree = try std.zig.parse(allocator, document.text); defer tree.deinit(); @@ -140,16 +154,7 @@ fn publishDiagnostics(document: *types.TextDocument) !void { try tree.renderError(err, fbs.outStream()); try diagnostics.append(.{ - .range = .{ - .start = .{ - .line = @intCast(i64, loc.line), - .character = @intCast(i64, loc.column), - }, - .end = .{ - .line = @intCast(i64, loc.line), - .character = @intCast(i64, loc.column), - }, - }, + .range = astLocationToRange(loc), .severity = .Error, .code = @tagName(err.*), .source = "zls", @@ -165,24 +170,37 @@ fn publishDiagnostics(document: *types.TextDocument) !void { switch (decl.id) { .FnProto => { const func = decl.cast(std.zig.ast.Node.FnProto).?; + const is_extern = func.extern_export_inline_token != null; + if (is_extern) + break; + if (func.name_token) |name_token| { const loc = tree.tokenLocation(0, name_token); - if (func.extern_export_inline_token == null and !analysis.isCamelCase(tree.tokenSlice(name_token))) { + + var is_type_function = switch (func.return_type) { + .Explicit => |node| if (node.cast(std.zig.ast.Node.Identifier)) |ident| + std.mem.eql(u8, tree.tokenSlice(ident.token), "type") + else + false, + .InferErrorSet => false, + }; + + const func_name = tree.tokenSlice(name_token); + if (!is_type_function and !analysis.isCamelCase(func_name)) { try diagnostics.append(.{ - .range = .{ - .start = .{ - .line = @intCast(i64, loc.line), - .character = @intCast(i64, loc.column), - }, - .end = .{ - .line = @intCast(i64, loc.line), - .character = @intCast(i64, loc.column), - }, - }, + .range = astLocationToRange(loc), .severity = .Information, .code = "BadStyle", .source = "zls", - .message = "Callables should be camelCase" + .message = "Functions should be camelCase" + }); + } else if (is_type_function and !analysis.isPascalCase(func_name)) { + try diagnostics.append(.{ + .range = astLocationToRange(loc), + .severity = .Information, + .code = "BadStyle", + .source = "zls", + .message = "Type functions should be PascalCase" }); } } From d8457c96a13fd8c40c13490c66df02d3e802a10d Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Fri, 8 May 2020 18:07:14 +0300 Subject: [PATCH 2/2] is_type_function is const --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index db92495..ce01b8b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -177,7 +177,7 @@ fn publishDiagnostics(document: *types.TextDocument) !void { if (func.name_token) |name_token| { const loc = tree.tokenLocation(0, name_token); - var is_type_function = switch (func.return_type) { + const is_type_function = switch (func.return_type) { .Explicit => |node| if (node.cast(std.zig.ast.Node.Identifier)) |ident| std.mem.eql(u8, tree.tokenSlice(ident.token), "type") else