Merge branch 'master' into master

This commit is contained in:
Auguste Rame 2020-05-18 08:58:32 -04:00 committed by GitHub
commit 5a6ff63d8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 117 deletions

View File

@ -206,7 +206,6 @@ pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
/// Gets the child of slice /// Gets the child of slice
pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*ast.Node { pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*ast.Node {
// var index: usize = 0;
for (nodes) |child| { for (nodes) |child| {
switch (child.id) { switch (child.id) {
.VarDecl => { .VarDecl => {
@ -227,14 +226,12 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*
}, },
else => {}, else => {},
} }
// index += 1;
} }
return null; return null;
} }
/// Resolves the type of a node /// Resolves the type of a node
pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.Node { pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.Node {
std.debug.warn("NODE {}\n", .{node});
switch (node.id) { switch (node.id) {
.VarDecl => { .VarDecl => {
const vari = node.cast(ast.Node.VarDecl).?; const vari = node.cast(ast.Node.VarDecl).?;
@ -257,22 +254,14 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
} }
}, },
.Identifier => { .Identifier => {
// std.debug.warn("IDENTIFIER {}\n", .{analysis_ctx.tree.getNodeSource(node)});
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| {
// std.debug.warn("CHILD {}\n", .{child});
return resolveTypeOfNode(analysis_ctx, child); return resolveTypeOfNode(analysis_ctx, child);
} else return null; } else return null;
}, },
.ContainerDecl => {
return node;
},
.ContainerField => { .ContainerField => {
const field = node.cast(ast.Node.ContainerField).?; const field = node.cast(ast.Node.ContainerField).?;
return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null); return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null);
}, },
.ErrorSetDecl => {
return node;
},
.SuffixOp => { .SuffixOp => {
const suffix_op = node.cast(ast.Node.SuffixOp).?; const suffix_op = node.cast(ast.Node.SuffixOp).?;
switch (suffix_op.op) { switch (suffix_op.op) {
@ -326,12 +315,8 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
break :block null; break :block null;
}; };
}, },
.MultilineStringLiteral, .StringLiteral => { .MultilineStringLiteral, .StringLiteral, .ContainerDecl, .ErrorSetDecl => return node,
return node; else => std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}),
},
else => {
std.debug.warn("Type resolution case not implemented; {}\n", .{node.id});
},
} }
return null; return null;
} }
@ -349,10 +334,8 @@ fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr:
/// Collects all imports we can find into a slice of import paths (without quotes). /// Collects all imports we can find into a slice of import paths (without quotes).
/// The import paths are valid as long as the tree is. /// The import paths are valid as long as the tree is.
pub fn collectImports(allocator: *std.mem.Allocator, tree: *ast.Tree) ![][]const u8 { pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !void {
// TODO: Currently only detects `const smth = @import("string literal")<.SometThing>;` // TODO: Currently only detects `const smth = @import("string literal")<.SomeThing>;`
var arr = std.ArrayList([]const u8).init(allocator);
var idx: usize = 0; var idx: usize = 0;
while (tree.root_node.iterate(idx)) |decl| : (idx += 1) { while (tree.root_node.iterate(idx)) |decl| : (idx += 1) {
if (decl.id != .VarDecl) continue; if (decl.id != .VarDecl) continue;
@ -362,7 +345,7 @@ pub fn collectImports(allocator: *std.mem.Allocator, tree: *ast.Tree) ![][]const
switch (var_decl.init_node.?.id) { switch (var_decl.init_node.?.id) {
.BuiltinCall => { .BuiltinCall => {
const builtin_call = var_decl.init_node.?.cast(ast.Node.BuiltinCall).?; const builtin_call = var_decl.init_node.?.cast(ast.Node.BuiltinCall).?;
try maybeCollectImport(tree, builtin_call, &arr); try maybeCollectImport(tree, builtin_call, import_arr);
}, },
.InfixOp => { .InfixOp => {
const infix_op = var_decl.init_node.?.cast(ast.Node.InfixOp).?; const infix_op = var_decl.init_node.?.cast(ast.Node.InfixOp).?;
@ -372,13 +355,11 @@ pub fn collectImports(allocator: *std.mem.Allocator, tree: *ast.Tree) ![][]const
else => continue, else => continue,
} }
if (infix_op.lhs.id != .BuiltinCall) continue; if (infix_op.lhs.id != .BuiltinCall) continue;
try maybeCollectImport(tree, infix_op.lhs.cast(ast.Node.BuiltinCall).?, &arr); try maybeCollectImport(tree, infix_op.lhs.cast(ast.Node.BuiltinCall).?, import_arr);
}, },
else => {}, else => {},
} }
} }
return arr.toOwnedSlice();
} }
pub fn getFieldAccessTypeNode(analysis_ctx: *AnalysisContext, tokenizer: *std.zig.Tokenizer) ?*ast.Node { pub fn getFieldAccessTypeNode(analysis_ctx: *AnalysisContext, tokenizer: *std.zig.Tokenizer) ?*ast.Node {
@ -387,12 +368,8 @@ pub fn getFieldAccessTypeNode(analysis_ctx: *AnalysisContext, tokenizer: *std.zi
while (true) { while (true) {
var next = tokenizer.next(); var next = tokenizer.next();
switch (next.id) { switch (next.id) {
.Eof => { .Eof => return current_node,
return current_node;
},
.Identifier => { .Identifier => {
// var root = current_node.cast(ast.Node.Root).?;
// current_node.
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| {
if (resolveTypeOfNode(analysis_ctx, child)) |node_type| { if (resolveTypeOfNode(analysis_ctx, child)) |node_type| {
current_node = node_type; current_node = node_type;
@ -411,9 +388,7 @@ pub fn getFieldAccessTypeNode(analysis_ctx: *AnalysisContext, tokenizer: *std.zi
} else return null; } else return null;
} }
}, },
else => { else => std.debug.warn("Not implemented; {}\n", .{next.id}),
std.debug.warn("Not implemented; {}\n", .{next.id});
},
} }
} }
@ -462,70 +437,44 @@ pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
return null; return null;
} }
pub fn declsFromIndexInternal(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node, nodes: *std.ArrayList(*ast.Node)) anyerror!void { pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) anyerror!void {
switch (node.id) { switch (node.id) {
.FnProto => { .FnProto => {
const func = node.cast(ast.Node.FnProto).?; const func = node.cast(ast.Node.FnProto).?;
var param_index: usize = 0; var param_index: usize = 0;
while (param_index < func.params.len) : (param_index += 1) while (param_index < func.params.len) : (param_index += 1)
try declsFromIndexInternal(allocator, tree, func.params.at(param_index).*, nodes); try declsFromIndexInternal(decls, tree, func.params.at(param_index).*);
if (func.body_node) |body_node| if (func.body_node) |body_node|
try declsFromIndexInternal(allocator, tree, body_node, nodes); try declsFromIndexInternal(decls, tree, body_node);
}, },
.Block => { .Block => {
var index: usize = 0; var index: usize = 0;
while (node.iterate(index)) |inode| : (index += 1) {
while (node.iterate(index)) |inode| { try declsFromIndexInternal(decls, tree, inode);
try declsFromIndexInternal(allocator, tree, inode, nodes);
index += 1;
} }
}, },
.VarDecl => { .VarDecl, .ParamDecl => try decls.append(node),
try nodes.append(node); else => try addChildrenNodes(decls, tree, node),
},
.ParamDecl => {
try nodes.append(node);
},
else => {
try nodes.appendSlice(try getCompletionsFromNode(allocator, tree, node));
},
} }
} }
pub fn getCompletionsFromNode(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node) ![]*ast.Node { pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void {
var nodes = std.ArrayList(*ast.Node).init(allocator);
var index: usize = 0; var index: usize = 0;
while (node.iterate(index)) |child_node| { while (node.iterate(index)) |child_node| : (index += 1) {
try nodes.append(child_node); try decls.append(child_node);
}
index += 1;
} }
return nodes.items; pub fn declsFromIndex(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, index: usize) !void {
}
pub fn declsFromIndex(allocator: *std.mem.Allocator, tree: *ast.Tree, index: usize) ![]*ast.Node {
var iindex: usize = 0;
var node = &tree.root_node.base; var node = &tree.root_node.base;
var nodes = std.ArrayList(*ast.Node).init(allocator);
try nodes.appendSlice(try getCompletionsFromNode(allocator, tree, node)); try addChildrenNodes(decls, tree, node);
var node_index: usize = 0;
while (node.iterate(iindex)) |inode| { while (node.iterate(node_index)) |inode| : (node_index += 1) {
if (tree.tokens.at(inode.firstToken()).start < index and index < tree.tokens.at(inode.lastToken()).start) { if (tree.tokens.at(inode.firstToken()).start < index and index < tree.tokens.at(inode.lastToken()).end) {
try declsFromIndexInternal(allocator, tree, inode, &nodes); try declsFromIndexInternal(decls, tree, inode);
} }
iindex += 1;
} }
if (tree.tokens.at(node.firstToken()).start < index and index < tree.tokens.at(node.lastToken()).start) {
return nodes.items;
}
return nodes.items;
} }

View File

@ -61,7 +61,6 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) !*Handle {
.mem = text, .mem = text,
}, },
}; };
try self.checkSanity(&handle);
const kv = try self.handles.getOrPutValue(uri, handle); const kv = try self.handles.getOrPutValue(uri, handle);
return &kv.value; return &kv.value;
} }
@ -117,10 +116,7 @@ pub fn getHandle(self: *DocumentStore, uri: []const u8) ?*Handle {
} }
// Check if the document text is now sane, move it to sane_text if so. // Check if the document text is now sane, move it to sane_text if so.
fn checkSanity(self: *DocumentStore, handle: *Handle) !void { fn removeOldImports(self: *DocumentStore, handle: *Handle) !void {
const tree = try handle.tree(self.allocator);
defer tree.deinit();
std.debug.warn("New text for document {}\n", .{handle.uri()}); std.debug.warn("New text for document {}\n", .{handle.uri()});
// TODO: Better algorithm or data structure? // TODO: Better algorithm or data structure?
// Removing the imports is costly since they live in an array list // Removing the imports is costly since they live in an array list
@ -129,19 +125,22 @@ fn checkSanity(self: *DocumentStore, handle: *Handle) !void {
// Try to detect removed imports and decrement their counts. // Try to detect removed imports and decrement their counts.
if (handle.import_uris.items.len == 0) return; if (handle.import_uris.items.len == 0) return;
const import_strs = try analysis.collectImports(self.allocator, tree); const tree = try handle.tree(self.allocator);
defer self.allocator.free(import_strs); defer tree.deinit();
const still_exist = try self.allocator.alloc(bool, handle.import_uris.items.len); var arena = std.heap.ArenaAllocator.init(self.allocator);
defer self.allocator.free(still_exist); defer arena.deinit();
var import_strs = std.ArrayList([]const u8).init(&arena.allocator);
try analysis.collectImports(&import_strs, tree);
const still_exist = try arena.allocator.alloc(bool, handle.import_uris.items.len);
for (still_exist) |*ex| { for (still_exist) |*ex| {
ex.* = false; ex.* = false;
} }
for (import_strs) |str| { for (import_strs.items) |str| {
const uri = (try uriFromImportStr(self, handle.*, str)) orelse continue; const uri = (try uriFromImportStr(self, &arena.allocator, handle.*, str)) orelse continue;
defer self.allocator.free(uri);
var idx: usize = 0; var idx: usize = 0;
exists_loop: while (idx < still_exist.len) : (idx += 1) { exists_loop: while (idx < still_exist.len) : (idx += 1) {
@ -222,29 +221,29 @@ pub fn applyChanges(self: *DocumentStore, handle: *Handle, content_changes: std.
} }
} }
try self.checkSanity(handle); try self.removeOldImports(handle);
} }
fn uriFromImportStr(store: *DocumentStore, handle: Handle, import_str: []const u8) !?[]const u8 { fn uriFromImportStr(store: *DocumentStore, allocator: *std.mem.Allocator, handle: Handle, import_str: []const u8) !?[]const u8 {
return if (std.mem.eql(u8, import_str, "std")) 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) if (store.std_uri) |std_root_uri| try std.mem.dupe(allocator, u8, std_root_uri)
else { else {
std.debug.warn("Cannot resolve std library import, path is null.\n", .{}); std.debug.warn("Cannot resolve std library import, path is null.\n", .{});
return null; return null;
} }
else b: { else b: {
// Find relative uri // Find relative uri
const path = try URI.parse(store.allocator, handle.uri()); const path = try URI.parse(allocator, handle.uri());
defer store.allocator.free(path); defer allocator.free(path);
const dir_path = std.fs.path.dirname(path) orelse ""; const dir_path = std.fs.path.dirname(path) orelse "";
const import_path = try std.fs.path.resolve(store.allocator, &[_][]const u8 { const import_path = try std.fs.path.resolve(allocator, &[_][]const u8 {
dir_path, import_str dir_path, import_str
}); });
defer store.allocator.free(import_path); defer allocator.free(import_path);
break :b (try URI.fromPath(store.allocator, import_path)); break :b (try URI.fromPath(allocator, import_path));
}; };
} }
@ -257,9 +256,15 @@ pub const AnalysisContext = struct {
tree: *std.zig.ast.Tree, tree: *std.zig.ast.Tree,
scope_nodes: []*std.zig.ast.Node, scope_nodes: []*std.zig.ast.Node,
fn refreshScopeNodes(self: *AnalysisContext) !void {
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&self.arena.allocator);
try analysis.addChildrenNodes(&scope_nodes, self.tree, &self.tree.root_node.base);
self.scope_nodes = scope_nodes.items;
}
pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node { pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node {
const allocator = self.store.allocator; 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.store.allocator, self.handle.*, import_str)) orelse return null;
std.debug.warn("Import final URI: {}\n", .{final_uri}); std.debug.warn("Import final URI: {}\n", .{final_uri});
var consumed_final_uri = false; var consumed_final_uri = false;
@ -273,6 +278,7 @@ pub const AnalysisContext = struct {
self.tree.deinit(); self.tree.deinit();
self.tree = try self.handle.tree(allocator); self.tree = try self.handle.tree(allocator);
try self.refreshScopeNodes();
return &self.tree.root_node.base; return &self.tree.root_node.base;
} }
} }
@ -286,6 +292,7 @@ pub const AnalysisContext = struct {
self.tree.deinit(); self.tree.deinit();
self.tree = try self.handle.tree(allocator); self.tree = try self.handle.tree(allocator);
try self.refreshScopeNodes();
return &self.tree.root_node.base; return &self.tree.root_node.base;
} }
@ -316,13 +323,16 @@ pub const AnalysisContext = struct {
// Swap handles and get new tree. // Swap handles and get new tree.
// This takes ownership of the passed uri and text. // This takes ownership of the passed uri and text.
self.handle = try newDocument(self.store, try std.mem.dupe(allocator, u8, final_uri), file_contents); 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);
} }
// Free old tree, add new one if it exists. // Free old tree, add new one if it exists.
// If we return null, no one should access the tree. // If we return null, no one should access the tree.
self.tree.deinit(); self.tree.deinit();
self.tree = try self.handle.tree(allocator); self.tree = try self.handle.tree(allocator);
try self.refreshScopeNodes();
return &self.tree.root_node.base; return &self.tree.root_node.base;
} }
@ -333,12 +343,16 @@ pub const AnalysisContext = struct {
pub fn analysisContext(self: *DocumentStore, handle: *Handle, arena: *std.heap.ArenaAllocator, position: types.Position) !AnalysisContext { pub fn analysisContext(self: *DocumentStore, handle: *Handle, arena: *std.heap.ArenaAllocator, position: types.Position) !AnalysisContext {
const tree = try handle.tree(self.allocator); const tree = try handle.tree(self.allocator);
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
try analysis.declsFromIndex(&scope_nodes, tree, try handle.document.positionToIndex(position));
return AnalysisContext{ return AnalysisContext{
.store = self, .store = self,
.handle = handle, .handle = handle,
.arena = arena, .arena = arena,
.tree = tree, .tree = tree,
.scope_nodes = try analysis.declsFromIndex(&arena.allocator, tree, try handle.document.positionToIndex(position)) .scope_nodes = scope_nodes.items,
}; };
} }

View File

@ -11,7 +11,7 @@ const analysis = @import("analysis.zig");
// Code is largely based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig // Code is largely based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig
var stdout: std.fs.File.OutStream = undefined; var stdout: std.io.BufferedOutStream(4096, std.fs.File.OutStream) = undefined;
var allocator: *std.mem.Allocator = undefined; var allocator: *std.mem.Allocator = undefined;
var document_store: DocumentStore = undefined; var document_store: DocumentStore = undefined;
@ -46,8 +46,11 @@ fn send(reqOrRes: var) !void {
var mem_buffer: [1024 * 128]u8 = undefined; var mem_buffer: [1024 * 128]u8 = undefined;
var fbs = std.io.fixedBufferStream(&mem_buffer); var fbs = std.io.fixedBufferStream(&mem_buffer);
try std.json.stringify(reqOrRes, std.json.StringifyOptions{}, fbs.outStream()); try std.json.stringify(reqOrRes, std.json.StringifyOptions{}, fbs.outStream());
try stdout.print("Content-Length: {}\r\n\r\n", .{fbs.pos});
try stdout.writeAll(fbs.getWritten()); const stdout_stream = stdout.outStream();
try stdout_stream.print("Content-Length: {}\r\n\r\n", .{fbs.pos});
try stdout_stream.writeAll(fbs.getWritten());
try stdout.flush();
} }
fn log(comptime fmt: []const u8, args: var) !void { fn log(comptime fmt: []const u8, args: var) !void {
@ -82,8 +85,11 @@ fn respondGeneric(id: i64, response: []const u8) !void {
// Numbers of character that will be printed from this string: len - 3 brackets // Numbers of character that will be printed from this string: len - 3 brackets
// 1 from the beginning (escaped) and the 2 from the arg {} // 1 from the beginning (escaped) and the 2 from the arg {}
const json_fmt = "{{\"jsonrpc\":\"2.0\",\"id\":{}"; const json_fmt = "{{\"jsonrpc\":\"2.0\",\"id\":{}";
try stdout.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_digits + json_fmt.len - 3, id });
try stdout.writeAll(response); const stdout_stream = stdout.outStream();
try stdout_stream.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_digits + json_fmt.len - 3, id });
try stdout_stream.writeAll(response);
try stdout.flush();
} }
// TODO: Is this correct or can we get a better end? // TODO: Is this correct or can we get a better end?
@ -280,9 +286,9 @@ fn completeGlobal(id: i64, pos_index: usize, handle: DocumentStore.Handle, confi
// Deallocate all temporary data. // Deallocate all temporary data.
defer arena.deinit(); defer arena.deinit();
// var decls = tree.root_node.decls.iterator(0); var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
var decls = try analysis.declsFromIndex(&arena.allocator, tree, pos_index); try analysis.declsFromIndex(&decl_nodes, tree, pos_index);
for (decls) |decl_ptr| { for (decl_nodes.items) |decl_ptr| {
var decl = decl_ptr.*; var decl = decl_ptr.*;
try nodeToCompletion(&completions, tree, decl_ptr, config); try nodeToCompletion(&completions, tree, decl_ptr, config);
} }
@ -310,7 +316,6 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
const 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 tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]);
// var decls = try analysis.declsFromIndex(&arena.allocator, analysis_ctx.tree, try handle.document.positionToIndex(position));
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| { if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| {
try nodeToCompletion(&completions, analysis_ctx.tree, node, config); try nodeToCompletion(&completions, analysis_ctx.tree, node, config);
} }
@ -609,19 +614,11 @@ pub fn main() anyerror!void {
allocator = &debug_alloc_state.allocator; allocator = &debug_alloc_state.allocator;
} }
// Init buffer for stdin read
var buffer = std.ArrayList(u8).init(allocator);
defer buffer.deinit();
try buffer.resize(4096);
// Init global vars // Init global vars
var buffered_stdin = std.io.bufferedInStream(std.io.getStdIn().inStream()); var buffered_stdin = std.io.bufferedInStream(std.io.getStdIn().inStream());
const in_stream = buffered_stdin.inStream(); const in_stream = buffered_stdin.inStream();
stdout = std.io.getStdOut().outStream(); stdout = std.io.bufferedOutStream(std.io.getStdOut().outStream());
// Read the configuration, if any. // Read the configuration, if any.
const config_parse_options = std.json.ParseOptions{ .allocator = allocator }; const config_parse_options = std.json.ParseOptions{ .allocator = allocator };