Merge pull request #73 from Vexu/str

Add goto definition support for import strings
This commit is contained in:
Auguste Rame 2020-05-22 13:49:37 -04:00 committed by GitHub
commit 39bbf9c8bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 18 deletions

View File

@ -262,13 +262,14 @@ fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto)
// If this is a type function and it only contains a single return statement that returns
// a container declaration, we will return that declaration.
const ret = findReturnStatement(fn_decl.body_node.?) orelse return null;
if (ret.rhs) |rhs| if (resolveTypeOfNode(analysis_ctx, rhs)) |res_rhs| switch(res_rhs.id) {
.ContainerDecl => {
analysis_ctx.onContainer(res_rhs.cast(ast.Node.ContainerDecl).?) catch return null;
return res_rhs;
},
else => return null,
};
if (ret.rhs) |rhs|
if (resolveTypeOfNode(analysis_ctx, rhs)) |res_rhs| switch (res_rhs.id) {
.ContainerDecl => {
analysis_ctx.onContainer(res_rhs.cast(ast.Node.ContainerDecl).?) catch return null;
return res_rhs;
},
else => return null,
};
return null;
}
@ -557,3 +558,34 @@ pub fn declsFromIndex(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, index:
}
}
}
fn nodeContainsSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
const first_token = tree.tokens.at(node.firstToken());
const last_token = tree.tokens.at(node.lastToken());
return source_index >= first_token.start and source_index <= last_token.end;
}
pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 {
var node = &tree.root_node.base;
var index: usize = 0;
while (node.iterate(index)) |child| {
if (!nodeContainsSourceIndex(tree, child, source_index)) {
index += 1;
continue;
}
if (child.cast(ast.Node.BuiltinCall)) |builtin_call| blk: {
const call_name = tree.tokenSlice(builtin_call.builtin_token);
if (!std.mem.eql(u8, call_name, "@import")) break :blk;
if (builtin_call.params.len != 1) break :blk;
const import_param = builtin_call.params.at(0).*;
const import_str_node = import_param.cast(ast.Node.StringLiteral) orelse break :blk;
const import_str = tree.tokenSlice(import_str_node.token);
return import_str[1 .. import_str.len - 1];
}
node = child;
index = 0;
}
return null;
}

View File

@ -216,19 +216,23 @@ pub fn applyChanges(
try self.removeOldImports(handle, zig_lib_path);
}
fn uriFromImportStr(
pub fn uriFromImportStr(
store: *DocumentStore,
allocator: *std.mem.Allocator,
handle: Handle,
import_str: []const u8,
std_uri: ?[]const u8,
) !?[]const u8 {
return if (std.mem.eql(u8, import_str, "std"))
if (std_uri) |uri| try std.mem.dupe(allocator, u8, uri) else {
if (std.mem.eql(u8, import_str, "std")) {
if (std_uri) |uri| return try std.mem.dupe(allocator, u8, uri) else {
std.debug.warn("Cannot resolve std library import, path is null.\n", .{});
return null;
}
else b: {
} else if (std.mem.eql(u8, import_str, "builtin")) {
return null; // TODO find the correct zig-cache folder
} else if (!std.mem.endsWith(u8, import_str, ".zig")) {
return null; // TODO find packages based on build.zig
} else {
// Find relative uri
const path = try URI.parse(allocator, handle.uri());
defer allocator.free(path);
@ -240,8 +244,8 @@ fn uriFromImportStr(
defer allocator.free(import_path);
break :b (try URI.fromPath(allocator, import_path));
};
return try URI.fromPath(allocator, import_path);
}
}
pub const AnalysisContext = struct {
@ -371,7 +375,7 @@ pub const AnalysisContext = struct {
}
};
fn stdUriFromLibPath(allocator: *std.mem.Allocator, zig_lib_path: ?[]const u8) !?[]const u8 {
pub fn stdUriFromLibPath(allocator: *std.mem.Allocator, zig_lib_path: ?[]const u8) !?[]const u8 {
if (zig_lib_path) |zpath| {
const std_path = std.fs.path.resolve(allocator, &[_][]const u8{
zpath, "./std/std.zig",

View File

@ -365,10 +365,36 @@ fn gotoDefinitionFieldAccess(
try respondGeneric(id, null_result_response);
}
fn completeGlobal(id: i64, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
fn gotoDefinitionString(id: i64, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
var tree = try handle.tree(allocator);
defer tree.deinit();
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const import_str = analysis.getImportStr(tree, pos_index) orelse return try respondGeneric(id, null_result_response);
const uri = (try document_store.uriFromImportStr(
&arena.allocator,
handle.*,
import_str,
try DocumentStore.stdUriFromLibPath(&arena.allocator, config.zig_lib_path),
)) orelse return try respondGeneric(id, null_result_response);
try send(types.Response{
.id = .{ .Integer = id },
.result = .{
.Location = .{
.uri = uri,
.range = .{
.start = .{ .line = 0, .character = 0 },
.end = .{ .line = 0, .character = 0 },
},
},
},
});
}
fn completeGlobal(id: i64, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
// We use a local arena allocator to deallocate all temporary data without iterating
var arena = std.heap.ArenaAllocator.init(allocator);
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
@ -382,7 +408,7 @@ fn completeGlobal(id: i64, pos_index: usize, handle: *DocumentStore.Handle, conf
defer analysis_ctx.deinit();
var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
try analysis.declsFromIndex(&decl_nodes, tree, pos_index);
try analysis.declsFromIndex(&decl_nodes, analysis_ctx.tree, pos_index);
for (decl_nodes.items) |decl_ptr| {
var decl = decl_ptr.*;
try nodeToCompletion(&completions, &analysis_ctx, decl_ptr, config);
@ -805,6 +831,7 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v
start_idx,
configFromUriOr(uri, config),
),
.string_literal => try gotoDefinitionString(id, pos_index, handle, config),
else => try respondGeneric(id, null_result_response),
}
}