-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e83199a
commit 3a951f2
Showing
16 changed files
with
412 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
TESTS_INIT=tests/minimal_init.lua | ||
TESTS_DIR=tests/ | ||
|
||
.PHONY: test | ||
|
||
test: | ||
@nvim \ | ||
--headless \ | ||
--noplugin \ | ||
-u ${TESTS_INIT} \ | ||
-c "PlenaryBustedDirectory ${TESTS_DIR} { minimal_init = '${TESTS_INIT}' }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,87 @@ | ||
local get_diagnostics = require("wtf.get_diagnostics") | ||
local get_filetype = require("wtf.get_filetype") | ||
local get_programming_language = require("wtf.utils.get_programming_language") | ||
local gpt = require("wtf.gpt") | ||
local display_popup = require("wtf.display_popup") | ||
local config = require("wtf.config") | ||
|
||
local function get_default_additional_instructions() | ||
return vim.g.wtf_default_additional_instructions or "" | ||
end | ||
local M = {} | ||
|
||
local function get_language() | ||
return vim.g.wtf_language | ||
local function get_content_between_lines(start_line, end_line) | ||
local lines = {} | ||
for line_num = start_line, end_line do | ||
local line = string.format("%d: %s", line_num, vim.fn.getline(line_num)) | ||
table.insert(lines, line) | ||
end | ||
return table.concat(lines, "\n") | ||
end | ||
|
||
local ai = function(additional_instructions) | ||
local diagnostics = get_diagnostics() | ||
local filetype = get_filetype() | ||
M.diagnose = function(line1, line2, instructions) | ||
local diagnostics = get_diagnostics(line1, line2) | ||
local programming_language = get_programming_language() | ||
local should_send_code = config.options.context | ||
|
||
if diagnostics == nil then | ||
return print("No diagnostics found!") | ||
if next(diagnostics) == nil then | ||
local message = "No diagnostics found!" | ||
vim.notify(message, vim.log.levels.WARN) | ||
return message | ||
end | ||
|
||
local concatenatedDiagnostics = "" | ||
for _, diagnostic in ipairs(diagnostics) do | ||
concatenatedDiagnostics = concatenatedDiagnostics .. diagnostic.severity .. ": " .. diagnostic.message .. "\n" | ||
for i, diagnostic in ipairs(diagnostics) do | ||
concatenatedDiagnostics = concatenatedDiagnostics | ||
.. i | ||
.. ". Issue " | ||
.. i | ||
.. "\n\t- Location: Line " | ||
.. diagnostic.line_number | ||
.. "\n\t- Severity: " | ||
.. diagnostic.severity | ||
.. "\n\t- Message: " | ||
.. diagnostic.message | ||
.. "\n" | ||
end | ||
|
||
local line = vim.fn.getline(".") | ||
local code = get_content_between_lines(line1, line2) | ||
|
||
local payload = "The coding language is " | ||
.. filetype | ||
local payload = "The programming language is " | ||
.. programming_language | ||
.. ".\nThis is a list of the diagnostic messages: \n" | ||
.. concatenatedDiagnostics | ||
.. "This is the line of code for context: \n" | ||
.. line | ||
|
||
if get_default_additional_instructions() ~= "" then | ||
payload = payload .. "\n" .. get_default_additional_instructions() | ||
if should_send_code then | ||
payload = payload .. "This is the code for context: \n" .. "```\n" .. code .. "\n```" | ||
end | ||
|
||
if additional_instructions then | ||
payload = payload .. "\n" .. additional_instructions | ||
if config.options.additional_instructions then | ||
payload = payload .. "\n" .. config.options.additional_instructions | ||
end | ||
|
||
if get_language() ~= "" and get_language() ~= "english" then | ||
payload = payload .. "\nRespond only in " .. get_language() | ||
if instructions then | ||
payload = payload .. "\n" .. instructions | ||
end | ||
|
||
print("Generating explanation...") | ||
local language = config.options.language | ||
if language and language ~= "english" then | ||
payload = payload .. "\nRespond only in " .. language | ||
end | ||
|
||
vim.notify("Generating explanation...", vim.log.levels.INFO) | ||
|
||
local messages = { | ||
{ | ||
role = "system", | ||
content = [[You are an expert coder and helpful assistant who can help debug code diagnostics, such as warning and error messages. | ||
When appropriate, give solutions with code snippets as fenced codeblocks with a language identifier to enable syntax highlighting."]], | ||
When appropriate, give solutions with code snippets as fenced codeblocks with a language identifier to enable syntax highlighting.]], | ||
}, | ||
{ | ||
role = "user", | ||
content = payload, | ||
}, | ||
} | ||
|
||
gpt.request(messages, display_popup) | ||
return gpt.request(messages, display_popup) | ||
end | ||
|
||
return ai | ||
M.get_status = gpt.get_status | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
local search_engines = require("wtf.search_engines") | ||
|
||
local M = {} | ||
|
||
M.options = {} | ||
|
||
function M.setup(opts) | ||
local default_opts = { | ||
openai_api_key = nil, | ||
openai_model_id = "gpt-3.5-turbo", | ||
language = "english", | ||
search_engine = "google", | ||
context = true, | ||
additional_instructions = nil, | ||
popup_type = "popup", | ||
hooks = { | ||
request_started = nil, | ||
request_finished = nil, | ||
}, | ||
} | ||
|
||
-- Merge default_opts with opts | ||
opts = vim.tbl_deep_extend("force", default_opts, opts or {}) | ||
|
||
vim.validate({ | ||
openai_api_key = { opts.openai_api_key, { "string", "nil" } }, | ||
openai_model_id = { opts.openai_model_id, "string" }, | ||
language = { opts.language, "string" }, | ||
search_engine = { | ||
opts.search_engine, | ||
function(search_engine) | ||
local selected_engine = search_engines.sources[search_engine] | ||
|
||
if not selected_engine then | ||
return false | ||
else | ||
return true | ||
end | ||
end, | ||
"supported search engine", | ||
}, | ||
context = { opts.context, "boolean" }, | ||
additional_instructions = { opts.additional_instructions, { "string", "nil" } }, | ||
popup_type = { | ||
opts.popup_type, | ||
function(popup_type) | ||
local popup_types = { "horizontal", "vertical", "popup" } | ||
|
||
for _, valid_type in ipairs(popup_types) do | ||
if popup_type == valid_type then | ||
return true | ||
end | ||
end | ||
return false | ||
end, | ||
"supported popup type", | ||
}, | ||
request_started = { | ||
opts.hooks.request_started, | ||
{ "function", "nil" }, | ||
}, | ||
request_finished = { | ||
opts.hooks.request_finished, | ||
{ "function", "nil" }, | ||
}, | ||
}) | ||
|
||
M.options = vim.tbl_extend("force", M.options, opts) | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.