Completed renaming
This commit is contained in:
parent
07e1013680
commit
8bc6087646
@ -1421,21 +1421,23 @@ pub const DeclWithHandle = struct {
|
|||||||
decl: *Declaration,
|
decl: *Declaration,
|
||||||
handle: *DocumentStore.Handle,
|
handle: *DocumentStore.Handle,
|
||||||
|
|
||||||
pub fn location(self: DeclWithHandle) ast.Tree.Location {
|
pub fn nameToken(self: DeclWithHandle) ast.TokenIndex {
|
||||||
const tree = self.handle.tree;
|
const tree = self.handle.tree;
|
||||||
return switch (self.decl.*) {
|
return switch (self.decl.*) {
|
||||||
.ast_node => |n| block: {
|
.ast_node => |n| getDeclNameToken(tree, n).?,
|
||||||
const name_token = getDeclNameToken(tree, n).?;
|
.param_decl => |p| p.name_token.?,
|
||||||
break :block tree.tokenLocation(0, name_token);
|
.pointer_payload => |pp| pp.node.value_symbol.firstToken(),
|
||||||
},
|
.array_payload => |ap| ap.identifier.firstToken(),
|
||||||
.param_decl => |p| tree.tokenLocation(0, p.name_token.?),
|
.switch_payload => |sp| sp.node.value_symbol.firstToken(),
|
||||||
.pointer_payload => |pp| tree.tokenLocation(0, pp.node.value_symbol.firstToken()),
|
.label_decl => |ld| ld.firstToken(),
|
||||||
.array_payload => |ap| tree.tokenLocation(0, ap.identifier.firstToken()),
|
|
||||||
.switch_payload => |sp| tree.tokenLocation(0, sp.node.value_symbol.firstToken()),
|
|
||||||
.label_decl => |ld| tree.tokenLocation(0, ld.firstToken()),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn location(self: DeclWithHandle) ast.Tree.Location {
|
||||||
|
const tree = self.handle.tree;
|
||||||
|
return tree.tokenLocation(0, self.nameToken());
|
||||||
|
}
|
||||||
|
|
||||||
fn isPublic(self: DeclWithHandle) bool {
|
fn isPublic(self: DeclWithHandle) bool {
|
||||||
return switch (self.decl.*) {
|
return switch (self.decl.*) {
|
||||||
.ast_node => |node| isNodePublic(self.handle.tree, node),
|
.ast_node => |node| isNodePublic(self.handle.tree, node),
|
||||||
|
40
src/main.zig
40
src/main.zig
@ -719,14 +719,46 @@ fn gotoDefinitionString(id: types.RequestId, pos_index: usize, handle: *Document
|
|||||||
|
|
||||||
fn renameDefinitionGlobal(id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
fn renameDefinitionGlobal(id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
||||||
// @TODO
|
// @TODO
|
||||||
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
|
defer arena.deinit();
|
||||||
|
|
||||||
|
const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
||||||
|
|
||||||
|
var workspace_edit = types.WorkspaceEdit{
|
||||||
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
||||||
|
};
|
||||||
|
try rename.renameSymbol(&arena, &document_store, decl, new_name, &workspace_edit.changes.?);
|
||||||
|
try send(types.Response{
|
||||||
|
.id = id,
|
||||||
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn renameDefinitionFieldAccess(id: types.RequestId, handle: *DocumentStore.Handle, position: types.Position, new_name: []const u8) !void {
|
fn renameDefinitionFieldAccess(
|
||||||
|
id: types.RequestId,
|
||||||
|
handle: *DocumentStore.Handle,
|
||||||
|
position: types.Position,
|
||||||
|
range: analysis.SourceRange,
|
||||||
|
new_name: []const u8,
|
||||||
|
config: Config,
|
||||||
|
) !void {
|
||||||
// @TODO
|
// @TODO
|
||||||
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
|
defer arena.deinit();
|
||||||
|
|
||||||
|
const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
||||||
|
|
||||||
|
var workspace_edit = types.WorkspaceEdit{
|
||||||
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
||||||
|
};
|
||||||
|
try rename.renameSymbol(&arena, &document_store, decl, new_name, &workspace_edit.changes.?);
|
||||||
|
try send(types.Response{
|
||||||
|
.id = id,
|
||||||
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn renameDefinitionLabel(id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
fn renameDefinitionLabel(id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
||||||
// @TODO
|
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
|
|
||||||
@ -736,7 +768,6 @@ fn renameDefinitionLabel(id: types.RequestId, handle: *DocumentStore.Handle, pos
|
|||||||
.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.?);
|
||||||
|
|
||||||
try send(types.Response{
|
try send(types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{ .WorkspaceEdit = workspace_edit },
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
||||||
@ -1362,9 +1393,10 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config, ke
|
|||||||
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);
|
||||||
|
|
||||||
|
const this_config = configFromUriOr(uri, config);
|
||||||
switch (pos_context) {
|
switch (pos_context) {
|
||||||
.var_access => try renameDefinitionGlobal(id, handle, pos_index, new_name),
|
.var_access => try renameDefinitionGlobal(id, handle, pos_index, new_name),
|
||||||
.field_access => try renameDefinitionFieldAccess(id, handle, pos, new_name),
|
.field_access => |range| try renameDefinitionFieldAccess(id, handle, pos, range, new_name, this_config),
|
||||||
.label => try renameDefinitionLabel(id, handle, pos_index, new_name),
|
.label => try renameDefinitionLabel(id, handle, pos_index, new_name),
|
||||||
else => try respondGeneric(id, null_result_response),
|
else => try respondGeneric(id, null_result_response),
|
||||||
}
|
}
|
||||||
|
354
src/rename.zig
354
src/rename.zig
@ -3,7 +3,9 @@ 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");
|
||||||
|
|
||||||
fn renameToken(handle: *DocumentStore.Handle, tok: std.zig.ast.TokenIndex, new_name: []const u8, edits: *std.ArrayList(types.TextEdit)) !void {
|
const ast = std.zig.ast;
|
||||||
|
|
||||||
|
fn renameToken(handle: *DocumentStore.Handle, tok: ast.TokenIndex, new_name: []const u8, edits: *std.ArrayList(types.TextEdit)) !void {
|
||||||
// const handle.tree.tokenLocation(start_index: usize, token: Token.Loc)
|
// const handle.tree.tokenLocation(start_index: usize, token: Token.Loc)
|
||||||
const loc = handle.tree.tokenLocation(0, tok);
|
const loc = handle.tree.tokenLocation(0, tok);
|
||||||
(try edits.addOne()).* = .{
|
(try edits.addOne()).* = .{
|
||||||
@ -37,7 +39,8 @@ pub fn renameLabel(arena: *std.heap.ArenaAllocator, decl: analysis.DeclWithHandl
|
|||||||
|
|
||||||
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) {
|
||||||
if (handle.tree.token_ids[curr_tok] == .Keyword_break and handle.tree.token_ids[curr_tok + 1] == .Colon and
|
const curr_id = handle.tree.token_ids[curr_tok];
|
||||||
|
if ((curr_id == .Keyword_break or curr_id == .Keyword_continue) and handle.tree.token_ids[curr_tok + 1] == .Colon and
|
||||||
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))) {
|
||||||
@ -48,3 +51,350 @@ pub fn renameLabel(arena: *std.heap.ArenaAllocator, decl: analysis.DeclWithHandl
|
|||||||
|
|
||||||
try edits.putNoClobber(handle.uri(), text_edits.items);
|
try edits.putNoClobber(handle.uri(), text_edits.items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn renameSymbolInternal(
|
||||||
|
arena: *std.heap.ArenaAllocator,
|
||||||
|
store: *DocumentStore,
|
||||||
|
node_handle: analysis.NodeWithHandle,
|
||||||
|
decl: analysis.DeclWithHandle,
|
||||||
|
new_name: []const u8,
|
||||||
|
edits: *std.ArrayList(types.TextEdit),
|
||||||
|
) error{OutOfMemory}!void {
|
||||||
|
const node = node_handle.node;
|
||||||
|
const handle = node_handle.handle;
|
||||||
|
|
||||||
|
switch (node.id) {
|
||||||
|
.ContainerDecl, .Root, .Block => {
|
||||||
|
var idx: usize = 0;
|
||||||
|
while (node.iterate(idx)) |child| : (idx += 1) {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.VarDecl => {
|
||||||
|
const var_decl = node.cast(ast.Node.VarDecl).?;
|
||||||
|
if (var_decl.type_node) |type_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
if (var_decl.init_node) |init_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.Use => {
|
||||||
|
const use = node.cast(ast.Node.Use).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = use.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
.ContainerField => {
|
||||||
|
const field = node.cast(ast.Node.ContainerField).?;
|
||||||
|
if (field.type_expr) |type_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
if (field.value_expr) |init_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.Identifier => {
|
||||||
|
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)) {
|
||||||
|
try renameToken(handle, node.firstToken(), new_name, edits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.FnProto => {
|
||||||
|
const fn_proto = node.cast(ast.Node.FnProto).?;
|
||||||
|
for (fn_proto.paramsConst()) |param| {
|
||||||
|
switch (param.param_type) {
|
||||||
|
.type_expr => |type_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch (fn_proto.return_type) {
|
||||||
|
.Explicit, .InferErrorSet => |type_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
if (fn_proto.align_expr) |align_expr| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = align_expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
if (fn_proto.section_expr) |section_expr| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = section_expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
if (fn_proto.callconv_expr) |callconv_expr| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = callconv_expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
if (fn_proto.body_node) |body| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = body, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.AnyFrameType => {
|
||||||
|
const anyframe_type = node.cast(ast.Node.AnyFrameType).?;
|
||||||
|
if (anyframe_type.result) |result| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = result.return_type, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.Defer => {
|
||||||
|
const defer_node = node.cast(ast.Node.Defer).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = defer_node.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
.Comptime => {
|
||||||
|
const comptime_node = node.cast(ast.Node.Comptime).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = comptime_node.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
.Nosuspend => {
|
||||||
|
const nosuspend_node = node.cast(ast.Node.Nosuspend).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = nosuspend_node.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
.Switch => {
|
||||||
|
// TODO When renaming a union(enum) field, also rename switch items that refer to it.
|
||||||
|
const switch_node = node.cast(ast.Node.Switch).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = switch_node.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
for (switch_node.casesConst()) |case| {
|
||||||
|
if (case.*.cast(ast.Node.SwitchCase)) |case_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = case_node.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.While => {
|
||||||
|
const while_node = node.cast(ast.Node.While).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = while_node.condition, .handle = handle }, decl, new_name, edits);
|
||||||
|
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 = while_node.body, .handle = handle }, decl, new_name, edits);
|
||||||
|
if (while_node.@"else") |else_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.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.body, .handle = handle }, decl, new_name, edits);
|
||||||
|
if (for_node.@"else") |else_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.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.body, .handle = handle }, decl, new_name, edits);
|
||||||
|
if (if_node.@"else") |else_node| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = else_node.body, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.InfixOp => {
|
||||||
|
const infix_op = node.cast(ast.Node.InfixOp).?;
|
||||||
|
switch (infix_op.op) {
|
||||||
|
.Period => {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = infix_op.lhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
|
||||||
|
const rhs_str = analysis.nodeToString(handle.tree, infix_op.rhs) orelse return;
|
||||||
|
var bound_type_params = analysis.BoundTypeParams.init(&arena.allocator);
|
||||||
|
const left_type = try analysis.resolveFieldAccessLhsType(
|
||||||
|
store,
|
||||||
|
arena,
|
||||||
|
(try analysis.resolveTypeOfNodeInternal(store, arena, .{
|
||||||
|
.node = infix_op.lhs,
|
||||||
|
.handle = handle,
|
||||||
|
}, &bound_type_params)) orelse return,
|
||||||
|
&bound_type_params,
|
||||||
|
);
|
||||||
|
|
||||||
|
const left_type_node = switch (left_type.type.data) {
|
||||||
|
.other => |n| n,
|
||||||
|
else => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (try analysis.lookupSymbolContainer(
|
||||||
|
store,
|
||||||
|
arena,
|
||||||
|
.{ .node = left_type_node, .handle = left_type.handle },
|
||||||
|
rhs_str,
|
||||||
|
!left_type.type.is_type_val,
|
||||||
|
)) |child| {
|
||||||
|
if (std.meta.eql(child, decl)) {
|
||||||
|
try renameToken(handle, infix_op.rhs.firstToken(), new_name, edits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = infix_op.lhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = infix_op.rhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.PrefixOp => {
|
||||||
|
const prefix_op = node.cast(ast.Node.PrefixOp).?;
|
||||||
|
switch (prefix_op.op) {
|
||||||
|
.ArrayType => |info| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = info.len_expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
if (info.sentinel) |sentinel| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.PtrType, .SliceType => |info| {
|
||||||
|
if (info.align_info) |align_info| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = align_info.node, .handle = handle }, decl, new_name, edits);
|
||||||
|
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.end, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (info.sentinel) |sentinel| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = prefix_op.rhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
.FieldInitializer => {
|
||||||
|
// TODO Rename field initializer names when needed
|
||||||
|
const field_init = node.cast(ast.Node.FieldInitializer).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = field_init.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
.ArrayInitializer => {
|
||||||
|
const array_init = node.cast(ast.Node.ArrayInitializer).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = array_init.lhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
for (array_init.listConst()) |child| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.ArrayInitializerDot => {
|
||||||
|
const array_init = node.cast(ast.Node.ArrayInitializerDot).?;
|
||||||
|
for (array_init.listConst()) |child| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.StructInitializer => {
|
||||||
|
// TODO Rename field initializer names when needed
|
||||||
|
const struct_init = node.cast(ast.Node.StructInitializer).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = struct_init.lhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
for (struct_init.listConst()) |child| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.StructInitializerDot => {
|
||||||
|
const struct_init = node.cast(ast.Node.StructInitializerDot).?;
|
||||||
|
for (struct_init.listConst()) |child| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = child, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.Call => {
|
||||||
|
const call = node.cast(ast.Node.Call).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = call.lhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
for (call.paramsConst()) |param| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = param, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.SuffixOp => {
|
||||||
|
const suffix_op = node.cast(ast.Node.SuffixOp).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = suffix_op.lhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
switch (suffix_op.op) {
|
||||||
|
.ArrayAccess => |acc| try renameSymbolInternal(arena, store, .{ .node = acc, .handle = handle }, decl, new_name, edits),
|
||||||
|
.Slice => |sl| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = sl.start, .handle = handle }, decl, new_name, edits);
|
||||||
|
if (sl.end) |end| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = end, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
if (sl.sentinel) |sentinel| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = sentinel, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.GroupedExpression => {
|
||||||
|
const grouped = node.cast(ast.Node.GroupedExpression).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = grouped.expr, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
.ControlFlowExpression => {
|
||||||
|
const cfe = node.cast(ast.Node.ControlFlowExpression).?;
|
||||||
|
if (cfe.rhs) |rhs| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = rhs, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.Suspend => {
|
||||||
|
const suspend_node = node.cast(ast.Node.Suspend).?;
|
||||||
|
if (suspend_node.body) |body| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = body, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.BuiltinCall => {
|
||||||
|
const builtin_call = node.cast(ast.Node.BuiltinCall).?;
|
||||||
|
for (builtin_call.paramsConst()) |param| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = param, .handle = handle }, decl, new_name, edits);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// TODO Inline asm expr
|
||||||
|
.TestDecl => {
|
||||||
|
const test_decl = node.cast(ast.Node.TestDecl).?;
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = test_decl.body_node, .handle = handle }, decl, new_name, edits);
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn renameSymbol(
|
||||||
|
arena: *std.heap.ArenaAllocator,
|
||||||
|
store: *DocumentStore,
|
||||||
|
decl_handle: analysis.DeclWithHandle,
|
||||||
|
new_name: []const u8,
|
||||||
|
edits: *std.StringHashMap([]types.TextEdit),
|
||||||
|
) !void {
|
||||||
|
std.debug.assert(decl_handle.decl.* != .label_decl);
|
||||||
|
const curr_handle = decl_handle.handle;
|
||||||
|
|
||||||
|
switch (decl_handle.decl.*) {
|
||||||
|
.ast_node => |decl_node| {
|
||||||
|
var handles = std.ArrayList(*DocumentStore.Handle).init(&arena.allocator);
|
||||||
|
var handle_it = store.handles.iterator();
|
||||||
|
while (handle_it.next()) |entry| {
|
||||||
|
try handles.append(entry.value);
|
||||||
|
}
|
||||||
|
for (handles.items) |handle| {
|
||||||
|
var text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator);
|
||||||
|
if (handle == curr_handle) {
|
||||||
|
try renameToken(curr_handle, decl_handle.nameToken(), new_name, &text_edits);
|
||||||
|
}
|
||||||
|
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = &handle.tree.root_node.base, .handle = handle }, decl_handle, new_name, &text_edits);
|
||||||
|
if (text_edits.items.len > 0) {
|
||||||
|
try edits.putNoClobber(handle.uri(), text_edits.items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.param_decl => |param| {
|
||||||
|
var curr_doc_text_edits = std.ArrayList(types.TextEdit).init(&arena.allocator);
|
||||||
|
// Rename the param tok.
|
||||||
|
try renameToken(curr_handle, decl_handle.nameToken(), new_name, &curr_doc_text_edits);
|
||||||
|
const fn_node = loop: for (curr_handle.document_scope.scopes) |scope| {
|
||||||
|
switch (scope.data) {
|
||||||
|
.function => |proto| {
|
||||||
|
const fn_proto = proto.cast(std.zig.ast.Node.FnProto).?;
|
||||||
|
for (fn_proto.paramsConst()) |*candidate| {
|
||||||
|
if (candidate == param)
|
||||||
|
break :loop fn_proto;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std.log.warn(.rename, "Could not find param decl's function", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if (fn_node.body_node) |body| {
|
||||||
|
try renameSymbolInternal(arena, store, .{ .node = body, .handle = curr_handle }, decl_handle, new_name, &curr_doc_text_edits);
|
||||||
|
}
|
||||||
|
try edits.putNoClobber(curr_handle.uri(), curr_doc_text_edits.items);
|
||||||
|
},
|
||||||
|
.pointer_payload, .array_payload, .switch_payload => {
|
||||||
|
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 renameSymbolInternal(arena, store, .{ .node = &curr_handle.tree.root_node.base, .handle = curr_handle }, decl_handle, new_name, &curr_doc_text_edits);
|
||||||
|
try edits.putNoClobber(curr_handle.uri(), curr_doc_text_edits.items);
|
||||||
|
},
|
||||||
|
.label_decl => unreachable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -18,18 +18,15 @@ pub const Object = json.ObjectMap;
|
|||||||
pub const DocumentUri = String;
|
pub const DocumentUri = String;
|
||||||
|
|
||||||
pub const Position = struct {
|
pub const Position = struct {
|
||||||
line: Integer,
|
line: Integer, character: Integer
|
||||||
character: Integer
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Range = struct {
|
pub const Range = struct {
|
||||||
start: Position,
|
start: Position, end: Position
|
||||||
end: Position
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Location = struct {
|
pub const Location = struct {
|
||||||
uri: DocumentUri,
|
uri: DocumentUri, range: Range
|
||||||
range: Range
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Id of a request
|
/// Id of a request
|
||||||
@ -43,9 +40,7 @@ pub const RequestId = union(enum) {
|
|||||||
pub const RequestParams = void;
|
pub const RequestParams = void;
|
||||||
|
|
||||||
pub const NotificationParams = union(enum) {
|
pub const NotificationParams = union(enum) {
|
||||||
LogMessageParams: LogMessageParams,
|
LogMessageParams: LogMessageParams, PublishDiagnosticsParams: PublishDiagnosticsParams, ShowMessageParams: ShowMessageParams
|
||||||
PublishDiagnosticsParams: PublishDiagnosticsParams,
|
|
||||||
ShowMessageParams: ShowMessageParams
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Hover response
|
/// Hover response
|
||||||
@ -73,17 +68,12 @@ pub const Error = struct {
|
|||||||
|
|
||||||
/// JSONRPC request
|
/// JSONRPC request
|
||||||
pub const Request = struct {
|
pub const Request = struct {
|
||||||
jsonrpc: String = "2.0",
|
jsonrpc: String = "2.0", method: String, id: ?RequestId = RequestId{ .Integer = 0 }, params: RequestParams
|
||||||
method: String,
|
|
||||||
id: ?RequestId = RequestId{.Integer = 0},
|
|
||||||
params: RequestParams
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// JSONRPC notifications
|
/// JSONRPC notifications
|
||||||
pub const Notification = struct {
|
pub const Notification = struct {
|
||||||
jsonrpc: String = "2.0",
|
jsonrpc: String = "2.0", method: String, params: NotificationParams
|
||||||
method: String,
|
|
||||||
params: NotificationParams
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// JSONRPC response
|
/// JSONRPC response
|
||||||
@ -112,8 +102,7 @@ pub const MessageType = enum(Integer) {
|
|||||||
|
|
||||||
/// Params for a LogMessage Notification (window/logMessage)
|
/// Params for a LogMessage Notification (window/logMessage)
|
||||||
pub const LogMessageParams = struct {
|
pub const LogMessageParams = struct {
|
||||||
type: MessageType,
|
type: MessageType, message: String
|
||||||
message: String
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const DiagnosticSeverity = enum(Integer) {
|
pub const DiagnosticSeverity = enum(Integer) {
|
||||||
@ -140,8 +129,7 @@ pub const Diagnostic = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const PublishDiagnosticsParams = struct {
|
pub const PublishDiagnosticsParams = struct {
|
||||||
uri: DocumentUri,
|
uri: DocumentUri, diagnostics: []Diagnostic
|
||||||
diagnostics: []Diagnostic
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const TextDocument = struct {
|
pub const TextDocument = struct {
|
||||||
@ -197,7 +185,7 @@ pub const TextDocument = struct {
|
|||||||
.end = .{
|
.end = .{
|
||||||
.line = line_idx,
|
.line = line_idx,
|
||||||
.character = @intCast(i64, curr_line.len),
|
.character = @intCast(i64, curr_line.len),
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -236,7 +224,7 @@ pub const TextEdit = struct {
|
|||||||
|
|
||||||
pub const MarkupKind = enum(u1) {
|
pub const MarkupKind = enum(u1) {
|
||||||
PlainText = 0, // plaintext
|
PlainText = 0, // plaintext
|
||||||
Markdown = 1, // markdown
|
Markdown = 1, // markdown
|
||||||
|
|
||||||
pub fn jsonStringify(
|
pub fn jsonStringify(
|
||||||
value: MarkupKind,
|
value: MarkupKind,
|
||||||
@ -252,8 +240,7 @@ pub const MarkupKind = enum(u1) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const MarkupContent = struct {
|
pub const MarkupContent = struct {
|
||||||
kind: MarkupKind = MarkupKind.Markdown,
|
kind: MarkupKind = MarkupKind.Markdown, value: String
|
||||||
value: String
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// pub const TextDocumentIdentifier = struct {
|
// pub const TextDocumentIdentifier = struct {
|
||||||
@ -329,15 +316,7 @@ pub const InsertTextFormat = enum(Integer) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const CompletionItem = struct {
|
pub const CompletionItem = struct {
|
||||||
label: String,
|
label: String, kind: CompletionItemKind, textEdit: ?TextEdit = null, filterText: ?String = null, insertText: ?String = null, insertTextFormat: ?InsertTextFormat = InsertTextFormat.PlainText, detail: ?String = null, documentation: ?MarkupContent = null
|
||||||
kind: CompletionItemKind,
|
|
||||||
textEdit: ?TextEdit = null,
|
|
||||||
filterText: ?String = null,
|
|
||||||
insertText: ?String = null,
|
|
||||||
insertTextFormat: ?InsertTextFormat = InsertTextFormat.PlainText,
|
|
||||||
|
|
||||||
detail: ?String = null,
|
|
||||||
documentation: ?MarkupContent = null
|
|
||||||
// filterText: String = .NotDefined,
|
// filterText: String = .NotDefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -379,16 +358,9 @@ const SymbolKind = enum {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const DocumentSymbol = struct {
|
pub const DocumentSymbol = struct {
|
||||||
name: String,
|
name: String, detail: ?String = null, kind: SymbolKind, deprecated: bool = false, range: Range, selectionRange: Range, children: []DocumentSymbol = &[_]DocumentSymbol{}
|
||||||
detail: ?String = null,
|
|
||||||
kind: SymbolKind,
|
|
||||||
deprecated: bool = false,
|
|
||||||
range: Range,
|
|
||||||
selectionRange: Range,
|
|
||||||
children: []DocumentSymbol = &[_]DocumentSymbol{}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ShowMessageParams = struct {
|
pub const ShowMessageParams = struct {
|
||||||
type: MessageType,
|
type: MessageType, message: String
|
||||||
message: String
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user