addet some snippets

This commit is contained in:
pika 2024-08-19 23:06:45 +02:00
parent ad33734063
commit cf3458042f
4 changed files with 30 additions and 13 deletions

View file

@ -3,7 +3,7 @@ local M = {}
local function get_snippets() local function get_snippets()
local snippets = {} local snippets = {}
local base_path = vim.fn.stdpath("config") .. "/lua/telesnip/snippets/" local base_path = vim.fn.stdpath("config") .. "/lua/telesnip/snippets/"
local languages = { "bash", "lua", "fish" } -- Extend this as needed local languages = vim.fn.readdir(base_path) -- Get all language directories automatically
for _, lang in ipairs(languages) do for _, lang in ipairs(languages) do
local path = base_path .. lang local path = base_path .. lang
@ -42,13 +42,8 @@ M.snippet_picker = function()
sorter = require("telescope.config").values.generic_sorter(opts), sorter = require("telescope.config").values.generic_sorter(opts),
previewer = require("telescope.previewers").new_buffer_previewer({ previewer = require("telescope.previewers").new_buffer_previewer({
define_preview = function(self, entry) define_preview = function(self, entry)
-- Read the snippet file content
local lines = vim.fn.readfile(entry.path) 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) 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") local ext = vim.fn.fnamemodify(entry.path, ":e")
vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", ext) vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", ext)
end, end,
@ -58,16 +53,26 @@ M.snippet_picker = function()
local selection = require("telescope.actions.state").get_selected_entry() local selection = require("telescope.actions.state").get_selected_entry()
local snippet_content = vim.fn.readfile(selection.path) local snippet_content = vim.fn.readfile(selection.path)
local current_buf = vim.api.nvim_get_current_buf() local current_buf = vim.api.nvim_get_current_buf()
local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) local win = vim.api.nvim_get_current_win()
local cursor = vim.api.nvim_win_get_cursor(win)
local row, col = cursor[1] - 1, cursor[2]
-- Insert snippet content at the current line -- Insert snippet content at the current cursor position
vim.api.nvim_buf_set_lines(current_buf, row - 1, row - 1, false, snippet_content) vim.api.nvim_buf_set_text(current_buf, row, col, row, col, snippet_content)
-- Check if the user is in normal mode, and switch to insert mode -- Move cursor to the end of the inserted snippet
if vim.fn.mode() == "n" then local new_row = row + #snippet_content
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, false, true), "n", true) local new_col = #snippet_content[#snippet_content]
end vim.api.nvim_win_set_cursor(win, { new_row, new_col })
-- Close Telescope prompt
require("telescope.actions").close(prompt_bufnr) require("telescope.actions").close(prompt_bufnr)
-- Schedule entering insert mode
vim.schedule(function()
vim.cmd("stopinsert")
vim.cmd("startinsert!")
end)
end) end)
return true return true
end, end,

View file

@ -0,0 +1,4 @@
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}

View file

@ -0,0 +1,4 @@
# ─< get the current ip as a 1 line >─────────────────────────────────────────────────────
get_ip() {
command ip a | command grep 'inet ' | command grep -v '127.0.0.1' | command awk '{print $2}' | command cut -d/ -f1 | head -n 1
}

View file

@ -0,0 +1,4 @@
# ─< Helper functions >─────────────────────────────────────────────────────────────────
function echo_error() { echo -e "\033[0;1;31mError: \033[0;31m\t${*}\033[0m"; }
function echo_binfo() { echo -e "\033[0;1;34mINFO: \033[0;34m\t${*}\033[0m"; }
function echo_info() { echo -e "\033[0;1;35mInfo: \033[0;35m${*}\033[0m"; }