More progress
This commit is contained in:
parent
9b08d9e88a
commit
d6609d918b
168
src/analysis.zig
168
src/analysis.zig
@ -1,5 +1,5 @@
|
||||
const std = @import("std");
|
||||
const AnalysisContext = @import("document_store.zig").AnalysisContext;
|
||||
const DocumentStore = @import("document_store.zig");
|
||||
const ast = std.zig.ast;
|
||||
const types = @import("types.zig");
|
||||
|
||||
@ -244,15 +244,14 @@ fn findReturnStatement(tree: *ast.Tree, fn_decl: *ast.Node.FnProto) ?*ast.Node.C
|
||||
}
|
||||
|
||||
/// Resolves the return type of a function
|
||||
fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto) ?*ast.Node {
|
||||
if (isTypeFunction(analysis_ctx.tree(), fn_decl) and fn_decl.body_node != null) {
|
||||
fn resolveReturnType(store: *DocumentStore, fn_decl: *ast.Node.FnProto, handle: *DocumentStore.Handle) !?NodeWithHandle {
|
||||
if (isTypeFunction(handle.tree, fn_decl) and fn_decl.body_node != null) {
|
||||
// If this is a type function and it only contains a single return statement that returns
|
||||
// a container declaration, we will return that declaration.
|
||||
const ret = findReturnStatement(analysis_ctx.tree(), fn_decl) orelse return null;
|
||||
const ret = findReturnStatement(handle.tree, fn_decl) orelse return null;
|
||||
if (ret.rhs) |rhs|
|
||||
if (resolveTypeOfNode(analysis_ctx, rhs)) |res_rhs| switch (res_rhs.id) {
|
||||
if (try resolveTypeOfNode(store, .{ .node = rhs, .handle = handle })) |res_rhs| switch (res_rhs.node.id) {
|
||||
.ContainerDecl => {
|
||||
analysis_ctx.onContainer(res_rhs) catch return null;
|
||||
return res_rhs;
|
||||
},
|
||||
else => return null,
|
||||
@ -262,16 +261,16 @@ fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto)
|
||||
}
|
||||
|
||||
return switch (fn_decl.return_type) {
|
||||
.Explicit, .InferErrorSet => |return_type| resolveTypeOfNode(analysis_ctx, return_type),
|
||||
.Explicit, .InferErrorSet => |return_type| try resolveTypeOfNode(store, .{ .node = return_type, .handle = handle }),
|
||||
.Invalid => null,
|
||||
};
|
||||
}
|
||||
|
||||
/// Resolves the child type of an optional type
|
||||
fn resolveUnwrapOptionalType(analysis_ctx: *AnalysisContext, opt: *ast.Node) ?*ast.Node {
|
||||
if (opt.cast(ast.Node.PrefixOp)) |prefix_op| {
|
||||
fn resolveUnwrapOptionalType(store: *DocumentStore, opt: NodeWithHandle) !?NodeWithHandle {
|
||||
if (opt.node.cast(ast.Node.PrefixOp)) |prefix_op| {
|
||||
if (prefix_op.op == .OptionalType) {
|
||||
return resolveTypeOfNode(analysis_ctx, prefix_op.rhs);
|
||||
return try resolveTypeOfNode(store, .{ .node = prefix_op.rhs, .handle = opt.handle });
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,12 +278,12 @@ fn resolveUnwrapOptionalType(analysis_ctx: *AnalysisContext, opt: *ast.Node) ?*a
|
||||
}
|
||||
|
||||
/// Resolves the child type of a defer type
|
||||
fn resolveDerefType(analysis_ctx: *AnalysisContext, deref: *ast.Node) ?*ast.Node {
|
||||
if (deref.cast(ast.Node.PrefixOp)) |pop| {
|
||||
fn resolveDerefType(store: *DocumentStore, deref: NodeWithHandle) !?NodeWithHandle {
|
||||
if (deref.node.cast(ast.Node.PrefixOp)) |pop| {
|
||||
if (pop.op == .PtrType) {
|
||||
const op_token_id = analysis_ctx.tree().token_ids[pop.op_token];
|
||||
const op_token_id = deref.handle.tree.token_ids[pop.op_token];
|
||||
switch (op_token_id) {
|
||||
.Asterisk => return resolveTypeOfNode(analysis_ctx, pop.rhs),
|
||||
.Asterisk => return try resolveTypeOfNode(store, .{ .node = pop.rhs, .handle = deref.handle }),
|
||||
.LBracket, .AsteriskAsterisk => return null,
|
||||
else => unreachable,
|
||||
}
|
||||
@ -293,9 +292,9 @@ fn resolveDerefType(analysis_ctx: *AnalysisContext, deref: *ast.Node) ?*ast.Node
|
||||
return null;
|
||||
}
|
||||
|
||||
fn makeSliceType(analysis_ctx: *AnalysisContext, child_type: *ast.Node) ?*ast.Node {
|
||||
fn makeSliceType(arena: *std.heap.ArenaAllocator, child_type: *ast.Node) ?*ast.Node {
|
||||
// TODO: Better values for fields, better way to do this?
|
||||
var slice_type = analysis_ctx.arena.allocator.create(ast.Node.PrefixOp) catch return null;
|
||||
var slice_type = arena.allocator.create(ast.Node.PrefixOp) catch return null;
|
||||
slice_type.* = .{
|
||||
.op_token = child_type.firstToken(),
|
||||
.op = .{
|
||||
@ -315,26 +314,27 @@ fn makeSliceType(analysis_ctx: *AnalysisContext, child_type: *ast.Node) ?*ast.No
|
||||
|
||||
/// Resolves bracket access type (both slicing and array access)
|
||||
fn resolveBracketAccessType(
|
||||
analysis_ctx: *AnalysisContext,
|
||||
lhs: *ast.Node,
|
||||
store: *DocumentStore,
|
||||
lhs: NodeWithHandle,
|
||||
arena: *std.heap.ArenaAllocator,
|
||||
rhs: enum { Single, Range },
|
||||
) ?*ast.Node {
|
||||
if (lhs.cast(ast.Node.PrefixOp)) |pop| {
|
||||
) !?NodeWithHandle {
|
||||
if (lhs.node.cast(ast.Node.PrefixOp)) |pop| {
|
||||
switch (pop.op) {
|
||||
.SliceType => {
|
||||
if (rhs == .Single) return resolveTypeOfNode(analysis_ctx, pop.rhs);
|
||||
if (rhs == .Single) return resolveTypeOfNode(store, .{ .node = pop.rhs, .handle = lhs.handle });
|
||||
return lhs;
|
||||
},
|
||||
.ArrayType => {
|
||||
if (rhs == .Single) return resolveTypeOfNode(analysis_ctx, pop.rhs);
|
||||
return makeSliceType(analysis_ctx, pop.rhs);
|
||||
if (rhs == .Single) return resolveTypeOfNode(store, .{ .node = pop.rhs, .handle = lhs.handle });
|
||||
return NodeWithHandle{ .node = makeSliceType(arena, pop.rhs), .handle = lhs.handle };
|
||||
},
|
||||
.PtrType => {
|
||||
if (pop.rhs.cast(std.zig.ast.Node.PrefixOp)) |child_pop| {
|
||||
switch (child_pop.op) {
|
||||
.ArrayType => {
|
||||
if (rhs == .Single) {
|
||||
return resolveTypeOfNode(analysis_ctx, child_pop.rhs);
|
||||
return resolveTypeOfNode(store, .{ .node = child_pop.rhs, .handle = lhs.handle });
|
||||
}
|
||||
return lhs;
|
||||
},
|
||||
@ -349,12 +349,13 @@ fn resolveBracketAccessType(
|
||||
}
|
||||
|
||||
/// Called to remove one level of pointerness before a field access
|
||||
fn resolveFieldAccessLhsType(analysis_ctx: *AnalysisContext, lhs: *ast.Node) *ast.Node {
|
||||
return resolveDerefType(analysis_ctx, lhs) orelse lhs;
|
||||
fn resolveFieldAccessLhsType(store: *DocumentStore, lhs: NodeWithHandle) !NodeWithHandle {
|
||||
return resolveDerefType(store, lhs) orelse lhs;
|
||||
}
|
||||
|
||||
// @TODO try errors
|
||||
/// Resolves the type of a node
|
||||
pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.Node {
|
||||
pub fn resolveTypeOfNode(store: *DocumentStore, node_handle: NodeWithHandle) !?NodeWithHandle {
|
||||
switch (node.id) {
|
||||
.VarDecl => {
|
||||
const vari = node.cast(ast.Node.VarDecl).?;
|
||||
@ -609,52 +610,50 @@ pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !
|
||||
}
|
||||
}
|
||||
|
||||
fn checkForContainerAndResolveFieldAccessLhsType(analysis_ctx: *AnalysisContext, node: *ast.Node) *ast.Node {
|
||||
const current_node = resolveFieldAccessLhsType(analysis_ctx, node);
|
||||
|
||||
if (current_node.id == .ContainerDecl or current_node.id == .Root) {
|
||||
// TODO: Handle errors
|
||||
analysis_ctx.onContainer(current_node) catch {};
|
||||
}
|
||||
|
||||
return current_node;
|
||||
}
|
||||
pub const NodeWithHandle = struct {
|
||||
node: *ast.Node,
|
||||
handle: *DocumentStore.Handle,
|
||||
};
|
||||
|
||||
pub fn getFieldAccessTypeNode(
|
||||
analysis_ctx: *AnalysisContext,
|
||||
store: *DocumentStore,
|
||||
arena: *std.heap.ArenaAllocator,
|
||||
handle: *DocumentStore.Handle,
|
||||
tokenizer: *std.zig.Tokenizer,
|
||||
) ?*ast.Node {
|
||||
var current_node = analysis_ctx.in_container;
|
||||
) !?NodeWithHandle {
|
||||
var current_node = NodeWithHandle{
|
||||
.node = &handle.tree.root_node.base,
|
||||
.handle = handle,
|
||||
};
|
||||
|
||||
while (true) {
|
||||
const tok = tokenizer.next();
|
||||
switch (tok.id) {
|
||||
.Eof => return resolveFieldAccessLhsType(analysis_ctx, current_node),
|
||||
.Eof => return try resolveFieldAccessLhsType(store, current_node),
|
||||
.Identifier => {
|
||||
if (getChildOfSlice(analysis_ctx.tree(), analysis_ctx.scope_nodes, tokenizer.buffer[tok.loc.start..tok.loc.end])) |child| {
|
||||
if (resolveTypeOfNode(analysis_ctx, child)) |child_type| {
|
||||
current_node = child_type;
|
||||
} else return null;
|
||||
if (try lookupSymbolGlobal(store, current_node.handle, tokenizer.buffer[tok.loc.start..tok.loc.end], tok.loc.start)) |child| {
|
||||
current_node = (try child.resolveType(store, arena)) orelse return null;
|
||||
} else return null;
|
||||
},
|
||||
.Period => {
|
||||
const after_period = tokenizer.next();
|
||||
switch (after_period.id) {
|
||||
.Eof => return resolveFieldAccessLhsType(analysis_ctx, current_node),
|
||||
.Eof => return try resolveFieldAccessLhsType(store, current_node),
|
||||
.Identifier => {
|
||||
if (after_period.loc.end == tokenizer.buffer.len) return resolveFieldAccessLhsType(analysis_ctx, current_node);
|
||||
if (after_period.loc.end == tokenizer.buffer.len) return try resolveFieldAccessLhsType(store, current_node);
|
||||
|
||||
current_node = checkForContainerAndResolveFieldAccessLhsType(analysis_ctx, current_node);
|
||||
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| {
|
||||
current_node = child_type;
|
||||
} else return null;
|
||||
current_node = resolveFieldAccessLhsType(store, current_node);
|
||||
if (current_node.node.id != .ContainerDecl and current_node.node.id != .Root) {
|
||||
// @TODO Is this ok?
|
||||
return null;
|
||||
}
|
||||
|
||||
if (lookupSymbolContainer(store, current_node, tokenizer.buffer[after_period.loc.start..after_period.loc.end], true)) |child| {
|
||||
current_node = (try child.resolveType(store, arena)) orelse return null;
|
||||
} else return null;
|
||||
},
|
||||
.QuestionMark => {
|
||||
if (resolveUnwrapOptionalType(analysis_ctx, current_node)) |child_type| {
|
||||
current_node = child_type;
|
||||
} else return null;
|
||||
current_node = (try resolveUnwrapOptionalType(store, current_node)) orelse return null;
|
||||
},
|
||||
else => {
|
||||
std.debug.warn("Unrecognized token {} after period.\n", .{after_period.id});
|
||||
@ -663,15 +662,13 @@ pub fn getFieldAccessTypeNode(
|
||||
}
|
||||
},
|
||||
.PeriodAsterisk => {
|
||||
if (resolveDerefType(analysis_ctx, current_node)) |child_type| {
|
||||
current_node = child_type;
|
||||
} else return null;
|
||||
current_node = (try resolveDerefType(store, current_node)) orelse return null;
|
||||
},
|
||||
.LParen => {
|
||||
switch (current_node.id) {
|
||||
switch (current_node.node.id) {
|
||||
.FnProto => {
|
||||
const func = current_node.cast(ast.Node.FnProto).?;
|
||||
if (resolveReturnType(analysis_ctx, func)) |ret| {
|
||||
const func = current_node.node.cast(ast.Node.FnProto).?;
|
||||
if (try resolveReturnType(store, func, current_node.handle)) |ret| {
|
||||
current_node = ret;
|
||||
// Skip to the right paren
|
||||
var paren_count: usize = 1;
|
||||
@ -704,26 +701,16 @@ pub fn getFieldAccessTypeNode(
|
||||
}
|
||||
} else return null;
|
||||
|
||||
if (resolveBracketAccessType(
|
||||
analysis_ctx,
|
||||
current_node,
|
||||
if (is_range) .Range else .Single,
|
||||
)) |child_type| {
|
||||
current_node = child_type;
|
||||
} else return null;
|
||||
current_node = (try resolveBracketAccessType(store, current_node, arena, if (is_range) .Range else .Single)) orelse return null;
|
||||
},
|
||||
else => {
|
||||
std.debug.warn("Unimplemented token: {}\n", .{tok.id});
|
||||
return null;
|
||||
},
|
||||
}
|
||||
|
||||
if (current_node.id == .ContainerDecl or current_node.id == .Root) {
|
||||
analysis_ctx.onContainer(current_node) catch return null;
|
||||
}
|
||||
}
|
||||
|
||||
return resolveFieldAccessLhsType(analysis_ctx, current_node);
|
||||
return try resolveFieldAccessLhsType(store, current_node);
|
||||
}
|
||||
|
||||
pub fn isNodePublic(tree: *ast.Tree, node: *ast.Node) bool {
|
||||
@ -1069,8 +1056,6 @@ pub fn getDocumentSymbols(allocator: *std.mem.Allocator, tree: *ast.Tree) ![]typ
|
||||
return symbols.items;
|
||||
}
|
||||
|
||||
const DocumentStore = @import("document_store.zig");
|
||||
|
||||
pub const Declaration = union(enum) {
|
||||
ast_node: *ast.Node,
|
||||
param_decl: *ast.Node.FnProto.ParamDecl,
|
||||
@ -1105,6 +1090,35 @@ pub const DeclWithHandle = struct {
|
||||
.switch_payload => |sp| tree.tokenLocation(0, sp.node.value_symbol.firstToken()),
|
||||
};
|
||||
}
|
||||
|
||||
fn resolveType(self: DeclWithHandle, store: *DocumentStore, arena: *std.heap.ArenaAllocator) !?NodeWithHandle {
|
||||
// resolveTypeOfNode(store: *DocumentStore, node_handle: NodeWithHandle)
|
||||
return switch (self.decl) {
|
||||
.ast_node => |node| try resolveTypeOfNode(store, .{ .node = node, .handle = self.handle }),
|
||||
.param_decl => |param_decl| switch (param_decl.param_type) {
|
||||
.type_expr => |type_node| try resolveTypeOfNode(store, .{ .node = node, .handle = self.handle }),
|
||||
else => null,
|
||||
},
|
||||
.pointer_payload => |pay| try resolveUnwrapOptionalType(
|
||||
store,
|
||||
try resolveTypeOfNode(store, .{
|
||||
.node = pay.condition,
|
||||
.handle = self.handle,
|
||||
}) orelse return null,
|
||||
),
|
||||
.array_payload => |pay| try resolveBracketAccessType(
|
||||
store,
|
||||
.{
|
||||
.node = pay.array_expr,
|
||||
.handle = self.handle,
|
||||
},
|
||||
arena,
|
||||
.Single,
|
||||
),
|
||||
// TODO Resolve switch payload types
|
||||
.switch_payload => |pay| return null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub fn iterateSymbolsGlobal(
|
||||
@ -1157,7 +1171,9 @@ pub fn lookupSymbolGlobal(store: *DocumentStore, handle: *DocumentStore.Handle,
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn lookupSymbolContainer(store: *DocumentScope, handle: *DocumentStore.Handle, container: *ast.Node, symbol: []const u8, accept_fields: bool) !?DeclWithHandle {
|
||||
pub fn lookupSymbolContainer(store: *DocumentScope, container_handle: NodeWithHandle, symbol: []const u8, accept_fields: bool) !?DeclWithHandle {
|
||||
const container = container_handle.node;
|
||||
const handle = container_handle.handle;
|
||||
std.debug.assert(container.id == .ContainerDecl or container.id == .Root);
|
||||
// Find the container scope.
|
||||
var maybe_container_scope: ?*Scope = null;
|
||||
|
@ -78,6 +78,7 @@ handles: std.StringHashMap(*Handle),
|
||||
zig_exe_path: ?[]const u8,
|
||||
build_files: std.ArrayListUnmanaged(*BuildFile),
|
||||
build_runner_path: []const u8,
|
||||
std_uri: ?[]const u8,
|
||||
|
||||
error_completions: TagStore,
|
||||
enum_completions: TagStore,
|
||||
@ -87,12 +88,14 @@ pub fn init(
|
||||
allocator: *std.mem.Allocator,
|
||||
zig_exe_path: ?[]const u8,
|
||||
build_runner_path: []const u8,
|
||||
zig_lib_path: ?[]const u8,
|
||||
) !void {
|
||||
self.allocator = allocator;
|
||||
self.handles = std.StringHashMap(*Handle).init(allocator);
|
||||
self.zig_exe_path = zig_exe_path;
|
||||
self.build_files = .{};
|
||||
self.build_runner_path = build_runner_path;
|
||||
self.std_uri = try stdUriFromLibPath(allocator, zig_lib_path);
|
||||
self.error_completions = TagStore.init(allocator);
|
||||
self.enum_completions = TagStore.init(allocator);
|
||||
}
|
||||
@ -393,7 +396,7 @@ fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const
|
||||
|
||||
const std_uri = try stdUriFromLibPath(&arena.allocator, zig_lib_path);
|
||||
for (import_strs.items) |str| {
|
||||
const uri = (try uriFromImportStr(self, &arena.allocator, handle.*, str, std_uri)) orelse continue;
|
||||
const uri = (try self.uriFromImportStr(&arena.allocator, handle.*, str)) orelse continue;
|
||||
|
||||
var idx: usize = 0;
|
||||
exists_loop: while (idx < still_exist.len) : (idx += 1) {
|
||||
@ -496,14 +499,13 @@ pub fn applyChanges(
|
||||
}
|
||||
|
||||
pub fn uriFromImportStr(
|
||||
store: *DocumentStore,
|
||||
self: *DocumentStore,
|
||||
allocator: *std.mem.Allocator,
|
||||
handle: Handle,
|
||||
import_str: []const u8,
|
||||
std_uri: ?[]const u8,
|
||||
) !?[]const u8 {
|
||||
if (std.mem.eql(u8, import_str, "std")) {
|
||||
if (std_uri) |uri| return try std.mem.dupe(allocator, u8, uri) else {
|
||||
if (self.std_uri) |uri| return try std.mem.dupe(allocator, u8, uri) else {
|
||||
std.debug.warn("Cannot resolve std library import, path is null.\n", .{});
|
||||
return null;
|
||||
}
|
||||
@ -534,93 +536,72 @@ pub fn uriFromImportStr(
|
||||
}
|
||||
}
|
||||
|
||||
pub const AnalysisContext = struct {
|
||||
store: *DocumentStore,
|
||||
handle: *Handle,
|
||||
// This arena is used for temporary allocations while analyzing,
|
||||
// not for the tree allocations.
|
||||
arena: *std.heap.ArenaAllocator,
|
||||
std_uri: ?[]const u8,
|
||||
error_completions: *TagStore,
|
||||
enum_completions: *TagStore,
|
||||
pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const u8) !?*Handle {
|
||||
const allocator = self.allocator;
|
||||
const final_uri = (try self.uriFromImportStr(
|
||||
self.allocator,
|
||||
handle.*,
|
||||
import_str,
|
||||
)) orelse return null;
|
||||
|
||||
pub fn tree(self: AnalysisContext) *std.zig.ast.Tree {
|
||||
return self.handle.tree;
|
||||
std.debug.warn("Import final URI: {}\n", .{final_uri});
|
||||
var consumed_final_uri = false;
|
||||
defer if (!consumed_final_uri) allocator.free(final_uri);
|
||||
|
||||
// Check if we already imported this.
|
||||
for (handle.import_uris.items) |uri| {
|
||||
// If we did, set our new handle and return the parsed tree root node.
|
||||
if (std.mem.eql(u8, uri, final_uri)) {
|
||||
return self.getHandle(final_uri);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node {
|
||||
const allocator = self.store.allocator;
|
||||
const final_uri = (try uriFromImportStr(
|
||||
self.store,
|
||||
self.store.allocator,
|
||||
self.handle.*,
|
||||
import_str,
|
||||
self.std_uri,
|
||||
)) orelse return null;
|
||||
// New import.
|
||||
// Check if the import is already opened by others.
|
||||
if (self.getHandle(final_uri)) |new_handle| {
|
||||
// If it is, append it to our imports, increment the count, set our new handle
|
||||
// and return the parsed tree root node.
|
||||
try handle.import_uris.append(final_uri);
|
||||
consumed_final_uri = true;
|
||||
|
||||
std.debug.warn("Import final URI: {}\n", .{final_uri});
|
||||
var consumed_final_uri = false;
|
||||
defer if (!consumed_final_uri) allocator.free(final_uri);
|
||||
new_handle.count += 1;
|
||||
return new_handle;
|
||||
}
|
||||
|
||||
// Check if we already imported this.
|
||||
for (self.handle.import_uris.items) |uri| {
|
||||
// If we did, set our new handle and return the parsed tree root node.
|
||||
if (std.mem.eql(u8, uri, final_uri)) {
|
||||
self.handle = self.store.getHandle(final_uri) orelse return null;
|
||||
return &self.tree().root_node.base;
|
||||
}
|
||||
}
|
||||
// New document, read the file then call into openDocument.
|
||||
const file_path = try URI.parse(allocator, final_uri);
|
||||
defer allocator.free(file_path);
|
||||
|
||||
// New import.
|
||||
// Check if the import is already opened by others.
|
||||
if (self.store.getHandle(final_uri)) |new_handle| {
|
||||
// If it is, append it to our imports, increment the count, set our new handle
|
||||
// and return the parsed tree root node.
|
||||
try self.handle.import_uris.append(final_uri);
|
||||
consumed_final_uri = true;
|
||||
var file = std.fs.cwd().openFile(file_path, .{}) catch {
|
||||
std.debug.warn("Cannot open import file {}\n", .{file_path});
|
||||
return null;
|
||||
};
|
||||
|
||||
new_handle.count += 1;
|
||||
self.handle = new_handle;
|
||||
return &self.tree().root_node.base;
|
||||
}
|
||||
defer file.close();
|
||||
const size = std.math.cast(usize, try file.getEndPos()) catch std.math.maxInt(usize);
|
||||
|
||||
// New document, read the file then call into openDocument.
|
||||
const file_path = try URI.parse(allocator, final_uri);
|
||||
defer allocator.free(file_path);
|
||||
{
|
||||
const file_contents = try allocator.alloc(u8, size);
|
||||
errdefer allocator.free(file_contents);
|
||||
|
||||
var file = std.fs.cwd().openFile(file_path, .{}) catch {
|
||||
std.debug.warn("Cannot open import file {}\n", .{file_path});
|
||||
file.inStream().readNoEof(file_contents) catch {
|
||||
std.debug.warn("Could not read from file {}\n", .{file_path});
|
||||
return null;
|
||||
};
|
||||
|
||||
defer file.close();
|
||||
const size = std.math.cast(usize, try file.getEndPos()) catch std.math.maxInt(usize);
|
||||
// Add to import table of current handle.
|
||||
try handle.import_uris.append(final_uri);
|
||||
consumed_final_uri = true;
|
||||
|
||||
{
|
||||
const file_contents = try allocator.alloc(u8, size);
|
||||
errdefer allocator.free(file_contents);
|
||||
|
||||
file.inStream().readNoEof(file_contents) catch {
|
||||
std.debug.warn("Could not read from file {}\n", .{file_path});
|
||||
return null;
|
||||
};
|
||||
|
||||
// Add to import table of current handle.
|
||||
try self.handle.import_uris.append(final_uri);
|
||||
consumed_final_uri = true;
|
||||
|
||||
// Swap handles.
|
||||
// This takes ownership of the passed uri and text.
|
||||
const duped_final_uri = try std.mem.dupe(allocator, u8, final_uri);
|
||||
errdefer allocator.free(duped_final_uri);
|
||||
self.handle = try newDocument(self.store, duped_final_uri, file_contents);
|
||||
}
|
||||
|
||||
return &self.tree().root_node.base;
|
||||
// Swap handles.
|
||||
// This takes ownership of the passed uri and text.
|
||||
const duped_final_uri = try std.mem.dupe(allocator, u8, final_uri);
|
||||
errdefer allocator.free(duped_final_uri);
|
||||
return try self.newDocument(duped_final_uri, file_contents);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn stdUriFromLibPath(allocator: *std.mem.Allocator, zig_lib_path: ?[]const u8) !?[]const u8 {
|
||||
fn stdUriFromLibPath(allocator: *std.mem.Allocator, zig_lib_path: ?[]const u8) !?[]const u8 {
|
||||
if (zig_lib_path) |zpath| {
|
||||
const std_path = std.fs.path.resolve(allocator, &[_][]const u8{
|
||||
zpath, "./std/std.zig",
|
||||
@ -637,23 +618,6 @@ pub fn stdUriFromLibPath(allocator: *std.mem.Allocator, zig_lib_path: ?[]const u
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn analysisContext(
|
||||
self: *DocumentStore,
|
||||
handle: *Handle,
|
||||
arena: *std.heap.ArenaAllocator,
|
||||
zig_lib_path: ?[]const u8,
|
||||
) !AnalysisContext {
|
||||
const std_uri = try stdUriFromLibPath(&arena.allocator, zig_lib_path);
|
||||
return AnalysisContext{
|
||||
.store = self,
|
||||
.handle = handle,
|
||||
.arena = arena,
|
||||
.std_uri = std_uri,
|
||||
.error_completions = &self.error_completions,
|
||||
.enum_completions = &self.enum_completions,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *DocumentStore) void {
|
||||
var entry_iterator = self.handles.iterator();
|
||||
while (entry_iterator.next()) |entry| {
|
||||
@ -681,6 +645,10 @@ pub fn deinit(self: *DocumentStore) void {
|
||||
self.allocator.destroy(build_file);
|
||||
}
|
||||
|
||||
if (self.std_uri) |std_uri| {
|
||||
self.allocator.free(std_uri);
|
||||
}
|
||||
|
||||
self.build_files.deinit(self.allocator);
|
||||
self.error_completions.deinit();
|
||||
self.enum_completions.deinit();
|
||||
|
23
src/main.zig
23
src/main.zig
@ -247,9 +247,7 @@ fn resolveVarDeclFnAlias(analysis_ctx: *DocumentStore.AnalysisContext, decl: *st
|
||||
|
||||
fn nodeToCompletion(
|
||||
list: *std.ArrayList(types.CompletionItem),
|
||||
analysis_ctx: *DocumentStore.AnalysisContext,
|
||||
orig_handle: *DocumentStore.Handle,
|
||||
node: *std.zig.ast.Node,
|
||||
node_handle: analysis.NodeWithHandle,
|
||||
config: Config,
|
||||
) error{OutOfMemory}!void {
|
||||
const doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree(), node)) |doc_comments|
|
||||
@ -519,7 +517,6 @@ fn gotoDefinitionString(id: types.RequestId, pos_index: usize, handle: *Document
|
||||
&arena.allocator,
|
||||
handle.*,
|
||||
import_str,
|
||||
try DocumentStore.stdUriFromLibPath(&arena.allocator, config.zig_lib_path),
|
||||
)) orelse return try respondGeneric(id, null_result_response);
|
||||
|
||||
try send(types.Response{
|
||||
@ -545,9 +542,7 @@ const DeclToCompletionContext = struct {
|
||||
fn decltoCompletion(context: DeclToCompletionContext, decl_handle: analysis.DeclWithHandle) !void {
|
||||
switch (decl_handle.decl.*) {
|
||||
.ast_node => |node| {
|
||||
// @TODO Remove analysis context
|
||||
var analysis_ctx = try document_store.analysisContext(decl_handle.handle, context.arena, context.config.zig_lib_path);
|
||||
try nodeToCompletion(context.completions, &analysis_ctx, decl_handle.handle, node, context.config.*);
|
||||
try nodeToCompletion(context.completions, .{ .node = node, .handle = decl_handle.handle }, context.config.*);
|
||||
},
|
||||
else => {},
|
||||
// @TODO The rest
|
||||
@ -568,16 +563,6 @@ fn completeGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.
|
||||
};
|
||||
try analysis.iterateSymbolsGlobal(&document_store, handle, pos_index, decltoCompletion, context);
|
||||
|
||||
// for (analysis_ctx.scope_nodes) |decl_ptr| {
|
||||
// var decl = decl_ptr.*;
|
||||
// if (decl.id == .Use) {
|
||||
// std.debug.warn("Found use!", .{});
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// try nodeToCompletion(&completions, &analysis_ctx, handle, decl_ptr, config);
|
||||
// }
|
||||
|
||||
try send(types.Response{
|
||||
.id = id,
|
||||
.result = .{
|
||||
@ -1115,13 +1100,13 @@ pub fn main() anyerror!void {
|
||||
}
|
||||
|
||||
if (config.build_runner_path) |build_runner_path| {
|
||||
try document_store.init(allocator, zig_exe_path, try std.mem.dupe(allocator, u8, build_runner_path));
|
||||
try document_store.init(allocator, zig_exe_path, try std.mem.dupe(allocator, u8, build_runner_path), config.zig_lib_path);
|
||||
} else {
|
||||
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
||||
const exe_dir_path = try std.fs.selfExeDirPath(&exe_dir_bytes);
|
||||
|
||||
const build_runner_path = try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" });
|
||||
try document_store.init(allocator, zig_exe_path, build_runner_path);
|
||||
try document_store.init(allocator, zig_exe_path, build_runner_path, config.zig_lib_path);
|
||||
}
|
||||
|
||||
defer document_store.deinit();
|
||||
|
Loading…
Reference in New Issue
Block a user