From 04072337f42b61598f47a84007dbfc388850fbae Mon Sep 17 00:00:00 2001 From: t0xa Date: Fri, 17 Oct 2025 10:09:33 +0300 Subject: [PATCH] Markdown: Add some tools to work with markdown files - Add links snippet - Add Telescope picker for headers --- ftplugin/markdown.lua | 50 ++++++++++++++++++++++++++++++++ lua/custom/snippets/markdown.lua | 22 ++++++++++++++ lua/custom/telescope.lua | 8 +++-- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 ftplugin/markdown.lua create mode 100644 lua/custom/snippets/markdown.lua diff --git a/ftplugin/markdown.lua b/ftplugin/markdown.lua new file mode 100644 index 0000000..868f479 --- /dev/null +++ b/ftplugin/markdown.lua @@ -0,0 +1,50 @@ +local keymap = vim.keymap + +-- Picker for markdown headers +local pickers = require("telescope.pickers") +local finders = require("telescope.finders") +local conf = require("telescope.config").values +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") + +local function markdown_headings() + local bufnr = vim.api.nvim_get_current_buf() + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + local headings = {} + + for lnum, line in ipairs(lines) do + local heading = line:match('^(#+%s+.+)$') + if heading then + table.insert(headings, { + lnum = lnum, + text = heading, + display = string.format("%4d: %s", lnum, heading) + }) + end + end + pickers.new({}, { + prompt_title = 'Markdown Headings', + finder = finders.new_table { + results = headings, + entry_maker = function(entry) + return { + value = entry, + display = entry.display, + ordinal = entry.text, + lnum = entry.lnum, + } + end + }, + sorter = conf.generic_sorter({}), + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + vim.api.nvim_win_set_cursor(0, {selection.value.lnum, 0}) + end) + return true + end, + }):find() +end + +keymap.set("n", "fdh", markdown_headings, { desc = "Grep: Markdown headings" }) diff --git a/lua/custom/snippets/markdown.lua b/lua/custom/snippets/markdown.lua new file mode 100644 index 0000000..01cd71e --- /dev/null +++ b/lua/custom/snippets/markdown.lua @@ -0,0 +1,22 @@ +require("luasnip.session.snippet_collection").clear_snippets "markdown" +local ls = require "luasnip" +local s = ls.snippet +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node + +ls.add_snippets( + "markdown", + { + s("link", { + t("["), i(1, "description"), t("]("), + f(function() + local clip = vim.fn.getreg("+") + if clip == "" then + return "url" + end + return clip + end), + t(")"), i(0), + }), + }) diff --git a/lua/custom/telescope.lua b/lua/custom/telescope.lua index c541742..396a18a 100644 --- a/lua/custom/telescope.lua +++ b/lua/custom/telescope.lua @@ -11,11 +11,12 @@ pcall(require("telescope").load_extension, "fzf") local function live_grep_with_glob() vim.ui.input({ prompt = "Glob pattern: ", - default = "", -- Optional default value + default = "/home/user/Work/root/ivideon/cloud_analytics/person_detection_node", -- Optional default value }, function(input) if input then require('telescope.builtin').live_grep({ - glob_pattern = input + -- glob_pattern = input, + search_dirs = { input } }) end end) @@ -23,6 +24,7 @@ end local builtin = require("telescope.builtin") local keymap = vim.keymap + -- Create a Neovim command vim.api.nvim_create_user_command('TelescopeGrepGlob', live_grep_with_glob, {}) @@ -33,5 +35,5 @@ keymap.set("n", "fg", builtin.live_grep, { desc = "FZF: find string in c keymap.set("n", "fb", builtin.buffers, { desc = "FZF: display opened buffers" }) keymap.set("n", "fh", builtin.help_tags, { desc = "FZF: display help tags" }) keymap.set("n", "ft", "TodoTelescope", { desc = "FZF: display TODO comments" }) -keymap.set("n", "fds", builtin.lsp_document_symbols, { desc = "FZF: Document symbols" }) +keymap.set("n", "fds", builtin.lsp_document_symbols, { desc = "Grep: Document symbols" }) keymap.set('n', 'fp', live_grep_with_glob, { desc = 'Live grep with glob pattern' })