Merge branch 'nvim-develop' of git.pro100code.ru:t0xa/t0vim into nvim-develop

This commit is contained in:
ashalimov 2025-02-03 09:00:37 +03:00
commit 835d2fa3c4
6 changed files with 117 additions and 10 deletions

View file

@ -1 +0,0 @@
print("Hello from golang file")

59
lua/custom/README.md Normal file
View file

@ -0,0 +1,59 @@
# Справка по интересным моментам в коде
## Функция получения версии `python` в `lsp.lua`
```lua
local function get_python_version()
local handle = io.popen("python --version 2>&1") -- Redirect stderr to stdout
if handle then
local result = handle:read("*a")
handle:close()
-- Check if the output contains a version number
local version = result:match("Python (%d+%.%d+%.%d+)")
if version then
return version
end
end
handle = io.popen("python3 --version 2>&1") -- Redirect stderr to stdout
if handle then
local result = handle:read("*a")
handle:close()
-- Check if the output contains a version number
local version = result:match("Python (%d+%.%d+%.%d+)")
if version then
return version
end
end
-- TODO: Add some exception handling if python not installed on user machine
return "Python 0.0.0"
end
```
Что тут есть примечательного:
- Получение данных, выполняя команду в shell оболочке системы с помощью
`io.popen`. *Важно*: Данная функция может работать не во всех ОС
- Функция выполняет команду, указанную в аргументе, и возвращает *файловый
дескриптор*, который можно использовать для чтения вывода команды (или
записи в нее)
Разберем флоу подробнее:
Если про `python --version` - все понятно, то вот:
```lua
2>&1
```
Уже надо прояснить:
- В UNIX-подобных системах есть три стандартных потока - `stdin` = 0; `stdout` = 1;
`strerr` = 2;
- По умолчанию `io.popen` передает только вывод `stdout` в handle, поэтому для обработки
ошибок, надо дополнительно захватывать поток `stderr`
- `2>&1` означает, что мы хотим перенаправить поток ошибок 2 в стандартный вывод 1;
- Это нужно для того, чтобы захватить не только вывод команды, но и возможные ошибки
(например если питон не установлен);
- `>&` - оператор перенаправления потоков.
Дополнительно:
`*a` внутри `read` - конструкция формата чтения файловых объектов в `Lua`. Они
указывают - как именно стоит читать данные из файла или потока. Помимо `a` есть
еще:
- `*a` - читать все содержимое
- `*l` - читать строку
- `*n` - читать число.

View file

@ -92,13 +92,42 @@ local ensure_installed = {
"prettier",
}
local function get_python_version()
local handle = io.popen("python --version 2>&1") -- Redirect stderr to stdout
if handle then
local result = handle:read("*a")
handle:close()
-- Check if the output contains a version number
local version = result:match("Python (%d+%.%d+%.%d+)")
if version then
return version
end
end
handle = io.popen("python3 --version 2>&1") -- Redirect stderr to stdout
if handle then
local result = handle:read("*a")
handle:close()
-- Check if the output contains a version number
local version = result:match("Python (%d+%.%d+%.%d+)")
if version then
return version
end
end
-- TODO: Add some exception handling if python not installed on user machine
return "Python 0.0.0"
end
-- Now we need to make things work in Astra
-- Main Astra cockblock - low python version, that prevents installation of
-- some modules, so in order to prevent mason from whining - disable some
-- autoinstall modules depending on currently installed python version
-- NOTE: You must install removed dependencies manually
local python_version = io.popen("python --version"):read("*a")
local _, minor, _ = python_version:match("Python (%d+)%.(%d+)%.(%d+)")
local python_version = get_python_version()
if python_version == nil then
python_version = "Python 3.13"
end
local _, minor, _ = python_version:match(".*(%d+)%.(%d+)%.(%d+)")
if tonumber(minor) < 8 then
for i, k in pairs(servers_to_install) do
-- Ruff is not supported for python lower than 3.8

View file

@ -8,13 +8,13 @@ ls.config.set_config {
store_selection_keys = "<c-s>",
}
vim.keymap.set({ "i", "s" }, "<c-k>", function()
vim.keymap.set({ "i", "s" }, "<c-j>", function()
if ls.expand_or_jumpable() then
ls.expand_or_jump()
end
end, { silent = true })
vim.keymap.set({ "i", "s" }, "<c-j>", function()
vim.keymap.set({ "i", "s" }, "<c-k>", function()
if ls.jumpable() then
ls.jump(-1)
end

View file

@ -14,6 +14,24 @@ local fmta = require("luasnip.extras.fmt").fmta
local rep = require("luasnip.extras").rep
ls.add_snippets("python", {
s(
{ trig = "ctable", dscr = "Create method or function with test stub" },
fmt(
[[
{} = Table(
"{}",
metadata_obj,
Column("id", Integer, primary_key=True),
{}
)
]],
{
i(1),
rep(1),
i(0)
}
)
),
s({ trig = "cmethod", dscr = "Create method or function with test stub" },
fmta(
[[

View file

@ -24,3 +24,5 @@ opt.swapfile = false
-- Add line at 80 symbols
opt.colorcolumn = "80"
-- config to set up clipboard as default register
opt.clipboard:append("unnamedplus")