Merge pull request #270 from InterplanetaryEngineer/master

Fix some crashes & find all @imports
This commit is contained in:
Alexandros Naskos 2021-03-30 02:29:55 -07:00 committed by GitHub
commit 2bf5a7cfc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 59 deletions

View File

@ -1182,64 +1182,29 @@ pub fn resolveTypeOfNode(store: *DocumentStore, arena: *std.heap.ArenaAllocator,
return resolveTypeOfNodeInternal(store, arena, node_handle, &bound_type_params); 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). /// 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 { 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.tokens.items(.tag);
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 init_node = var_decl.ast.init_node; var i: usize = 0;
const init_node_tag = tags[init_node]; while (i < tags.len) : (i += 1) {
switch (init_node_tag) { if (tags[i] != .builtin)
.builtin_call, continue;
.builtin_call_comma, const text = tree.tokenSlice(@intCast(u32, i));
.builtin_call_two,
.builtin_call_two_comma, if (std.mem.eql(u8, text, "@import")) {
=> try maybeCollectImport(tree, init_node, import_arr), if (i + 3 >= tags.len)
.field_access => { break;
const lhs = tree.nodes.items(.data)[init_node].lhs; if (tags[i + 1] != .l_paren)
if (isBuiltinCall(tree, lhs)) { continue;
try maybeCollectImport(tree, lhs, import_arr); if (tags[i + 2] != .string_literal)
} continue;
}, if (tags[i + 3] != .r_paren)
else => {}, continue;
const str = tree.tokenSlice(@intCast(u32, i + 2));
try import_arr.append(str[1..str.len-1]);
} }
} }
} }

View File

@ -422,13 +422,16 @@ pub fn applyChanges(
if (change.Object.get("range")) |range| { if (change.Object.get("range")) |range| {
std.debug.assert(document.text.ptr == document.mem.ptr); 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{ const start_pos = types.Position{
.line = range.Object.get("start").?.Object.get("line").?.Integer, .line = start_obj.get("line").?.Integer,
.character = range.Object.get("start").?.Object.get("character").?.Integer, .character = start_obj.get("character").?.Integer,
}; };
const end_obj = range.Object.get("end").?.Object;
const end_pos = types.Position{ const end_pos = types.Position{
.line = range.Object.get("end").?.Object.get("line").?.Integer, .line = end_obj.get("line").?.Integer,
.character = range.Object.get("end").?.Object.get("character").?.Integer, .character = end_obj.get("character").?.Integer,
}; };
const change_text = change.Object.get("text").?.String; const change_text = change.Object.get("text").?.String;