diff --git a/src/analysis.zig b/src/analysis.zig index b060848..97578b4 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -1182,64 +1182,29 @@ pub fn resolveTypeOfNode(store: *DocumentStore, arena: *std.heap.ArenaAllocator, return resolveTypeOfNodeInternal(store, arena, node_handle, &bound_type_params); } -fn maybeCollectImport(tree: ast.Tree, builtin_call: ast.Node.Index, arr: *std.ArrayList([]const u8)) !void { - const tags = tree.nodes.items(.tag); - const datas = tree.nodes.items(.data); - - const builtin_tag = tags[builtin_call]; - const data = datas[builtin_call]; - - std.debug.assert(isBuiltinCall(tree, builtin_call)); - if (!std.mem.eql(u8, tree.tokenSlice(builtin_call), "@import")) return; - - const params = switch (builtin_tag) { - .builtin_call, .builtin_call_comma => tree.extra_data[data.lhs..data.rhs], - .builtin_call_two, .builtin_call_two_comma => if (data.lhs == 0) - &[_]ast.Node.Index{} - else if (data.rhs == 0) - &[_]ast.Node.Index{data.lhs} - else - &[_]ast.Node.Index{ data.lhs, data.rhs }, - else => unreachable, - }; - if (params.len != 1) return; - - if (tags[params[0]] != .string_literal) return; - - const import_str = tree.tokenSlice(tree.nodes.items(.main_token)[params[0]]); - try arr.append(import_str[1 .. import_str.len - 1]); -} - /// Collects all imports we can find into a slice of import paths (without quotes). -/// The import paths are valid as long as the tree is. pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: ast.Tree) !void { - // TODO: Currently only detects `const smth = @import("string literal")<.SomeThing>;` - const tags = tree.nodes.items(.tag); - for (tree.rootDecls()) |decl_idx| { - const var_decl_maybe: ?ast.full.VarDecl = switch (tags[decl_idx]) { - .global_var_decl => tree.globalVarDecl(decl_idx), - .local_var_decl => tree.localVarDecl(decl_idx), - .simple_var_decl => tree.simpleVarDecl(decl_idx), - else => null, - }; - const var_decl = var_decl_maybe orelse continue; - if (var_decl.ast.init_node == 0) continue; + const tags = tree.tokens.items(.tag); - const init_node = var_decl.ast.init_node; - const init_node_tag = tags[init_node]; - switch (init_node_tag) { - .builtin_call, - .builtin_call_comma, - .builtin_call_two, - .builtin_call_two_comma, - => try maybeCollectImport(tree, init_node, import_arr), - .field_access => { - const lhs = tree.nodes.items(.data)[init_node].lhs; - if (isBuiltinCall(tree, lhs)) { - try maybeCollectImport(tree, lhs, import_arr); - } - }, - else => {}, + var i: usize = 0; + while (i < tags.len) : (i += 1) { + if (tags[i] != .builtin) + continue; + const text = tree.tokenSlice(@intCast(u32, i)); + + if (std.mem.eql(u8, text, "@import")) { + if (i + 3 >= tags.len) + break; + if (tags[i + 1] != .l_paren) + continue; + if (tags[i + 2] != .string_literal) + continue; + if (tags[i + 3] != .r_paren) + continue; + + + const str = tree.tokenSlice(@intCast(u32, i + 2)); + try import_arr.append(str[1..str.len-1]); } } } diff --git a/src/document_store.zig b/src/document_store.zig index c67cbba..2560a0c 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -422,13 +422,16 @@ pub fn applyChanges( if (change.Object.get("range")) |range| { std.debug.assert(document.text.ptr == document.mem.ptr); + // TODO: add tests and validate the JSON + const start_obj = range.Object.get("start").?.Object; const start_pos = types.Position{ - .line = range.Object.get("start").?.Object.get("line").?.Integer, - .character = range.Object.get("start").?.Object.get("character").?.Integer, + .line = start_obj.get("line").?.Integer, + .character = start_obj.get("character").?.Integer, }; + const end_obj = range.Object.get("end").?.Object; const end_pos = types.Position{ - .line = range.Object.get("end").?.Object.get("line").?.Integer, - .character = range.Object.get("end").?.Object.get("character").?.Integer, + .line = end_obj.get("line").?.Integer, + .character = end_obj.get("character").?.Integer, }; const change_text = change.Object.get("text").?.String;