Misc cleanups
This commit is contained in:
parent
a2402b9885
commit
c1599c755d
@ -52,11 +52,6 @@ pub fn init(self: *DocumentStore, allocator: *std.mem.Allocator, zig_lib_path: ?
|
||||
fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) !*Handle {
|
||||
std.debug.warn("Opened document: {}\n", .{uri});
|
||||
|
||||
errdefer {
|
||||
self.allocator.free(uri);
|
||||
self.allocator.free(text);
|
||||
}
|
||||
|
||||
var handle = Handle{
|
||||
.count = 1,
|
||||
.import_uris = std.ArrayList([]const u8).init(self.allocator),
|
||||
@ -67,8 +62,8 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) !*Handle {
|
||||
},
|
||||
};
|
||||
try self.checkSanity(&handle);
|
||||
try self.handles.putNoClobber(uri, handle);
|
||||
return &(self.handles.get(uri) orelse unreachable).value;
|
||||
const kv = try self.handles.getOrPutValue(uri, handle);
|
||||
return &kv.value;
|
||||
}
|
||||
|
||||
pub fn openDocument(self: *DocumentStore, uri: []const u8, text: []const u8) !*Handle {
|
||||
@ -84,7 +79,7 @@ pub fn openDocument(self: *DocumentStore, uri: []const u8, text: []const u8) !*H
|
||||
const duped_uri = try std.mem.dupe(self.allocator, u8, uri);
|
||||
errdefer self.allocator.free(duped_uri);
|
||||
|
||||
return self.newDocument(duped_uri, duped_text);
|
||||
return try self.newDocument(duped_uri, duped_text);
|
||||
}
|
||||
|
||||
fn decrementCount(self: *DocumentStore, uri: []const u8) void {
|
||||
@ -145,7 +140,7 @@ fn checkSanity(self: *DocumentStore, handle: *Handle) !void {
|
||||
}
|
||||
|
||||
for (import_strs) |str| {
|
||||
const uri = (try uriFromImportStr(self, handle, str)) orelse continue;
|
||||
const uri = (try uriFromImportStr(self, handle.*, str)) orelse continue;
|
||||
defer self.allocator.free(uri);
|
||||
|
||||
var idx: usize = 0;
|
||||
@ -175,7 +170,7 @@ fn checkSanity(self: *DocumentStore, handle: *Handle) !void {
|
||||
}
|
||||
|
||||
pub fn applyChanges(self: *DocumentStore, handle: *Handle, content_changes: std.json.Array) !void {
|
||||
var document = &handle.document;
|
||||
const document = &handle.document;
|
||||
|
||||
for (content_changes.items) |change| {
|
||||
if (change.Object.getValue("range")) |range| {
|
||||
@ -230,7 +225,7 @@ pub fn applyChanges(self: *DocumentStore, handle: *Handle, content_changes: std.
|
||||
try self.checkSanity(handle);
|
||||
}
|
||||
|
||||
fn uriFromImportStr(store: *DocumentStore, handle: *Handle, import_str: []const u8) !?[]const u8 {
|
||||
fn uriFromImportStr(store: *DocumentStore, handle: Handle, import_str: []const u8) !?[]const u8 {
|
||||
return if (std.mem.eql(u8, import_str, "std"))
|
||||
if (store.std_uri) |std_root_uri| try std.mem.dupe(store.allocator, u8, std_root_uri)
|
||||
else {
|
||||
@ -264,7 +259,7 @@ pub const AnalysisContext = struct {
|
||||
|
||||
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.handle, import_str)) orelse return null;
|
||||
const final_uri = (try uriFromImportStr(self.store, self.handle.*, import_str)) orelse return null;
|
||||
|
||||
std.debug.warn("Import final URI: {}\n", .{final_uri});
|
||||
var consumed_final_uri = false;
|
||||
|
25
src/main.zig
25
src/main.zig
@ -285,7 +285,7 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
|
||||
|
||||
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
||||
|
||||
var line = try handle.document.getLine(@intCast(usize, position.line));
|
||||
const line = try handle.document.getLine(@intCast(usize, position.line));
|
||||
var tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]);
|
||||
|
||||
// var decls = try analysis.declsFromIndex(&arena.allocator, analysis_ctx.tree, try handle.document.positionToIndex(position));
|
||||
@ -607,9 +607,10 @@ pub fn main() anyerror!void {
|
||||
const stdin = std.io.getStdIn().inStream();
|
||||
stdout = std.io.getStdOut().outStream();
|
||||
|
||||
// Read he configuration, if any.
|
||||
var config = Config{};
|
||||
// Read the configuration, if any.
|
||||
const config_parse_options = std.json.ParseOptions{ .allocator = allocator };
|
||||
var config = Config{};
|
||||
defer std.json.parseFree(Config, config, config_parse_options);
|
||||
|
||||
// TODO: Investigate using std.fs.Watch to detect writes to the config and reload it.
|
||||
config_read: {
|
||||
@ -619,31 +620,27 @@ pub fn main() anyerror!void {
|
||||
var exec_dir = std.fs.cwd().openDir(exec_dir_path, .{}) catch break :config_read;
|
||||
defer exec_dir.close();
|
||||
|
||||
var conf_file = exec_dir.openFile("zls.json", .{}) catch break :config_read;
|
||||
const conf_file = exec_dir.openFile("zls.json", .{}) catch break :config_read;
|
||||
defer conf_file.close();
|
||||
|
||||
const conf_file_stat = conf_file.stat() catch break :config_read;
|
||||
|
||||
// Allocate enough memory for the whole file.
|
||||
var file_buf = try allocator.alloc(u8, conf_file_stat.size);
|
||||
// Max 1MB
|
||||
const file_buf = conf_file.inStream().readAllAlloc(allocator, 0x1000000) catch break :config_read;
|
||||
defer allocator.free(file_buf);
|
||||
|
||||
const bytes_read = conf_file.readAll(file_buf) catch break :config_read;
|
||||
if (bytes_read != conf_file_stat.size) break :config_read;
|
||||
|
||||
// TODO: Better errors? Doesn't seem like std.json can provide us positions or context.
|
||||
config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), config_parse_options) catch |err| {
|
||||
std.debug.warn("Error while parsing configuration file: {}\nUsing default config.\n", .{err});
|
||||
break :config_read;
|
||||
};
|
||||
}
|
||||
defer std.json.parseFree(Config, config, config_parse_options);
|
||||
|
||||
if (config.zig_lib_path != null and !std.fs.path.isAbsolute(config.zig_lib_path.?)) {
|
||||
if (config.zig_lib_path) |zig_lib_path| {
|
||||
if (!std.fs.path.isAbsolute(zig_lib_path)) {
|
||||
std.debug.warn("zig library path is not absolute, defaulting to null.\n", .{});
|
||||
allocator.free(config.zig_lib_path.?);
|
||||
allocator.free(zig_lib_path);
|
||||
config.zig_lib_path = null;
|
||||
}
|
||||
}
|
||||
|
||||
try document_store.init(allocator, config.zig_lib_path);
|
||||
defer document_store.deinit();
|
||||
|
@ -185,11 +185,11 @@ pub const MarkupKind = enum(u1) {
|
||||
options: json.StringifyOptions,
|
||||
out_stream: var,
|
||||
) !void {
|
||||
if (@enumToInt(value) == 0) {
|
||||
try json.stringify("plaintext", options, out_stream);
|
||||
} else {
|
||||
try json.stringify("markdown", options, out_stream);
|
||||
}
|
||||
const str = switch (value) {
|
||||
.PlainText => "plaintext",
|
||||
.Markdown => "markdown",
|
||||
};
|
||||
try json.stringify(str, options, out_stream);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -14,7 +14,7 @@ pub fn fromPath(allocator: *std.mem.Allocator, path: []const u8) ![]const u8 {
|
||||
var buf = std.ArrayList(u8).init(allocator);
|
||||
try buf.appendSlice(prefix);
|
||||
|
||||
var out_stream = buf.outStream();
|
||||
const out_stream = buf.outStream();
|
||||
|
||||
for (path) |char| {
|
||||
if (char == std.fs.path.sep) {
|
||||
@ -55,17 +55,16 @@ fn parseHex(c: u8) !u8 {
|
||||
pub fn parse(allocator: *std.mem.Allocator, str: []const u8) ![]u8 {
|
||||
if (str.len < 7 or !std.mem.eql(u8, "file://", str[0..7])) return error.UriBadScheme;
|
||||
|
||||
var uri = try allocator.alloc(u8, str.len - (if (std.fs.path.sep == '\\') 8 else 7));
|
||||
const uri = try allocator.alloc(u8, str.len - (if (std.fs.path.sep == '\\') 8 else 7));
|
||||
errdefer allocator.free(uri);
|
||||
|
||||
const path = if (std.fs.path.sep == '\\') str[8..] else str[7..];
|
||||
|
||||
var i: usize = 0;
|
||||
var j: usize = 0;
|
||||
var e: usize = path.len;
|
||||
while (j < e) : (i += 1) {
|
||||
while (j < path.len) : (i += 1) {
|
||||
if (path[j] == '%') {
|
||||
if (j + 2 >= e) return error.UriBadEscape;
|
||||
if (j + 2 >= path.len) return error.UriBadEscape;
|
||||
const upper = try parseHex(path[j + 1]);
|
||||
const lower = try parseHex(path[j + 2]);
|
||||
uri[i] = (upper << 4) + lower;
|
||||
|
Loading…
Reference in New Issue
Block a user