|
| 1 | +local Logger = require("eca.logger") |
| 2 | +local cmp = require("cmp") |
| 3 | +---@param context eca.ChatContext |
| 4 | +---@return lsp.CompletionItem |
| 5 | +local function as_completion_item(context) |
| 6 | + ---@type lsp.CompletionItem |
| 7 | + ---@diagnostic disable-next-line: missing-fields |
| 8 | + local item = {} |
| 9 | + if context.type == "file" then |
| 10 | + item.label = string.format("@%s", vim.fn.fnamemodify(context.path, ":.")) |
| 11 | + item.kind = cmp.lsp.CompletionItemKind.File |
| 12 | + item.data = { |
| 13 | + context_item = context, |
| 14 | + } |
| 15 | + elseif context.type == "directory" then |
| 16 | + item.label = string.format("@%s", vim.fn.fnamemodify(context.path, ":.")) |
| 17 | + item.kind = cmp.lsp.CompletionItemKind.Folder |
| 18 | + elseif context.type == "web" then |
| 19 | + item.label = context.url |
| 20 | + item.kind = cmp.lsp.CompletionItemKind.File |
| 21 | + elseif context.type == "repoMap" then |
| 22 | + item.label = "repoMap" |
| 23 | + item.kind = cmp.lsp.CompletionItemKind.Module |
| 24 | + item.detail = "Summary view of workspace files." |
| 25 | + elseif context.type == "mcpResource" then |
| 26 | + item.label = string.format("%s:%s", context.server, context.name) |
| 27 | + item.kind = cmp.lsp.CompletionItemKind.Struct |
| 28 | + item.detail = context.description |
| 29 | + end |
| 30 | + if not item.label then |
| 31 | + return {} |
| 32 | + end |
| 33 | + return item |
| 34 | +end |
| 35 | + |
| 36 | +-- Query server for available contexts |
| 37 | +-- @param query string |
| 38 | +-- |
| 39 | +local function query_server_contexts(query, callback) |
| 40 | + local eca = require("eca") |
| 41 | + if not eca.server or not eca.server:is_running() then |
| 42 | + callback({}) |
| 43 | + return |
| 44 | + end |
| 45 | + |
| 46 | + eca.server:send_request("chat/queryContext", { query = query }, function(err, result) |
| 47 | + Logger.debug("chat/queryContext query " .. vim.inspect({ query = query })) |
| 48 | + if err then |
| 49 | + Logger.debug("chat/queryContext err: " .. err) |
| 50 | + callback({}) |
| 51 | + return |
| 52 | + end |
| 53 | + |
| 54 | + local items = vim.iter(result.contexts):map(as_completion_item):totable() |
| 55 | + callback({ items = items, isIncomplete = true }) |
| 56 | + end) |
| 57 | +end |
| 58 | + |
| 59 | +local source = {} |
| 60 | + |
| 61 | +function source.new() |
| 62 | + return setmetatable({}, { __index = source }) |
| 63 | +end |
| 64 | + |
| 65 | +function source:get_trigger_characters() |
| 66 | + return { "@" } |
| 67 | +end |
| 68 | + |
| 69 | +function source:get_keyword_pattern() |
| 70 | + return [[\k\+]] |
| 71 | +end |
| 72 | + |
| 73 | +function source:is_available() |
| 74 | + return vim.bo.filetype == "eca-input" |
| 75 | +end |
| 76 | + |
| 77 | +---@param cursor_line string |
| 78 | +---@param cursor_position lsp.Position|vim.Position |
| 79 | +---@return string |
| 80 | +local function get_query(cursor_line, cursor_position) |
| 81 | + local before_cursor = cursor_line:sub(1, cursor_position.col) |
| 82 | + ---@type string[] |
| 83 | + local matches = {} |
| 84 | + local it = before_cursor:gmatch("@([%w%./_\\%-~]*)") |
| 85 | + for match in it do |
| 86 | + table.insert(matches, match) |
| 87 | + end |
| 88 | + return matches[#matches] |
| 89 | +end |
| 90 | + |
| 91 | +---@param params cmp.SourceCompletionApiParams |
| 92 | +---@diagnostic disable-next-line: unused-local |
| 93 | +function source:complete(params, callback) |
| 94 | + local query = get_query(params.context.cursor_line, params.context.cursor) |
| 95 | + if query then |
| 96 | + query_server_contexts(query, function(items) |
| 97 | + callback(items) |
| 98 | + end) |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +--- Taken from https://github.com/hrsh7th/cmp-path/blob/9a16c8e5d0be845f1d1b64a0331b155a9fe6db4d/lua/cmp_path/init.lua |
| 103 | +--- Show a small preview of file contexft items in the documentation window. |
| 104 | +---@param data eca.ChatContext |
| 105 | +---@return lsp.MarkupContent |
| 106 | +source._get_documentation = function(_, data, count) |
| 107 | + if data and data.path then |
| 108 | + local filename = data.path |
| 109 | + local binary = assert(io.open(data.path, "rb")) |
| 110 | + local first_kb = binary:read(1024) |
| 111 | + if first_kb and first_kb:find("\0") then |
| 112 | + return { kind = vim.lsp.protocol.MarkupKind.PlainText, value = "binary file" } |
| 113 | + end |
| 114 | + |
| 115 | + local content = io.lines(data.path) |
| 116 | + |
| 117 | + --- Try to support line ranges, I don't know if this works or not yet |
| 118 | + local start = data.lines_range and data.lines_range.start or 1 |
| 119 | + local last = data.lines_range and data.lines_range["end"] or count |
| 120 | + local skip_lines = start - 1 |
| 121 | + local take_lines = last - start |
| 122 | + local contents = vim.iter(content):skip(skip_lines):take(take_lines):totable() |
| 123 | + |
| 124 | + local filetype = vim.filetype.match({ filename = filename }) |
| 125 | + if not filetype then |
| 126 | + return { kind = vim.lsp.protocol.MarkupKind.PlainText, value = table.concat(contents, "\n") } |
| 127 | + end |
| 128 | + |
| 129 | + table.insert(contents, 1, "```" .. filetype) |
| 130 | + table.insert(contents, "```") |
| 131 | + return { kind = vim.lsp.protocol.MarkupKind.Markdown, value = table.concat(contents, "\n") } |
| 132 | + end |
| 133 | + return {} |
| 134 | +end |
| 135 | + |
| 136 | +---@param completion_item lsp.CompletionItem |
| 137 | +function source:resolve(completion_item, callback) |
| 138 | + if completion_item.data then |
| 139 | + local context_item = completion_item.data.context_item |
| 140 | + ---@cast context_item eca.ChatContext |
| 141 | + if context_item.type == "file" then |
| 142 | + completion_item.documentation = self:_get_documentation(context_item, 20) |
| 143 | + end |
| 144 | + callback(completion_item) |
| 145 | + end |
| 146 | +end |
| 147 | + |
| 148 | +return source |
0 commit comments