Compiles without errors. Still needs improvement

This commit is contained in:
Luuk de Gram 2021-03-01 22:18:38 +01:00
parent ada0d13ba9
commit c8a2467fac
No known key found for this signature in database
GPG Key ID: A002B174963DBB7D
4 changed files with 181 additions and 136 deletions

View File

@ -1055,13 +1055,12 @@ pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: ast.Tree) !v
const init_node_tag = tags[init_node]; const init_node_tag = tags[init_node];
switch (init_node_tag) { switch (init_node_tag) {
.builtin_call => try maybeCollectImport(tree, init_node, import_arr), .builtin_call => try maybeCollectImport(tree, init_node, import_arr),
// @TODO: FIX ME what is the syntax to support for imports using dot notation? .field_access => {
// .Period => { const lhs = tree.nodes.items(.data)[init_node].lhs;
// const infix_op = init_node.cast(ast.Node.SimpleInfixOp).?; if (isBuiltinCall(tree, lhs)) {
try maybeCollectImport(tree, lhs, import_arr);
// if (infix_op.lhs.tag != .BuiltinCall) continue; }
// try maybeCollectImport(tree, infix_op.lhs.castTag(.BuiltinCall).?, import_arr); },
// },
else => {}, else => {},
} }
} }
@ -1268,7 +1267,7 @@ fn isBuiltinCall(tree: ast.Tree, node: ast.Node.Index) bool {
}; };
} }
fn builtinCallParams(tree: ast.Tree, node: ast.Node.Index) []const ast.Node.Index { pub fn builtinCallParams(tree: ast.Tree, node: ast.Node.Index) []const ast.Node.Index {
std.debug.assert(isBuiltinCall(tree, node)); std.debug.assert(isBuiltinCall(tree, node));
const datas = tree.nodes.items(.data); const datas = tree.nodes.items(.data);
@ -1290,7 +1289,7 @@ pub fn fnProto(tree: ast.Tree, node: ast.Node.Index, buf: *[1]ast.Node.Index) ?a
.fn_proto_multi => tree.fnProtoMulti(node), .fn_proto_multi => tree.fnProtoMulti(node),
.fn_proto_one => tree.fnProtoOne(buf, node), .fn_proto_one => tree.fnProtoOne(buf, node),
.fn_proto_simple => tree.fnProtoSimple(buf, node), .fn_proto_simple => tree.fnProtoSimple(buf, node),
.fn_decl => tree.fnProto(tree.nodes.items(.data)[node].lhs), // .fn_decl => tree.fnProto(tree.nodes.items(.data)[node].lhs),
else => null, else => null,
}; };
} }
@ -1479,107 +1478,147 @@ pub fn documentPositionContext(arena: *std.heap.ArenaAllocator, document: types.
}; };
} }
fn addOutlineNodes(allocator: *std.mem.Allocator, tree: ast.Tree, parent: ast.Node.Index, context: *GetDocumentSymbolsContext) anyerror!void { fn addOutlineNodes(allocator: *std.mem.Allocator, tree: ast.Tree, child: ast.Node.Index, context: *GetDocumentSymbolsContext) anyerror!void {
switch (tree.nodes.items(.tag)[parent]) { switch (tree.nodes.items(.tag)[child]) {
.StringLiteral, .string_literal,
.IntegerLiteral, .integer_literal,
.BuiltinCall, .builtin_call,
.Call, .builtin_call_comma,
.Identifier, .builtin_call_two,
.Add, .builtin_call_two_comma,
.AddWrap, .call,
.ArrayCat, .call_comma,
.ArrayMult, .call_one,
.Assign, .call_one_comma,
.AssignBitAnd, .async_call,
.AssignBitOr, .async_call_comma,
.AssignBitShiftLeft, .async_call_one,
.AssignBitShiftRight, .async_call_one_comma,
.AssignBitXor, .identifier,
.AssignDiv, .add,
.AssignSub, .add_wrap,
.AssignSubWrap, .array_cat,
.AssignMod, .array_mult,
.AssignAdd, .assign,
.AssignAddWrap, .assign_bit_and,
.AssignMul, .assign_bit_or,
.AssignMulWrap, .assign_bit_shift_left,
.BangEqual, .assign_bit_shift_right,
.BitAnd, .assign_bit_xor,
.BitOr, .assign_div,
.BitShiftLeft, .assign_sub,
.BitShiftRight, .assign_sub_wrap,
.BitXor, .assign_mod,
.BoolAnd, .assign_add,
.BoolOr, .assign_add_wrap,
.Div, .assign_mul,
.EqualEqual, .assign_mul_wrap,
.ErrorUnion, .bang_equal,
.GreaterOrEqual, .bit_and,
.GreaterThan, .bit_or,
.LessOrEqual, .bit_shift_left,
.LessThan, .bit_shift_right,
.MergeErrorSets, .bit_xor,
.Mod, .bool_and,
.Mul, .bool_or,
.MulWrap, .div,
.Period, .equal_equal,
.Range, .error_union,
.Sub, .greater_or_equal,
.SubWrap, .greater_than,
.OrElse, .less_or_equal,
.AddressOf, .less_than,
.Await, .merge_error_sets,
.BitNot, .mod,
.BoolNot, .mul,
.OptionalType, .mul_wrap,
.Negation, .field_access,
.NegationWrap, .switch_range,
.Resume, .sub,
.Try, .sub_wrap,
.ArrayType, .@"orelse",
.ArrayTypeSentinel, .address_of,
.PtrType, .@"await",
.SliceType, .bit_not,
.Slice, .bool_not,
.Deref, .optional_type,
.UnwrapOptional, .negation,
.ArrayAccess, .negation_wrap,
.Return, .@"resume",
.Break, .@"try",
.Continue, .array_type,
.ArrayInitializerDot, .array_type_sentinel,
.SwitchElse, .ptr_type,
.SwitchCase, .ptr_type_aligned,
.For, .ptr_type_bit_range,
.EnumLiteral, .ptr_type_sentinel,
.PointerIndexPayload, .slice_open,
.StructInitializerDot, .slice_sentinel,
.PointerPayload, .deref,
.While, .unwrap_optional,
.Switch, .array_access,
.Else, .@"return",
.BoolLiteral, .@"break",
.NullLiteral, .@"continue",
.Defer, .array_init,
.StructInitializer, .array_init_comma,
.FieldInitializer, .array_init_dot,
.If, .array_init_dot_comma,
.MultilineStringLiteral, .array_init_dot_two,
.UndefinedLiteral, .array_init_dot_two_comma,
.AnyType, .array_init_one,
.Block, .array_init_one_comma,
.ErrorSetDecl, .@"switch",
.switch_comma,
.switch_case,
.switch_case_one,
.@"for",
.for_simple,
.enum_literal,
.struct_init,
.struct_init_comma,
.struct_init_dot,
.struct_init_dot_comma,
.struct_init_dot_two,
.struct_init_dot_two_comma,
.struct_init_one,
.struct_init_one_comma,
.@"while",
.while_simple,
.while_cont,
.true_literal,
.false_literal,
.null_literal,
.@"defer",
.@"if",
.if_simple,
.multiline_string_literal,
.undefined_literal,
.@"anytype",
.block,
.block_semicolon,
.block_two,
.block_two_semicolon,
.error_set_decl,
=> return, => return,
.container_decl,
.ContainerDecl => { .container_decl_arg,
const decl = child.castTag(.ContainerDecl).?; .container_decl_arg_trailing,
.container_decl_two,
for (decl.fieldsAndDecls()) |cchild| .container_decl_two_trailing,
try addOutlineNodes(allocator, tree, cchild, context); .tagged_union,
.tagged_union_trailing,
.tagged_union_enum_tag,
.tagged_union_enum_tag_trailing,
.tagged_union_two,
.tagged_union_two_trailing,
=> {
var buf: [2]ast.Node.Index = undefined;
for (declMembers(tree, tree.nodes.items(.tag)[child], child, &buf)) |member|
try addOutlineNodes(allocator, tree, member, context);
return; return;
}, },
else => {}, else => |t| {},
} }
try getDocumentSymbolsInternal(allocator, tree, child, context); try getDocumentSymbolsInternal(allocator, tree, child, context);
} }
@ -1614,6 +1653,7 @@ fn getDocumentSymbolsInternal(allocator: *std.mem.Allocator, tree: ast.Tree, nod
}; };
const tags = tree.nodes.items(.tag); const tags = tree.nodes.items(.tag);
log.debug("{s} - {s}", .{ name, tags[node] });
(try context.symbols.addOne()).* = .{ (try context.symbols.addOne()).* = .{
.name = name, .name = name,
.kind = switch (tags[node]) { .kind = switch (tags[node]) {
@ -1621,6 +1661,7 @@ fn getDocumentSymbolsInternal(allocator: *std.mem.Allocator, tree: ast.Tree, nod
.fn_proto_simple, .fn_proto_simple,
.fn_proto_multi, .fn_proto_multi,
.fn_proto_one, .fn_proto_one,
.fn_decl,
=> .Function, => .Function,
.local_var_decl, .local_var_decl,
.global_var_decl, .global_var_decl,
@ -1632,6 +1673,10 @@ fn getDocumentSymbolsInternal(allocator: *std.mem.Allocator, tree: ast.Tree, nod
.container_field_init, .container_field_init,
.tagged_union_enum_tag, .tagged_union_enum_tag,
.tagged_union_enum_tag_trailing, .tagged_union_enum_tag_trailing,
.tagged_union,
.tagged_union_trailing,
.tagged_union_two,
.tagged_union_two_trailing,
=> .Field, => .Field,
else => .Variable, else => .Variable,
}, },
@ -1647,14 +1692,16 @@ fn getDocumentSymbolsInternal(allocator: *std.mem.Allocator, tree: ast.Tree, nod
.encoding = context.encoding, .encoding = context.encoding,
}; };
var index: usize = 0; if (isContainer(tags[node])) {
if (true) @panic("FIX: addOutlineNodes"); var buf: [2]ast.Node.Index = undefined;
// try addOutlineNodes(allocator, tree, node, &child_context); for (declMembers(tree, tags[node], node, &buf)) |child|
try addOutlineNodes(allocator, tree, child, &child_context);
// while (node.iterate(index)) |child| : (index += 1) { }
// try addOutlineNodes(allocator, tree, child, &child_context);
// }
if (varDecl(tree, node)) |var_decl| {
if (var_decl.ast.init_node != 0)
try addOutlineNodes(allocator, tree, var_decl.ast.init_node, &child_context);
}
break :ch children.items; break :ch children.items;
}, },
}; };
@ -2056,7 +2103,7 @@ fn lookupSymbolGlobalInternal(
source_index: usize, source_index: usize,
use_trail: *std.ArrayList(ast.Node.Index), use_trail: *std.ArrayList(ast.Node.Index),
) error{OutOfMemory}!?DeclWithHandle { ) error{OutOfMemory}!?DeclWithHandle {
for (handle.document_scope.scopes) |scope| { for (handle.document_scope.scopes) |scope, i| {
if (source_index >= scope.range.start and source_index < scope.range.end) { if (source_index >= scope.range.start and source_index < scope.range.end) {
if (scope.decls.getEntry(symbol)) |candidate| { if (scope.decls.getEntry(symbol)) |candidate| {
switch (candidate.value) { switch (candidate.value) {
@ -2266,10 +2313,10 @@ pub fn declMembers(tree: ast.Tree, tag: ast.Node.Tag, node_idx: ast.Node.Index,
return switch (tag) { return switch (tag) {
.container_decl, .container_decl_trailing => tree.containerDecl(node_idx).ast.members, .container_decl, .container_decl_trailing => tree.containerDecl(node_idx).ast.members,
.container_decl_arg, .container_decl_arg_trailing => tree.containerDeclArg(node_idx).ast.members, .container_decl_arg, .container_decl_arg_trailing => tree.containerDeclArg(node_idx).ast.members,
.container_decl_two, .container_decl_two_trailing => tree.containerDeclTwo(&buffer, node_idx).ast.members, .container_decl_two, .container_decl_two_trailing => tree.containerDeclTwo(buffer, node_idx).ast.members,
.tagged_union, .tagged_union_trailing => tree.taggedUnion(node_idx).ast.members, .tagged_union, .tagged_union_trailing => tree.taggedUnion(node_idx).ast.members,
.tagged_union_enum_tag, .tagged_union_enum_tag_trailing => tree.taggedUnionEnumTag(node_idx).ast.members, .tagged_union_enum_tag, .tagged_union_enum_tag_trailing => tree.taggedUnionEnumTag(node_idx).ast.members,
.tagged_union_two, .tagged_union_two_trailing => tree.taggedUnionTwo(&buffer, node_idx).ast.members, .tagged_union_two, .tagged_union_two_trailing => tree.taggedUnionTwo(buffer, node_idx).ast.members,
.root => tree.rootDecls(), .root => tree.rootDecls(),
// @TODO: Fix error set declarations // @TODO: Fix error set declarations
.error_set_decl => &[_]ast.Node.Index{}, .error_set_decl => &[_]ast.Node.Index{},
@ -2307,7 +2354,7 @@ fn makeScopeInternal(
if (isContainer(node)) { if (isContainer(node)) {
var buf: [2]ast.Node.Index = undefined; var buf: [2]ast.Node.Index = undefined;
const ast_decls = declMembers(tree, node, node_idx); const ast_decls = declMembers(tree, node, node_idx, &buf);
(try scopes.addOne(allocator)).* = .{ (try scopes.addOne(allocator)).* = .{
.range = nodeSourceRange(tree, node_idx), .range = nodeSourceRange(tree, node_idx),
@ -2412,7 +2459,10 @@ fn makeScopeInternal(
} }
switch (node) { switch (node) {
.fn_proto, .fn_proto_one, .fn_proto_simple, .fn_proto_multi, .fn_decl => { .fn_decl => {
try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, data[node_idx].rhs);
},
.fn_proto, .fn_proto_one, .fn_proto_simple, .fn_proto_multi => {
var buf: [1]ast.Node.Index = undefined; var buf: [1]ast.Node.Index = undefined;
const func = fnProto(tree, node_idx, &buf).?; const func = fnProto(tree, node_idx, &buf).?;
@ -2435,10 +2485,6 @@ fn makeScopeInternal(
} }
} }
if (node == .fn_decl) {
try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, data[node_idx].rhs);
}
return; return;
}, },
.test_decl => { .test_decl => {

View File

@ -650,6 +650,7 @@ fn getLabelGlobal(pos_index: usize, handle: *DocumentStore.Handle) !?analysis.De
fn getSymbolGlobal(arena: *std.heap.ArenaAllocator, pos_index: usize, handle: *DocumentStore.Handle) !?analysis.DeclWithHandle { fn getSymbolGlobal(arena: *std.heap.ArenaAllocator, pos_index: usize, handle: *DocumentStore.Handle) !?analysis.DeclWithHandle {
const name = identifierFromPosition(pos_index, handle.*); const name = identifierFromPosition(pos_index, handle.*);
logger.debug("Name: {s}", .{name});
if (name.len == 0) return null; if (name.len == 0) return null;
return try analysis.lookupSymbolGlobal(&document_store, arena, handle, name, pos_index); return try analysis.lookupSymbolGlobal(&document_store, arena, handle, name, pos_index);
@ -1314,7 +1315,6 @@ fn hoverHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: reque
if (req.params.position.character >= 0) { if (req.params.position.character >= 0) {
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding); const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position); const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position);
switch (pos_context) { switch (pos_context) {
.builtin => try hoverDefinitionBuiltin(arena, id, doc_position.absolute_index, handle), .builtin => try hoverDefinitionBuiltin(arena, id, doc_position.absolute_index, handle),
.var_access => try hoverDefinitionGlobal(arena, id, doc_position.absolute_index, handle, config), .var_access => try hoverDefinitionGlobal(arena, id, doc_position.absolute_index, handle, config),

View File

@ -71,14 +71,14 @@ pub const TokenLocation = struct {
}; };
pub fn tokenRelativeLocation(tree: std.zig.ast.Tree, start_index: usize, token: std.zig.ast.TokenIndex, encoding: Encoding) !TokenLocation { pub fn tokenRelativeLocation(tree: std.zig.ast.Tree, start_index: usize, token: std.zig.ast.TokenIndex, encoding: Encoding) !TokenLocation {
const token_loc = tree.tokenLocation(@truncate(u32, start_index), token); const start = tree.tokens.items(.start)[token];
var loc = TokenLocation{ var loc = TokenLocation{
.line = 0, .line = 0,
.column = 0, .column = 0,
.offset = 0, .offset = 0,
}; };
const token_start = token_loc.line_start; const token_start = start;
const source = tree.source[start_index..]; const source = tree.source[start_index..];
var i: usize = 0; var i: usize = 0;
while (i + start_index < token_start) { while (i + start_index < token_start) {

View File

@ -136,7 +136,7 @@ fn symbolReferencesInternal(
// try symbolReferencesInternal(arena, store, .{ .node = use.expr, .handle = handle }, decl, encoding, context, handler); // try symbolReferencesInternal(arena, store, .{ .node = use.expr, .handle = handle }, decl, encoding, context, handler);
// }, // },
.container_field, .container_field_align, .container_field_init => { .container_field, .container_field_align, .container_field_init => {
const field = analysis.containerField(node).?; const field = analysis.containerField(tree, node).?;
if (field.ast.type_expr != 0) { if (field.ast.type_expr != 0) {
try symbolReferencesInternal(arena, store, .{ .node = field.ast.type_expr, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = field.ast.type_expr, .handle = handle }, decl, encoding, context, handler);
} }
@ -145,7 +145,7 @@ fn symbolReferencesInternal(
} }
}, },
.identifier => { .identifier => {
if (try analysis.lookupSymbolGlobal(store, arena, handle, tree.getNodeSource(node), starts[main_tokens[nodes]])) |child| { if (try analysis.lookupSymbolGlobal(store, arena, handle, tree.getNodeSource(node), starts[main_tokens[node]])) |child| {
if (std.meta.eql(decl, child)) { if (std.meta.eql(decl, child)) {
try tokenReference(handle, main_tokens[node], encoding, context, handler); try tokenReference(handle, main_tokens[node], encoding, context, handler);
} }
@ -204,14 +204,14 @@ fn symbolReferencesInternal(
}, },
.switch_case => { .switch_case => {
const case = tree.switchCase(node); const case = tree.switchCase(node);
for (case_one.ast.values) |val| for (case.ast.values) |val|
try symbolReferencesInternal(arena, store, .{ .node = val, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = val, .handle = handle }, decl, encoding, context, handler);
}, },
.@"while", .while_simple, .while_con, .for_simple, .@"for" => { .@"while", .while_simple, .while_cont, .for_simple, .@"for" => {
const loop: ast.full.While = switch (node_tags[node]) { const loop: ast.full.While = switch (node_tags[node]) {
.@"while" => tree.whileFull(node), .@"while" => tree.whileFull(node),
.while_simple => tree.whileSimple(node), .while_simple => tree.whileSimple(node),
.while_con => tree.whileCont(node), .while_cont => tree.whileCont(node),
.for_simple => tree.forSimple(node), .for_simple => tree.forSimple(node),
.@"for" => tree.forFull(node), .@"for" => tree.forFull(node),
else => unreachable, else => unreachable,
@ -276,7 +276,7 @@ fn symbolReferencesInternal(
const array_init = switch (n) { const array_init = switch (n) {
.array_init, .array_init_comma => tree.arrayInit(node), .array_init, .array_init_comma => tree.arrayInit(node),
.array_init_dot, .array_init_dot_comma => tree.arrayInitDot(node), .array_init_dot, .array_init_dot_comma => tree.arrayInitDot(node),
.array_init_one, .array_init_one_comma => tree.arrayInitOne(&buf[0..1], node), .array_init_one, .array_init_one_comma => tree.arrayInitOne(buf[0..1], node),
.array_init_dot_two, .array_init_dot_two_comma => tree.arrayInitDotTwo(&buf, node), .array_init_dot_two, .array_init_dot_two_comma => tree.arrayInitDotTwo(&buf, node),
else => unreachable, else => unreachable,
}; };
@ -298,7 +298,7 @@ fn symbolReferencesInternal(
const struct_init: ast.full.StructInit = switch (n) { const struct_init: ast.full.StructInit = switch (n) {
.struct_init, .struct_init_comma => tree.structInit(node), .struct_init, .struct_init_comma => tree.structInit(node),
.struct_init_dot, .struct_init_dot_comma => tree.structInitDot(node), .struct_init_dot, .struct_init_dot_comma => tree.structInitDot(node),
.struct_init_one, .struct_init_one_comma => tree.structInitOne(&buf[0..1], node), .struct_init_one, .struct_init_one_comma => tree.structInitOne(buf[0..1], node),
.struct_init_dot_two, .struct_init_dot_two_comma => tree.structInitDotTwo(&buf, node), .struct_init_dot_two, .struct_init_dot_two_comma => tree.structInitDotTwo(&buf, node),
else => unreachable, else => unreachable,
}; };
@ -369,8 +369,7 @@ fn symbolReferencesInternal(
.builtin_call_two, .builtin_call_two,
.builtin_call_two_comma, .builtin_call_two_comma,
=> { => {
const builtin_call = analysis.builtinCallParams(); for (analysis.builtinCallParams(tree, node)) |param|
for (analysis.builtinCallParams()) |param|
try symbolReferencesInternal(arena, store, .{ .node = param, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = param, .handle = handle }, decl, encoding, context, handler);
}, },
.@"asm", .asm_simple => |a| { .@"asm", .asm_simple => |a| {
@ -455,7 +454,7 @@ fn symbolReferencesInternal(
.mod, .mod,
.mul, .mul,
.mul_wrap, .mul_wrap,
.range, .switch_range,
.sub, .sub,
.sub_wrap, .sub_wrap,
.@"orelse", .@"orelse",