-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathclose_all_diff_tabs.lua
More file actions
102 lines (89 loc) · 3.01 KB
/
close_all_diff_tabs.lua
File metadata and controls
102 lines (89 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
--- Tool implementation for closing all diff tabs.
local schema = {
description = "Close all diff tabs in the editor",
inputSchema = {
type = "object",
additionalProperties = false,
["$schema"] = "http://json-schema.org/draft-07/schema#",
},
}
--- Handles the closeAllDiffTabs tool invocation.
-- Closes all diff tabs/windows in the editor.
-- @param _params table The input parameters for the tool (currently unused).
-- @return table MCP-compliant response with content array indicating number of closed tabs.
-- @error table A table with code, message, and data for JSON-RPC error if failed.
local function handler(_params) -- Prefix unused params with underscore
local closed_count = 0
-- Get all windows
local windows = vim.api.nvim_list_wins()
local windows_to_close = {} -- Use set to avoid duplicates
for _, win in ipairs(windows) do
local buf = vim.api.nvim_win_get_buf(win)
local buftype = vim.api.nvim_buf_get_option(buf, "buftype")
local diff_mode = vim.api.nvim_win_get_option(win, "diff")
local should_close = false
-- Check if this is a diff window
if diff_mode then
should_close = true
end
-- Also check for diff-related buffer names or types
local buf_name = vim.api.nvim_buf_get_name(buf)
if buf_name:match("%.diff$") or buf_name:match("diff://") then
should_close = true
end
-- Check for special diff buffer types
if buftype == "nofile" and buf_name:match("^fugitive://") then
should_close = true
end
-- Add to close set only once (prevents duplicates)
if should_close then
windows_to_close[win] = true
end
end
-- Close the identified diff windows
for win, _ in pairs(windows_to_close) do
if vim.api.nvim_win_is_valid(win) then
local success = pcall(vim.api.nvim_win_close, win, false)
if success then
closed_count = closed_count + 1
end
end
end
-- Also check for buffers that might be diff-related but not currently in windows
local buffers = vim.api.nvim_list_bufs()
for _, buf in ipairs(buffers) do
if vim.api.nvim_buf_is_loaded(buf) then
local buf_name = vim.api.nvim_buf_get_name(buf)
local buftype = vim.api.nvim_buf_get_option(buf, "buftype")
-- Check for diff-related buffers
if
buf_name:match("%.diff$")
or buf_name:match("diff://")
or (buftype == "nofile" and buf_name:match("^fugitive://"))
then
-- Delete the buffer if it's not in any window
local buf_windows = vim.fn.win_findbuf(buf)
if #buf_windows == 0 then
local success = pcall(vim.api.nvim_buf_delete, buf, { force = true })
if success then
closed_count = closed_count + 1
end
end
end
end
end
-- Return MCP-compliant format matching VS Code extension
return {
content = {
{
type = "text",
text = "CLOSED_" .. closed_count .. "_DIFF_TABS",
},
},
}
end
return {
name = "closeAllDiffTabs",
schema = schema,
handler = handler,
}