zls/src/rename.zig

60 lines
1.9 KiB
Zig
Raw Normal View History

2020-06-27 07:18:05 +01:00
const std = @import("std");
const DocumentStore = @import("document_store.zig");
const analysis = @import("analysis.zig");
const references = @import("references.zig");
2020-06-27 07:18:05 +01:00
const types = @import("types.zig");
2020-07-04 23:40:18 +01:00
const offsets = @import("offsets.zig");
2020-06-27 07:18:05 +01:00
2020-06-27 13:29:45 +01:00
const ast = std.zig.ast;
// TODO Use an map to array lists and collect at the end instead?
const RefHandlerContext = struct {
edits: *std.StringHashMap([]types.TextEdit),
allocator: *std.mem.Allocator,
new_name: []const u8,
};
2020-06-27 07:18:05 +01:00
fn refHandler(context: RefHandlerContext, loc: types.Location) !void {
var text_edits = if (context.edits.getValue(loc.uri)) |slice|
std.ArrayList(types.TextEdit).fromOwnedSlice(context.allocator, slice)
else
std.ArrayList(types.TextEdit).init(context.allocator);
2020-06-27 07:18:05 +01:00
(try text_edits.addOne()).* = .{
.range = loc.range,
.newText = context.new_name,
};
_ = try context.edits.put(loc.uri, text_edits.toOwnedSlice());
2020-06-27 07:18:05 +01:00
}
2020-06-27 13:29:45 +01:00
pub fn renameSymbol(
2020-06-27 13:29:45 +01:00
arena: *std.heap.ArenaAllocator,
store: *DocumentStore,
decl_handle: analysis.DeclWithHandle,
2020-06-27 13:29:45 +01:00
new_name: []const u8,
edits: *std.StringHashMap([]types.TextEdit),
2020-07-04 23:40:18 +01:00
encoding: offsets.Encoding,
) !void {
std.debug.assert(decl_handle.decl.* != .label_decl);
try references.symbolReferences(arena, store, decl_handle, encoding, true, RefHandlerContext{
.edits = edits,
.allocator = &arena.allocator,
.new_name = new_name,
}, refHandler);
2020-06-27 13:29:45 +01:00
}
pub fn renameLabel(
2020-06-27 13:29:45 +01:00
arena: *std.heap.ArenaAllocator,
decl_handle: analysis.DeclWithHandle,
new_name: []const u8,
edits: *std.StringHashMap([]types.TextEdit),
2020-07-04 23:40:18 +01:00
encoding: offsets.Encoding,
2020-06-27 13:29:45 +01:00
) !void {
std.debug.assert(decl_handle.decl.* == .label_decl);
try references.labelReferences(arena, decl_handle, encoding, true, RefHandlerContext{
.edits = edits,
.allocator = &arena.allocator,
.new_name = new_name,
}, refHandler);
2020-06-27 13:29:45 +01:00
}