super basic completion, tons of things to iron out

This commit is contained in:
SuperAuguste 2020-05-13 10:10:20 -04:00
parent a3067f88b1
commit 9ae912efdd
2 changed files with 56 additions and 18 deletions

View File

@ -298,27 +298,53 @@ fn completeGlobal(id: i64, document: *types.TextDocument, config: Config) !void
}); });
} }
fn completeFieldAccess(id: i64, document: *types.TextDocument, index: usize, config: Config) !void { fn completeFieldAccess(id: i64, document: *types.TextDocument, position: types.Position, config: Config) !void {
var tree = try std.zig.parse(allocator, sane_text); if (document.sane_text) |sane_text| {
defer tree.deinit(); var tree = try std.zig.parse(allocator, sane_text);
defer tree.deinit();
// We use a local arena allocator to deallocate all temporary data without iterating // We use a local arena allocator to deallocate all temporary data without iterating
var arena = std.heap.ArenaAllocator.init(allocator); var arena = std.heap.ArenaAllocator.init(allocator);
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator); var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
// Deallocate all temporary data. // Deallocate all temporary data.
defer arena.deinit(); defer arena.deinit();
try log("{}", .{}); var line = try document.getLine(@intCast(usize, position.line));
try log("{}", .{line});
var tokenizer = std.zig.Tokenizer.init(line);
try send(types.Response{ if (analysis.getNodeFromTokens(tree, &tree.root_node.base, &tokenizer)) |node| {
.id = .{.Integer = id}, var index: usize = 0;
.result = .{ while (node.iterate(index)) |child_node| {
.CompletionList = .{ try completions.append(.{
.isIncomplete = false, .label = analysis.nodeToString(tree, child_node),
.items = completions.items, .kind = .Variable,
});
index += 1;
}
}
try send(types.Response{
.id = .{.Integer = id},
.result = .{
.CompletionList = .{
.isIncomplete = false,
.items = completions.items,
},
}, },
}, });
}); } else {
return try send(types.Response{
.id = .{.Integer = id},
.result = .{
.CompletionList = .{
.isIncomplete = false,
.items = &[_]types.CompletionItem{},
},
},
});
}
} }
// Compute builtin completions at comptime. // Compute builtin completions at comptime.
@ -588,7 +614,7 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v
} else if (pos_context == .var_access or pos_context == .empty) { } else if (pos_context == .var_access or pos_context == .empty) {
try completeGlobal(id, document, config); try completeGlobal(id, document, config);
} else if (pos_context == .field_access) { } else if (pos_context == .field_access) {
try completeFieldAccess(id, document, pos_index, config); try completeFieldAccess(id, document, pos, config);
} else { } else {
try respondGeneric(id, no_completions_response); try respondGeneric(id, no_completions_response);
} }

View File

@ -158,6 +158,18 @@ pub const TextDocument = struct {
return @intCast(usize, index); return @intCast(usize, index);
} }
pub fn getLine(self: TextDocument, target_line: usize) ![]const u8 {
var split_iterator = std.mem.split(self.text, "\n");
var line: i64 = 0;
while (line < target_line) : (line += 1) {
_ = split_iterator.next() orelse return error.InvalidParams;
}
if (split_iterator.next()) |next| {
return next;
} else return error.InvalidParams;
}
}; };
pub const TextEdit = struct { pub const TextEdit = struct {