Now compiles on the new parser branch
This commit is contained in:
parent
30cb9e06e4
commit
11b5da3a91
189
src/analysis.zig
189
src/analysis.zig
@ -51,24 +51,31 @@ pub fn getDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast
|
|||||||
return try collectDocComments(allocator, tree, doc_comments);
|
return try collectDocComments(allocator, tree, doc_comments);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.ParamDecl => {
|
// @TODO: ParamDecl is no longer a node.
|
||||||
const param = node.cast(ast.Node.ParamDecl).?;
|
// .ParamDecl => {
|
||||||
if (param.doc_comments) |doc_comments| {
|
// const param = node.cast(ast.Node.ParamDecl).?;
|
||||||
return try collectDocComments(allocator, tree, doc_comments);
|
// if (param.doc_comments) |doc_comments| {
|
||||||
}
|
// return try collectDocComments(allocator, tree, doc_comments);
|
||||||
},
|
// }
|
||||||
|
// },
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_comments: *ast.Node.DocComment) ![]const u8 {
|
fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_comments: *ast.Node.DocComment) ![]const u8 {
|
||||||
var doc_it = doc_comments.lines.iterator(0);
|
|
||||||
var lines = std.ArrayList([]const u8).init(allocator);
|
var lines = std.ArrayList([]const u8).init(allocator);
|
||||||
defer lines.deinit();
|
defer lines.deinit();
|
||||||
|
|
||||||
while (doc_it.next()) |doc_comment| {
|
var curr_line_tok = doc_comments.first_line;
|
||||||
_ = try lines.append(std.fmt.trim(tree.tokenSlice(doc_comment.*)[3..]));
|
while (true) : (curr_line_tok += 1) {
|
||||||
|
switch (tree.token_ids[curr_line_tok]) {
|
||||||
|
.LineComment => continue,
|
||||||
|
.DocComment, .ContainerDocComment => {
|
||||||
|
try lines.append(std.fmt.trim(tree.tokenSlice(curr_line_tok)[3..]));
|
||||||
|
},
|
||||||
|
else => break,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return try std.mem.join(allocator, "\n", lines.items);
|
return try std.mem.join(allocator, "\n", lines.items);
|
||||||
@ -76,11 +83,11 @@ fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_commen
|
|||||||
|
|
||||||
/// Gets a function signature (keywords, name, return value)
|
/// Gets a function signature (keywords, name, return value)
|
||||||
pub fn getFunctionSignature(tree: *ast.Tree, func: *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 start = tree.token_locs[func.firstToken()].start;
|
||||||
const end = tree.tokens.at(switch (func.return_type) {
|
const end = tree.token_locs[switch (func.return_type) {
|
||||||
.Explicit, .InferErrorSet => |node| node.lastToken(),
|
.Explicit, .InferErrorSet => |node| node.lastToken(),
|
||||||
.Invalid => |r_paren| r_paren,
|
.Invalid => |r_paren| r_paren,
|
||||||
}).end;
|
}].end;
|
||||||
return tree.source[start..end];
|
return tree.source[start..end];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,38 +103,33 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
|
|||||||
|
|
||||||
var buf_stream = buffer.outStream();
|
var buf_stream = buffer.outStream();
|
||||||
|
|
||||||
var param_num = @as(usize, 1);
|
for (func.paramsConst()) |param, param_num| {
|
||||||
var param_it = func.params.iterator(0);
|
if (param_num != 0) try buffer.appendSlice(", ${") else try buffer.appendSlice("${");
|
||||||
while (param_it.next()) |param_ptr| : (param_num += 1) {
|
|
||||||
const param = param_ptr.*;
|
|
||||||
const param_decl = param.cast(ast.Node.ParamDecl).?;
|
|
||||||
|
|
||||||
if (param_num != 1) try buffer.appendSlice(", ${") else try buffer.appendSlice("${");
|
try buf_stream.print("{}:", .{param_num + 1});
|
||||||
|
|
||||||
try buf_stream.print("{}:", .{param_num});
|
if (param.comptime_token) |_| {
|
||||||
|
|
||||||
if (param_decl.comptime_token) |_| {
|
|
||||||
try buffer.appendSlice("comptime ");
|
try buffer.appendSlice("comptime ");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param_decl.noalias_token) |_| {
|
if (param.noalias_token) |_| {
|
||||||
try buffer.appendSlice("noalias ");
|
try buffer.appendSlice("noalias ");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param_decl.name_token) |name_token| {
|
if (param.name_token) |name_token| {
|
||||||
try buffer.appendSlice(tree.tokenSlice(name_token));
|
try buffer.appendSlice(tree.tokenSlice(name_token));
|
||||||
try buffer.appendSlice(": ");
|
try buffer.appendSlice(": ");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (param_decl.param_type) {
|
switch (param.param_type) {
|
||||||
.var_args => try buffer.appendSlice("..."),
|
.var_args => try buffer.appendSlice("..."),
|
||||||
.var_type => try buffer.appendSlice("var"),
|
.var_type => try buffer.appendSlice("var"),
|
||||||
.type_expr => |type_expr| {
|
.type_expr => |type_expr| {
|
||||||
var curr_tok = type_expr.firstToken();
|
var curr_tok = type_expr.firstToken();
|
||||||
var end_tok = type_expr.lastToken();
|
var end_tok = type_expr.lastToken();
|
||||||
while (curr_tok <= end_tok) : (curr_tok += 1) {
|
while (curr_tok <= end_tok) : (curr_tok += 1) {
|
||||||
const id = tree.tokens.at(curr_tok).id;
|
const id = tree.token_ids[curr_tok];
|
||||||
const is_comma = tree.tokens.at(curr_tok).id == .Comma;
|
const is_comma = id == .Comma;
|
||||||
|
|
||||||
if (curr_tok == end_tok and is_comma) continue;
|
if (curr_tok == end_tok and is_comma) continue;
|
||||||
|
|
||||||
@ -146,8 +148,8 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
|
|||||||
|
|
||||||
/// Gets a function signature (keywords, name, return value)
|
/// Gets a function signature (keywords, name, return value)
|
||||||
pub fn getVariableSignature(tree: *ast.Tree, var_decl: *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 start = tree.token_locs[var_decl.firstToken()].start;
|
||||||
const end = tree.tokens.at(var_decl.semicolon_token).start;
|
const end = tree.token_locs[var_decl.semicolon_token].start;
|
||||||
return tree.source[start..end];
|
return tree.source[start..end];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,11 +188,12 @@ pub fn getDeclNameToken(tree: *ast.Tree, node: *ast.Node) ?ast.TokenIndex {
|
|||||||
const vari = node.cast(ast.Node.VarDecl).?;
|
const vari = node.cast(ast.Node.VarDecl).?;
|
||||||
return vari.name_token;
|
return vari.name_token;
|
||||||
},
|
},
|
||||||
.ParamDecl => {
|
// @TODO Param decl is no longer a node
|
||||||
const decl = node.cast(ast.Node.ParamDecl).?;
|
// .ParamDecl => {
|
||||||
if (decl.name_token == null) return null;
|
// const decl = node.cast(ast.Node.ParamDecl).?;
|
||||||
return decl.name_token.?;
|
// if (decl.name_token == null) return null;
|
||||||
},
|
// return decl.name_token.?;
|
||||||
|
// },
|
||||||
.FnProto => {
|
.FnProto => {
|
||||||
const func = node.cast(ast.Node.FnProto).?;
|
const func = node.cast(ast.Node.FnProto).?;
|
||||||
if (func.name_token == null) return null;
|
if (func.name_token == null) return null;
|
||||||
@ -217,8 +220,8 @@ fn getDeclName(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
|
|||||||
|
|
||||||
/// Gets the child of node
|
/// Gets the child of node
|
||||||
pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
|
pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
|
||||||
var index: usize = 0;
|
var child_it = node.iterate();
|
||||||
while (node.iterate(index)) |child| : (index += 1) {
|
while (child_it.next()) |child| {
|
||||||
const child_name = getDeclName(tree, child) orelse continue;
|
const child_name = getDeclName(tree, child) orelse continue;
|
||||||
if (std.mem.eql(u8, child_name, name)) return child;
|
if (std.mem.eql(u8, child_name, name)) return child;
|
||||||
}
|
}
|
||||||
@ -236,8 +239,8 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*
|
|||||||
|
|
||||||
fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast.Node.ControlFlowExpression {
|
fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast.Node.ControlFlowExpression {
|
||||||
var result: ?*ast.Node.ControlFlowExpression = null;
|
var result: ?*ast.Node.ControlFlowExpression = null;
|
||||||
var idx: usize = 0;
|
var child_it = base_node.iterate();
|
||||||
while (base_node.iterate(idx)) |child_node| : (idx += 1) {
|
while (child_it.next()) |child_node| {
|
||||||
switch (child_node.id) {
|
switch (child_node.id) {
|
||||||
.ControlFlowExpression => {
|
.ControlFlowExpression => {
|
||||||
const cfe = child_node.cast(ast.Node.ControlFlowExpression).?;
|
const cfe = child_node.cast(ast.Node.ControlFlowExpression).?;
|
||||||
@ -293,15 +296,16 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
|||||||
|
|
||||||
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
|
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
|
||||||
},
|
},
|
||||||
.ParamDecl => {
|
// @TODO Param decl is no longer a node type.
|
||||||
const decl = node.cast(ast.Node.ParamDecl).?;
|
// .ParamDecl => {
|
||||||
switch (decl.param_type) {
|
// const decl = node.cast(ast.Node.ParamDecl).?;
|
||||||
.var_type, .type_expr => |var_type| {
|
// switch (decl.param_type) {
|
||||||
return resolveTypeOfNode(analysis_ctx, var_type) orelse null;
|
// .var_type, .type_expr => |var_type| {
|
||||||
},
|
// return resolveTypeOfNode(analysis_ctx, var_type) orelse null;
|
||||||
else => {},
|
// },
|
||||||
}
|
// else => {},
|
||||||
},
|
// }
|
||||||
|
// },
|
||||||
.Identifier => {
|
.Identifier => {
|
||||||
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| {
|
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| {
|
||||||
return resolveTypeOfNode(analysis_ctx, child);
|
return resolveTypeOfNode(analysis_ctx, child);
|
||||||
@ -311,18 +315,21 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
|||||||
const field = node.cast(ast.Node.ContainerField).?;
|
const field = node.cast(ast.Node.ContainerField).?;
|
||||||
return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null);
|
return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null);
|
||||||
},
|
},
|
||||||
.SuffixOp => {
|
.Call => {
|
||||||
const suffix_op = node.cast(ast.Node.SuffixOp).?;
|
const call = node.cast(ast.Node.Call).?;
|
||||||
switch (suffix_op.op) {
|
const decl = resolveTypeOfNode(analysis_ctx, call.lhs) orelse return null;
|
||||||
.Call, .StructInitializer => {
|
return switch (decl.id) {
|
||||||
const decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node) orelse return null;
|
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
|
||||||
return switch (decl.id) {
|
else => decl,
|
||||||
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
|
};
|
||||||
else => decl,
|
},
|
||||||
};
|
.StructInitializer => {
|
||||||
},
|
const struct_init = node.cast(ast.Node.StructInitializer).?;
|
||||||
else => {},
|
const decl = resolveTypeOfNode(analysis_ctx, struct_init.lhs) orelse return null;
|
||||||
}
|
return switch (decl.id) {
|
||||||
|
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
|
||||||
|
else => decl,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
.InfixOp => {
|
.InfixOp => {
|
||||||
const infix_op = node.cast(ast.Node.InfixOp).?;
|
const infix_op = node.cast(ast.Node.InfixOp).?;
|
||||||
@ -344,8 +351,8 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
|||||||
switch (prefix_op.op) {
|
switch (prefix_op.op) {
|
||||||
.SliceType, .ArrayType => return node,
|
.SliceType, .ArrayType => return node,
|
||||||
.PtrType => {
|
.PtrType => {
|
||||||
const op_token = analysis_ctx.tree.tokens.at(prefix_op.op_token);
|
const op_token_id = analysis_ctx.tree.token_ids[prefix_op.op_token];
|
||||||
switch (op_token.id) {
|
switch (op_token_id) {
|
||||||
.Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs),
|
.Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs),
|
||||||
.LBracket, .AsteriskAsterisk => return null,
|
.LBracket, .AsteriskAsterisk => return null,
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
@ -369,14 +376,14 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
|||||||
const builtin_call = node.cast(ast.Node.BuiltinCall).?;
|
const builtin_call = node.cast(ast.Node.BuiltinCall).?;
|
||||||
const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token);
|
const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token);
|
||||||
if (std.mem.eql(u8, call_name, "@This")) {
|
if (std.mem.eql(u8, call_name, "@This")) {
|
||||||
if (builtin_call.params.len != 0) return null;
|
if (builtin_call.params_len != 0) return null;
|
||||||
return analysis_ctx.last_this_node;
|
return analysis_ctx.last_this_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!std.mem.eql(u8, call_name, "@import")) return null;
|
if (!std.mem.eql(u8, call_name, "@import")) return null;
|
||||||
if (builtin_call.params.len > 1) return null;
|
if (builtin_call.params_len > 1) return null;
|
||||||
|
|
||||||
const import_param = builtin_call.params.at(0).*;
|
const import_param = builtin_call.paramsConst()[0];
|
||||||
if (import_param.id != .StringLiteral) return null;
|
if (import_param.id != .StringLiteral) return null;
|
||||||
|
|
||||||
const import_str = analysis_ctx.tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
|
const import_str = analysis_ctx.tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
|
||||||
@ -397,9 +404,9 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
|||||||
|
|
||||||
fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr: *std.ArrayList([]const u8)) !void {
|
fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr: *std.ArrayList([]const u8)) !void {
|
||||||
if (!std.mem.eql(u8, tree.tokenSlice(builtin_call.builtin_token), "@import")) return;
|
if (!std.mem.eql(u8, tree.tokenSlice(builtin_call.builtin_token), "@import")) return;
|
||||||
if (builtin_call.params.len > 1) return;
|
if (builtin_call.params_len > 1) return;
|
||||||
|
|
||||||
const import_param = builtin_call.params.at(0).*;
|
const import_param = builtin_call.paramsConst()[0];
|
||||||
if (import_param.id != .StringLiteral) return;
|
if (import_param.id != .StringLiteral) return;
|
||||||
|
|
||||||
const import_str = tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
|
const import_str = tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
|
||||||
@ -410,8 +417,7 @@ fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr:
|
|||||||
/// The import paths are valid as long as the tree is.
|
/// The import paths are valid as long as the tree is.
|
||||||
pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !void {
|
pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !void {
|
||||||
// TODO: Currently only detects `const smth = @import("string literal")<.SomeThing>;`
|
// TODO: Currently only detects `const smth = @import("string literal")<.SomeThing>;`
|
||||||
var idx: usize = 0;
|
for (tree.root_node.decls()) |decl| {
|
||||||
while (tree.root_node.iterate(idx)) |decl| : (idx += 1) {
|
|
||||||
if (decl.id != .VarDecl) continue;
|
if (decl.id != .VarDecl) continue;
|
||||||
const var_decl = decl.cast(ast.Node.VarDecl).?;
|
const var_decl = decl.cast(ast.Node.VarDecl).?;
|
||||||
if (var_decl.init_node == null) continue;
|
if (var_decl.init_node == null) continue;
|
||||||
@ -448,7 +454,7 @@ pub fn getFieldAccessTypeNode(
|
|||||||
switch (next.id) {
|
switch (next.id) {
|
||||||
.Eof => return current_node,
|
.Eof => return current_node,
|
||||||
.Identifier => {
|
.Identifier => {
|
||||||
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| {
|
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.loc.start..next.loc.end])) |child| {
|
||||||
if (resolveTypeOfNode(analysis_ctx, child)) |node_type| {
|
if (resolveTypeOfNode(analysis_ctx, child)) |node_type| {
|
||||||
current_node = node_type;
|
current_node = node_type;
|
||||||
} else return null;
|
} else return null;
|
||||||
@ -460,9 +466,9 @@ pub fn getFieldAccessTypeNode(
|
|||||||
return current_node;
|
return current_node;
|
||||||
} else if (after_period.id == .Identifier) {
|
} else if (after_period.id == .Identifier) {
|
||||||
// TODO: This works for now, maybe we should filter based on the partial identifier ourselves?
|
// TODO: This works for now, maybe we should filter based on the partial identifier ourselves?
|
||||||
if (after_period.end == line_length) return current_node;
|
if (after_period.loc.end == line_length) return current_node;
|
||||||
|
|
||||||
if (getChild(analysis_ctx.tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| {
|
if (getChild(analysis_ctx.tree, current_node, tokenizer.buffer[after_period.loc.start..after_period.loc.end])) |child| {
|
||||||
if (resolveTypeOfNode(analysis_ctx, child)) |child_type| {
|
if (resolveTypeOfNode(analysis_ctx, child)) |child_type| {
|
||||||
current_node = child_type;
|
current_node = child_type;
|
||||||
} else return null;
|
} else return null;
|
||||||
@ -521,8 +527,8 @@ pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
|
|||||||
pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node, source_index: usize) error{OutOfMemory}!void {
|
pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node, source_index: usize) error{OutOfMemory}!void {
|
||||||
switch (node.id) {
|
switch (node.id) {
|
||||||
.Root, .ContainerDecl => {
|
.Root, .ContainerDecl => {
|
||||||
var node_index: usize = 0;
|
var node_it = node.iterate();
|
||||||
while (node.iterate(node_index)) |child_node| : (node_index += 1) {
|
while (node_it.next()) |child_node| {
|
||||||
// Skip over container fields, we can only dot access those.
|
// Skip over container fields, we can only dot access those.
|
||||||
if (child_node.id == .ContainerField) continue;
|
if (child_node.id == .ContainerField) continue;
|
||||||
|
|
||||||
@ -537,9 +543,10 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
|
|||||||
.FnProto => {
|
.FnProto => {
|
||||||
const func = node.cast(ast.Node.FnProto).?;
|
const func = node.cast(ast.Node.FnProto).?;
|
||||||
|
|
||||||
var param_index: usize = 0;
|
for (func.paramsConst()) |param| {
|
||||||
while (param_index < func.params.len) : (param_index += 1)
|
// try decls.append(node)
|
||||||
try declsFromIndexInternal(decls, tree, func.params.at(param_index).*, source_index);
|
// @TODO We need to store something else than nodes from now on.
|
||||||
|
}
|
||||||
|
|
||||||
if (func.body_node) |body_node| {
|
if (func.body_node) |body_node| {
|
||||||
if (!nodeContainsSourceIndex(tree, body_node, source_index)) return;
|
if (!nodeContainsSourceIndex(tree, body_node, source_index)) return;
|
||||||
@ -552,8 +559,8 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
|
|||||||
try declsFromIndexInternal(decls, tree, test_decl.body_node, source_index);
|
try declsFromIndexInternal(decls, tree, test_decl.body_node, source_index);
|
||||||
},
|
},
|
||||||
.Block => {
|
.Block => {
|
||||||
var index: usize = 0;
|
var node_it = node.iterate();
|
||||||
while (node.iterate(index)) |inode| : (index += 1) {
|
while (node_it.next()) |inode| {
|
||||||
if (nodeComesAfterSourceIndex(tree, inode, source_index)) return;
|
if (nodeComesAfterSourceIndex(tree, inode, source_index)) return;
|
||||||
try declsFromIndexInternal(decls, tree, inode, source_index);
|
try declsFromIndexInternal(decls, tree, inode, source_index);
|
||||||
}
|
}
|
||||||
@ -617,8 +624,7 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
|
|||||||
},
|
},
|
||||||
.Switch => {
|
.Switch => {
|
||||||
const switch_node = node.cast(ast.Node.Switch).?;
|
const switch_node = node.cast(ast.Node.Switch).?;
|
||||||
var case_it = switch_node.cases.iterator(0);
|
for (switch_node.casesConst()) |case| {
|
||||||
while (case_it.next()) |case| {
|
|
||||||
const case_node = case.*.cast(ast.Node.SwitchCase).?;
|
const case_node = case.*.cast(ast.Node.SwitchCase).?;
|
||||||
if (nodeContainsSourceIndex(tree, case_node.expr, source_index)) {
|
if (nodeContainsSourceIndex(tree, case_node.expr, source_index)) {
|
||||||
if (case_node.payload) |payload| {
|
if (case_node.payload) |payload| {
|
||||||
@ -646,14 +652,13 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.ParamDecl => try decls.append(node),
|
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void {
|
pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void {
|
||||||
var index: usize = 0;
|
var node_it = node.iterate();
|
||||||
while (node.iterate(index)) |child_node| : (index += 1) {
|
while (node_it.next()) |child_node| {
|
||||||
try decls.append(child_node);
|
try decls.append(child_node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -664,38 +669,38 @@ pub fn declsFromIndex(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, source_
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn nodeContainsSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
|
fn nodeContainsSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
|
||||||
const first_token = tree.tokens.at(node.firstToken());
|
const first_token = tree.token_locs[node.firstToken()];
|
||||||
const last_token = tree.tokens.at(node.lastToken());
|
const last_token = tree.token_locs[node.lastToken()];
|
||||||
return source_index >= first_token.start and source_index <= last_token.end;
|
return source_index >= first_token.start and source_index <= last_token.end;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn nodeComesAfterSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
|
fn nodeComesAfterSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
|
||||||
const first_token = tree.tokens.at(node.firstToken());
|
const first_token = tree.token_locs[node.firstToken()];
|
||||||
const last_token = tree.tokens.at(node.lastToken());
|
const last_token = tree.token_locs[node.lastToken()];
|
||||||
return source_index < first_token.start;
|
return source_index < first_token.start;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 {
|
pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 {
|
||||||
var node = &tree.root_node.base;
|
var node = &tree.root_node.base;
|
||||||
var index: usize = 0;
|
|
||||||
while (node.iterate(index)) |child| {
|
var child_it = node.iterate();
|
||||||
|
while (child_it.next()) |child| {
|
||||||
if (!nodeContainsSourceIndex(tree, child, source_index)) {
|
if (!nodeContainsSourceIndex(tree, child, source_index)) {
|
||||||
index += 1;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (child.cast(ast.Node.BuiltinCall)) |builtin_call| blk: {
|
if (child.cast(ast.Node.BuiltinCall)) |builtin_call| blk: {
|
||||||
const call_name = tree.tokenSlice(builtin_call.builtin_token);
|
const call_name = tree.tokenSlice(builtin_call.builtin_token);
|
||||||
|
|
||||||
if (!std.mem.eql(u8, call_name, "@import")) break :blk;
|
if (!std.mem.eql(u8, call_name, "@import")) break :blk;
|
||||||
if (builtin_call.params.len != 1) break :blk;
|
if (builtin_call.params_len != 1) break :blk;
|
||||||
|
|
||||||
const import_param = builtin_call.params.at(0).*;
|
const import_param = builtin_call.paramsConst()[0];
|
||||||
const import_str_node = import_param.cast(ast.Node.StringLiteral) orelse break :blk;
|
const import_str_node = import_param.cast(ast.Node.StringLiteral) orelse break :blk;
|
||||||
const import_str = tree.tokenSlice(import_str_node.token);
|
const import_str = tree.tokenSlice(import_str_node.token);
|
||||||
return import_str[1 .. import_str.len - 1];
|
return import_str[1 .. import_str.len - 1];
|
||||||
}
|
}
|
||||||
node = child;
|
node = child;
|
||||||
index = 0;
|
child_it = node.iterate();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
34
src/main.zig
34
src/main.zig
@ -100,8 +100,7 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
|
|||||||
|
|
||||||
var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator);
|
var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator);
|
||||||
|
|
||||||
var error_it = tree.errors.iterator(0);
|
for (tree.errors) |*err| {
|
||||||
while (error_it.next()) |err| {
|
|
||||||
const loc = tree.tokenLocation(0, err.loc());
|
const loc = tree.tokenLocation(0, err.loc());
|
||||||
|
|
||||||
var mem_buffer: [256]u8 = undefined;
|
var mem_buffer: [256]u8 = undefined;
|
||||||
@ -119,9 +118,7 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (tree.errors.len == 0) {
|
if (tree.errors.len == 0) {
|
||||||
var decls = tree.root_node.decls.iterator(0);
|
for (tree.root_node.decls()) |decl| {
|
||||||
while (decls.next()) |decl_ptr| {
|
|
||||||
var decl = decl_ptr.*;
|
|
||||||
switch (decl.id) {
|
switch (decl.id) {
|
||||||
.FnProto => blk: {
|
.FnProto => blk: {
|
||||||
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
||||||
@ -179,8 +176,8 @@ fn containerToCompletion(
|
|||||||
container: *std.zig.ast.Node,
|
container: *std.zig.ast.Node,
|
||||||
config: Config,
|
config: Config,
|
||||||
) !void {
|
) !void {
|
||||||
var index: usize = 0;
|
var child_it = container.iterate();
|
||||||
while (container.iterate(index)) |child_node| : (index += 1) {
|
while (child_it.next()) |child_node| {
|
||||||
// Declarations in the same file do not need to be public.
|
// Declarations in the same file do not need to be public.
|
||||||
if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) {
|
if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) {
|
||||||
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config);
|
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config);
|
||||||
@ -229,7 +226,7 @@ fn nodeToCompletion(
|
|||||||
},
|
},
|
||||||
.VarDecl => {
|
.VarDecl => {
|
||||||
const var_decl = node.cast(std.zig.ast.Node.VarDecl).?;
|
const var_decl = node.cast(std.zig.ast.Node.VarDecl).?;
|
||||||
const is_const = analysis_ctx.tree.tokens.at(var_decl.mut_token).id == .Keyword_const;
|
const is_const = analysis_ctx.tree.token_ids[var_decl.mut_token] == .Keyword_const;
|
||||||
|
|
||||||
var child_analysis_context = try analysis_ctx.clone();
|
var child_analysis_context = try analysis_ctx.clone();
|
||||||
defer child_analysis_context.deinit();
|
defer child_analysis_context.deinit();
|
||||||
@ -259,16 +256,17 @@ fn nodeToCompletion(
|
|||||||
.detail = analysis.getVariableSignature(analysis_ctx.tree, var_decl),
|
.detail = analysis.getVariableSignature(analysis_ctx.tree, var_decl),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
.ParamDecl => {
|
// @TODO: No more ParamDecl node.
|
||||||
const param = node.cast(std.zig.ast.Node.ParamDecl).?;
|
// .ParamDecl => {
|
||||||
if (param.name_token) |name_token|
|
// const param = node.cast(std.zig.ast.Node.ParamDecl).?;
|
||||||
try list.append(.{
|
// if (param.name_token) |name_token|
|
||||||
.label = analysis_ctx.tree.tokenSlice(name_token),
|
// try list.append(.{
|
||||||
.kind = .Constant,
|
// .label = analysis_ctx.tree.tokenSlice(name_token),
|
||||||
.documentation = doc,
|
// .kind = .Constant,
|
||||||
.detail = analysis.getParamSignature(analysis_ctx.tree, param),
|
// .documentation = doc,
|
||||||
});
|
// .detail = analysis.getParamSignature(analysis_ctx.tree, param),
|
||||||
},
|
// });
|
||||||
|
// },
|
||||||
.PrefixOp => {
|
.PrefixOp => {
|
||||||
try list.append(.{
|
try list.append(.{
|
||||||
.label = "len",
|
.label = "len",
|
||||||
|
Loading…
Reference in New Issue
Block a user