2020-05-14 00:10:41 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const types = @import("types.zig");
|
2020-05-14 02:54:05 +01:00
|
|
|
const URI = @import("uri.zig");
|
2020-05-14 12:51:07 +01:00
|
|
|
const analysis = @import("analysis.zig");
|
2020-05-14 00:10:41 +01:00
|
|
|
|
|
|
|
const DocumentStore = @This();
|
|
|
|
|
|
|
|
pub const Handle = struct {
|
|
|
|
document: types.TextDocument,
|
|
|
|
count: usize,
|
2020-05-14 02:54:05 +01:00
|
|
|
import_uris: std.ArrayList([]const u8),
|
2020-05-24 17:00:21 +01:00
|
|
|
tree: *std.zig.ast.Tree,
|
2020-05-14 00:10:41 +01:00
|
|
|
|
|
|
|
pub fn uri(handle: Handle) []const u8 {
|
|
|
|
return handle.document.uri;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
allocator: *std.mem.Allocator,
|
2020-05-18 08:28:36 +01:00
|
|
|
handles: std.StringHashMap(*Handle),
|
2020-05-14 00:10:41 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
pub fn init(self: *DocumentStore, allocator: *std.mem.Allocator) !void {
|
2020-05-14 00:10:41 +01:00
|
|
|
self.allocator = allocator;
|
2020-05-18 08:28:36 +01:00
|
|
|
self.handles = std.StringHashMap(*Handle).init(allocator);
|
2020-05-14 00:10:41 +01:00
|
|
|
errdefer self.handles.deinit();
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:22:15 +01:00
|
|
|
/// This function asserts the document is not open yet and takes ownership
|
2020-05-14 09:51:49 +01:00
|
|
|
/// of the uri and text passed in.
|
|
|
|
fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) !*Handle {
|
|
|
|
std.debug.warn("Opened document: {}\n", .{uri});
|
|
|
|
|
2020-05-18 09:37:15 +01:00
|
|
|
var handle = try self.allocator.create(Handle);
|
|
|
|
errdefer self.allocator.destroy(handle);
|
|
|
|
|
|
|
|
handle.* = Handle{
|
2020-05-14 09:51:49 +01:00
|
|
|
.count = 1,
|
|
|
|
.import_uris = std.ArrayList([]const u8).init(self.allocator),
|
|
|
|
.document = .{
|
|
|
|
.uri = uri,
|
|
|
|
.text = text,
|
|
|
|
.mem = text,
|
|
|
|
},
|
2020-05-24 17:00:21 +01:00
|
|
|
.tree = try std.zig.parse(self.allocator, text),
|
2020-05-14 09:51:49 +01:00
|
|
|
};
|
2020-05-24 17:00:21 +01:00
|
|
|
|
|
|
|
try self.handles.putNoClobber(uri, handle);
|
|
|
|
return handle;
|
2020-05-14 09:51:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
pub fn openDocument(self: *DocumentStore, uri: []const u8, text: []const u8) !*Handle {
|
|
|
|
if (self.handles.get(uri)) |entry| {
|
|
|
|
std.debug.warn("Document already open: {}, incrementing count\n", .{uri});
|
|
|
|
entry.value.count += 1;
|
|
|
|
std.debug.warn("New count: {}\n", .{entry.value.count});
|
2020-05-18 08:28:36 +01:00
|
|
|
return entry.value;
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const duped_text = try std.mem.dupe(self.allocator, u8, text);
|
|
|
|
errdefer self.allocator.free(duped_text);
|
|
|
|
const duped_uri = try std.mem.dupe(self.allocator, u8, uri);
|
|
|
|
errdefer self.allocator.free(duped_uri);
|
|
|
|
|
2020-05-17 15:39:04 +01:00
|
|
|
return try self.newDocument(duped_uri, duped_text);
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn decrementCount(self: *DocumentStore, uri: []const u8) void {
|
|
|
|
if (self.handles.get(uri)) |entry| {
|
|
|
|
entry.value.count -= 1;
|
2020-05-14 09:40:17 +01:00
|
|
|
if (entry.value.count > 0)
|
|
|
|
return;
|
2020-05-14 00:10:41 +01:00
|
|
|
|
2020-05-14 09:40:17 +01:00
|
|
|
std.debug.warn("Freeing document: {}\n", .{uri});
|
2020-05-24 17:00:21 +01:00
|
|
|
|
|
|
|
entry.value.tree.deinit();
|
2020-05-14 00:10:41 +01:00
|
|
|
self.allocator.free(entry.value.document.mem);
|
|
|
|
|
2020-05-14 02:54:05 +01:00
|
|
|
for (entry.value.import_uris.items) |import_uri| {
|
2020-05-14 00:10:41 +01:00
|
|
|
self.decrementCount(import_uri);
|
|
|
|
self.allocator.free(import_uri);
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:54:05 +01:00
|
|
|
entry.value.import_uris.deinit();
|
2020-05-14 00:10:41 +01:00
|
|
|
|
|
|
|
const uri_key = entry.key;
|
|
|
|
self.handles.removeAssertDiscard(uri);
|
|
|
|
self.allocator.free(uri_key);
|
2020-05-18 08:28:36 +01:00
|
|
|
self.allocator.destroy(entry.value);
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closeDocument(self: *DocumentStore, uri: []const u8) void {
|
|
|
|
self.decrementCount(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getHandle(self: *DocumentStore, uri: []const u8) ?*Handle {
|
|
|
|
if (self.handles.get(uri)) |entry| {
|
2020-05-18 08:28:36 +01:00
|
|
|
return entry.value;
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the document text is now sane, move it to sane_text if so.
|
2020-05-24 17:00:21 +01:00
|
|
|
fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const u8) !void {
|
2020-05-14 15:22:15 +01:00
|
|
|
std.debug.warn("New text for document {}\n", .{handle.uri()});
|
2020-05-24 17:00:21 +01:00
|
|
|
handle.tree.deinit();
|
|
|
|
handle.tree = try std.zig.parse(self.allocator, handle.document.text);
|
|
|
|
|
2020-05-14 12:51:07 +01:00
|
|
|
// TODO: Better algorithm or data structure?
|
|
|
|
// Removing the imports is costly since they live in an array list
|
|
|
|
// Perhaps we should use an AutoHashMap([]const u8, {}) ?
|
|
|
|
|
|
|
|
// Try to detect removed imports and decrement their counts.
|
|
|
|
if (handle.import_uris.items.len == 0) return;
|
|
|
|
|
2020-05-18 13:14:16 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
|
|
|
defer arena.deinit();
|
2020-05-14 12:51:07 +01:00
|
|
|
|
2020-05-18 13:14:16 +01:00
|
|
|
var import_strs = std.ArrayList([]const u8).init(&arena.allocator);
|
2020-05-24 17:00:21 +01:00
|
|
|
try analysis.collectImports(&import_strs, handle.tree);
|
2020-05-14 12:51:07 +01:00
|
|
|
|
2020-05-18 13:14:16 +01:00
|
|
|
const still_exist = try arena.allocator.alloc(bool, handle.import_uris.items.len);
|
2020-05-14 12:51:07 +01:00
|
|
|
for (still_exist) |*ex| {
|
|
|
|
ex.* = false;
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:33:15 +01:00
|
|
|
const std_uri = try stdUriFromLibPath(&arena.allocator, zig_lib_path);
|
2020-05-18 13:14:16 +01:00
|
|
|
for (import_strs.items) |str| {
|
2020-05-19 20:09:00 +01:00
|
|
|
const uri = (try uriFromImportStr(self, &arena.allocator, handle.*, str, std_uri)) orelse continue;
|
2020-05-14 12:51:07 +01:00
|
|
|
|
|
|
|
var idx: usize = 0;
|
|
|
|
exists_loop: while (idx < still_exist.len) : (idx += 1) {
|
|
|
|
if (still_exist[idx]) continue;
|
|
|
|
|
|
|
|
if (std.mem.eql(u8, handle.import_uris.items[idx], uri)) {
|
|
|
|
still_exist[idx] = true;
|
|
|
|
break :exists_loop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through still_exist, remove the items that are false and decrement their handle counts.
|
|
|
|
var offset: usize = 0;
|
|
|
|
var idx: usize = 0;
|
|
|
|
while (idx < still_exist.len) : (idx += 1) {
|
|
|
|
if (still_exist[idx]) continue;
|
|
|
|
|
|
|
|
std.debug.warn("Import removed: {}\n", .{handle.import_uris.items[idx - offset]});
|
|
|
|
const uri = handle.import_uris.orderedRemove(idx - offset);
|
|
|
|
offset += 1;
|
|
|
|
|
2020-05-24 17:00:21 +01:00
|
|
|
self.decrementCount(uri);
|
2020-05-14 12:51:07 +01:00
|
|
|
self.allocator.free(uri);
|
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
pub fn applyChanges(
|
|
|
|
self: *DocumentStore,
|
|
|
|
handle: *Handle,
|
|
|
|
content_changes: std.json.Array,
|
|
|
|
zig_lib_path: ?[]const u8,
|
|
|
|
) !void {
|
2020-05-17 15:39:04 +01:00
|
|
|
const document = &handle.document;
|
2020-05-14 00:10:41 +01:00
|
|
|
|
|
|
|
for (content_changes.items) |change| {
|
|
|
|
if (change.Object.getValue("range")) |range| {
|
|
|
|
const start_pos = types.Position{
|
|
|
|
.line = range.Object.getValue("start").?.Object.getValue("line").?.Integer,
|
2020-05-17 07:47:48 +01:00
|
|
|
.character = range.Object.getValue("start").?.Object.getValue("character").?.Integer,
|
2020-05-14 00:10:41 +01:00
|
|
|
};
|
|
|
|
const end_pos = types.Position{
|
|
|
|
.line = range.Object.getValue("end").?.Object.getValue("line").?.Integer,
|
2020-05-17 07:47:48 +01:00
|
|
|
.character = range.Object.getValue("end").?.Object.getValue("character").?.Integer,
|
2020-05-14 00:10:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const change_text = change.Object.getValue("text").?.String;
|
|
|
|
const start_index = try document.positionToIndex(start_pos);
|
|
|
|
const end_index = try document.positionToIndex(end_pos);
|
|
|
|
|
|
|
|
const old_len = document.text.len;
|
|
|
|
const new_len = old_len + change_text.len;
|
|
|
|
if (new_len > document.mem.len) {
|
|
|
|
// We need to reallocate memory.
|
|
|
|
// We reallocate twice the current filesize or the new length, if it's more than that
|
|
|
|
// so that we can reduce the amount of realloc calls.
|
|
|
|
// We can tune this to find a better size if needed.
|
|
|
|
const realloc_len = std.math.max(2 * old_len, new_len);
|
|
|
|
document.mem = try self.allocator.realloc(document.mem, realloc_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first part of the string, [0 .. start_index] need not be changed.
|
|
|
|
// We then copy the last part of the string, [end_index ..] to its
|
|
|
|
// new position, [start_index + change_len .. ]
|
2020-05-17 07:47:48 +01:00
|
|
|
std.mem.copy(u8, document.mem[start_index + change_text.len ..][0 .. old_len - end_index], document.mem[end_index..old_len]);
|
2020-05-14 00:10:41 +01:00
|
|
|
// Finally, we copy the changes over.
|
2020-05-17 07:47:48 +01:00
|
|
|
std.mem.copy(u8, document.mem[start_index..][0..change_text.len], change_text);
|
2020-05-14 00:10:41 +01:00
|
|
|
|
|
|
|
// Reset the text substring.
|
2020-05-17 07:47:48 +01:00
|
|
|
document.text = document.mem[0..new_len];
|
2020-05-14 00:10:41 +01:00
|
|
|
} else {
|
|
|
|
const change_text = change.Object.getValue("text").?.String;
|
|
|
|
const old_len = document.text.len;
|
|
|
|
|
|
|
|
if (change_text.len > document.mem.len) {
|
|
|
|
// Like above.
|
|
|
|
const realloc_len = std.math.max(2 * old_len, change_text.len);
|
|
|
|
document.mem = try self.allocator.realloc(document.mem, realloc_len);
|
|
|
|
}
|
|
|
|
|
2020-05-17 07:47:48 +01:00
|
|
|
std.mem.copy(u8, document.mem[0..change_text.len], change_text);
|
|
|
|
document.text = document.mem[0..change_text.len];
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:00:21 +01:00
|
|
|
try self.refreshDocument(handle, zig_lib_path);
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:51:57 +01:00
|
|
|
pub fn uriFromImportStr(
|
2020-05-19 20:09:00 +01:00
|
|
|
store: *DocumentStore,
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
handle: Handle,
|
|
|
|
import_str: []const u8,
|
|
|
|
std_uri: ?[]const u8,
|
|
|
|
) !?[]const u8 {
|
2020-05-22 16:56:57 +01:00
|
|
|
if (std.mem.eql(u8, import_str, "std")) {
|
|
|
|
if (std_uri) |uri| return try std.mem.dupe(allocator, u8, uri) else {
|
2020-05-14 12:51:07 +01:00
|
|
|
std.debug.warn("Cannot resolve std library import, path is null.\n", .{});
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-22 16:56:57 +01:00
|
|
|
} else if (std.mem.eql(u8, import_str, "builtin")) {
|
|
|
|
return null; // TODO find the correct zig-cache folder
|
|
|
|
} else if (!std.mem.endsWith(u8, import_str, ".zig")) {
|
|
|
|
return null; // TODO find packages based on build.zig
|
|
|
|
} else {
|
2020-05-14 12:51:07 +01:00
|
|
|
// Find relative uri
|
2020-05-18 13:14:16 +01:00
|
|
|
const path = try URI.parse(allocator, handle.uri());
|
|
|
|
defer allocator.free(path);
|
2020-05-14 12:51:07 +01:00
|
|
|
|
|
|
|
const dir_path = std.fs.path.dirname(path) orelse "";
|
2020-05-19 10:02:48 +01:00
|
|
|
const import_path = try std.fs.path.resolve(allocator, &[_][]const u8{
|
|
|
|
dir_path, import_str,
|
2020-05-14 12:51:07 +01:00
|
|
|
});
|
|
|
|
|
2020-05-18 13:14:16 +01:00
|
|
|
defer allocator.free(import_path);
|
2020-05-14 12:51:07 +01:00
|
|
|
|
2020-05-22 16:56:57 +01:00
|
|
|
return try URI.fromPath(allocator, import_path);
|
|
|
|
}
|
2020-05-14 12:51:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:23:20 +01:00
|
|
|
pub const AnalysisContext = struct {
|
2020-05-14 02:54:05 +01:00
|
|
|
store: *DocumentStore,
|
|
|
|
handle: *Handle,
|
2020-05-14 10:23:20 +01:00
|
|
|
// This arena is used for temporary allocations while analyzing,
|
|
|
|
// not for the tree allocations.
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-05-16 19:06:48 +01:00
|
|
|
scope_nodes: []*std.zig.ast.Node,
|
2020-05-23 21:22:50 +01:00
|
|
|
in_container: *std.zig.ast.Node,
|
2020-05-19 20:09:00 +01:00
|
|
|
std_uri: ?[]const u8,
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-05-24 17:00:21 +01:00
|
|
|
pub fn tree(self: AnalysisContext) *std.zig.ast.Tree {
|
|
|
|
return self.handle.tree;
|
|
|
|
}
|
|
|
|
|
2020-05-18 12:26:52 +01:00
|
|
|
fn refreshScopeNodes(self: *AnalysisContext) !void {
|
|
|
|
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&self.arena.allocator);
|
2020-05-24 17:00:21 +01:00
|
|
|
try analysis.addChildrenNodes(&scope_nodes, self.tree(), &self.tree().root_node.base);
|
2020-05-18 12:26:52 +01:00
|
|
|
self.scope_nodes = scope_nodes.items;
|
2020-05-24 17:00:21 +01:00
|
|
|
self.in_container = &self.tree().root_node.base;
|
2020-05-18 12:26:52 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 13:24:18 +01:00
|
|
|
pub fn onContainer(self: *AnalysisContext, container: *std.zig.ast.Node.ContainerDecl) !void {
|
|
|
|
if (self.in_container != &container.base) {
|
|
|
|
self.in_container = &container.base;
|
|
|
|
|
2020-05-24 17:26:46 +01:00
|
|
|
var scope_nodes = std.ArrayList(*std.zig.ast.Node).fromOwnedSlice(&self.arena.allocator, self.scope_nodes);
|
2020-05-24 17:00:21 +01:00
|
|
|
try analysis.addChildrenNodes(&scope_nodes, self.tree(), &container.base);
|
2020-05-24 13:24:18 +01:00
|
|
|
self.scope_nodes = scope_nodes.items;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 10:23:20 +01:00
|
|
|
pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node {
|
2020-05-14 02:54:05 +01:00
|
|
|
const allocator = self.store.allocator;
|
2020-05-19 20:09:00 +01:00
|
|
|
const final_uri = (try uriFromImportStr(
|
|
|
|
self.store,
|
|
|
|
self.store.allocator,
|
|
|
|
self.handle.*,
|
|
|
|
import_str,
|
|
|
|
self.std_uri,
|
|
|
|
)) orelse return null;
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-05-14 03:14:37 +01:00
|
|
|
std.debug.warn("Import final URI: {}\n", .{final_uri});
|
2020-05-14 09:40:17 +01:00
|
|
|
var consumed_final_uri = false;
|
|
|
|
defer if (!consumed_final_uri) allocator.free(final_uri);
|
2020-05-14 03:14:37 +01:00
|
|
|
|
2020-05-14 09:51:49 +01:00
|
|
|
// 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;
|
2020-05-18 12:26:52 +01:00
|
|
|
try self.refreshScopeNodes();
|
2020-05-24 17:00:21 +01:00
|
|
|
return &self.tree().root_node.base;
|
2020-05-14 02:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New import.
|
|
|
|
// Check if the import is already opened by others.
|
|
|
|
if (self.store.getHandle(final_uri)) |new_handle| {
|
|
|
|
// If it is, increment the count, set our new handle and return the parsed tree root node.
|
|
|
|
new_handle.count += 1;
|
|
|
|
self.handle = new_handle;
|
2020-05-18 12:26:52 +01:00
|
|
|
try self.refreshScopeNodes();
|
2020-05-24 17:00:21 +01:00
|
|
|
return &self.tree().root_node.base;
|
2020-05-14 02:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New document, read the file then call into openDocument.
|
|
|
|
const file_path = try URI.parse(allocator, final_uri);
|
|
|
|
defer allocator.free(file_path);
|
|
|
|
|
|
|
|
var file = std.fs.cwd().openFile(file_path, .{}) catch {
|
2020-05-14 09:40:17 +01:00
|
|
|
std.debug.warn("Cannot open import file {}\n", .{file_path});
|
2020-05-14 02:54:05 +01:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
defer file.close();
|
|
|
|
const size = std.math.cast(usize, try file.getEndPos()) catch std.math.maxInt(usize);
|
|
|
|
|
2020-05-14 09:51:49 +01:00
|
|
|
{
|
|
|
|
const file_contents = try allocator.alloc(u8, size);
|
|
|
|
errdefer allocator.free(file_contents);
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-05-14 09:51:49 +01:00
|
|
|
file.inStream().readNoEof(file_contents) catch {
|
|
|
|
std.debug.warn("Could not read from file {}\n", .{file_path});
|
|
|
|
return null;
|
|
|
|
};
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-05-14 09:51:49 +01:00
|
|
|
// Add to import table of current handle.
|
|
|
|
try self.handle.import_uris.append(final_uri);
|
|
|
|
consumed_final_uri = true;
|
|
|
|
|
2020-05-24 17:00:21 +01:00
|
|
|
// Swap handles.
|
2020-05-14 09:51:49 +01:00
|
|
|
// This takes ownership of the passed uri and text.
|
2020-05-17 16:21:02 +01:00
|
|
|
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);
|
2020-05-14 09:51:49 +01:00
|
|
|
}
|
2020-05-14 09:40:17 +01:00
|
|
|
|
2020-05-18 12:26:52 +01:00
|
|
|
try self.refreshScopeNodes();
|
2020-05-24 17:00:21 +01:00
|
|
|
return &self.tree().root_node.base;
|
2020-05-14 02:54:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:00:21 +01:00
|
|
|
pub fn clone(self: *AnalysisContext) AnalysisContext {
|
2020-05-19 10:02:48 +01:00
|
|
|
return AnalysisContext{
|
2020-05-19 05:12:05 +01:00
|
|
|
.store = self.store,
|
|
|
|
.handle = self.handle,
|
|
|
|
.arena = self.arena,
|
|
|
|
.scope_nodes = self.scope_nodes,
|
2020-05-23 21:22:50 +01:00
|
|
|
.in_container = self.in_container,
|
2020-05-19 20:09:00 +01:00
|
|
|
.std_uri = self.std_uri,
|
2020-05-19 05:12:05 +01:00
|
|
|
};
|
|
|
|
}
|
2020-05-14 02:54:05 +01:00
|
|
|
};
|
|
|
|
|
2020-05-22 16:51:57 +01:00
|
|
|
pub fn stdUriFromLibPath(allocator: *std.mem.Allocator, zig_lib_path: ?[]const u8) !?[]const u8 {
|
2020-05-19 20:09:00 +01:00
|
|
|
if (zig_lib_path) |zpath| {
|
|
|
|
const std_path = std.fs.path.resolve(allocator, &[_][]const u8{
|
|
|
|
zpath, "./std/std.zig",
|
|
|
|
}) catch |err| block: {
|
|
|
|
std.debug.warn("Failed to resolve zig std library path, error: {}\n", .{err});
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
defer allocator.free(std_path);
|
|
|
|
// Get the std_path as a URI, so we can just append to it!
|
|
|
|
return try URI.fromPath(allocator, std_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn analysisContext(
|
|
|
|
self: *DocumentStore,
|
|
|
|
handle: *Handle,
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-05-23 21:22:50 +01:00
|
|
|
position: usize,
|
2020-05-19 20:09:00 +01:00
|
|
|
zig_lib_path: ?[]const u8,
|
|
|
|
) !AnalysisContext {
|
2020-05-18 12:26:52 +01:00
|
|
|
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
|
2020-05-24 17:00:21 +01:00
|
|
|
const in_container = try analysis.declsFromIndex(arena, &scope_nodes, handle.tree, position);
|
2020-05-18 12:26:52 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
const std_uri = try stdUriFromLibPath(&arena.allocator, zig_lib_path);
|
2020-05-14 10:23:20 +01:00
|
|
|
return AnalysisContext{
|
2020-05-14 02:54:05 +01:00
|
|
|
.store = self,
|
|
|
|
.handle = handle,
|
2020-05-14 10:23:20 +01:00
|
|
|
.arena = arena,
|
2020-05-18 12:26:52 +01:00
|
|
|
.scope_nodes = scope_nodes.items,
|
2020-05-23 21:22:50 +01:00
|
|
|
.in_container = in_container,
|
2020-05-19 20:09:00 +01:00
|
|
|
.std_uri = std_uri,
|
2020-05-14 02:54:05 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
pub fn deinit(self: *DocumentStore) void {
|
2020-05-14 13:26:10 +01:00
|
|
|
var entry_iterator = self.handles.iterator();
|
|
|
|
while (entry_iterator.next()) |entry| {
|
|
|
|
self.allocator.free(entry.value.document.mem);
|
|
|
|
|
|
|
|
for (entry.value.import_uris.items) |uri| {
|
|
|
|
self.allocator.free(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.value.import_uris.deinit();
|
|
|
|
self.allocator.free(entry.key);
|
2020-05-18 08:28:36 +01:00
|
|
|
self.allocator.destroy(entry.value);
|
2020-05-14 13:26:10 +01:00
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
|
|
|
|
self.handles.deinit();
|
|
|
|
}
|