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