addet plugin telesnip and some other changes
This commit is contained in:
parent
84adb02beb
commit
ad33734063
9 changed files with 258 additions and 161 deletions
78
lua/telesnip/init.lua
Normal file
78
lua/telesnip/init.lua
Normal file
|
@ -0,0 +1,78 @@
|
|||
local M = {}
|
||||
|
||||
local function get_snippets()
|
||||
local snippets = {}
|
||||
local base_path = vim.fn.stdpath("config") .. "/lua/telesnip/snippets/"
|
||||
local languages = { "bash", "lua", "fish" } -- Extend this as needed
|
||||
|
||||
for _, lang in ipairs(languages) do
|
||||
local path = base_path .. lang
|
||||
local files = vim.fn.glob(path .. "/*", false, true)
|
||||
for _, file in ipairs(files) do
|
||||
local snippet_name = vim.fn.fnamemodify(file, ":t")
|
||||
table.insert(snippets, {
|
||||
name = snippet_name,
|
||||
path = file,
|
||||
language = lang,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
return snippets
|
||||
end
|
||||
|
||||
M.snippet_picker = function()
|
||||
local snippets = get_snippets()
|
||||
|
||||
local opts = require("telescope.themes").get_dropdown({})
|
||||
require("telescope.pickers")
|
||||
.new(opts, {
|
||||
prompt_title = "Snippets",
|
||||
finder = require("telescope.finders").new_table({
|
||||
results = snippets,
|
||||
entry_maker = function(entry)
|
||||
return {
|
||||
value = entry,
|
||||
display = entry.name,
|
||||
ordinal = entry.name,
|
||||
path = entry.path,
|
||||
}
|
||||
end,
|
||||
}),
|
||||
sorter = require("telescope.config").values.generic_sorter(opts),
|
||||
previewer = require("telescope.previewers").new_buffer_previewer({
|
||||
define_preview = function(self, entry)
|
||||
-- Read the snippet file content
|
||||
local lines = vim.fn.readfile(entry.path)
|
||||
|
||||
-- Set the buffer's lines
|
||||
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
|
||||
|
||||
-- Get file extension for syntax highlighting
|
||||
local ext = vim.fn.fnamemodify(entry.path, ":e")
|
||||
vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", ext)
|
||||
end,
|
||||
}),
|
||||
attach_mappings = function(_, map)
|
||||
map("i", "<CR>", function(prompt_bufnr)
|
||||
local selection = require("telescope.actions.state").get_selected_entry()
|
||||
local snippet_content = vim.fn.readfile(selection.path)
|
||||
local current_buf = vim.api.nvim_get_current_buf()
|
||||
local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
|
||||
-- Insert snippet content at the current line
|
||||
vim.api.nvim_buf_set_lines(current_buf, row - 1, row - 1, false, snippet_content)
|
||||
|
||||
-- Check if the user is in normal mode, and switch to insert mode
|
||||
if vim.fn.mode() == "n" then
|
||||
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, false, true), "n", true)
|
||||
end
|
||||
require("telescope.actions").close(prompt_bufnr)
|
||||
end)
|
||||
return true
|
||||
end,
|
||||
})
|
||||
:find()
|
||||
end
|
||||
|
||||
return M
|
15
lua/telesnip/snippets/bash/check_root.sh
Normal file
15
lua/telesnip/snippets/bash/check_root.sh
Normal file
|
@ -0,0 +1,15 @@
|
|||
# Check if the user is root and set sudo variable if necessary
|
||||
check_root() {
|
||||
if [[ "${EUID}" -ne 0 ]]; then
|
||||
if command_exists sudo; then
|
||||
echo_binfo "User is not root. Using sudo for privileged operations."
|
||||
_sudo="sudo"
|
||||
else
|
||||
echo_error "No sudo found and you're not root! Can't install packages."
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo_binfo "Root access confirmed."
|
||||
_sudo=""
|
||||
fi
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue