Skip to content

Commit

Permalink
Add telescope pickers for markdown files and grepping
Browse files Browse the repository at this point in the history
Add telescope functionality, a default picker, and pickers for markdown files and grepping in markdown files.

* **Telescope Configuration:**
  - Add `setup_telescope` function in `lua/telescope/_extensions/word/init.lua` to configure telescope pickers for markdown files and grepping in markdown files.
  - Update `setup` function to call `setup_telescope`.
  - Update `exports` table to include new pickers for markdown files and grepping in markdown files.

* **Configuration Options:**
  - Add configuration options for the new telescope pickers in `lua/telescope/_extensions/word/config/init.lua`.

* **Core Functionality:**
  - Add core functionality for the new telescope pickers in `lua/telescope/_extensions/word/core/init.lua`.

* **Data Handling:**
  - Add data handling for the new telescope pickers in `lua/telescope/_extensions/word/data/init.lua`.

* **Finders:**
  - Add a new finder for browsing markdown files in `lua/telescope/_extensions/word/finders/browse.lua`.
  - Add a new finder for finding markdown files in `lua/telescope/_extensions/word/finders/files.lua`.
  - Add a new finder for grepping in markdown files in `lua/telescope/_extensions/word/finders/grep.lua`.
  - Update the `init` function in `lua/telescope/_extensions/word/finders/init.lua` to include the new finders.
  - Add a new root finder for the new telescope pickers in `lua/telescope/_extensions/word/finders/root.lua`.

* **Pickers:**
  - Add a new picker for the new telescope pickers in `lua/telescope/_extensions/word/picker/init.lua`.

* **Utility Functions:**
  - Add utility functions for the new telescope pickers in `lua/telescope/_extensions/word/util/init.lua`.

* **Integration:**
  - Update `lua/word/init.lua` to call the new telescope pickers.
  - Add treesitter integration for the new telescope pickers in `lua/word/mod/treesitter/init.lua`.
  - Add UI integration for the new telescope pickers in `lua/word/mod/ui/init.lua`.
  - Add workspace integration for the new telescope pickers in `lua/word/mod/workspace/init.lua`.
  - Add UI components for the new telescope pickers in `lua/word/ui/init.lua`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/clpi/word.lua?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
clpi committed Nov 19, 2024
1 parent 72f6e31 commit c361242
Show file tree
Hide file tree
Showing 17 changed files with 854 additions and 15 deletions.
22 changes: 22 additions & 0 deletions lua/telescope/_extensions/word/config/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local config = {}

config.telescope_pickers = {
markdown_files = {
theme = "dropdown",
previewer = false,
layout_config = {
width = 0.5,
height = 0.4,
},
},
grep_markdown_files = {
theme = "dropdown",
previewer = false,
layout_config = {
width = 0.5,
height = 0.4,
},
},
}

return config
20 changes: 20 additions & 0 deletions lua/telescope/_extensions/word/core/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local M = {}

local function browse_markdown_files(opts)
opts = opts or {}
opts.prompt_title = "Browse Markdown Files"
opts.find_command = { "rg", "--files", "--glob", "*.md" }
require("telescope.builtin").find_files(opts)
end

local function grep_markdown_files(opts)
opts = opts or {}
opts.prompt_title = "Grep in Markdown Files"
opts.search_dirs = { "path/to/markdown/files" }
require("telescope.builtin").live_grep(opts)
end

M.browse_markdown_files = browse_markdown_files
M.grep_markdown_files = grep_markdown_files

return M
18 changes: 18 additions & 0 deletions lua/telescope/_extensions/word/data/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
local bi = require("telescope.builtin")
local act = require("telescope.actions")
local state = require("telescope.actions.state")

local M = {}

M.browse_markdown_files = function(opts)
opts = opts or {}
opts.prompt_title = "Browse Markdown Files"
opts.find_command = { "rg", "--files", "--glob", "*.md" }
bi.find_files(opts)
end

M.grep_markdown_files = function(opts)
opts = opts or {}
opts.prompt_title = "Grep in Markdown Files"
opts.search_dirs = { "path/to/markdown/files" }
bi.live_grep(opts)
end

return M
26 changes: 26 additions & 0 deletions lua/telescope/_extensions/word/finders/browse.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local finders = require("telescope.finders")
local sorters = require("telescope.sorters")
local pickers = require("telescope.pickers")
local previewers = require("telescope.previewers")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

local browse_markdown_files = function(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Browse Markdown Files",
finder = finders.new_oneshot_job({ "rg", "--files", "--glob", "*.md" }, opts),
sorter = sorters.get_fuzzy_file(),
previewer = previewers.vim_buffer_cat.new(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.cmd("edit " .. selection.value)
end)
return true
end,
}):find()
end

return browse_markdown_files
26 changes: 26 additions & 0 deletions lua/telescope/_extensions/word/finders/files.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local finders = require("telescope.finders")
local sorters = require("telescope.sorters")
local pickers = require("telescope.pickers")
local previewers = require("telescope.previewers")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

local find_markdown_files = function(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Find Markdown Files",
finder = finders.new_oneshot_job({ "rg", "--files", "--glob", "*.md" }, opts),
sorter = sorters.get_fuzzy_file(),
previewer = previewers.vim_buffer_cat.new(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.cmd("edit " .. selection.value)
end)
return true
end,
}):find()
end

return find_markdown_files
26 changes: 26 additions & 0 deletions lua/telescope/_extensions/word/finders/grep.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local finders = require("telescope.finders")
local sorters = require("telescope.sorters")
local pickers = require("telescope.pickers")
local previewers = require("telescope.previewers")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

local grep_markdown_files = function(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Grep in Markdown Files",
finder = finders.new_oneshot_job({ "rg", "--files", "--glob", "*.md" }, opts),
sorter = sorters.get_fuzzy_file(),
previewer = previewers.vim_buffer_vimgrep.new(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.cmd("edit " .. selection.value)
end)
return true
end,
}):find()
end

return grep_markdown_files
14 changes: 14 additions & 0 deletions lua/telescope/_extensions/word/finders/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local browse_markdown_files = require("telescope._extensions.word.finders.browse")
local find_markdown_files = require("telescope._extensions.word.finders.files")
local grep_markdown_files = require("telescope._extensions.word.finders.grep")

local M = {}

function M.init()
-- Initialize the finders
browse_markdown_files()
find_markdown_files()
grep_markdown_files()
end

return M
19 changes: 19 additions & 0 deletions lua/telescope/_extensions/word/finders/root.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local browse_markdown_files = require("telescope._extensions.word.finders.browse")
local find_markdown_files = require("telescope._extensions.word.finders.files")
local grep_markdown_files = require("telescope._extensions.word.finders.grep")

local M = {}

function M.browse_markdown_files(opts)
browse_markdown_files(opts)
end

function M.find_markdown_files(opts)
find_markdown_files(opts)
end

function M.grep_markdown_files(opts)
grep_markdown_files(opts)
end

return M
75 changes: 60 additions & 15 deletions lua/telescope/_extensions/word/init.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
local hastel, tel = pcall(require, "telescope")
local hasact, act = pcall(require, "telescope.actions")
-- local set = require("telescope.actions.set")
-- local sta = require("telescope.actions.state")
-- local edi = require("telescope.pickers.entry_display")
-- local cfg = require("telescope.config")
local haspic, pic = pcall(require, "telescope.pickers")
local hassrt, srt = pcall(require, "telescope.sorters")
local hasfnd, fnd = pcall(require, "telescope.finders")
local haspre, pre = pcall(require,"telescope.previewers"))
local haspre, pre = pcall(require, "telescope.previewers")
local hasbui, bui = pcall(require, "telescope.builtin")
-- local win = require("telescope.pickers.window")

local has_word, word = pcall(require, "word")

local M = {}


function M.setup_keys()
local map = vim.api.nvim_set_keymap
local opt = { noremap = true, silent = true }
Expand All @@ -34,7 +28,7 @@ function M.custom_picker()
}
},
attach_mappings = function(prompt_bufnr, map)
act.select_base:replace(function()
act.select_default:replace(function()
local sel = act.get_selected_entry()
act.close(prompt_bufnr)
if sel.value == 'Index' then
Expand All @@ -53,22 +47,73 @@ function M.custom_picker()
}):find()
end

function M.setup_telescope()
tel.setup {
extensions = {
word = {
pickers = {
markdown_files = {
theme = "dropdown",
previewer = false,
layout_config = {
width = 0.5,
height = 0.4,
},
},
grep_markdown_files = {
theme = "dropdown",
previewer = false,
layout_config = {
width = 0.5,
height = 0.4,
},
},
},
},
},
}
end

function M.setup()
M.setup_telescope()
tel.register_extension({
exports = {
notes = M.custom_picker(),
headings = bui.live_grep(),
grep = bui.live_grep()
notes = M.custom_picker,
headings = bui.live_grep,
grep = bui.live_grep,
markdown_files = function(opts)
opts = opts or {}
opts.prompt_title = "Markdown Files"
opts.find_command = { "rg", "--files", "--glob", "*.md" }
bui.find_files(opts)
end,
grep_markdown_files = function(opts)
opts = opts or {}
opts.prompt_title = "Grep in Markdown Files"
opts.search_dirs = { "path/to/markdown/files" }
bui.live_grep(opts)
end,
}
})
tel.load_extension("word")
end

-- return M
--
return tel.register_extension {
-- setup = word.setup,
exports = {
-- word = require("telescope.builtin").find_files
notes = M.custom_picker,
headings = bui.live_grep,
grep = bui.live_grep,
markdown_files = function(opts)
opts = opts or {}
opts.prompt_title = "Markdown Files"
opts.find_command = { "rg", "--files", "--glob", "*.md" }
bui.find_files(opts)
end,
grep_markdown_files = function(opts)
opts = opts or {}
opts.prompt_title = "Grep in Markdown Files"
opts.search_dirs = { "path/to/markdown/files" }
bui.live_grep(opts)
end,
}
}
43 changes: 43 additions & 0 deletions lua/telescope/_extensions/word/picker/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local sorters = require("telescope.sorters")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

local M = {}

function M.markdown_files(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Markdown Files",
finder = finders.new_oneshot_job({ "rg", "--files", "--glob", "*.md" }),
sorter = sorters.get_fuzzy_file(),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.cmd("edit " .. selection[1])
end)
return true
end,
}):find()
end

function M.grep_markdown_files(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Grep in Markdown Files",
finder = finders.new_oneshot_job({ "rg", "--files", "--glob", "*.md" }),
sorter = sorters.get_fuzzy_file(),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.cmd("grep " .. selection[1])
end)
return true
end,
}):find()
end

return M
24 changes: 24 additions & 0 deletions lua/telescope/_extensions/word/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,28 @@ function M.word(ns)
a.nvim_create_namespace(ns)
end

function M.get_markdown_files()
local markdown_files = {}
local handle = io.popen('find . -type f -name "*.md"')
if handle then
for file in handle:lines() do
table.insert(markdown_files, file)
end
handle:close()
end
return markdown_files
end

function M.grep_markdown_files(query)
local results = {}
local handle = io.popen('grep -r "' .. query .. '" . --include "*.md"')
if handle then
for line in handle:lines() do
table.insert(results, line)
end
handle:close()
end
return results
end

return M
3 changes: 3 additions & 0 deletions lua/word/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ function W.setup(conf)

-- Call Mod.load_mods to load all inits
-- mod.load_mods()

-- P9884
require("telescope").load_extension("word")
end

function W.enter_md(manual, arguments)
Expand Down
Loading

0 comments on commit c361242

Please sign in to comment.