Feat: add telescope file_browser and rewrite keymap

This commit is contained in:
Bertrand Benjamin 2022-08-20 06:16:14 +02:00
parent 4324070cac
commit 01d09bc9c0
2 changed files with 80 additions and 34 deletions

View File

@ -1,46 +1,91 @@
local status, telescope = pcall(require, "telescope")
if (not status) then return end
local builtin = require("telescope.builtin")
local previewers = require('telescope.previewers') local previewers = require('telescope.previewers')
local Job = require('plenary.job') local Job = require('plenary.job')
local new_maker = function(filepath, bufnr, opts) local new_maker = function(filepath, bufnr, opts)
filepath = vim.fn.expand(filepath) filepath = vim.fn.expand(filepath)
Job:new({ Job:new({
command = 'file', command = 'file',
args = { '--mime-type', '-b', filepath }, args = { '--mime-type', '-b', filepath },
on_exit = function(j) on_exit = function(j)
local mime_type = vim.split(j:result()[1], '/')[1] local mime_type = vim.split(j:result()[1], '/')[1]
if mime_type == "text" then if mime_type == "text" then
previewers.buffer_previewer_maker(filepath, bufnr, opts) previewers.buffer_previewer_maker(filepath, bufnr, opts)
else else
-- maybe we want to write something to the buffer here -- maybe we want to write something to the buffer here
vim.schedule(function() vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'BINARY' }) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'BINARY' })
end) end)
end end
end end
}):sync() }):sync()
end end
require('telescope').setup { local fb_actions = require "telescope".extensions.file_browser.actions
defaults = { local function telescope_buffer_dir()
buffer_previewer_maker = new_maker, return vim.fn.expand('%:p:h')
file_ignore_patterns = {"**/*.pdf"}, end
},
telescope.setup {
defaults = {
buffer_previewer_maker = new_maker,
file_ignore_patterns = { "**/*.pdf" },
},
extensions = { extensions = {
fzf = { fzf = {
fuzzy = true, -- false will only do exact matching fuzzy = true, -- false will only do exact matching
override_generic_sorter = true, -- override the generic sorter override_generic_sorter = true, -- override the generic sorter
override_file_sorter = true, -- override the file sorter override_file_sorter = true, -- override the file sorter
case_mode = "smart_case", -- or "ignore_case" or "respect_case" case_mode = "smart_case", -- or "ignore_case" or "respect_case"
-- the default case_mode is "smart_case" -- the default case_mode is "smart_case"
} },
} file_browser = {
theme = "dropdown",
-- disables netrw and use telescope-file-browser in its place
hijack_netrw = true,
mappings = {
["n"] = {
-- your custom normal mode mappings
["h"] = fb_actions.goto_parent_dir,
},
},
},
}
} }
require('telescope').load_extension('fzf') telescope.load_extension('fzf')
telescope.load_extension('file_browser')
local map = vim.api.nvim_set_keymap
-- Find files using Telescope command-line sugar.
map('n', '<leader>e', '<cmd>Telescope find_files<cr>', {})
map('n', '<leader>g', '<cmd>Telescope live_grep<cr>', {})
map('n', '<leader>b', '<cmd>Telescope buffers<cr>', {})
map('n', '<leader>h', '<cmd>Telescope help_tags<cr>', {})
-- keymaps
vim.keymap.set('n', '<leader>e',
function()
builtin.find_files({
})
end)
vim.keymap.set('n', '<leader>g', function()
builtin.live_grep()
end)
vim.keymap.set('n', '<leader>b', function()
builtin.buffers()
end)
vim.keymap.set('n', '<leader>h', function()
builtin.resume()
end)
-- vim.keymap.set('n', '<leader>d', function()
-- builtin.diagnostics()
-- end)
vim.keymap.set("n", "<leader>fb", function()
telescope.extensions.file_browser.file_browser({
path = "%:p:h",
cwd = telescope_buffer_dir(),
respect_gitignore = false,
hidden = true,
grouped = true,
previewer = false,
initial_mode = "normal",
layout_config = { height = 40 }
})
end)

View File

@ -93,6 +93,7 @@ return packer.startup(function(use)
} }
use {'nvim-telescope/telescope-fzf-native.nvim', run = 'make' } use {'nvim-telescope/telescope-fzf-native.nvim', run = 'make' }
use { "nvim-telescope/telescope-file-browser.nvim" }
use { use {
'lervag/vimtex', 'lervag/vimtex',