-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathcheck_document_dirty.lua
More file actions
70 lines (63 loc) · 1.9 KB
/
check_document_dirty.lua
File metadata and controls
70 lines (63 loc) · 1.9 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
--- Tool implementation for checking if a document is dirty.
local schema = {
description = "Check if a document has unsaved changes (is dirty)",
inputSchema = {
type = "object",
properties = {
filePath = {
type = "string",
description = "Path to the file to check",
},
},
required = { "filePath" },
additionalProperties = false,
["$schema"] = "http://json-schema.org/draft-07/schema#",
},
}
--- Handles the checkDocumentDirty tool invocation.
-- Checks if the specified file (buffer) has unsaved changes.
-- @param params table The input parameters for the tool.
-- @field params.filePath string Path to the file to check.
-- @return table A table indicating if the document is dirty.
-- @error table A table with code, message, and data for JSON-RPC error if failed.
local function handler(params)
if not params.filePath then
error({ code = -32602, message = "Invalid params", data = "Missing filePath parameter" })
end
local bufnr = vim.fn.bufnr(params.filePath)
if bufnr == -1 then
-- Return success: false when document not open, matching VS Code behavior
return {
content = {
{
type = "text",
text = vim.json.encode({
success = false,
message = "Document not open: " .. params.filePath,
}, { indent = 2 }),
},
},
}
end
local is_dirty = vim.api.nvim_buf_get_option(bufnr, "modified")
local is_untitled = vim.api.nvim_buf_get_name(bufnr) == ""
-- Return MCP-compliant format with JSON-stringified result
return {
content = {
{
type = "text",
text = vim.json.encode({
success = true,
filePath = params.filePath,
isDirty = is_dirty,
isUntitled = is_untitled,
}, { indent = 2 }),
},
},
}
end
return {
name = "checkDocumentDirty",
schema = schema,
handler = handler,
}