Skip to content

Commit

Permalink
Adds @include command
Browse files Browse the repository at this point in the history
Behaves exactly like @file, but does not inject the file name or a
backtick fence around the contents
  • Loading branch information
Odie committed Aug 4, 2024
1 parent 89e1545 commit d8a1651
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lua/gp/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ function source.complete(self, request, callback)

local items = {}
local isIncomplete = true
local cmd_type = cmd_parts[1]

if cmd_parts[1]:match("@file") then
if cmd_type:match("@file") or cmd_type:match("@include") then
-- What's the path we're trying to provide completion for?
local path = cmd_parts[2] or ""

Expand All @@ -161,7 +162,7 @@ function source.complete(self, request, callback)
-- Say that the entire list has been provided
-- cmp won't call us again to provide an updated list
isIncomplete = false
elseif cmd_parts[1]:match("@code") then
elseif cmd_type:match("@code") then
local partial_fn_name = cmd_parts[2] or ""

-- When the user confirms completion of an item, we alter the
Expand All @@ -180,6 +181,7 @@ function source.complete(self, request, callback)
items = {
{ label = "code", kind = require("cmp").lsp.CompletionItemKind.Keyword },
{ label = "file", kind = require("cmp").lsp.CompletionItemKind.Keyword },
{ label = "include", kind = require("cmp").lsp.CompletionItemKind.Keyword },
}
isIncomplete = false
else
Expand Down
15 changes: 12 additions & 3 deletions lua/gp/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ function Context.insert_contexts(msg)
for cmd in msg:gmatch("@file:[%w%p]+") do
table.insert(cmds, cmd)
end
for cmd in msg:gmatch("@include:[%w%p]+") do
table.insert(cmds, cmd)
end
for cmd in msg:gmatch("@code:[%w%p]+[:%w_-]+") do
table.insert(cmds, cmd)
end
Expand All @@ -108,8 +111,9 @@ function Context.insert_contexts(msg)
-- inserted as additional context
for _, cmd in ipairs(cmds) do
local cmd_parts = Context.cmd_split(cmd)
local cmd_type = cmd_parts[1]

if cmd_parts[1] == "@file" then
if cmd_type == "@file" or cmd_type == "@include" then
-- Read the reqested file and produce a msg snippet to be joined later
local filepath = cmd_parts[2]

Expand All @@ -118,10 +122,15 @@ function Context.insert_contexts(msg)

local content = read_file(fullpath)
if content then
local result = string.format("%s\n```%s```", filepath, content)
local result
if cmd_type == "@file" then
result = string.format("%s\n```%s```", filepath, content)
else
result = content
end
table.insert(context_texts, result)
end
elseif cmd_parts[1] == "@code" then
elseif cmd_type == "@code" then
local rel_path = cmd_parts[2]
local full_fn_name = cmd_parts[3]
if not rel_path or not full_fn_name then
Expand Down

0 comments on commit d8a1651

Please sign in to comment.