|
| 1 | +local utils = require("utils") |
| 2 | +local opt = vim.opt |
| 3 | + |
| 4 | +opt.wrap = false |
| 5 | +opt.sidescroll = 5 |
| 6 | +opt.sidescrolloff = 2 |
| 7 | +opt.colorcolumn = "100" |
| 8 | + |
| 9 | +opt.tabstop = 4 -- Number of visual spaces per TAB |
| 10 | +opt.softtabstop = 4 -- Number of spaces in tab when editing |
| 11 | +opt.shiftwidth = 4 -- Number of spaces to use for autoindent |
| 12 | +opt.expandtab = true -- Expand tab to spaces so that tabs are spaces |
| 13 | + |
| 14 | +local get_proj_root = function() |
| 15 | + local project_marker = { ".git", "pyproject.toml" } |
| 16 | + local project_root = vim.fs.root(0, project_marker) |
| 17 | + |
| 18 | + return project_root |
| 19 | +end |
| 20 | + |
| 21 | +local get_py_env = function() |
| 22 | + local project_root = get_proj_root() |
| 23 | + |
| 24 | + local venv_name = utils.get_virtual_env() |
| 25 | + |
| 26 | + if venv_name ~= "" then |
| 27 | + return "plain_venv" |
| 28 | + end |
| 29 | + |
| 30 | + -- check if this is uv-managed project |
| 31 | + local uv_lock_path = vim.fs.joinpath(project_root, "uv.lock") |
| 32 | + if vim.fn.filereadable(uv_lock_path) == 1 then |
| 33 | + return "uv" |
| 34 | + end |
| 35 | + |
| 36 | + return "" |
| 37 | +end |
| 38 | + |
| 39 | +local py_env = get_py_env() |
| 40 | + |
| 41 | +if vim.fn.exists(":AsyncRun") == 2 then |
| 42 | + local py_cmd = "python" |
| 43 | + |
| 44 | + if py_env == "uv" then |
| 45 | + py_cmd = "uv run python" |
| 46 | + end |
| 47 | + |
| 48 | + local rhs = string.format(":<C-U>AsyncRun %s -u %%<CR>", py_cmd) |
| 49 | + |
| 50 | + vim.keymap.set("n", "<F9>", rhs, { |
| 51 | + buffer = true, |
| 52 | + silent = true, |
| 53 | + }) |
| 54 | +end |
| 55 | + |
| 56 | +-- format current file |
| 57 | + |
| 58 | +local py_fmt_cmd = "!black" |
| 59 | +if py_env == "uv" then |
| 60 | + py_fmt_cmd = "!uv run black" |
| 61 | +end |
| 62 | + |
| 63 | +local rhs = string.format("<cmd>silent %s %%<CR>", py_fmt_cmd) |
| 64 | +vim.keymap.set("n", "<space>f", rhs, { |
| 65 | + buffer = true, |
| 66 | + silent = true, |
| 67 | +}) |
0 commit comments