Use LSP offsets in rename.zig

This commit is contained in:
Alexandros Naskos 2020-07-05 01:40:18 +03:00
parent aac8fc7b0c
commit cbfb316763
2 changed files with 77 additions and 75 deletions

View File

@ -699,7 +699,7 @@ fn renameDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId,
var workspace_edit = types.WorkspaceEdit{ var workspace_edit = types.WorkspaceEdit{
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator), .changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
}; };
try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?); try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?, offset_encoding);
try send(arena, types.Response{ try send(arena, types.Response{
.id = id, .id = id,
.result = .{ .WorkspaceEdit = workspace_edit }, .result = .{ .WorkspaceEdit = workspace_edit },
@ -720,7 +720,7 @@ fn renameDefinitionFieldAccess(
var workspace_edit = types.WorkspaceEdit{ var workspace_edit = types.WorkspaceEdit{
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator), .changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
}; };
try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?); try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?, offset_encoding);
try send(arena, types.Response{ try send(arena, types.Response{
.id = id, .id = id,
.result = .{ .WorkspaceEdit = workspace_edit }, .result = .{ .WorkspaceEdit = workspace_edit },
@ -733,7 +733,7 @@ fn renameDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, h
var workspace_edit = types.WorkspaceEdit{ var workspace_edit = types.WorkspaceEdit{
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator), .changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
}; };
try rename.renameLabel(arena, decl, new_name, &workspace_edit.changes.?); try rename.renameLabel(arena, decl, new_name, &workspace_edit.changes.?, offset_encoding);
try send(arena, types.Response{ try send(arena, types.Response{
.id = id, .id = id,
.result = .{ .WorkspaceEdit = workspace_edit }, .result = .{ .WorkspaceEdit = workspace_edit },

View File

@ -2,12 +2,12 @@ const std = @import("std");
const DocumentStore = @import("document_store.zig"); const DocumentStore = @import("document_store.zig");
const analysis = @import("analysis.zig"); const analysis = @import("analysis.zig");
const types = @import("types.zig"); const types = @import("types.zig");
const offsets = @import("offsets.zig");
const ast = std.zig.ast; const ast = std.zig.ast;
fn renameToken(handle: *DocumentStore.Handle, tok: ast.TokenIndex, new_name: []const u8, edits: *std.ArrayList(types.TextEdit)) !void { fn renameToken(handle: *DocumentStore.Handle, tok: ast.TokenIndex, new_name: []const u8, edits: *std.ArrayList(types.TextEdit), encoding: offsets.Encoding) !void {
// const handle.tree.tokenLocation(start_index: usize, token: Token.Loc) const loc = offsets.tokenRelativeLocation(handle.tree, 0, tok, encoding) catch return;
const loc = handle.tree.tokenLocation(0, tok);
(try edits.addOne()).* = .{ (try edits.addOne()).* = .{
.range = .{ .range = .{
.start = .{ .start = .{
@ -16,14 +16,14 @@ fn renameToken(handle: *DocumentStore.Handle, tok: ast.TokenIndex, new_name: []c
}, },
.end = .{ .end = .{
.line = @intCast(types.Integer, loc.line), .line = @intCast(types.Integer, loc.line),
.character = @intCast(types.Integer, loc.column + handle.tree.token_locs[tok].end - handle.tree.token_locs[tok].start), .character = @intCast(types.Integer, loc.column + offsets.tokenLength(handle.tree, tok, encoding)),
}, },
}, },
.newText = new_name, .newText = new_name,
}; };
} }
pub fn renameLabel(arena: *std.heap.ArenaAllocator, decl: analysis.DeclWithHandle, new_name: []const u8, edits: *std.StringHashMap([]types.TextEdit)) !void { pub fn renameLabel(arena: *std.heap.ArenaAllocator, decl: analysis.DeclWithHandle, new_name: []const u8, edits: *std.StringHashMap([]types.TextEdit), encoding: offsets.Encoding) !void {
std.debug.assert(decl.decl.* == .label_decl); std.debug.assert(decl.decl.* == .label_decl);
const handle = decl.handle; const handle = decl.handle;
@ -35,7 +35,7 @@ pub fn renameLabel(arena: *std.heap.ArenaAllocator, decl: analysis.DeclWithHandl
const last_tok = decl.decl.label_decl.lastToken(); const last_tok = decl.decl.label_decl.lastToken();
// The first token is always going to be the label // The first token is always going to be the label
try renameToken(handle, first_tok, new_name, &text_edits); try renameToken(handle, first_tok, new_name, &text_edits, encoding);
var curr_tok = first_tok + 1; var curr_tok = first_tok + 1;
while (curr_tok < last_tok - 2) : (curr_tok += 1) { while (curr_tok < last_tok - 2) : (curr_tok += 1) {
@ -44,7 +44,7 @@ pub fn renameLabel(arena: *std.heap.ArenaAllocator, decl: analysis.DeclWithHandl
handle.tree.token_ids[curr_tok + 2] == .Identifier) handle.tree.token_ids[curr_tok + 2] == .Identifier)
{ {
if (std.mem.eql(u8, handle.tree.tokenSlice(curr_tok + 2), handle.tree.tokenSlice(first_tok))) { if (std.mem.eql(u8, handle.tree.tokenSlice(curr_tok + 2), handle.tree.tokenSlice(first_tok))) {
try renameToken(handle, curr_tok + 2, new_name, &text_edits); try renameToken(handle, curr_tok + 2, new_name, &text_edits, encoding);
} }
} }
} }
@ -59,6 +59,7 @@ fn renameSymbolInternal(
decl: analysis.DeclWithHandle, decl: analysis.DeclWithHandle,
new_name: []const u8, new_name: []const u8,
edits: *std.ArrayList(types.TextEdit), edits: *std.ArrayList(types.TextEdit),
encoding: offsets.Encoding,
) error{OutOfMemory}!void { ) error{OutOfMemory}!void {
const node = node_handle.node; const node = node_handle.node;
const handle = node_handle.handle; const handle = node_handle.handle;
@ -67,35 +68,35 @@ fn renameSymbolInternal(
.ContainerDecl, .Root, .Block => { .ContainerDecl, .Root, .Block => {
var idx: usize = 0; var idx: usize = 0;
while (node.iterate(idx)) |child| : (idx += 1) { while (node.iterate(idx)) |child| : (idx += 1) {
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.VarDecl => { .VarDecl => {
const var_decl = node.cast(ast.Node.VarDecl).?; const var_decl = node.cast(ast.Node.VarDecl).?;
if (var_decl.type_node) |type_node| { if (var_decl.type_node) |type_node| {
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits, encoding);
} }
if (var_decl.init_node) |init_node| { if (var_decl.init_node) |init_node| {
try renameSymbolInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.Use => { .Use => {
const use = node.cast(ast.Node.Use).?; const use = node.cast(ast.Node.Use).?;
try renameSymbolInternal(arena, store, .{ .node = use.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = use.expr, .handle = handle }, decl, new_name, edits, encoding);
}, },
.ContainerField => { .ContainerField => {
const field = node.cast(ast.Node.ContainerField).?; const field = node.cast(ast.Node.ContainerField).?;
if (field.type_expr) |type_node| { if (field.type_expr) |type_node| {
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits, encoding);
} }
if (field.value_expr) |init_node| { if (field.value_expr) |init_node| {
try renameSymbolInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.Identifier => { .Identifier => {
if (try analysis.lookupSymbolGlobal(store, arena, handle, handle.tree.getNodeSource(node), handle.tree.token_locs[node.firstToken()].start)) |child| { if (try analysis.lookupSymbolGlobal(store, arena, handle, handle.tree.getNodeSource(node), handle.tree.token_locs[node.firstToken()].start)) |child| {
if (std.meta.eql(decl, child)) { if (std.meta.eql(decl, child)) {
try renameToken(handle, node.firstToken(), new_name, edits); try renameToken(handle, node.firstToken(), new_name, edits, encoding);
} }
} }
}, },
@ -104,90 +105,90 @@ fn renameSymbolInternal(
for (fn_proto.paramsConst()) |param| { for (fn_proto.paramsConst()) |param| {
switch (param.param_type) { switch (param.param_type) {
.type_expr => |type_node| { .type_expr => |type_node| {
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits, encoding);
}, },
else => {}, else => {},
} }
} }
switch (fn_proto.return_type) { switch (fn_proto.return_type) {
.Explicit, .InferErrorSet => |type_node| { .Explicit, .InferErrorSet => |type_node| {
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits, encoding);
}, },
else => {}, else => {},
} }
if (fn_proto.align_expr) |align_expr| { if (fn_proto.align_expr) |align_expr| {
try renameSymbolInternal(arena, store, .{ .node = align_expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = align_expr, .handle = handle }, decl, new_name, edits, encoding);
} }
if (fn_proto.section_expr) |section_expr| { if (fn_proto.section_expr) |section_expr| {
try renameSymbolInternal(arena, store, .{ .node = section_expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = section_expr, .handle = handle }, decl, new_name, edits, encoding);
} }
if (fn_proto.callconv_expr) |callconv_expr| { if (fn_proto.callconv_expr) |callconv_expr| {
try renameSymbolInternal(arena, store, .{ .node = callconv_expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = callconv_expr, .handle = handle }, decl, new_name, edits, encoding);
} }
if (fn_proto.body_node) |body| { if (fn_proto.body_node) |body| {
try renameSymbolInternal(arena, store, .{ .node = body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = body, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.AnyFrameType => { .AnyFrameType => {
const anyframe_type = node.cast(ast.Node.AnyFrameType).?; const anyframe_type = node.cast(ast.Node.AnyFrameType).?;
if (anyframe_type.result) |result| { if (anyframe_type.result) |result| {
try renameSymbolInternal(arena, store, .{ .node = result.return_type, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = result.return_type, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.Defer => { .Defer => {
const defer_node = node.cast(ast.Node.Defer).?; const defer_node = node.cast(ast.Node.Defer).?;
try renameSymbolInternal(arena, store, .{ .node = defer_node.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = defer_node.expr, .handle = handle }, decl, new_name, edits, encoding);
}, },
.Comptime => { .Comptime => {
const comptime_node = node.cast(ast.Node.Comptime).?; const comptime_node = node.cast(ast.Node.Comptime).?;
try renameSymbolInternal(arena, store, .{ .node = comptime_node.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = comptime_node.expr, .handle = handle }, decl, new_name, edits, encoding);
}, },
.Nosuspend => { .Nosuspend => {
const nosuspend_node = node.cast(ast.Node.Nosuspend).?; const nosuspend_node = node.cast(ast.Node.Nosuspend).?;
try renameSymbolInternal(arena, store, .{ .node = nosuspend_node.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = nosuspend_node.expr, .handle = handle }, decl, new_name, edits, encoding);
}, },
.Switch => { .Switch => {
// TODO When renaming a union(enum) field, also rename switch items that refer to it. // TODO When renaming a union(enum) field, also rename switch items that refer to it.
const switch_node = node.cast(ast.Node.Switch).?; const switch_node = node.cast(ast.Node.Switch).?;
try renameSymbolInternal(arena, store, .{ .node = switch_node.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = switch_node.expr, .handle = handle }, decl, new_name, edits, encoding);
for (switch_node.casesConst()) |case| { for (switch_node.casesConst()) |case| {
if (case.*.cast(ast.Node.SwitchCase)) |case_node| { if (case.*.cast(ast.Node.SwitchCase)) |case_node| {
try renameSymbolInternal(arena, store, .{ .node = case_node.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = case_node.expr, .handle = handle }, decl, new_name, edits, encoding);
} }
} }
}, },
.While => { .While => {
const while_node = node.cast(ast.Node.While).?; const while_node = node.cast(ast.Node.While).?;
try renameSymbolInternal(arena, store, .{ .node = while_node.condition, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = while_node.condition, .handle = handle }, decl, new_name, edits, encoding);
if (while_node.continue_expr) |cont_expr| { if (while_node.continue_expr) |cont_expr| {
try renameSymbolInternal(arena, store, .{ .node = cont_expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = cont_expr, .handle = handle }, decl, new_name, edits, encoding);
} }
try renameSymbolInternal(arena, store, .{ .node = while_node.body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = while_node.body, .handle = handle }, decl, new_name, edits, encoding);
if (while_node.@"else") |else_node| { if (while_node.@"else") |else_node| {
try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.For => { .For => {
const for_node = node.cast(ast.Node.For).?; const for_node = node.cast(ast.Node.For).?;
try renameSymbolInternal(arena, store, .{ .node = for_node.array_expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = for_node.array_expr, .handle = handle }, decl, new_name, edits, encoding);
try renameSymbolInternal(arena, store, .{ .node = for_node.body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = for_node.body, .handle = handle }, decl, new_name, edits, encoding);
if (for_node.@"else") |else_node| { if (for_node.@"else") |else_node| {
try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.If => { .If => {
const if_node = node.cast(ast.Node.If).?; const if_node = node.cast(ast.Node.If).?;
try renameSymbolInternal(arena, store, .{ .node = if_node.condition, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = if_node.condition, .handle = handle }, decl, new_name, edits, encoding);
try renameSymbolInternal(arena, store, .{ .node = if_node.body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = if_node.body, .handle = handle }, decl, new_name, edits, encoding);
if (if_node.@"else") |else_node| { if (if_node.@"else") |else_node| {
try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.InfixOp => { .InfixOp => {
const infix_op = node.cast(ast.Node.InfixOp).?; const infix_op = node.cast(ast.Node.InfixOp).?;
switch (infix_op.op) { switch (infix_op.op) {
.Period => { .Period => {
try renameSymbolInternal(arena, store, .{ .node = infix_op.lhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = infix_op.lhs, .handle = handle }, decl, new_name, edits, encoding);
const rhs_str = analysis.nodeToString(handle.tree, infix_op.rhs) orelse return; const rhs_str = analysis.nodeToString(handle.tree, infix_op.rhs) orelse return;
var bound_type_params = analysis.BoundTypeParams.init(&arena.allocator); var bound_type_params = analysis.BoundTypeParams.init(&arena.allocator);
@ -214,13 +215,13 @@ fn renameSymbolInternal(
!left_type.type.is_type_val, !left_type.type.is_type_val,
)) |child| { )) |child| {
if (std.meta.eql(child, decl)) { if (std.meta.eql(child, decl)) {
try renameToken(handle, infix_op.rhs.firstToken(), new_name, edits); try renameToken(handle, infix_op.rhs.firstToken(), new_name, edits, encoding);
} }
} }
}, },
else => { else => {
try renameSymbolInternal(arena, store, .{ .node = infix_op.lhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = infix_op.lhs, .handle = handle }, decl, new_name, edits, encoding);
try renameSymbolInternal(arena, store, .{ .node = infix_op.rhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = infix_op.rhs, .handle = handle }, decl, new_name, edits, encoding);
}, },
} }
}, },
@ -228,78 +229,78 @@ fn renameSymbolInternal(
const prefix_op = node.cast(ast.Node.PrefixOp).?; const prefix_op = node.cast(ast.Node.PrefixOp).?;
switch (prefix_op.op) { switch (prefix_op.op) {
.ArrayType => |info| { .ArrayType => |info| {
try renameSymbolInternal(arena, store, .{ .node = info.len_expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = info.len_expr, .handle = handle }, decl, new_name, edits, encoding);
if (info.sentinel) |sentinel| { if (info.sentinel) |sentinel| {
try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.PtrType, .SliceType => |info| { .PtrType, .SliceType => |info| {
if (info.align_info) |align_info| { if (info.align_info) |align_info| {
try renameSymbolInternal(arena, store, .{ .node = align_info.node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = align_info.node, .handle = handle }, decl, new_name, edits, encoding);
if (align_info.bit_range) |range| { if (align_info.bit_range) |range| {
try renameSymbolInternal(arena, store, .{ .node = range.start, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = range.start, .handle = handle }, decl, new_name, edits, encoding);
try renameSymbolInternal(arena, store, .{ .node = range.end, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = range.end, .handle = handle }, decl, new_name, edits, encoding);
} }
} }
if (info.sentinel) |sentinel| { if (info.sentinel) |sentinel| {
try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
else => {}, else => {},
} }
try renameSymbolInternal(arena, store, .{ .node = prefix_op.rhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = prefix_op.rhs, .handle = handle }, decl, new_name, edits, encoding);
}, },
.FieldInitializer => { .FieldInitializer => {
// TODO Rename field initializer names when needed // TODO Rename field initializer names when needed
const field_init = node.cast(ast.Node.FieldInitializer).?; const field_init = node.cast(ast.Node.FieldInitializer).?;
try renameSymbolInternal(arena, store, .{ .node = field_init.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = field_init.expr, .handle = handle }, decl, new_name, edits, encoding);
}, },
.ArrayInitializer => { .ArrayInitializer => {
const array_init = node.cast(ast.Node.ArrayInitializer).?; const array_init = node.cast(ast.Node.ArrayInitializer).?;
try renameSymbolInternal(arena, store, .{ .node = array_init.lhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = array_init.lhs, .handle = handle }, decl, new_name, edits, encoding);
for (array_init.listConst()) |child| { for (array_init.listConst()) |child| {
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.ArrayInitializerDot => { .ArrayInitializerDot => {
const array_init = node.cast(ast.Node.ArrayInitializerDot).?; const array_init = node.cast(ast.Node.ArrayInitializerDot).?;
for (array_init.listConst()) |child| { for (array_init.listConst()) |child| {
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.StructInitializer => { .StructInitializer => {
// TODO Rename field initializer names when needed // TODO Rename field initializer names when needed
const struct_init = node.cast(ast.Node.StructInitializer).?; const struct_init = node.cast(ast.Node.StructInitializer).?;
try renameSymbolInternal(arena, store, .{ .node = struct_init.lhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = struct_init.lhs, .handle = handle }, decl, new_name, edits, encoding);
for (struct_init.listConst()) |child| { for (struct_init.listConst()) |child| {
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.StructInitializerDot => { .StructInitializerDot => {
const struct_init = node.cast(ast.Node.StructInitializerDot).?; const struct_init = node.cast(ast.Node.StructInitializerDot).?;
for (struct_init.listConst()) |child| { for (struct_init.listConst()) |child| {
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.Call => { .Call => {
const call = node.cast(ast.Node.Call).?; const call = node.cast(ast.Node.Call).?;
try renameSymbolInternal(arena, store, .{ .node = call.lhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = call.lhs, .handle = handle }, decl, new_name, edits, encoding);
for (call.paramsConst()) |param| { for (call.paramsConst()) |param| {
try renameSymbolInternal(arena, store, .{ .node = param, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = param, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.SuffixOp => { .SuffixOp => {
const suffix_op = node.cast(ast.Node.SuffixOp).?; const suffix_op = node.cast(ast.Node.SuffixOp).?;
try renameSymbolInternal(arena, store, .{ .node = suffix_op.lhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = suffix_op.lhs, .handle = handle }, decl, new_name, edits, encoding);
switch (suffix_op.op) { switch (suffix_op.op) {
.ArrayAccess => |acc| try renameSymbolInternal(arena, store, .{ .node = acc, .handle = handle }, decl, new_name, edits), .ArrayAccess => |acc| try renameSymbolInternal(arena, store, .{ .node = acc, .handle = handle }, decl, new_name, edits, encoding),
.Slice => |sl| { .Slice => |sl| {
try renameSymbolInternal(arena, store, .{ .node = sl.start, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = sl.start, .handle = handle }, decl, new_name, edits, encoding);
if (sl.end) |end| { if (sl.end) |end| {
try renameSymbolInternal(arena, store, .{ .node = end, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = end, .handle = handle }, decl, new_name, edits, encoding);
} }
if (sl.sentinel) |sentinel| { if (sl.sentinel) |sentinel| {
try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
else => {}, else => {},
@ -307,30 +308,30 @@ fn renameSymbolInternal(
}, },
.GroupedExpression => { .GroupedExpression => {
const grouped = node.cast(ast.Node.GroupedExpression).?; const grouped = node.cast(ast.Node.GroupedExpression).?;
try renameSymbolInternal(arena, store, .{ .node = grouped.expr, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = grouped.expr, .handle = handle }, decl, new_name, edits, encoding);
}, },
.ControlFlowExpression => { .ControlFlowExpression => {
const cfe = node.cast(ast.Node.ControlFlowExpression).?; const cfe = node.cast(ast.Node.ControlFlowExpression).?;
if (cfe.rhs) |rhs| { if (cfe.rhs) |rhs| {
try renameSymbolInternal(arena, store, .{ .node = rhs, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = rhs, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.Suspend => { .Suspend => {
const suspend_node = node.cast(ast.Node.Suspend).?; const suspend_node = node.cast(ast.Node.Suspend).?;
if (suspend_node.body) |body| { if (suspend_node.body) |body| {
try renameSymbolInternal(arena, store, .{ .node = body, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = body, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
.BuiltinCall => { .BuiltinCall => {
const builtin_call = node.cast(ast.Node.BuiltinCall).?; const builtin_call = node.cast(ast.Node.BuiltinCall).?;
for (builtin_call.paramsConst()) |param| { for (builtin_call.paramsConst()) |param| {
try renameSymbolInternal(arena, store, .{ .node = param, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = param, .handle = handle }, decl, new_name, edits, encoding);
} }
}, },
// TODO Inline asm expr // TODO Inline asm expr
.TestDecl => { .TestDecl => {
const test_decl = node.cast(ast.Node.TestDecl).?; const test_decl = node.cast(ast.Node.TestDecl).?;
try renameSymbolInternal(arena, store, .{ .node = test_decl.body_node, .handle = handle }, decl, new_name, edits); try renameSymbolInternal(arena, store, .{ .node = test_decl.body_node, .handle = handle }, decl, new_name, edits, encoding);
}, },
else => {}, else => {},
} }
@ -342,6 +343,7 @@ pub fn renameSymbol(
decl_handle: analysis.DeclWithHandle, decl_handle: analysis.DeclWithHandle,
new_name: []const u8, new_name: []const u8,
edits: *std.StringHashMap([]types.TextEdit), edits: *std.StringHashMap([]types.TextEdit),
encoding: offsets.Encoding,
) !void { ) !void {
std.debug.assert(decl_handle.decl.* != .label_decl); std.debug.assert(decl_handle.decl.* != .label_decl);
const curr_handle = decl_handle.handle; const curr_handle = decl_handle.handle;
@ -356,10 +358,10 @@ pub fn renameSymbol(
for (handles.items) |handle| { for (handles.items) |handle| {
var text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator); var text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator);
if (handle == curr_handle) { if (handle == curr_handle) {
try renameToken(curr_handle, decl_handle.nameToken(), new_name, &text_edits); try renameToken(curr_handle, decl_handle.nameToken(), new_name, &text_edits, encoding);
} }
try renameSymbolInternal(arena, store, .{ .node = &handle.tree.root_node.base, .handle = handle }, decl_handle, new_name, &text_edits); try renameSymbolInternal(arena, store, .{ .node = &handle.tree.root_node.base, .handle = handle }, decl_handle, new_name, &text_edits, encoding);
if (text_edits.items.len > 0) { if (text_edits.items.len > 0) {
try edits.putNoClobber(handle.uri(), text_edits.items); try edits.putNoClobber(handle.uri(), text_edits.items);
} }
@ -368,7 +370,7 @@ pub fn renameSymbol(
.param_decl => |param| { .param_decl => |param| {
var curr_doc_text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator); var curr_doc_text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator);
// Rename the param tok. // Rename the param tok.
try renameToken(curr_handle, decl_handle.nameToken(), new_name, &curr_doc_text_edits); try renameToken(curr_handle, decl_handle.nameToken(), new_name, &curr_doc_text_edits, encoding);
const fn_node = loop: for (curr_handle.document_scope.scopes) |scope| { const fn_node = loop: for (curr_handle.document_scope.scopes) |scope| {
switch (scope.data) { switch (scope.data) {
.function => |proto| { .function => |proto| {
@ -385,14 +387,14 @@ pub fn renameSymbol(
return; return;
}; };
if (fn_node.body_node) |body| { if (fn_node.body_node) |body| {
try renameSymbolInternal(arena, store, .{ .node = body, .handle = curr_handle }, decl_handle, new_name, &curr_doc_text_edits); try renameSymbolInternal(arena, store, .{ .node = body, .handle = curr_handle }, decl_handle, new_name, &curr_doc_text_edits, encoding);
} }
try edits.putNoClobber(curr_handle.uri(), curr_doc_text_edits.items); try edits.putNoClobber(curr_handle.uri(), curr_doc_text_edits.items);
}, },
.pointer_payload, .array_payload, .switch_payload => { .pointer_payload, .array_payload, .switch_payload => {
var curr_doc_text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator); var curr_doc_text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator);
try renameToken(curr_handle, decl_handle.nameToken(), new_name, &curr_doc_text_edits); try renameToken(curr_handle, decl_handle.nameToken(), new_name, &curr_doc_text_edits, encoding);
try renameSymbolInternal(arena, store, .{ .node = &curr_handle.tree.root_node.base, .handle = curr_handle }, decl_handle, new_name, &curr_doc_text_edits); try renameSymbolInternal(arena, store, .{ .node = &curr_handle.tree.root_node.base, .handle = curr_handle }, decl_handle, new_name, &curr_doc_text_edits, encoding);
try edits.putNoClobber(curr_handle.uri(), curr_doc_text_edits.items); try edits.putNoClobber(curr_handle.uri(), curr_doc_text_edits.items);
}, },
.label_decl => unreachable, .label_decl => unreachable,