Start refactoring main -> Server's globals

This commit is contained in:
Auguste Rame 2022-07-17 12:00:29 +02:00
parent ba68a4dcb5
commit 1d910ed973
No known key found for this signature in database
GPG Key ID: 3A5E3F90DF2AAEFE
4 changed files with 411 additions and 301 deletions

View File

@ -6,7 +6,7 @@ pub fn build(b: *std.build.Builder) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const exe = b.addExecutable("zls", "src/main.zig"); const exe = b.addExecutable("zls", "src/Server.zig");
const exe_options = b.addOptions(); const exe_options = b.addOptions();
exe.addOptions("build_options", exe_options); exe.addOptions("build_options", exe_options);

View File

@ -4,6 +4,7 @@ const Config = @This();
const std = @import("std"); const std = @import("std");
const setup = @import("setup.zig"); const setup = @import("setup.zig");
const tracy = @import("tracy.zig");
const known_folders = @import("known-folders"); const known_folders = @import("known-folders");
const logger = std.log.scoped(.config); const logger = std.log.scoped(.config);
@ -53,6 +54,50 @@ skip_std_references: bool = false,
/// Path to "builtin;" useful for debugging, automatically set if let null /// Path to "builtin;" useful for debugging, automatically set if let null
builtin_path: ?[]const u8 = null, builtin_path: ?[]const u8 = null,
pub fn loadFromFile(allocator: std.mem.Allocator, file_path: []const u8) ?Config {
@setEvalBranchQuota(5000);
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
var file = std.fs.cwd().openFile(file_path, .{}) catch |err| {
if (err != error.FileNotFound)
logger.warn("Error while reading configuration file: {}", .{err});
return null;
};
defer file.close();
const file_buf = file.readToEndAlloc(allocator, 0x1000000) catch return null;
defer allocator.free(file_buf);
@setEvalBranchQuota(3000);
// TODO: Better errors? Doesn't seem like std.json can provide us positions or context.
var config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), std.json.ParseOptions{ .allocator = allocator }) catch |err| {
logger.warn("Error while parsing configuration file: {}", .{err});
return null;
};
if (config.zig_lib_path) |zig_lib_path| {
if (!std.fs.path.isAbsolute(zig_lib_path)) {
logger.warn("zig library path is not absolute, defaulting to null.", .{});
allocator.free(zig_lib_path);
config.zig_lib_path = null;
}
}
return config;
}
pub fn loadFromFolder(allocator: std.mem.Allocator, folder_path: []const u8) ?Config {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const full_path = std.fs.path.resolve(allocator, &.{ folder_path, "zls.json" }) catch return null;
defer allocator.free(full_path);
return loadFromFile(allocator, full_path);
}
/// Invoke this once all config values have been changed.
pub fn configChanged(config: *Config, allocator: std.mem.Allocator, builtin_creation_dir: ?[]const u8) !void { pub fn configChanged(config: *Config, allocator: std.mem.Allocator, builtin_creation_dir: ?[]const u8) !void {
// Find the zig executable in PATH // Find the zig executable in PATH
find_zig: { find_zig: {

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ const DocumentStore = @import("DocumentStore.zig");
const types = @import("types.zig"); const types = @import("types.zig");
const Ast = std.zig.Ast; const Ast = std.zig.Ast;
const Token = std.zig.Token; const Token = std.zig.Token;
const identifierFromPosition = @import("main.zig").identifierFromPosition; const identifierFromPosition = @import("Server.zig").identifierFromPosition;
const ast = @import("ast.zig"); const ast = @import("ast.zig");
fn fnProtoToSignatureInfo(document_store: *DocumentStore, arena: *std.heap.ArenaAllocator, commas: u32, skip_self_param: bool, handle: *DocumentStore.Handle, fn_node: Ast.Node.Index, proto: Ast.full.FnProto) !types.SignatureInformation { fn fnProtoToSignatureInfo(document_store: *DocumentStore, arena: *std.heap.ArenaAllocator, commas: u32, skip_self_param: bool, handle: *DocumentStore.Handle, fn_node: Ast.Node.Index, proto: Ast.full.FnProto) !types.SignatureInformation {