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();
|
|
|
|
|
2020-05-25 15:24:44 +01:00
|
|
|
const BuildFile = struct {
|
|
|
|
const Pkg = struct {
|
|
|
|
name: []const u8,
|
|
|
|
uri: []const u8,
|
|
|
|
};
|
|
|
|
|
2020-05-25 18:42:58 +01:00
|
|
|
refs: usize,
|
2020-05-25 15:24:44 +01:00
|
|
|
uri: []const u8,
|
|
|
|
packages: std.ArrayListUnmanaged(Pkg),
|
|
|
|
};
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
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-06-10 14:12:00 +01:00
|
|
|
document_scope: analysis.DocumentScope,
|
2020-05-14 00:10:41 +01:00
|
|
|
|
2020-05-25 15:24:44 +01:00
|
|
|
associated_build_file: ?*BuildFile,
|
|
|
|
is_build_file: ?*BuildFile,
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
pub fn uri(handle: Handle) []const u8 {
|
|
|
|
return handle.document.uri;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-28 16:18:48 +01:00
|
|
|
pub const TagStore = struct {
|
2020-05-16 17:04:07 +01:00
|
|
|
values: std.StringHashMap(void),
|
2020-05-28 16:18:48 +01:00
|
|
|
completions: std.ArrayListUnmanaged(types.CompletionItem),
|
2020-05-16 17:04:07 +01:00
|
|
|
|
2020-05-28 16:18:48 +01:00
|
|
|
pub fn init(allocator: *std.mem.Allocator) TagStore {
|
2020-05-16 17:04:07 +01:00
|
|
|
return .{
|
|
|
|
.values = std.StringHashMap(void).init(allocator),
|
2020-05-28 16:18:48 +01:00
|
|
|
.completions = .{},
|
2020-05-16 17:04:07 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-28 16:18:48 +01:00
|
|
|
pub fn deinit(self: *TagStore) void {
|
2020-05-16 17:04:07 +01:00
|
|
|
const alloc = self.values.allocator;
|
|
|
|
for (self.completions.items) |item| {
|
|
|
|
alloc.free(item.label);
|
|
|
|
if (item.documentation) |some| alloc.free(some.value);
|
|
|
|
}
|
|
|
|
self.values.deinit();
|
2020-05-28 16:18:48 +01:00
|
|
|
self.completions.deinit(self.values.allocator);
|
2020-05-16 17:04:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 16:18:48 +01:00
|
|
|
pub fn add(self: *TagStore, tree: *std.zig.ast.Tree, tag: *std.zig.ast.Node) !void {
|
|
|
|
const name = analysis.nodeToString(tree, tag).?;
|
2020-05-16 17:04:07 +01:00
|
|
|
if (self.values.contains(name)) return;
|
|
|
|
const alloc = self.values.allocator;
|
|
|
|
const item = types.CompletionItem{
|
|
|
|
.label = try std.mem.dupe(alloc, u8, name),
|
|
|
|
.kind = .Constant,
|
2020-06-12 15:42:41 +01:00
|
|
|
.documentation = if (try analysis.getDocComments(alloc, tree, tag, .Markdown)) |docs|
|
2020-05-16 17:04:07 +01:00
|
|
|
.{
|
|
|
|
.kind = .Markdown,
|
|
|
|
.value = docs,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
null,
|
|
|
|
};
|
|
|
|
|
|
|
|
try self.values.putNoClobber(item.label, {});
|
2020-05-28 16:18:48 +01:00
|
|
|
try self.completions.append(self.values.allocator, item);
|
2020-05-16 17:04:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
allocator: *std.mem.Allocator,
|
2020-05-18 08:28:36 +01:00
|
|
|
handles: std.StringHashMap(*Handle),
|
2020-05-30 21:36:18 +01:00
|
|
|
zig_exe_path: ?[]const u8,
|
2020-05-25 15:24:44 +01:00
|
|
|
build_files: std.ArrayListUnmanaged(*BuildFile),
|
2020-05-25 17:33:08 +01:00
|
|
|
build_runner_path: []const u8,
|
2020-06-10 17:54:01 +01:00
|
|
|
std_uri: ?[]const u8,
|
2020-05-14 00:10:41 +01:00
|
|
|
|
2020-05-28 16:18:48 +01:00
|
|
|
error_completions: TagStore,
|
|
|
|
enum_completions: TagStore,
|
2020-05-16 17:04:07 +01:00
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
pub fn init(
|
|
|
|
self: *DocumentStore,
|
|
|
|
allocator: *std.mem.Allocator,
|
2020-05-30 21:36:18 +01:00
|
|
|
zig_exe_path: ?[]const u8,
|
2020-05-25 17:33:08 +01:00
|
|
|
build_runner_path: []const u8,
|
2020-06-10 17:54:01 +01:00
|
|
|
zig_lib_path: ?[]const u8,
|
2020-05-25 17:33:08 +01:00
|
|
|
) !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-30 21:36:18 +01:00
|
|
|
self.zig_exe_path = zig_exe_path;
|
2020-05-25 15:24:44 +01:00
|
|
|
self.build_files = .{};
|
2020-05-25 17:33:08 +01:00
|
|
|
self.build_runner_path = build_runner_path;
|
2020-06-10 17:54:01 +01:00
|
|
|
self.std_uri = try stdUriFromLibPath(allocator, zig_lib_path);
|
2020-05-28 16:18:48 +01:00
|
|
|
self.error_completions = TagStore.init(allocator);
|
|
|
|
self.enum_completions = TagStore.init(allocator);
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
const LoadPackagesContext = struct {
|
|
|
|
build_file: *BuildFile,
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
build_runner_path: []const u8,
|
2020-05-30 21:36:18 +01:00
|
|
|
zig_exe_path: []const u8,
|
2020-05-25 17:33:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
fn loadPackages(context: LoadPackagesContext) !void {
|
|
|
|
const allocator = context.allocator;
|
|
|
|
const build_file = context.build_file;
|
|
|
|
const build_runner_path = context.build_runner_path;
|
2020-05-30 21:36:18 +01:00
|
|
|
const zig_exe_path = context.zig_exe_path;
|
2020-05-25 17:33:08 +01:00
|
|
|
|
|
|
|
const directory_path = try URI.parse(allocator, build_file.uri[0 .. build_file.uri.len - "build.zig".len]);
|
|
|
|
defer allocator.free(directory_path);
|
|
|
|
|
|
|
|
const target_path = try std.fs.path.resolve(allocator, &[_][]const u8{ directory_path, "build_runner.zig" });
|
|
|
|
defer allocator.free(target_path);
|
|
|
|
|
2020-05-25 18:21:53 +01:00
|
|
|
// For example, instead of testing if a file exists and then opening it, just
|
|
|
|
// open it and handle the error for file not found.
|
|
|
|
var file_exists = true;
|
|
|
|
check_file_exists: {
|
2020-05-16 17:04:07 +01:00
|
|
|
var fhandle = std.fs.cwd().openFile(target_path, .{ .read = true, .write = false }) catch |err| switch (err) {
|
2020-05-25 18:21:53 +01:00
|
|
|
error.FileNotFound => {
|
|
|
|
file_exists = false;
|
|
|
|
break :check_file_exists;
|
|
|
|
},
|
|
|
|
else => break :check_file_exists,
|
|
|
|
};
|
|
|
|
fhandle.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists) return error.BuildRunnerFileExists;
|
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
try std.fs.copyFileAbsolute(build_runner_path, target_path, .{});
|
|
|
|
defer std.fs.deleteFileAbsolute(target_path) catch {};
|
|
|
|
|
|
|
|
const zig_run_result = try std.ChildProcess.exec(.{
|
|
|
|
.allocator = allocator,
|
2020-05-30 21:36:18 +01:00
|
|
|
.argv = &[_][]const u8{ zig_exe_path, "run", "build_runner.zig" },
|
2020-05-25 17:33:08 +01:00
|
|
|
.cwd = directory_path,
|
|
|
|
});
|
|
|
|
|
|
|
|
defer {
|
|
|
|
allocator.free(zig_run_result.stdout);
|
|
|
|
allocator.free(zig_run_result.stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (zig_run_result.term) {
|
|
|
|
.Exited => |exit_code| {
|
|
|
|
if (exit_code == 0) {
|
|
|
|
std.debug.warn("Finished zig run for build file {}\n", .{build_file.uri});
|
|
|
|
|
|
|
|
for (build_file.packages.items) |old_pkg| {
|
|
|
|
allocator.free(old_pkg.name);
|
|
|
|
allocator.free(old_pkg.uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
build_file.packages.shrink(allocator, 0);
|
|
|
|
var line_it = std.mem.split(zig_run_result.stdout, "\n");
|
|
|
|
while (line_it.next()) |line| {
|
|
|
|
if (std.mem.indexOfScalar(u8, line, '\x00')) |zero_byte_idx| {
|
|
|
|
const name = line[0..zero_byte_idx];
|
|
|
|
const rel_path = line[zero_byte_idx + 1 ..];
|
|
|
|
|
|
|
|
const pkg_abs_path = try std.fs.path.resolve(allocator, &[_][]const u8{ directory_path, rel_path });
|
|
|
|
defer allocator.free(pkg_abs_path);
|
|
|
|
|
|
|
|
const pkg_uri = try URI.fromPath(allocator, pkg_abs_path);
|
|
|
|
errdefer allocator.free(pkg_uri);
|
|
|
|
|
|
|
|
const duped_name = try std.mem.dupe(allocator, u8, name);
|
|
|
|
errdefer allocator.free(duped_name);
|
|
|
|
|
|
|
|
(try build_file.packages.addOne(allocator)).* = .{
|
|
|
|
.name = duped_name,
|
|
|
|
.uri = pkg_uri,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-05-27 11:41:50 +01:00
|
|
|
else => return error.RunFailed,
|
2020-05-25 17:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 15:24:44 +01:00
|
|
|
|
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.
|
2020-05-25 17:33:08 +01:00
|
|
|
fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) anyerror!*Handle {
|
2020-05-14 09:51:49 +01:00
|
|
|
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);
|
|
|
|
|
2020-06-10 14:12:00 +01:00
|
|
|
const tree = try std.zig.parse(self.allocator, text);
|
|
|
|
errdefer tree.deinit();
|
|
|
|
|
|
|
|
const document_scope = try analysis.makeDocumentScope(self.allocator, tree);
|
|
|
|
errdefer document_scope.deinit(self.allocator);
|
|
|
|
|
2020-05-18 09:37:15 +01:00
|
|
|
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-06-10 14:12:00 +01:00
|
|
|
.tree = tree,
|
|
|
|
.document_scope = document_scope,
|
2020-05-25 15:24:44 +01:00
|
|
|
.associated_build_file = null,
|
|
|
|
.is_build_file = null,
|
2020-05-14 09:51:49 +01:00
|
|
|
};
|
2020-05-24 17:00:21 +01:00
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
// TODO: Better logic for detecting std or subdirectories?
|
2020-05-25 15:48:25 +01:00
|
|
|
const in_std = std.mem.indexOf(u8, uri, "/std/") != null;
|
2020-05-30 21:36:18 +01:00
|
|
|
if (self.zig_exe_path != null and std.mem.endsWith(u8, uri, "/build.zig") and !in_std) {
|
2020-05-25 15:24:44 +01:00
|
|
|
std.debug.warn("Document is a build file, extracting packages...\n", .{});
|
|
|
|
// This is a build file.
|
|
|
|
var build_file = try self.allocator.create(BuildFile);
|
2020-05-25 15:48:25 +01:00
|
|
|
errdefer self.allocator.destroy(build_file);
|
2020-05-25 15:24:44 +01:00
|
|
|
|
|
|
|
build_file.* = .{
|
2020-05-25 18:42:58 +01:00
|
|
|
.refs = 1,
|
2020-05-25 15:24:44 +01:00
|
|
|
.uri = try std.mem.dupe(self.allocator, u8, uri),
|
|
|
|
.packages = .{},
|
|
|
|
};
|
|
|
|
|
2020-05-25 15:48:25 +01:00
|
|
|
try self.build_files.append(self.allocator, build_file);
|
2020-05-25 15:24:44 +01:00
|
|
|
handle.is_build_file = build_file;
|
2020-05-25 17:33:08 +01:00
|
|
|
|
|
|
|
// TODO: Do this in a separate thread?
|
|
|
|
// It can take quite long.
|
2020-05-25 18:21:53 +01:00
|
|
|
loadPackages(.{
|
2020-05-25 17:33:08 +01:00
|
|
|
.build_file = build_file,
|
|
|
|
.allocator = self.allocator,
|
|
|
|
.build_runner_path = self.build_runner_path,
|
2020-05-30 21:36:18 +01:00
|
|
|
.zig_exe_path = self.zig_exe_path.?,
|
2020-05-27 11:41:50 +01:00
|
|
|
}) catch |err| {
|
2020-05-16 17:04:07 +01:00
|
|
|
std.debug.warn("Failed to load packages of build file {} (error: {})\n", .{ build_file.uri, err });
|
2020-05-25 18:21:53 +01:00
|
|
|
};
|
2020-05-30 21:36:18 +01:00
|
|
|
} else if (self.zig_exe_path != null and !in_std) associate_build_file: {
|
2020-05-25 15:24:44 +01:00
|
|
|
// Look into build files to see if we already have one that fits
|
|
|
|
for (self.build_files.items) |build_file| {
|
|
|
|
const build_file_base_uri = build_file.uri[0 .. std.mem.lastIndexOfScalar(u8, build_file.uri, '/').? + 1];
|
|
|
|
|
|
|
|
if (std.mem.startsWith(u8, uri, build_file_base_uri)) {
|
|
|
|
std.debug.warn("Found an associated build file: {}\n", .{build_file.uri});
|
2020-05-25 18:42:58 +01:00
|
|
|
build_file.refs += 1;
|
2020-05-25 15:24:44 +01:00
|
|
|
handle.associated_build_file = build_file;
|
|
|
|
break :associate_build_file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise, try to find a build file.
|
|
|
|
var curr_path = try URI.parse(self.allocator, uri);
|
|
|
|
defer self.allocator.free(curr_path);
|
|
|
|
while (true) {
|
2020-05-25 15:48:25 +01:00
|
|
|
if (curr_path.len == 0) break :associate_build_file;
|
2020-05-25 15:24:44 +01:00
|
|
|
|
|
|
|
if (std.mem.lastIndexOfScalar(u8, curr_path[0 .. curr_path.len - 1], std.fs.path.sep)) |idx| {
|
|
|
|
// This includes the last separator
|
2020-05-25 17:33:08 +01:00
|
|
|
curr_path = curr_path[0 .. idx + 1];
|
|
|
|
var candidate_path = try std.mem.concat(self.allocator, u8, &[_][]const u8{ curr_path, "build.zig" });
|
2020-05-25 15:24:44 +01:00
|
|
|
defer self.allocator.free(candidate_path);
|
|
|
|
// Try to open the file, read it and add the new document if we find it.
|
|
|
|
var file = std.fs.cwd().openFile(candidate_path, .{ .read = true, .write = false }) catch continue;
|
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
const build_file_text = try file.inStream().readAllAlloc(self.allocator, std.math.maxInt(usize));
|
|
|
|
errdefer self.allocator.free(build_file_text);
|
|
|
|
|
|
|
|
const build_file_uri = try URI.fromPath(self.allocator, candidate_path);
|
|
|
|
errdefer self.allocator.free(build_file_uri);
|
|
|
|
|
|
|
|
const build_file_handle = try self.newDocument(build_file_uri, build_file_text);
|
2020-05-25 15:48:25 +01:00
|
|
|
|
2020-06-15 01:59:49 +01:00
|
|
|
if (build_file_handle.is_build_file) |build_file| {
|
|
|
|
build_file.refs += 1;
|
|
|
|
}
|
2020-05-25 15:24:44 +01:00
|
|
|
handle.associated_build_file = build_file_handle.is_build_file;
|
|
|
|
break;
|
|
|
|
} else break :associate_build_file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2020-05-25 18:42:58 +01:00
|
|
|
if (entry.value.is_build_file) |build_file| {
|
|
|
|
build_file.refs += 1;
|
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-26 11:30:28 +01:00
|
|
|
fn decrementBuildFileRefs(self: *DocumentStore, build_file: *BuildFile) void {
|
2020-05-25 18:42:58 +01:00
|
|
|
build_file.refs -= 1;
|
|
|
|
if (build_file.refs == 0) {
|
|
|
|
std.debug.warn("Freeing build file {}\n", .{build_file.uri});
|
|
|
|
for (build_file.packages.items) |pkg| {
|
|
|
|
self.allocator.free(pkg.name);
|
|
|
|
self.allocator.free(pkg.uri);
|
|
|
|
}
|
|
|
|
build_file.packages.deinit(self.allocator);
|
|
|
|
|
|
|
|
// Decrement count of the document since one count comes
|
|
|
|
// from the build file existing.
|
2020-05-26 11:30:28 +01:00
|
|
|
self.decrementCount(build_file.uri);
|
2020-05-25 18:42:58 +01:00
|
|
|
self.allocator.free(build_file.uri);
|
|
|
|
|
|
|
|
// Remove the build file from the array list
|
|
|
|
_ = self.build_files.swapRemove(std.mem.indexOfScalar(*BuildFile, self.build_files.items, build_file).?);
|
|
|
|
self.allocator.destroy(build_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-25 18:42:58 +01:00
|
|
|
|
2020-06-14 19:19:27 +01:00
|
|
|
if (entry.value.count > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std.debug.warn("Freeing document: {}\n", .{uri});
|
|
|
|
|
2020-05-26 11:30:28 +01:00
|
|
|
if (entry.value.associated_build_file) |build_file| {
|
|
|
|
self.decrementBuildFileRefs(build_file);
|
2020-05-25 18:42:58 +01:00
|
|
|
}
|
|
|
|
|
2020-06-14 19:19:27 +01:00
|
|
|
if (entry.value.is_build_file) |build_file| {
|
|
|
|
self.decrementBuildFileRefs(build_file);
|
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
|
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-27 11:41:50 +01:00
|
|
|
self.allocator.destroy(entry.value);
|
2020-05-14 00:10:41 +01:00
|
|
|
const uri_key = entry.key;
|
|
|
|
self.handles.removeAssertDiscard(uri);
|
|
|
|
self.allocator.free(uri_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-10 14:12:00 +01:00
|
|
|
handle.document_scope.deinit(self.allocator);
|
|
|
|
handle.document_scope = try analysis.makeDocumentScope(self.allocator, handle.tree);
|
|
|
|
|
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-06-10 17:54:01 +01:00
|
|
|
const uri = (try self.uriFromImportStr(&arena.allocator, handle.*, str)) 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-06-03 09:32:05 +01:00
|
|
|
pub fn applySave(self: *DocumentStore, handle: *Handle) !void {
|
|
|
|
if (handle.is_build_file) |build_file| {
|
|
|
|
loadPackages(.{
|
|
|
|
.build_file = build_file,
|
|
|
|
.allocator = self.allocator,
|
|
|
|
.build_runner_path = self.build_runner_path,
|
|
|
|
.zig_exe_path = self.zig_exe_path.?,
|
|
|
|
}) catch |err| {
|
|
|
|
std.debug.warn("Failed to load packages of build file {} (error: {})\n", .{ build_file.uri, err });
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-10 17:54:01 +01:00
|
|
|
self: *DocumentStore,
|
2020-05-19 20:09:00 +01:00
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
handle: Handle,
|
|
|
|
import_str: []const u8,
|
|
|
|
) !?[]const u8 {
|
2020-05-22 16:56:57 +01:00
|
|
|
if (std.mem.eql(u8, import_str, "std")) {
|
2020-06-10 17:54:01 +01:00
|
|
|
if (self.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")) {
|
2020-05-25 17:33:08 +01:00
|
|
|
if (handle.associated_build_file) |build_file| {
|
|
|
|
for (build_file.packages.items) |pkg| {
|
|
|
|
if (std.mem.eql(u8, import_str, pkg.name)) {
|
|
|
|
return try std.mem.dupe(allocator, u8, pkg.uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-05-22 16:56:57 +01:00
|
|
|
} 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-06-10 17:54:01 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
2020-05-14 02:54:05 +01:00
|
|
|
}
|
2020-06-10 17:54:01 +01:00
|
|
|
}
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
// 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;
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
new_handle.count += 1;
|
|
|
|
return new_handle;
|
|
|
|
}
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +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);
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
var file = std.fs.cwd().openFile(file_path, .{}) catch {
|
|
|
|
std.debug.warn("Cannot open import file {}\n", .{file_path});
|
|
|
|
return null;
|
|
|
|
};
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
defer file.close();
|
|
|
|
const size = std.math.cast(usize, try file.getEndPos()) catch std.math.maxInt(usize);
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
{
|
|
|
|
const file_contents = try allocator.alloc(u8, size);
|
|
|
|
errdefer allocator.free(file_contents);
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
file.inStream().readNoEof(file_contents) catch {
|
|
|
|
std.debug.warn("Could not read from file {}\n", .{file_path});
|
|
|
|
return null;
|
|
|
|
};
|
2020-05-14 09:51:49 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
// Add to import table of current handle.
|
|
|
|
try handle.import_uris.append(final_uri);
|
|
|
|
consumed_final_uri = true;
|
2020-05-14 09:40:17 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
// 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);
|
2020-05-14 02:54:05 +01:00
|
|
|
}
|
2020-06-10 17:54:01 +01:00
|
|
|
}
|
2020-05-14 02:54:05 +01:00
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-06-10 14:12:00 +01:00
|
|
|
|
|
|
|
entry.value.document_scope.deinit(self.allocator);
|
2020-05-14 13:26:10 +01:00
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
|
|
|
|
self.handles.deinit();
|
2020-05-25 17:49:04 +01:00
|
|
|
|
|
|
|
for (self.build_files.items) |build_file| {
|
|
|
|
for (build_file.packages.items) |pkg| {
|
|
|
|
self.allocator.free(pkg.name);
|
|
|
|
self.allocator.free(pkg.uri);
|
|
|
|
}
|
|
|
|
self.allocator.free(build_file.uri);
|
|
|
|
self.allocator.destroy(build_file);
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:54:01 +01:00
|
|
|
if (self.std_uri) |std_uri| {
|
|
|
|
self.allocator.free(std_uri);
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:49:04 +01:00
|
|
|
self.build_files.deinit(self.allocator);
|
2020-05-16 17:04:07 +01:00
|
|
|
self.error_completions.deinit();
|
2020-05-28 16:18:48 +01:00
|
|
|
self.enum_completions.deinit();
|
2020-05-14 00:10:41 +01:00
|
|
|
}
|