Merge pull request #546 from zigtools/some-main-globals
Refactor out most main globals!
This commit is contained in:
commit
63eb5fa8fc
@ -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);
|
||||||
|
|
||||||
|
@ -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
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user