Go to declaration will not resolve aliases (go to definition still does)

This commit is contained in:
Alexandros Naskos 2020-06-15 12:37:42 +03:00
parent f6e3109ff3
commit 2da58c5068

View File

@ -376,14 +376,16 @@ fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []cons
return text[start_idx + 1 .. end_idx]; return text[start_idx + 1 .. end_idx];
} }
fn gotoDefinitionSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle) !void { fn gotoDefinitionSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle, resolve_alias: bool) !void {
var handle = decl_handle.handle; var handle = decl_handle.handle;
const location = switch (decl_handle.decl.*) { const location = switch (decl_handle.decl.*) {
.ast_node => |node| block: { .ast_node => |node| block: {
if (try analysis.resolveVarDeclAlias(&document_store, arena, .{ .node = node, .handle = handle })) |result| { if (resolve_alias) {
handle = result.handle; if (try analysis.resolveVarDeclAlias(&document_store, arena, .{ .node = node, .handle = handle })) |result| {
break :block result.location(); handle = result.handle;
break :block result.location();
}
} }
const name_token = analysis.getDeclNameToken(handle.tree, node) orelse const name_token = analysis.getDeclNameToken(handle.tree, node) orelse
@ -502,15 +504,15 @@ fn gotoDefinitionLabel(id: types.RequestId, pos_index: usize, handle: *DocumentS
defer arena.deinit(); defer arena.deinit();
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response); const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
return try gotoDefinitionSymbol(id, &arena, decl); return try gotoDefinitionSymbol(id, &arena, decl, false);
} }
fn gotoDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void { fn gotoDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config, resolve_alias: bool) !void {
var arena = std.heap.ArenaAllocator.init(allocator); var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit(); defer arena.deinit();
const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response); const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
return try gotoDefinitionSymbol(id, &arena, decl); return try gotoDefinitionSymbol(id, &arena, decl, resolve_alias);
} }
fn hoverDefinitionLabel(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void { fn hoverDefinitionLabel(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
@ -555,12 +557,13 @@ fn gotoDefinitionFieldAccess(
position: types.Position, position: types.Position,
range: analysis.SourceRange, range: analysis.SourceRange,
config: Config, config: Config,
resolve_alias: bool,
) !void { ) !void {
var arena = std.heap.ArenaAllocator.init(allocator); var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit(); defer arena.deinit();
const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response); const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
return try gotoDefinitionSymbol(id, &arena, decl); return try gotoDefinitionSymbol(id, &arena, decl, resolve_alias);
} }
fn hoverDefinitionFieldAccess( fn hoverDefinitionFieldAccess(
@ -1092,12 +1095,13 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v
.character = position.getValue("character").?.Integer - 1, .character = position.getValue("character").?.Integer - 1,
}; };
if (pos.character >= 0) { if (pos.character >= 0) {
const resolve_alias = !std.mem.eql(u8, method, "textDocument/declaration");
const pos_index = try handle.document.positionToIndex(pos); const pos_index = try handle.document.positionToIndex(pos);
const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos); const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
switch (pos_context) { switch (pos_context) {
.var_access => try gotoDefinitionGlobal(id, pos_index, handle, configFromUriOr(uri, config)), .var_access => try gotoDefinitionGlobal(id, pos_index, handle, configFromUriOr(uri, config), resolve_alias),
.field_access => |range| try gotoDefinitionFieldAccess(id, handle, pos, range, configFromUriOr(uri, config)), .field_access => |range| try gotoDefinitionFieldAccess(id, handle, pos, range, configFromUriOr(uri, config), resolve_alias),
.string_literal => try gotoDefinitionString(id, pos_index, handle, config), .string_literal => try gotoDefinitionString(id, pos_index, handle, config),
.label => try gotoDefinitionLabel(id, pos_index, handle, configFromUriOr(uri, config)), .label => try gotoDefinitionLabel(id, pos_index, handle, configFromUriOr(uri, config)),
else => try respondGeneric(id, null_result_response), else => try respondGeneric(id, null_result_response),