From cf3458042f8c27da088575f00118477034847619 Mon Sep 17 00:00:00 2001 From: pika Date: Mon, 19 Aug 2024 23:06:45 +0200 Subject: [PATCH] addet some snippets --- lua/telesnip/init.lua | 31 ++++++++++++-------- lua/telesnip/snippets/bash/command_exists.sh | 4 +++ lua/telesnip/snippets/bash/get_ip.sh | 4 +++ lua/telesnip/snippets/bash/info_error.sh | 4 +++ 4 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 lua/telesnip/snippets/bash/command_exists.sh create mode 100644 lua/telesnip/snippets/bash/get_ip.sh create mode 100644 lua/telesnip/snippets/bash/info_error.sh diff --git a/lua/telesnip/init.lua b/lua/telesnip/init.lua index 56a5400..eaeae35 100644 --- a/lua/telesnip/init.lua +++ b/lua/telesnip/init.lua @@ -3,7 +3,7 @@ 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 + local languages = vim.fn.readdir(base_path) -- Get all language directories automatically for _, lang in ipairs(languages) do local path = base_path .. lang @@ -42,13 +42,8 @@ M.snippet_picker = function() 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, @@ -58,16 +53,26 @@ M.snippet_picker = function() 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)) + 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 - vim.api.nvim_buf_set_lines(current_buf, row - 1, row - 1, false, snippet_content) + -- Insert snippet content at the current cursor position + 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 - if vim.fn.mode() == "n" then - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, false, true), "n", true) - end + -- Move cursor to the end of the inserted snippet + local new_row = row + #snippet_content + local new_col = #snippet_content[#snippet_content] + vim.api.nvim_win_set_cursor(win, { new_row, new_col }) + + -- Close Telescope prompt require("telescope.actions").close(prompt_bufnr) + + -- Schedule entering insert mode + vim.schedule(function() + vim.cmd("stopinsert") + vim.cmd("startinsert!") + end) end) return true end, diff --git a/lua/telesnip/snippets/bash/command_exists.sh b/lua/telesnip/snippets/bash/command_exists.sh new file mode 100644 index 0000000..98ded58 --- /dev/null +++ b/lua/telesnip/snippets/bash/command_exists.sh @@ -0,0 +1,4 @@ +# ─< Check if the given command exists silently >───────────────────────────────────────── +command_exists() { + command -v "$@" >/dev/null 2>&1 +} diff --git a/lua/telesnip/snippets/bash/get_ip.sh b/lua/telesnip/snippets/bash/get_ip.sh new file mode 100644 index 0000000..a5f2bf9 --- /dev/null +++ b/lua/telesnip/snippets/bash/get_ip.sh @@ -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 +} diff --git a/lua/telesnip/snippets/bash/info_error.sh b/lua/telesnip/snippets/bash/info_error.sh new file mode 100644 index 0000000..c52511b --- /dev/null +++ b/lua/telesnip/snippets/bash/info_error.sh @@ -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"; }