add completion for arrays and slices
This commit is contained in:
parent
a2402b9885
commit
6e8165804f
@ -12,7 +12,7 @@ pub fn getFunctionByName(tree: *ast.Tree, name: []const u8) ?*ast.Node.FnProto {
|
||||
const func = decl.cast(ast.Node.FnProto).?;
|
||||
if (std.mem.eql(u8, tree.tokenSlice(func.name_token.?), name)) return func;
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,9 +55,9 @@ pub fn getDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast
|
||||
const param = node.cast(ast.Node.ParamDecl).?;
|
||||
if (param.doc_comments) |doc_comments| {
|
||||
return try collectDocComments(allocator, tree, doc_comments);
|
||||
}
|
||||
}
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -102,8 +102,7 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
|
||||
const param = param_ptr.*;
|
||||
const param_decl = param.cast(ast.Node.ParamDecl).?;
|
||||
|
||||
if (param_num != 1) try buffer.appendSlice(", ${")
|
||||
else try buffer.appendSlice("${");
|
||||
if (param_num != 1) try buffer.appendSlice(", ${") else try buffer.appendSlice("${");
|
||||
|
||||
try buf_stream.print("{}:", .{param_num});
|
||||
|
||||
@ -135,7 +134,7 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
|
||||
try buffer.appendSlice(tree.tokenSlice(curr_tok));
|
||||
if (is_comma or id == .Keyword_const) try buffer.append(' ');
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
try buffer.append('}');
|
||||
@ -159,6 +158,16 @@ pub fn getParamSignature(tree: *ast.Tree, param: *ast.Node.ParamDecl) []const u8
|
||||
return tree.source[start..end];
|
||||
}
|
||||
|
||||
pub fn isTypeFunction(tree: *ast.Tree, func: *ast.Node.FnProto) bool {
|
||||
switch (func.return_type) {
|
||||
.Explicit => |node| return if (node.cast(std.zig.ast.Node.Identifier)) |ident|
|
||||
std.mem.eql(u8, tree.tokenSlice(ident.token), "type")
|
||||
else
|
||||
false,
|
||||
.InferErrorSet, .Invalid => return false,
|
||||
}
|
||||
}
|
||||
|
||||
// STYLE
|
||||
|
||||
pub fn isCamelCase(name: []const u8) bool {
|
||||
@ -188,7 +197,7 @@ pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
|
||||
const field = child.cast(ast.Node.ContainerField).?;
|
||||
if (std.mem.eql(u8, tree.tokenSlice(field.name_token), name)) return child;
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
@ -216,7 +225,7 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*
|
||||
const field = child.cast(ast.Node.ContainerField).?;
|
||||
if (std.mem.eql(u8, tree.tokenSlice(field.name_token), name)) return child;
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
// index += 1;
|
||||
}
|
||||
@ -237,7 +246,7 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
||||
.var_type, .type_expr => |var_type| {
|
||||
return resolveTypeOfNode(analysis_ctx, var_type) orelse null;
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
.FnProto => {
|
||||
@ -270,7 +279,7 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
||||
.Call, .StructInitializer => {
|
||||
return resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node);
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
.InfixOp => {
|
||||
@ -285,16 +294,22 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
||||
const left = resolveTypeOfNode(analysis_ctx, infix_op.lhs) orelse return null;
|
||||
return resolveTypeOfNode(analysis_ctx, getChild(analysis_ctx.tree, left, rhs_str) orelse return null);
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
.PrefixOp => {
|
||||
const prefix_op = node.cast(ast.Node.PrefixOp).?;
|
||||
switch (prefix_op.op) {
|
||||
.SliceType, .ArrayType => return node,
|
||||
.PtrType => {
|
||||
return resolveTypeOfNode(analysis_ctx, prefix_op.rhs);
|
||||
const op_token = analysis_ctx.tree.tokens.at(prefix_op.op_token);
|
||||
switch (op_token.id) {
|
||||
.Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs),
|
||||
.LBracket, .AsteriskAsterisk => return null,
|
||||
else => unreachable,
|
||||
}
|
||||
},
|
||||
else => {}
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
.BuiltinCall => {
|
||||
@ -307,13 +322,16 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
||||
|
||||
const import_str = analysis_ctx.tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
|
||||
return analysis_ctx.onImport(import_str[1 .. import_str.len - 1]) catch |err| block: {
|
||||
std.debug.warn("Error {} while processing import {}\n", .{err, import_str});
|
||||
std.debug.warn("Error {} while processing import {}\n", .{ err, import_str });
|
||||
break :block null;
|
||||
};
|
||||
},
|
||||
.MultilineStringLiteral, .StringLiteral => {
|
||||
return node;
|
||||
},
|
||||
else => {
|
||||
std.debug.warn("Type resolution case not implemented; {}\n", .{node.id});
|
||||
}
|
||||
},
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -340,16 +358,16 @@ pub fn collectImports(allocator: *std.mem.Allocator, tree: *ast.Tree) ![][]const
|
||||
if (decl.id != .VarDecl) continue;
|
||||
const var_decl = decl.cast(ast.Node.VarDecl).?;
|
||||
if (var_decl.init_node == null) continue;
|
||||
|
||||
switch(var_decl.init_node.?.id) {
|
||||
|
||||
switch (var_decl.init_node.?.id) {
|
||||
.BuiltinCall => {
|
||||
const builtin_call = var_decl.init_node.?.cast(ast.Node.BuiltinCall).?;
|
||||
try maybeCollectImport(tree, builtin_call, &arr);
|
||||
},
|
||||
.InfixOp => {
|
||||
const infix_op = var_decl.init_node.?.cast(ast.Node.InfixOp).?;
|
||||
|
||||
switch(infix_op.op) {
|
||||
|
||||
switch (infix_op.op) {
|
||||
.Period => {},
|
||||
else => continue,
|
||||
}
|
||||
@ -395,7 +413,7 @@ pub fn getFieldAccessTypeNode(analysis_ctx: *AnalysisContext, tokenizer: *std.zi
|
||||
},
|
||||
else => {
|
||||
std.debug.warn("Not implemented; {}\n", .{next.id});
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -412,15 +430,8 @@ pub fn isNodePublic(tree: *ast.Tree, node: *ast.Node) bool {
|
||||
const func = node.cast(ast.Node.FnProto).?;
|
||||
return func.visib_token != null;
|
||||
},
|
||||
.ContainerField, .ErrorTag => {
|
||||
return true;
|
||||
},
|
||||
else => {
|
||||
return false;
|
||||
}
|
||||
else => return true,
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
|
||||
@ -445,9 +456,9 @@ pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
|
||||
},
|
||||
else => {
|
||||
std.debug.warn("INVALID: {}\n", .{node.id});
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -461,7 +472,7 @@ pub fn declsFromIndexInternal(allocator: *std.mem.Allocator, tree: *ast.Tree, no
|
||||
try declsFromIndexInternal(allocator, tree, func.params.at(param_index).*, nodes);
|
||||
|
||||
if (func.body_node) |body_node|
|
||||
try declsFromIndexInternal(allocator, tree, body_node, nodes);
|
||||
try declsFromIndexInternal(allocator, tree, body_node, nodes);
|
||||
},
|
||||
.Block => {
|
||||
var index: usize = 0;
|
||||
@ -479,7 +490,7 @@ pub fn declsFromIndexInternal(allocator: *std.mem.Allocator, tree: *ast.Tree, no
|
||||
},
|
||||
else => {
|
||||
try nodes.appendSlice(try getCompletionsFromNode(allocator, tree, node));
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -489,7 +500,7 @@ pub fn getCompletionsFromNode(allocator: *std.mem.Allocator, tree: *ast.Tree, no
|
||||
var index: usize = 0;
|
||||
while (node.iterate(index)) |child_node| {
|
||||
try nodes.append(child_node);
|
||||
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
|
93
src/main.zig
93
src/main.zig
@ -142,13 +142,7 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
|
||||
if (func.name_token) |name_token| {
|
||||
const loc = tree.tokenLocation(0, name_token);
|
||||
|
||||
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
|
||||
false,
|
||||
.InferErrorSet, .Invalid => false,
|
||||
};
|
||||
const is_type_function = analysis.isTypeFunction(tree, func);
|
||||
|
||||
const func_name = tree.tokenSlice(name_token);
|
||||
if (!is_type_function and !analysis.isCamelCase(func_name)) {
|
||||
@ -187,8 +181,17 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
|
||||
});
|
||||
}
|
||||
|
||||
fn nodeToCompletion(alloc: *std.mem.Allocator, tree: *std.zig.ast.Tree, decl: *std.zig.ast.Node, config: Config) !?types.CompletionItem {
|
||||
var doc = if (try analysis.getDocComments(alloc, tree, decl)) |doc_comments|
|
||||
fn containerToCompletion(list: *std.ArrayList(types.CompletionItem), tree: *std.zig.ast.Tree, container: *std.zig.ast.Node, config: Config) !void {
|
||||
var index: usize = 0;
|
||||
while (container.iterate(index)) |child_node| : (index+=1) {
|
||||
if (analysis.isNodePublic(tree, child_node)) {
|
||||
try nodeToCompletion(list, tree, child_node, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn nodeToCompletion(list: *std.ArrayList(types.CompletionItem), tree: *std.zig.ast.Tree, node: *std.zig.ast.Node, config: Config) error{OutOfMemory}!void {
|
||||
var doc = if (try analysis.getDocComments(list.allocator, tree, node)) |doc_comments|
|
||||
types.MarkupContent{
|
||||
.kind = .Markdown,
|
||||
.value = doc_comments,
|
||||
@ -196,54 +199,74 @@ fn nodeToCompletion(alloc: *std.mem.Allocator, tree: *std.zig.ast.Tree, decl: *s
|
||||
else
|
||||
null;
|
||||
|
||||
switch (decl.id) {
|
||||
switch (node.id) {
|
||||
.ErrorSetDecl, .Root, .ContainerDecl => {
|
||||
try containerToCompletion(list, tree, node, config);
|
||||
},
|
||||
.FnProto => {
|
||||
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
||||
const func = node.cast(std.zig.ast.Node.FnProto).?;
|
||||
if (func.name_token) |name_token| {
|
||||
const insert_text = if (config.enable_snippets)
|
||||
try analysis.getFunctionSnippet(alloc, tree, func)
|
||||
try analysis.getFunctionSnippet(list.allocator, tree, func)
|
||||
else
|
||||
null;
|
||||
|
||||
return types.CompletionItem{
|
||||
const is_type_function = analysis.isTypeFunction(tree, func);
|
||||
|
||||
try list.append(.{
|
||||
.label = tree.tokenSlice(name_token),
|
||||
.kind = .Function,
|
||||
.kind = if (is_type_function) .Struct else .Function,
|
||||
.documentation = doc,
|
||||
.detail = analysis.getFunctionSignature(tree, func),
|
||||
.insertText = insert_text,
|
||||
.insertTextFormat = if (config.enable_snippets) .Snippet else .PlainText,
|
||||
};
|
||||
});
|
||||
}
|
||||
},
|
||||
.VarDecl => {
|
||||
const var_decl = decl.cast(std.zig.ast.Node.VarDecl).?;
|
||||
return types.CompletionItem{
|
||||
const var_decl = node.cast(std.zig.ast.Node.VarDecl).?;
|
||||
const is_const = tree.tokens.at(var_decl.mut_token).id == .Keyword_const;
|
||||
try list.append(.{
|
||||
.label = tree.tokenSlice(var_decl.name_token),
|
||||
.kind = .Variable,
|
||||
.kind = if (is_const) .Constant else .Variable,
|
||||
.documentation = doc,
|
||||
.detail = analysis.getVariableSignature(tree, var_decl),
|
||||
};
|
||||
});
|
||||
},
|
||||
.ParamDecl => {
|
||||
const param = decl.cast(std.zig.ast.Node.ParamDecl).?;
|
||||
const param = node.cast(std.zig.ast.Node.ParamDecl).?;
|
||||
if (param.name_token) |name_token|
|
||||
return types.CompletionItem{
|
||||
try list.append(.{
|
||||
.label = tree.tokenSlice(name_token),
|
||||
.kind = .Variable,
|
||||
.kind = .Constant,
|
||||
.documentation = doc,
|
||||
.detail = analysis.getParamSignature(tree, param),
|
||||
};
|
||||
});
|
||||
},
|
||||
else => if (analysis.nodeToString(tree, decl)) |string| {
|
||||
return types.CompletionItem{
|
||||
.PrefixOp => {
|
||||
try list.append(.{
|
||||
.label = "len",
|
||||
.kind = .Field,
|
||||
});
|
||||
try list.append(.{
|
||||
.label = "ptr",
|
||||
.kind = .Field,
|
||||
});
|
||||
},
|
||||
.StringLiteral => {
|
||||
try list.append(.{
|
||||
.label = "len",
|
||||
.kind = .Field,
|
||||
});
|
||||
},
|
||||
else => if (analysis.nodeToString(tree, node)) |string| {
|
||||
try list.append(.{
|
||||
.label = string,
|
||||
.kind = .Field,
|
||||
.documentation = doc,
|
||||
};
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
fn completeGlobal(id: i64, pos_index: usize, handle: DocumentStore.Handle, config: Config) !void {
|
||||
@ -260,9 +283,7 @@ fn completeGlobal(id: i64, pos_index: usize, handle: DocumentStore.Handle, confi
|
||||
var decls = try analysis.declsFromIndex(&arena.allocator, tree, pos_index);
|
||||
for (decls) |decl_ptr| {
|
||||
var decl = decl_ptr.*;
|
||||
if (try nodeToCompletion(&arena.allocator, tree, decl_ptr, config)) |completion| {
|
||||
try completions.append(completion);
|
||||
}
|
||||
try nodeToCompletion(&completions, tree, decl_ptr, config);
|
||||
}
|
||||
|
||||
try send(types.Response{
|
||||
@ -290,15 +311,7 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
|
||||
|
||||
// var decls = try analysis.declsFromIndex(&arena.allocator, analysis_ctx.tree, try handle.document.positionToIndex(position));
|
||||
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| {
|
||||
var index: usize = 0;
|
||||
while (node.iterate(index)) |child_node| {
|
||||
if (analysis.isNodePublic(analysis_ctx.tree, child_node)) {
|
||||
if (try nodeToCompletion(&arena.allocator, analysis_ctx.tree, child_node, config)) |completion| {
|
||||
try completions.append(completion);
|
||||
}
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
try nodeToCompletion(&completions, analysis_ctx.tree, node, config);
|
||||
}
|
||||
|
||||
try send(types.Response{
|
||||
|
Loading…
Reference in New Issue
Block a user