From 89400c9167b51225b06f5b1b85e890018d4de4d0 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 30 Jan 2022 20:18:01 +0000 Subject: [PATCH] refactor(nvim): Telescope key bindings --- roles/neovim/files/init.lua | 9 +- roles/neovim/files/lua/opdavies/lsp/init.lua | 15 +-- .../files/lua/opdavies/telescope/init.lua | 114 +++++++++--------- .../files/lua/opdavies/telescope/mappings.lua | 55 ++++----- .../files/lua/opdavies/telescope/setup.lua | 69 +++++++++++ 5 files changed, 162 insertions(+), 100 deletions(-) create mode 100644 roles/neovim/files/lua/opdavies/telescope/setup.lua diff --git a/roles/neovim/files/init.lua b/roles/neovim/files/init.lua index 8daa64f..efa5e5c 100644 --- a/roles/neovim/files/init.lua +++ b/roles/neovim/files/init.lua @@ -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" diff --git a/roles/neovim/files/lua/opdavies/lsp/init.lua b/roles/neovim/files/lua/opdavies/lsp/init.lua index e18d832..5dd6691 100644 --- a/roles/neovim/files/lua/opdavies/lsp/init.lua +++ b/roles/neovim/files/lua/opdavies/lsp/init.lua @@ -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 { "", vim.lsp.buf.signature_help } - buf_nnoremap { "ca", "Telescope lsp_code_actions sorting_strategy=ascending theme=dropdown" } - buf_nnoremap { "dl", "Telescope diagnostics" } buf_nnoremap { "dn", vim.diagnostic.goto_next } buf_nnoremap { "dp", vim.diagnostic.goto_prev } buf_nnoremap { "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 { "gI", handlers.implementation } - - -- telescope_mapper("ca", "lsp_code_actions", nil, true) - -- telescope_mapper("wd", "lsp_document_symbols", { ignore_filename = true }, true) - -- telescope_mapper("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("ca", "lsp_code_actions", nil, true) + telescope_mapper("dl", "diagnostics", nil, true) + -- Set autocommands conditional on server_capabilities if client.resolved_capabilities.document_highlight then vim.cmd [[ diff --git a/roles/neovim/files/lua/opdavies/telescope/init.lua b/roles/neovim/files/lua/opdavies/telescope/init.lua index 5c37562..fbed8e0 100644 --- a/roles/neovim/files/lua/opdavies/telescope/init.lua +++ b/roles/neovim/files/lua/opdavies/telescope/init.lua @@ -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) - else - vim.schedule(function() - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "BINARY" }) - end) - end - end, - }) - :sync() -end +local M = {} -telescope.setup { - defaults = { - buffer_previewer_maker = new_maker, - prompt_prefix = "$ ", - }, - extensions = { - file_browser = { - theme = "ivy", +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, + }, }, - }, -} + } -telescope.load_extension "file_browser" -telescope.load_extension "fzf" -telescope.load_extension "refactoring" + require("telescope.builtin").find_files(opts) +end -local map = vim.api.nvim_set_keymap +function M.file_browser() + local opts = { cwd = vim.fn.expand "%:p:h" } -local options = { - noremap = true, - silent = true, -} + require("telescope").extensions.file_browser.file_browser(opts) +end --- Builtin -map("n", "fb", 'lua require("telescope.builtin").buffers()', options) -map("n", "fc", 'lua require("telescope.builtin").lsp_code_actions()', options) -map("n", "fd", 'lua require("telescope.builtin").lsp_workspace_diagnostics()', options) -map("n", "fe", 'lua R "opdavies.telescope.mappings".file_browser()', options) -map("n", "fg", 'lua require("telescope.builtin").git_files{}', options) -map("n", "fh", 'lua require("telescope.builtin").help_tags()', options) -map("n", "fl", 'lua require("telescope.builtin").live_grep({ sorting_strategy="ascending" })', options) -map("n", "fr", 'lua require("telescope.builtin").registers()', options) +function M.fd() + local opts = themes.get_ivy() --- Custom -map("n", "en", 'lua R "opdavies.telescope.mappings".edit_neovim()', options) -map("n", "ff", 'lua R "opdavies.telescope.mappings".fd()', options) + 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 + return require("telescope.builtin")[k] + end + end, +}) diff --git a/roles/neovim/files/lua/opdavies/telescope/mappings.lua b/roles/neovim/files/lua/opdavies/telescope/mappings.lua index b14ce89..0226eb4 100644 --- a/roles/neovim/files/lua/opdavies/telescope/mappings.lua +++ b/roles/neovim/files/lua/opdavies/telescope/mappings.lua @@ -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("lua R('opdavies.telescope')['%s'](TelescopeMapArgs['%s'])", 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("fb", "buffers") +telescope_mapper("fe", "file_browser") +telescope_mapper("fg", "git_files") +telescope_mapper("fh", "help_tags") +telescope_mapper("fl", "live_grep") +telescope_mapper("ff", "fd") - require("telescope").extensions.file_browser.file_browser(opts) -end +telescope_mapper("en", "edit_neovim") +telescope_mapper("ez", "edit_zsh") -function M.fd() - local opts = themes.get_ivy() - - require("telescope.builtin").find_files(opts) -end - -return M +return telescope_mapper diff --git a/roles/neovim/files/lua/opdavies/telescope/setup.lua b/roles/neovim/files/lua/opdavies/telescope/setup.lua new file mode 100644 index 0000000..89d8920 --- /dev/null +++ b/roles/neovim/files/lua/opdavies/telescope/setup.lua @@ -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({ "fb", 'lua require("telescope.builtin").buffers()', opts }) +-- nmap({ "fc", 'lua require("telescope.builtin").lsp_code_actions()', opts }) +-- nmap({ "fd", 'lua require("telescope.builtin").lsp_workspace_diagnostics()', opts }) +-- nmap({ "fg", 'lua require("telescope.builtin").git_files{}', opts }) +-- nmap({ "fh", 'lua require("telescope.builtin").help_tags()', opts }) +-- nmap({ "fl", 'lua require("telescope.builtin").live_grep({ sorting_strategy="ascending" })', opts }) +-- nmap({ "fr", 'lua require("telescope.builtin").registers()', opts }) +-- +-- -- Custom +-- nmap({ "fe", tele_mappings.file_browser, opts }) +-- nmap({ "en", tele_mappings.edit_neovim, opts }) +-- nmap({ "ff", tele_mappings.fd, opts })