refactor(nvim): Telescope key bindings

This commit is contained in:
Oliver Davies 2022-01-30 20:18:01 +00:00
parent 5ea401884e
commit 89400c9167
5 changed files with 162 additions and 100 deletions

View file

@ -1,5 +1,10 @@
require "opdavies.globals"
require "opdavies.lsp"
require("opdavies.options").setup()
require "opdavies.plugins"
require "opdavies.telescope"
require "opdavies.lsp"
require "opdavies.telescope.setup"
require "opdavies.telescope.mappings"

View file

@ -8,6 +8,8 @@ local nvim_status = require "lsp-status"
local imap = require("opdavies.keymap").imap
local nmap = require("opdavies.keymap").nmap
local telescope_mapper = require("opdavies.telescope.mappings")
local buf_nnoremap = function(opts)
opts.buffer = 0
nmap(opts)
@ -34,8 +36,6 @@ local custom_attach = function(client)
-- Keymaps
buf_inoremap { "<c-s>", vim.lsp.buf.signature_help }
buf_nnoremap { "<leader>ca", "<cmd>Telescope lsp_code_actions sorting_strategy=ascending theme=dropdown<cr>" }
buf_nnoremap { "<leader>dl", "<cmd>Telescope diagnostics<cr>" }
buf_nnoremap { "<leader>dn", vim.diagnostic.goto_next }
buf_nnoremap { "<leader>dp", vim.diagnostic.goto_prev }
buf_nnoremap { "<leader>rn", vim.lsp.buf.rename }
@ -46,18 +46,13 @@ local custom_attach = function(client)
buf_nnoremap { "gT", vim.lsp.buf.type_definition }
buf_nnoremap { "gd", vim.lsp.buf.definition }
-- buf_nnoremap { "<space>gI", handlers.implementation }
-- telescope_mapper("<leader>ca", "lsp_code_actions", nil, true)
-- telescope_mapper("<leader>wd", "lsp_document_symbols", { ignore_filename = true }, true)
-- telescope_mapper("<leader>ww", "lsp_dynamic_workspace_symbols", { ignore_filename = true }, true)
-- telescope_mapper("gI", "lsp_implementations", nil, true)
-- telescope_mapper("gr", "lsp_references", nil, true)
if filetype ~= "lua" then
buf_nnoremap { "K", vim.lsp.buf.hover }
end
telescope_mapper("<leader>ca", "lsp_code_actions", nil, true)
telescope_mapper("<leader>dl", "diagnostics", nil, true)
-- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then
vim.cmd [[

View file

@ -1,66 +1,68 @@
local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
return
SHOULD_RELOAD_TELESCOPE = true
local reloader = function()
if SHOULD_RELOAD_TELESCOPE then
RELOAD "plenary"
RELOAD "telescope"
RELOAD "opdavies.telescope.setup"
end
end
local previewers = require "telescope.previewers"
local Job = require "plenary.job"
local themes = require "telescope.themes"
-- Create a new maker that won't preview binary files
-- https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes#dont-preview-binaries
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)
local M = {}
function M.edit_neovim()
local opts = {
cwd = "~/.config/nvim",
find_command = { "rg", "--no-ignore", "--files", "--follow" },
path_display = { "shorten" },
prompt_title = "~ dotfiles ~",
layout_strategy = "flex",
layout_config = {
height = 0.8,
prompt_position = "top",
width = 0.9,
horizontal = {
width = { padding = 0.15 },
},
vertical = {
preview_height = 0.75,
},
},
}
require("telescope.builtin").find_files(opts)
end
function M.file_browser()
local opts = { cwd = vim.fn.expand "%:p:h" }
require("telescope").extensions.file_browser.file_browser(opts)
end
function M.fd()
local opts = themes.get_ivy()
require("telescope.builtin").find_files(opts)
end
function M.live_grep()
require("telescope.builtin").live_grep {
sorting_strategy = "ascending"
}
end
return setmetatable({}, {
__index = function(_, k)
reloader()
if M[k] then
return M[k]
else
vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "BINARY" })
end)
return require("telescope.builtin")[k]
end
end,
})
:sync()
end
telescope.setup {
defaults = {
buffer_previewer_maker = new_maker,
prompt_prefix = "$ ",
},
extensions = {
file_browser = {
theme = "ivy",
},
},
}
telescope.load_extension "file_browser"
telescope.load_extension "fzf"
telescope.load_extension "refactoring"
local map = vim.api.nvim_set_keymap
local options = {
noremap = true,
silent = true,
}
-- Builtin
map("n", "<leader>fb", '<cmd>lua require("telescope.builtin").buffers()<cr>', options)
map("n", "<leader>fc", '<cmd>lua require("telescope.builtin").lsp_code_actions()<cr>', options)
map("n", "<leader>fd", '<cmd>lua require("telescope.builtin").lsp_workspace_diagnostics()<cr>', options)
map("n", "<leader>fe", '<cmd>lua R "opdavies.telescope.mappings".file_browser()<cr>', options)
map("n", "<leader>fg", '<cmd>lua require("telescope.builtin").git_files{}<cr>', options)
map("n", "<leader>fh", '<cmd>lua require("telescope.builtin").help_tags()<cr>', options)
map("n", "<leader>fl", '<cmd>lua require("telescope.builtin").live_grep({ sorting_strategy="ascending" })<cr>', options)
map("n", "<leader>fr", '<cmd>lua require("telescope.builtin").registers()<cr>', options)
-- Custom
map("n", "<Leader>en", '<cmd>lua R "opdavies.telescope.mappings".edit_neovim()<cr>', options)
map("n", "<Leader>ff", '<cmd>lua R "opdavies.telescope.mappings".fd()<cr>', options)

View file

@ -1,42 +1,33 @@
local M = {}
TelescopeMapArgs = TelescopeMapArgs or {}
local themes = require "telescope.themes"
local telescope_mapper = function(key, f, options, buffer)
local map_key = vim.api.nvim_replace_termcodes(key .. f, true, true, true)
function M.edit_neovim()
local opts = {
cwd = "~/.config/nvim",
find_command = { "rg", "--no-ignore", "--files", "--follow" },
path_display = { "shorten" },
prompt_title = "~ dotfiles ~",
TelescopeMapArgs[map_key] = options or {}
layout_strategy = "flex",
layout_config = {
height = 0.8,
prompt_position = "top",
width = 0.9,
local mode = "n"
local rhs = string.format("<cmd>lua R('opdavies.telescope')['%s'](TelescopeMapArgs['%s'])<CR>", f, map_key)
horizontal = {
width = { padding = 0.15 },
},
vertical = {
preview_height = 0.75,
},
},
local map_options = {
noremap = true,
silent = true,
}
require("telescope.builtin").find_files(opts)
if not buffer then
vim.api.nvim_set_keymap(mode, key, rhs, map_options)
else
vim.api.nvim_buf_set_keymap(0, mode, key, rhs, map_options)
end
end
function M.file_browser()
local opts = { cwd = vim.fn.expand "%:p:h" }
telescope_mapper("<leader>fb", "buffers")
telescope_mapper("<leader>fe", "file_browser")
telescope_mapper("<leader>fg", "git_files")
telescope_mapper("<leader>fh", "help_tags")
telescope_mapper("<leader>fl", "live_grep")
telescope_mapper("<leader>ff", "fd")
require("telescope").extensions.file_browser.file_browser(opts)
end
telescope_mapper("<leader>en", "edit_neovim")
telescope_mapper("<leader>ez", "edit_zsh")
function M.fd()
local opts = themes.get_ivy()
require("telescope.builtin").find_files(opts)
end
return M
return telescope_mapper

View file

@ -0,0 +1,69 @@
local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
return
end
local previewers = require "telescope.previewers"
local Job = require "plenary.job"
-- Create a new maker that won't preview binary files
-- https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes#dont-preview-binaries
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
vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "BINARY" })
end)
end
end,
})
:sync()
end
telescope.setup {
defaults = {
buffer_previewer_maker = new_maker,
prompt_prefix = "$ ",
},
extensions = {
file_browser = {
theme = "ivy",
},
},
}
telescope.load_extension "file_browser"
telescope.load_extension "fzf"
telescope.load_extension "refactoring"
-- local map = vim.api.nvim_set_keymap
--
-- local options = {
-- noremap = true,
-- silent = true,
-- }
--
-- local nmap = require "opdavies.keymap".nmap
-- local tele_mappings = R "opdavies.telescope.mappings"
--
-- -- Builtin
-- nmap({ "<leader>fb", '<cmd>lua require("telescope.builtin").buffers()<cr>', opts })
-- nmap({ "<leader>fc", '<cmd>lua require("telescope.builtin").lsp_code_actions()<cr>', opts })
-- nmap({ "<leader>fd", '<cmd>lua require("telescope.builtin").lsp_workspace_diagnostics()<cr>', opts })
-- nmap({ "<leader>fg", '<cmd>lua require("telescope.builtin").git_files{}<cr>', opts })
-- nmap({ "<leader>fh", '<cmd>lua require("telescope.builtin").help_tags()<cr>', opts })
-- nmap({ "<leader>fl", '<cmd>lua require("telescope.builtin").live_grep({ sorting_strategy="ascending" })<cr>', opts })
-- nmap({ "<leader>fr", '<cmd>lua require("telescope.builtin").registers()<cr>', opts })
--
-- -- Custom
-- nmap({ "<leader>fe", tele_mappings.file_browser, opts })
-- nmap({ "<Leader>en", tele_mappings.edit_neovim, opts })
-- nmap({ "<Leader>ff", tele_mappings.fd, opts })