13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/nvim-mcp-server/tools/update_buffer_tool.rb', line 13
def call(project_name:, file_path:, content:)
socket_path = "/tmp/nvim-#{project_name}.sock"
log(:info, "Updating buffer #{file_path} for project #{project_name} using socket #{socket_path}")
result = {
status: "success",
project_name: project_name,
socket_path: socket_path,
file_path: file_path,
updated: false,
saved: false,
reloaded: false
}
begin
unless File.exist?(socket_path)
result[:status] = "error"
result[:message] = "Neovim socket not found: #{socket_path}"
return result
end
client = Neovim.attach_unix(socket_path)
all_buffers = client.list_bufs
target_buffer = nil
all_buffers.each do |buffer|
buffer_name = buffer.name
next if buffer_name.nil? || buffer_name.empty?
if buffer_name == file_path || buffer_name.end_with?(file_path)
target_buffer = buffer
result[:actual_path] = buffer_name
break
end
end
if target_buffer.nil?
result[:status] = "error"
result[:message] = "Buffer not found for file: #{file_path}"
return result
end
original_line_count = target_buffer.line_count
result[:original_line_count] = original_line_count
windows = client.list_wins
target_window = windows.find { |win| win.buffer.number == target_buffer.number }
if target_window
client.set_current_win(target_window)
else
client.command("buffer #{target_buffer.number}")
end
new_lines = content.split("\n")
result[:new_line_count] = new_lines.length
target_buffer.lines = new_lines
result[:updated] = true
client.command("write")
result[:saved] = true
log(:info, "Saved updated buffer: #{result[:actual_path]}")
client.command("edit")
result[:reloaded] = true
result[:message] = "Successfully updated, saved, and reloaded buffer"
client.shutdown
rescue => e
log(:error, "Error updating buffer in Neovim using socket #{socket_path}: #{e.message}")
result[:status] = "error"
result[:message] = e.message
result[:updated] = false
result[:saved] = false
result[:reloaded] = false
end
result
end
|