Feat: add telescope file_browser and rewrite keymap
This commit is contained in:
parent
4324070cac
commit
01d09bc9c0
@ -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 Job = require('plenary.job')
|
||||
local new_maker = function(filepath, bufnr, opts)
|
||||
filepath = vim.fn.expand(filepath)
|
||||
Job:new({
|
||||
command = 'file',
|
||||
args = { '--mime-type', '-b', filepath },
|
||||
on_exit = function(j)
|
||||
local mime_type = vim.split(j:result()[1], '/')[1]
|
||||
if mime_type == "text" then
|
||||
previewers.buffer_previewer_maker(filepath, bufnr, opts)
|
||||
else
|
||||
-- maybe we want to write something to the buffer here
|
||||
vim.schedule(function()
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'BINARY' })
|
||||
end)
|
||||
end
|
||||
end
|
||||
}):sync()
|
||||
filepath = vim.fn.expand(filepath)
|
||||
Job:new({
|
||||
command = 'file',
|
||||
args = { '--mime-type', '-b', filepath },
|
||||
on_exit = function(j)
|
||||
local mime_type = vim.split(j:result()[1], '/')[1]
|
||||
if mime_type == "text" then
|
||||
previewers.buffer_previewer_maker(filepath, bufnr, opts)
|
||||
else
|
||||
-- maybe we want to write something to the buffer here
|
||||
vim.schedule(function()
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'BINARY' })
|
||||
end)
|
||||
end
|
||||
end
|
||||
}):sync()
|
||||
end
|
||||
|
||||
require('telescope').setup {
|
||||
defaults = {
|
||||
buffer_previewer_maker = new_maker,
|
||||
file_ignore_patterns = {"**/*.pdf"},
|
||||
},
|
||||
local fb_actions = require "telescope".extensions.file_browser.actions
|
||||
local function telescope_buffer_dir()
|
||||
return vim.fn.expand('%:p:h')
|
||||
end
|
||||
|
||||
telescope.setup {
|
||||
defaults = {
|
||||
buffer_previewer_maker = new_maker,
|
||||
file_ignore_patterns = { "**/*.pdf" },
|
||||
},
|
||||
extensions = {
|
||||
fzf = {
|
||||
fuzzy = true, -- false will only do exact matching
|
||||
override_generic_sorter = true, -- override the generic sorter
|
||||
override_file_sorter = true, -- override the file sorter
|
||||
case_mode = "smart_case", -- or "ignore_case" or "respect_case"
|
||||
fuzzy = true, -- false will only do exact matching
|
||||
override_generic_sorter = true, -- override the generic sorter
|
||||
override_file_sorter = true, -- override the file sorter
|
||||
case_mode = "smart_case", -- or "ignore_case" or "respect_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)
|
||||
|
@ -93,6 +93,7 @@ return packer.startup(function(use)
|
||||
}
|
||||
|
||||
use {'nvim-telescope/telescope-fzf-native.nvim', run = 'make' }
|
||||
use { "nvim-telescope/telescope-file-browser.nvim" }
|
||||
|
||||
use {
|
||||
'lervag/vimtex',
|
||||
|
Loading…
Reference in New Issue
Block a user