Skip to content

Commit 50ad77e

Browse files
committed
feat: add upward .sgpt.md search and inject md_context into templates
1 parent 68cdf64 commit 50ad77e

15 files changed

Lines changed: 32 additions & 16 deletions

lua/simplegpt/tpl.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,24 @@ function M.RegQAUI:get_special()
418418
end
419419

420420
-- 7) Get the content in `.sgpt.md` and render it as {{md_context}}
421-
local md_file_path = ".sgpt.md"
422-
if vim.loop.fs_stat(md_file_path) then
421+
-- Search upward from cwd to filesystem root, stop at the first match
422+
local function find_sgpt_md()
423+
local dir = vim.fn.getcwd()
424+
while true do
425+
local candidate = dir .. "/.sgpt.md"
426+
if vim.loop.fs_stat(candidate) then
427+
return candidate
428+
end
429+
local parent = vim.fn.fnamemodify(dir, ":h")
430+
if parent == dir then
431+
break -- reached filesystem root
432+
end
433+
dir = parent
434+
end
435+
return nil
436+
end
437+
local md_file_path = find_sgpt_md()
438+
if md_file_path then
423439
local md_content = {}
424440
for line in io.lines(md_file_path) do
425441
table.insert(md_content, line)

qa_tpls/code_explain.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"t": "You are an expert in programming.\n{% if p %}\nHere are a list of files for reference.\n{{p-}}\n{% endif %}\n\nWe have a file named {{filename}} with content:\n````{{filetype}} \n{{content}}\n````\n\n{%if terminal%}\nHere is some related output from the terminal\n```\n{{terminal}}\n```\n{% endif %}\n\n\n{% if visual %}\nYou are focusing on the following content\n```\n{{visual}}\n```\n{% else %}\nDescribe the full file. If parts depend on each other, start with the top-level sections, then explain step by step how each function is used. Focus on runtime dependencies, for example: the main entry is A, which calls B and C during execution. Keep it concise.\n{% endif %}\n\n{{q-}}"}
1+
{"t": "You are an expert in programming.\n\n{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\n{% if p %}\nHere are a list of files for reference.\n{{p-}}\n{% endif %}\n\nWe have a file named {{filename}} with content:\n````{{filetype}} \n{{content}}\n````\n\n{%if terminal%}\nHere is some related output from the terminal\n```\n{{terminal}}\n```\n{% endif %}\n\n\n{% if visual %}\nYou are focusing on the following content\n```\n{{visual}}\n```\n{% else %}\nDescribe the full file. If parts depend on each other, start with the top-level sections, then explain step by step how each function is used. Focus on runtime dependencies, for example: the main entry is A, which calls B and C during execution. Keep it concise.\n{% endif %}\n\n{{q-}}"}

qa_tpls/code_review.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"t": "You are an expert in coding.\n\nHere is the output of `git --no-pager diff` in your terminal\n```\n{{full_content}}\n```\n\n{{all_buf}}\n\nPlease review the changes. Please point out the errors that will certainly introduce bugs in the program. \nWhen you point them out, please specify the concrete exception that will be raised or the side effect/consequence.\nOnly point out the errors that are brought about by the changes.\nIf you want to point out other errors you find, please use a clear divider like \"---\" to distinguish the errors brought about by the changes from others.\n"}
1+
{"t": "You are an expert in coding.\n\n{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\nHere is the output of `git --no-pager diff` in your terminal\n```\n{{full_content}}\n```\n\n{{all_buf}}\n\nPlease review the changes. Please point out the errors that will certainly introduce bugs in the program. \nWhen you point them out, please specify the concrete exception that will be raised or the side effect/consequence.\nOnly point out the errors that are brought about by the changes.\nIf you want to point out other errors you find, please use a clear divider like \"---\" to distinguish the errors brought about by the changes from others.\n"}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"q": "The focused part contains requirements:\n- Guidelines for writing text.\n- Some existing text that outlines my initial idea of the requirements.\n\nPlease rewrite the focused part in a more fluent way to meet the requirements.\nAvoid using complex words. Change the original wording only when necessary.\nJust output the new paragraph without extra explanation.", "t": "You are an expert in writing all kinds of materials.\nWe have a file with content:\n```{{filetype}} \n{{context}}\n```\n{% if p %}\nHere are a list of files for reference.\n{{p-}}\n{% endif %}\n\nYou are focusing on following part\n```\n{{visual}}\n```\n{{f-}}\n{{q}}"}
1+
{"q": "The focused part contains requirements:\n- Guidelines for writing text.\n- Some existing text that outlines my initial idea of the requirements.\n\nPlease rewrite the focused part in a more fluent way to meet the requirements.\nAvoid using complex words. Change the original wording only when necessary.\nJust output the new paragraph without extra explanation.", "t": "You are an expert in writing all kinds of materials.\n\n{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\nWe have a file with content:\n```{{filetype}} \n{{context}}\n```\n{% if p %}\nHere are a list of files for reference.\n{{p-}}\n{% endif %}\n\nYou are focusing on following part\n```\n{{visual}}\n```\n{{f-}}\n{{q}}"}

qa_tpls/condensing.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"q": "Please make the content shorter and keep your best efforts to keep all the information.\nYou can fix the grammar error.\nJust output the new paragraph without extra explanation\nDon't use back quotes to fence the output.\n", "t": "You are an expert in writing. You are good at producing content that is fluent and easy to read.\nWe have a file with content:\n```{{filetype}} \n{{context}}\n```\nYou focus on this paragraph.\n```\n{{visual}}\n```\n{{q}}"}
1+
{"q": "Please make the content shorter and keep your best efforts to keep all the information.\nYou can fix the grammar error.\nJust output the new paragraph without extra explanation\nDon't use back quotes to fence the output.\n", "t": "You are an expert in writing. You are good at producing content that is fluent and easy to read.\n\n{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\nWe have a file with content:\n```{{filetype}} \n{{context}}\n```\nYou focus on this paragraph.\n```\n{{visual}}\n```\n{{q}}"}

qa_tpls/continue.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"q": "Please continue the context. If there are questions, please answer it.", "t": "You are an expert in writing all kinds of materials.\nWe have a file with content:\n```{{filetype}} \n{{context}}\n```\nYou are focusing on following part\n```\n{{visual}}\n```\n{{q}}"}
1+
{"q": "Please continue the context. If there are questions, please answer it.", "t": "You are an expert in writing all kinds of materials.\n\n{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\nWe have a file with content:\n```{{filetype}} \n{{context}}\n```\nYou are focusing on following part\n```\n{{visual}}\n```\n{{q}}"}

qa_tpls/dictionary.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"t": "We have a file with content:\n```{{filetype}} \n{{context}}\n```\nYou are focusing on following part\n```\n{{visual}}\n```\n\nIf it is a word, the explanation should include the following parts:\n- The word's phonetic transcription\n- The common meanings of the word (the format can refer to the Collins Dictionary) & related example sentences\n- The meaning of the word in the context of this text\n- The etymology of the word\n- How to memorize this word\n\nIf it is a phrase, the explanation should include the following parts:\n- The meaning of the phrase\n- How these words derive the meaning of the phrase\n- The origin of the phrase\n- How to memorize this phrase\n- Common example sentences of the phrase\n\nPlease response mainly in language:{{l}}", "l": "Chinese"}
1+
{"t": "{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\nWe have a file with content:\n```{{filetype}} \n{{context}}\n```\nYou are focusing on following part\n```\n{{visual}}\n```\n\nIf it is a word, the explanation should include the following parts:\n- The word's phonetic transcription\n- The common meanings of the word (the format can refer to the Collins Dictionary) & related example sentences\n- The meaning of the word in the context of this text\n- The etymology of the word\n- How to memorize this word\n\nIf it is a phrase, the explanation should include the following parts:\n- The meaning of the phrase\n- How these words derive the meaning of the phrase\n- The origin of the phrase\n- How to memorize this phrase\n- Common example sentences of the phrase\n\nPlease response mainly in language:{{l}}", "l": "Chinese"}

qa_tpls/explain_text.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"t": "以下是文章中的上下文\n```\n{{context}}\n```\n您是一位精通各种知识和中英文的老师, \n我会给你发送一些英文文字, 希望您对其进行翻译,并解释其意义\n\n例如:\nUser:\n```\nCompetitive bids are used to set the price of the debt issue, while\nnoncompetitive bids are guaranteed to have their allocation met at the price\ndetermined by the competitive bids. \n```\nAssistant\n```\n翻译: 通过竞争性投标来确定债券发行的价格,而非竞争性投标则保证以竞争性投标确定的价格满足其分配\n解释:在债券发行过程中,投标者可以通过竞争性投标来确定债券的价格,而那些选择非竞争性投标的投标者则不需要参与竞价,他们的投标将以竞争性投标确定的价格得到满足。\n```\n\n新一轮对对话:\nUser:\n```\n{{visual}}\n```\n\n请回答:"}
1+
{"t": "{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\n以下是文章中的上下文\n```\n{{context}}\n```\n您是一位精通各种知识和中英文的老师, \n我会给你发送一些英文文字, 希望您对其进行翻译,并解释其意义\n\n例如:\nUser:\n```\nCompetitive bids are used to set the price of the debt issue, while\nnoncompetitive bids are guaranteed to have their allocation met at the price\ndetermined by the competitive bids. \n```\nAssistant\n```\n翻译: 通过竞争性投标来确定债券发行的价格,而非竞争性投标则保证以竞争性投标确定的价格满足其分配\n解释:在债券发行过程中,投标者可以通过竞争性投标来确定债券的价格,而那些选择非竞争性投标的投标者则不需要参与竞价,他们的投标将以竞争性投标确定的价格得到满足。\n```\n\n新一轮对对话:\nUser:\n```\n{{visual}}\n```\n\n请回答:"}

qa_tpls/file_edit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"t": "You are an expert in programming.\n{{p-}}\n\nWe have a file with following content:\n```{{filetype}} \n{{full_content}}\n```\n\nYou are going to edit this file to accomplish the following task:\n```\n{{q-}}\n```\n\n{% if visual %}\nYou are focusing on the following content\n```\n{{visual}}\n```\n{% endif %}\n\nPlease output the entire content to replace the file to be edit.\n\nNo extra explanations. No block quotes\nTry to keep all the comments (You can modify them to make it better).\nKeep original indent so that we can replace the original code with the newly generated one."}
1+
{"t": "You are an expert in programming.\n\n{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\n{{p-}}\n\nWe have a file with following content:\n```{{filetype}} \n{{full_content}}\n```\n\nYou are going to edit this file to accomplish the following task:\n```\n{{q-}}\n```\n\n{% if visual %}\nYou are focusing on the following content\n```\n{{visual}}\n```\n{% endif %}\n\nPlease output the entire content to replace the file to be edit.\n\nNo extra explanations. No block quotes\nTry to keep all the comments (You can modify them to make it better).\nKeep original indent so that we can replace the original code with the newly generated one."}

qa_tpls/fix_bug_with_err.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"q": "Please return the solution to fix the errors.\n", "t": "You are an expert in programming.\n\n{% if p %}\nHere are a list of files for reference.\n{{p-}}\n{% endif %}\n\nWe have a file named {{filename}} with content:\n````{{filetype}} \n{{content}}\n````\n\n{%if terminal%}\nYou encountered error when running or compiling it\n```\n{{terminal}}\n```\n{% endif %}\n\n\n{% if visual %}The error are potentially caused by the following code block(We call it *focused code block*).\n```{{filetype}} \n{{visual}}\n```\nPlease only return the code to replace the *focused code block*{% else %}\n{{f-}}\n{% endif %}\n\n{{q}}"}
1+
{"q": "Please return the solution to fix the errors.\n", "t": "You are an expert in programming.\n\n{% if md_context %}\nYou have some information for reference.\n````markdown\n{{md_context}}\n````\n{% endif %}\n\n{% if p %}\nHere are a list of files for reference.\n{{p-}}\n{% endif %}\n\nWe have a file named {{filename}} with content:\n````{{filetype}} \n{{content}}\n````\n\n{%if terminal%}\nYou encountered error when running or compiling it\n```\n{{terminal}}\n```\n{% endif %}\n\n\n{% if visual %}The error are potentially caused by the following code block(We call it *focused code block*).\n```{{filetype}} \n{{visual}}\n```\nPlease only return the code to replace the *focused code block*{% else %}\n{{f-}}\n{% endif %}\n\n{{q}}"}

0 commit comments

Comments
 (0)