More python stuff

This commit is contained in:
Smaug123
2024-03-29 11:58:52 +00:00
parent 6256ad908f
commit 59e1e8637c
3 changed files with 71 additions and 1 deletions

View File

@@ -272,7 +272,7 @@
vimdiffAlias = true; vimdiffAlias = true;
withPython3 = true; withPython3 = true;
extraLuaConfig = builtins.readFile ./nvim/build-utils.lua + "\n" + builtins.readFile ./nvim/dotnet.lua + "\n" + builtins.replaceStrings ["%PYTHONENV%"] ["${pythonEnv}"] (builtins.readFile ./nvim/init.lua); extraLuaConfig = builtins.readFile ./nvim/build-utils.lua + "\n" + builtins.readFile ./nvim/dotnet.lua + "\n" + builtins.replaceStrings ["%PYTHONENV%"] ["${pythonEnv}"] (builtins.readFile ./nvim/init.lua) + "\n" + builtins.readFile ./nvim/python.lua;
package = nixpkgs.neovim-nightly; package = nixpkgs.neovim-nightly;
}; };

View File

@@ -0,0 +1,69 @@
local function pytest_on_line(_, _, _) end
local function pytest_on_complete(_, code, _)
if code ~= 0 then
print("Exit code " .. code)
end
end
function RunPythonTestAtCursor()
local api = vim.api
-- Get the current buffer and cursor position
local bufnr = api.nvim_get_current_buf()
local line_nr = api.nvim_win_get_cursor(0)[1]
local filename = api.nvim_buf_get_name(bufnr)
-- Read the file content
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
-- Find the test function
local test_name = nil
for i = line_nr, 1, -1 do
local line = lines[i]
if line:match("^def test_") then
test_name = line:match("^def (%S+)%(")
break
end
end
if test_name then
-- Run pytest for the found test function
local context = BuildUtils.create_window()
BuildUtils.run(
"pytest",
{ filename .. "::" .. test_name },
"Run PyTest (" .. test_name .. ")",
context,
pytest_on_line,
pytest_on_complete
)
else
print("No test function found at or above line " .. line_nr)
end
end
function RunPythonTestsInFile()
local file_path = vim.fn.expand("%:p")
local context = BuildUtils.create_window()
BuildUtils.run("pytest", { file_path }, "Run PyTest", context, pytest_on_line, pytest_on_complete)
end
function RunAllPythonTests()
local context = BuildUtils.create_window()
BuildUtils.run("pytest", {}, "Run PyTest", context, pytest_on_line, pytest_on_complete)
end
do
local whichkey = require("which-key")
whichkey.register({
p = {
name = "Python-related commands",
t = {
"Run Python tests",
f = { RunPythonTestsInFile, "Run Python tests in the current file" },
a = { RunAllPythonTests, "Run all Python tests" },
c = { RunPythonTestAtCursor, "Run the Python test under the cursor" },
},
},
}, { prefix = vim.api.nvim_get_var("maplocalleader") })
end

View File

@@ -3,6 +3,7 @@ local venv_selector = require("venv-selector")
venv_selector.setup({ venv_selector.setup({
changed_venv_hooks = { venv_selector.hooks.pyright }, changed_venv_hooks = { venv_selector.hooks.pyright },
name = { "venv", ".venv" }, name = { "venv", ".venv" },
search_venv_managers = true,
}) })
vim.api.nvim_create_autocmd("VimEnter", { vim.api.nvim_create_autocmd("VimEnter", {