diff --git a/src/analysis.zig b/src/analysis.zig index 524a078..e5c3c0e 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -1,13 +1,15 @@ const std = @import("std"); +const ast = std.zig.ast; + /// REALLY BAD CODE, PLEASE DON'T USE THIS!!!!!!! (only for testing) -pub fn getFunctionByName(tree: *std.zig.ast.Tree, name: []const u8) ?*std.zig.ast.Node.FnProto { +pub fn getFunctionByName(tree: *ast.Tree, name: []const u8) ?*ast.Node.FnProto { var decls = tree.root_node.decls.iterator(0); while (decls.next()) |decl_ptr| { var decl = decl_ptr.*; switch (decl.id) { .FnProto => { - const func = decl.cast(std.zig.ast.Node.FnProto).?; + const func = decl.cast(ast.Node.FnProto).?; if (std.mem.eql(u8, tree.tokenSlice(func.name_token.?), name)) return func; }, else => {} @@ -23,10 +25,10 @@ pub fn getFunctionByName(tree: *std.zig.ast.Tree, name: []const u8) ?*std.zig.as ///var comments = getFunctionDocComments(allocator, tree, func); ///defer if (comments) |comments_pointer| allocator.free(comments_pointer); ///``` -pub fn getDocComments(allocator: *std.mem.Allocator, tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) !?[]const u8 { +pub fn getDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node) !?[]const u8 { switch (node.id) { .FnProto => { - const func = node.cast(std.zig.ast.Node.FnProto).?; + const func = node.cast(ast.Node.FnProto).?; if (func.doc_comments) |doc_comments| { var doc_it = doc_comments.lines.iterator(0); var lines = std.ArrayList([]const u8).init(allocator); @@ -42,7 +44,7 @@ pub fn getDocComments(allocator: *std.mem.Allocator, tree: *std.zig.ast.Tree, no } }, .VarDecl => { - const var_decl = node.cast(std.zig.ast.Node.VarDecl).?; + const var_decl = node.cast(ast.Node.VarDecl).?; if (var_decl.doc_comments) |doc_comments| { var doc_it = doc_comments.lines.iterator(0); var lines = std.ArrayList([]const u8).init(allocator); @@ -62,18 +64,54 @@ pub fn getDocComments(allocator: *std.mem.Allocator, tree: *std.zig.ast.Tree, no } /// Gets a function signature (keywords, name, return value) -pub fn getFunctionSignature(tree: *std.zig.ast.Tree, func: *std.zig.ast.Node.FnProto) []const u8 { +pub fn getFunctionSignature(tree: *ast.Tree, func: *ast.Node.FnProto) []const u8 { const start = tree.tokens.at(func.firstToken()).start; - const end = - if (func.body_node) |body| tree.tokens.at(body.firstToken()).start - else tree.tokens.at(switch (func.return_type) { - .Explicit, .InferErrorSet => |node| node.lastToken() - }).end; + const end = tree.tokens.at(switch (func.return_type) { + .Explicit, .InferErrorSet => |node| node.lastToken() + }).end; return tree.source[start..end]; } +/// Gets a function snippet insert text +pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func: *ast.Node.FnProto) ![]const u8 { + const name_tok = func.name_token orelse unreachable; + + var buffer = std.ArrayList(u8).init(allocator); + try buffer.ensureCapacity(128); + + try buffer.appendSlice(tree.tokenSlice(name_tok)); + try buffer.append('('); + + var buf_stream = buffer.outStream(); + + var param_num = @as(usize, 1); + var param_it = func.params.iterator(0); + while (param_it.next()) |param_ptr| : (param_num += 1) { + const param = param_ptr.*; + + if (param_num != 1) try buffer.appendSlice(", ${") + else try buffer.appendSlice("${"); + + try buf_stream.print("{}:", .{param_num}); + var curr_tok = param.firstToken(); + const end_tok = param.lastToken(); + + var first_tok = true; + while (curr_tok <= end_tok) : (curr_tok += 1) { + try buffer.appendSlice(tree.tokenSlice(curr_tok)); + if (!first_tok and curr_tok != end_tok) try buffer.append(' ') + else first_tok = false; + } + + try buffer.append('}'); + } + try buffer.append(')'); + + return buffer.toOwnedSlice(); +} + /// Gets a function signature (keywords, name, return value) -pub fn getVariableSignature(tree: *std.zig.ast.Tree, var_decl: *std.zig.ast.Node.VarDecl) []const u8 { +pub fn getVariableSignature(tree: *ast.Tree, var_decl: *ast.Node.VarDecl) []const u8 { const start = tree.tokens.at(var_decl.firstToken()).start; const end = tree.tokens.at(var_decl.semicolon_token).start; // var end = diff --git a/src/main.zig b/src/main.zig index 3958cb3..eadd35b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -143,26 +143,25 @@ fn publishDiagnostics(document: *types.TextDocument) !void { var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator); - if (tree.errors.len > 0) { - var index: usize = 0; - while (index < tree.errors.len) : (index += 1) { - const err = tree.errors.at(index); - const loc = tree.tokenLocation(0, err.loc()); + var error_it = tree.errors.iterator(0); + while (error_it.next()) |err| { + const loc = tree.tokenLocation(0, err.loc()); - var mem_buffer: [256]u8 = undefined; - var fbs = std.io.fixedBufferStream(&mem_buffer); - try tree.renderError(err, fbs.outStream()); + var mem_buffer: [256]u8 = undefined; + var fbs = std.io.fixedBufferStream(&mem_buffer); + try tree.renderError(err, fbs.outStream()); - try diagnostics.append(.{ - .range = astLocationToRange(loc), - .severity = .Error, - .code = @tagName(err.*), - .source = "zls", - .message = try std.mem.dupe(&arena.allocator, u8, fbs.getWritten()), - // .relatedInformation = undefined - }); - } - } else { + try diagnostics.append(.{ + .range = astLocationToRange(loc), + .severity = .Error, + .code = @tagName(err.*), + .source = "zls", + .message = try std.mem.dupe(&arena.allocator, u8, fbs.getWritten()), + // .relatedInformation = undefined + }); + } + + if (tree.errors.len == 0) { try cacheSane(document); var decls = tree.root_node.decls.iterator(0); while (decls.next()) |decl_ptr| { @@ -231,7 +230,7 @@ fn completeGlobal(id: i64, document: *types.TextDocument) !void { tree = try std.zig.parse(allocator, sane_text); } else return try respondGeneric(id, no_completions_response); } - else {try cacheSane(document);} + else try cacheSane(document); defer tree.deinit(); @@ -241,15 +240,18 @@ fn completeGlobal(id: i64, document: *types.TextDocument) !void { // Deallocate all temporary data. defer arena.deinit(); - // try log("{}", .{&tree.root_node.decls}); var decls = tree.root_node.decls.iterator(0); while (decls.next()) |decl_ptr| { - var decl = decl_ptr.*; switch (decl.id) { .FnProto => { const func = decl.cast(std.zig.ast.Node.FnProto).?; if (func.name_token) |name_token| { + const insert_text = if (build_options.no_snippets) + null + else + try analysis.getFunctionSnippet(&arena.allocator, tree, func); + var doc_comments = try analysis.getDocComments(&arena.allocator, tree, decl); var doc = types.MarkupContent{ .kind = .Markdown, @@ -260,6 +262,8 @@ fn completeGlobal(id: i64, document: *types.TextDocument) !void { .kind = .Function, .documentation = doc, .detail = analysis.getFunctionSignature(tree, func), + .insertText = insert_text, + .insertTextFormat = if(build_options.no_snippets) .PlainText else .Snippet, }); } }, @@ -301,12 +305,14 @@ const builtin_completions = block: { for (data.builtins) |builtin, i| { var cutoff = std.mem.indexOf(u8, builtin, "(") orelse builtin.len; + const insert_text = if(build_options.no_snippets) builtin[1..cutoff] else builtin[1..]; + temp[i] = .{ .label = builtin[0..cutoff], .kind = .Function, .filterText = builtin[1..cutoff], - .insertText = builtin[1..], + .insertText = insert_text, .detail = data.builtin_details[i], .documentation = .{ .kind = .Markdown,