local var completion
This commit is contained in:
parent
afc85fa964
commit
220d668929
122
src/analysis.zig
122
src/analysis.zig
@ -149,9 +149,13 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
|
|||||||
pub fn getVariableSignature(tree: *ast.Tree, var_decl: *ast.Node.VarDecl) []const u8 {
|
pub fn getVariableSignature(tree: *ast.Tree, var_decl: *ast.Node.VarDecl) []const u8 {
|
||||||
const start = tree.tokens.at(var_decl.firstToken()).start;
|
const start = tree.tokens.at(var_decl.firstToken()).start;
|
||||||
const end = tree.tokens.at(var_decl.semicolon_token).start;
|
const end = tree.tokens.at(var_decl.semicolon_token).start;
|
||||||
// var end =
|
return tree.source[start..end];
|
||||||
// if (var_decl.init_n) |body| tree.tokens.at(body.firstToken()).start
|
}
|
||||||
// else tree.tokens.at(var_decl.name_token).end;
|
|
||||||
|
/// Gets a param signature
|
||||||
|
pub fn getParamSignature(tree: *ast.Tree, param: *ast.Node.ParamDecl) []const u8 {
|
||||||
|
const start = tree.tokens.at(param.firstToken()).start;
|
||||||
|
const end = tree.tokens.at(param.lastToken()).end;
|
||||||
return tree.source[start..end];
|
return tree.source[start..end];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,13 +195,51 @@ pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the child of slice
|
||||||
|
pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*ast.Node {
|
||||||
|
// var index: usize = 0;
|
||||||
|
for (nodes) |child| {
|
||||||
|
switch (child.id) {
|
||||||
|
.VarDecl => {
|
||||||
|
const vari = child.cast(ast.Node.VarDecl).?;
|
||||||
|
if (std.mem.eql(u8, tree.tokenSlice(vari.name_token), name)) return child;
|
||||||
|
},
|
||||||
|
.ParamDecl => {
|
||||||
|
const decl = child.cast(ast.Node.ParamDecl).?;
|
||||||
|
if (decl.name_token != null and std.mem.eql(u8, tree.tokenSlice(decl.name_token.?), name)) return child;
|
||||||
|
},
|
||||||
|
.FnProto => {
|
||||||
|
const func = child.cast(ast.Node.FnProto).?;
|
||||||
|
if (func.name_token != null and std.mem.eql(u8, tree.tokenSlice(func.name_token.?), name)) return child;
|
||||||
|
},
|
||||||
|
.ContainerField => {
|
||||||
|
const field = child.cast(ast.Node.ContainerField).?;
|
||||||
|
if (std.mem.eql(u8, tree.tokenSlice(field.name_token), name)) return child;
|
||||||
|
},
|
||||||
|
else => {}
|
||||||
|
}
|
||||||
|
// index += 1;
|
||||||
|
}
|
||||||
|
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).?;
|
||||||
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
|
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
|
||||||
},
|
},
|
||||||
|
.ParamDecl => {
|
||||||
|
const decl = node.cast(ast.Node.ParamDecl).?;
|
||||||
|
switch (decl.param_type) {
|
||||||
|
.var_type, .type_expr => |var_type| {
|
||||||
|
return resolveTypeOfNode(analysis_ctx, var_type) orelse null;
|
||||||
|
},
|
||||||
|
else => {}
|
||||||
|
}
|
||||||
|
},
|
||||||
.FnProto => {
|
.FnProto => {
|
||||||
const func = node.cast(ast.Node.FnProto).?;
|
const func = node.cast(ast.Node.FnProto).?;
|
||||||
switch (func.return_type) {
|
switch (func.return_type) {
|
||||||
@ -206,7 +248,9 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
.Identifier => {
|
.Identifier => {
|
||||||
if (getChild(analysis_ctx.tree, &analysis_ctx.tree.root_node.base, analysis_ctx.tree.getNodeSource(node))) |child| {
|
// 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| {
|
||||||
|
// std.debug.warn("CHILD {}\n", .{child});
|
||||||
return resolveTypeOfNode(analysis_ctx, child);
|
return resolveTypeOfNode(analysis_ctx, child);
|
||||||
} else return null;
|
} else return null;
|
||||||
},
|
},
|
||||||
@ -331,7 +375,7 @@ pub fn getFieldAccessTypeNode(analysis_ctx: *AnalysisContext, tokenizer: *std.zi
|
|||||||
.Identifier => {
|
.Identifier => {
|
||||||
// var root = current_node.cast(ast.Node.Root).?;
|
// var root = current_node.cast(ast.Node.Root).?;
|
||||||
// current_node.
|
// current_node.
|
||||||
if (getChild(analysis_ctx.tree, current_node, 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;
|
||||||
} else return null;
|
} else return null;
|
||||||
@ -406,3 +450,71 @@ 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 {
|
||||||
|
switch (node.id) {
|
||||||
|
.FnProto => {
|
||||||
|
const func = node.cast(ast.Node.FnProto).?;
|
||||||
|
|
||||||
|
var param_index: usize = 0;
|
||||||
|
while (param_index < func.params.len) : (param_index += 1)
|
||||||
|
try declsFromIndexInternal(allocator, tree, func.params.at(param_index).*, nodes);
|
||||||
|
|
||||||
|
if (func.body_node) |body_node|
|
||||||
|
try declsFromIndexInternal(allocator, tree, body_node, nodes);
|
||||||
|
},
|
||||||
|
.Block => {
|
||||||
|
var index: usize = 0;
|
||||||
|
|
||||||
|
while (node.iterate(index)) |inode| {
|
||||||
|
try declsFromIndexInternal(allocator, tree, inode, nodes);
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.VarDecl => {
|
||||||
|
try nodes.append(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 {
|
||||||
|
var nodes = std.ArrayList(*ast.Node).init(allocator);
|
||||||
|
|
||||||
|
var index: usize = 0;
|
||||||
|
while (node.iterate(index)) |child_node| {
|
||||||
|
try nodes.append(child_node);
|
||||||
|
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes.items;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 nodes = std.ArrayList(*ast.Node).init(allocator);
|
||||||
|
|
||||||
|
try nodes.appendSlice(try getCompletionsFromNode(allocator, tree, node));
|
||||||
|
|
||||||
|
while (node.iterate(iindex)) |inode| {
|
||||||
|
if (tree.tokens.at(inode.firstToken()).start < index and index < tree.tokens.at(inode.lastToken()).start) {
|
||||||
|
try declsFromIndexInternal(allocator, tree, inode, &nodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
iindex += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tree.tokens.at(node.firstToken()).start < index and index < tree.tokens.at(node.lastToken()).start) {
|
||||||
|
return nodes.items;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes.items;
|
||||||
|
}
|
||||||
|
@ -260,6 +260,7 @@ pub const AnalysisContext = struct {
|
|||||||
// not for the tree allocations.
|
// not for the tree allocations.
|
||||||
arena: *std.heap.ArenaAllocator,
|
arena: *std.heap.ArenaAllocator,
|
||||||
tree: *std.zig.ast.Tree,
|
tree: *std.zig.ast.Tree,
|
||||||
|
scope_nodes: []*std.zig.ast.Node,
|
||||||
|
|
||||||
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;
|
||||||
@ -335,12 +336,14 @@ pub const AnalysisContext = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn analysisContext(self: *DocumentStore, handle: *Handle, arena: *std.heap.ArenaAllocator) !AnalysisContext {
|
pub fn analysisContext(self: *DocumentStore, handle: *Handle, arena: *std.heap.ArenaAllocator, position: types.Position) !AnalysisContext {
|
||||||
|
const tree = try handle.tree(self.allocator);
|
||||||
return AnalysisContext{
|
return AnalysisContext{
|
||||||
.store = self,
|
.store = self,
|
||||||
.handle = handle,
|
.handle = handle,
|
||||||
.arena = arena,
|
.arena = arena,
|
||||||
.tree = try handle.tree(self.allocator),
|
.tree = tree,
|
||||||
|
.scope_nodes = try analysis.declsFromIndex(&arena.allocator, tree, try handle.document.positionToIndex(position))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
src/main.zig
24
src/main.zig
@ -223,6 +223,16 @@ fn nodeToCompletion(alloc: *std.mem.Allocator, tree: *std.zig.ast.Tree, decl: *s
|
|||||||
.detail = analysis.getVariableSignature(tree, var_decl),
|
.detail = analysis.getVariableSignature(tree, var_decl),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
.ParamDecl => {
|
||||||
|
const param = decl.cast(std.zig.ast.Node.ParamDecl).?;
|
||||||
|
if (param.name_token) |name_token|
|
||||||
|
return types.CompletionItem{
|
||||||
|
.label = tree.tokenSlice(name_token),
|
||||||
|
.kind = .Variable,
|
||||||
|
.documentation = doc,
|
||||||
|
.detail = analysis.getParamSignature(tree, param),
|
||||||
|
};
|
||||||
|
},
|
||||||
else => if (analysis.nodeToString(tree, decl)) |string| {
|
else => if (analysis.nodeToString(tree, decl)) |string| {
|
||||||
return types.CompletionItem{
|
return types.CompletionItem{
|
||||||
.label = string,
|
.label = string,
|
||||||
@ -235,7 +245,7 @@ fn nodeToCompletion(alloc: *std.mem.Allocator, tree: *std.zig.ast.Tree, decl: *s
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn completeGlobal(id: i64, handle: DocumentStore.Handle, config: Config) !void {
|
fn completeGlobal(id: i64, pos_index: usize, handle: DocumentStore.Handle, config: Config) !void {
|
||||||
var tree = try handle.tree(allocator);
|
var tree = try handle.tree(allocator);
|
||||||
defer tree.deinit();
|
defer tree.deinit();
|
||||||
|
|
||||||
@ -245,10 +255,11 @@ fn completeGlobal(id: i64, handle: DocumentStore.Handle, config: Config) !void {
|
|||||||
// Deallocate all temporary data.
|
// Deallocate all temporary data.
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
|
|
||||||
var decls = tree.root_node.decls.iterator(0);
|
// var decls = tree.root_node.decls.iterator(0);
|
||||||
while (decls.next()) |decl_ptr| {
|
var decls = try analysis.declsFromIndex(&arena.allocator, tree, pos_index);
|
||||||
|
for (decls) |decl_ptr| {
|
||||||
var decl = decl_ptr.*;
|
var decl = decl_ptr.*;
|
||||||
if (try nodeToCompletion(&arena.allocator, tree, decl, config)) |completion| {
|
if (try nodeToCompletion(&arena.allocator, tree, decl_ptr, config)) |completion| {
|
||||||
try completions.append(completion);
|
try completions.append(completion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -268,7 +279,7 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
|
|||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
|
|
||||||
var analysis_ctx = try document_store.analysisContext(handle, &arena);
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, position);
|
||||||
defer analysis_ctx.deinit();
|
defer analysis_ctx.deinit();
|
||||||
|
|
||||||
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
||||||
@ -276,6 +287,7 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
|
|||||||
var line = try handle.document.getLine(@intCast(usize, position.line));
|
var 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| {
|
||||||
var index: usize = 0;
|
var index: usize = 0;
|
||||||
while (node.iterate(index)) |child_node| {
|
while (node.iterate(index)) |child_node| {
|
||||||
@ -537,7 +549,7 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
.var_access, .empty => try completeGlobal(id, handle.*, config),
|
.var_access, .empty => try completeGlobal(id, pos_index, handle.*, config),
|
||||||
.field_access => |start_idx| try completeFieldAccess(id, handle, pos, start_idx, config),
|
.field_access => |start_idx| try completeFieldAccess(id, handle, pos, start_idx, config),
|
||||||
else => try respondGeneric(id, no_completions_response),
|
else => try respondGeneric(id, no_completions_response),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user