From 11a3c706093f6656ce33dabe0810f96490403818 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Mon, 7 Sep 2020 00:20:08 -0400 Subject: [PATCH] Begin work on larger global config, to tailor defaults --- lua/telescope/builtin.lua | 8 ++- lua/telescope/config.lua | 64 ++++++++++++++++----- lua/telescope/init.lua | 61 ++++++++++++++------ lua/telescope/make_entry.lua | 27 ++++++++- lua/telescope/opts.lua | 17 ++++++ lua/telescope/pickers.lua | 18 +++--- lua/telescope/pickers/layout_strategies.lua | 26 +++++---- 7 files changed, 165 insertions(+), 56 deletions(-) create mode 100644 lua/telescope/opts.lua diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua index fe2528e..513e3e4 100644 --- a/lua/telescope/builtin.lua +++ b/lua/telescope/builtin.lua @@ -313,6 +313,8 @@ builtin.builtin = function(opts) end +-- TODO: Maybe just change this to `find`. +-- Support `find` and maybe let peopel do other stuff with it as well. builtin.fd = function(opts) opts = opts or {} @@ -328,8 +330,6 @@ builtin.fd = function(opts) return end - -- TODO: CWD not 100% supported at this moment. - -- Previewers don't work. We'll have to try out something for that later local cwd = opts.cwd if cwd then cwd = vim.fn.expand(cwd) @@ -388,7 +388,9 @@ end builtin.treesitter = function(opts) opts = opts or {} - local has_nvim_treesitter, nvim_treesitter = pcall(require, 'nvim-treesitter') + opts.show_line = utils.get_default(opts.show_line, true) + + local has_nvim_treesitter, _ = pcall(require, 'nvim-treesitter') if not has_nvim_treesitter then print('You need to install nvim-treesitter') return diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 6139d36..9e1c337 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -1,25 +1,61 @@ -local get_default = require('telescope.utils').get_default +-- Keep the values around between reloads +_TelescopeConfigurationValues = _TelescopeConfigurationValues or {} +local function first_non_null(...) + local n = select('#', ...) + for i = 1, n do + local value = select(i, ...) + + if value ~= nil then + return value + end + end +end -- TODO: Add other major configuration points here. -- border -- borderchars -- selection_strategy --- TODO: use `require('telescope').setup { }` +local config = {} -_TelescopeConfigurationValues = _TelescopeConfigurationValues or {} +config.values = _TelescopeConfigurationValues -_TelescopeConfigurationValues.default_layout_strategy = get_default( - _TelescopeConfigurationValues.default_layout_strategy, - 'horizontal' -) +function config.set_defaults(defaults) + defaults = defaults or {} --- TODO: this should probably be more complicated than just a number. --- If you're going to allow a bunch of layout strats, they should have nested info or something -_TelescopeConfigurationValues.default_window_width = get_default( - _TelescopeConfigurationValues.default_window_width, - 0.75 -) + local function get(name, default_val) + return first_non_null(defaults[name], config.values[name], default_val) + end -return _TelescopeConfigurationValues + local function set(name, default_val) + config.values[name] = get(name, default_val) + end + + set("selection_strategy", "reset") + + set("layout_strategy", "horizontal") + set("width", 0.75) + set("winblend", 0) + + -- TODO: Shortenpath + -- Decide how to propagate that to all the opts everywhere. + + -- NOT STABLE. DO NOT USE + set("horizontal_config", { + get_preview_width = function(columns, _) + return math.floor(columns * 0.75) + end, + }) +end + +function config.clear_defaults() + for k, _ in pairs(config.values) do + config.values[k] = nil + end +end + +config.set_defaults() + + +return config diff --git a/lua/telescope/init.lua b/lua/telescope/init.lua index 8fdd56f..38c27ff 100644 --- a/lua/telescope/init.lua +++ b/lua/telescope/init.lua @@ -1,25 +1,50 @@ --- TODO: Debounce preview window maybe --- TODO: Make filters --- "fzf --filter" --- jobstart() -> | fzf --filter "input on prompt" +local telescope = {} -local finders = require('telescope.finders') -local pickers = require('telescope.pickers') -local previewers = require('telescope.previewers') -local sorters = require('telescope.sorters') -local state = require('telescope.state') -local builtin = require('telescope.builtin') +--[[ +local actions = require('telescope.actions') -local telescope = { --- -- .new { } --- finders = finders, --- pickers = pickers, --- previewers = previewers, --- sorters = sorters, +require('telescope').setup { + defaults = { + -- Picker Configuration + border = {}, + borderchars = { '─', '│', '─', '│', '┌', '┐', '┘', '└'}, + preview_cutoff = 120, + selection_strategy = "reset", --- state = state, + -- Can choose EITHER one of these: + layout_strategy = "horizontal", --- builtin = builtin, + get_window_options = function(...) end, + + default_mappings = { + i = { + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + }, + + n = { + [""] = actions.close, + [""] = actions.goto_file_selection_edit, + }, + }, + + shorten_path = true, + + winblend = 10, -- help winblend + + winblend = { + preview = 0, + prompt = 20, + results = 20, + }, + + }, } +--]] + +function telescope.setup(opts) + require('telescope.config').set_defaults(opts.defaults) +end + return telescope diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index a25758e..1b7354f 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -193,17 +193,40 @@ end function make_entry.gen_from_treesitter(opts) opts = opts or {} + + local bufnr = opts.bufnr or vim.api.nvim_get_current_buf() + + local make_display = function(entry) + if opts.show_line then + if not tonumber(opts.show_line) then + opts.show_line = 30 + end + + local spacing = string.rep(" ", opts.show_line - #entry.ordinal) + + return entry.ordinal .. spacing .. ": " .. (vim.api.nvim_buf_get_lines( + bufnr, + entry.lnum - 1, + entry.lnum, + false + )[1] or '') + else + return entry.ordinal + end + end + return function(entry) local ts_utils = require('nvim-treesitter.ts_utils') local start_row, start_col, end_row, end_col = ts_utils.get_node_range(entry.node) local node_text = ts_utils.get_node_text(entry.node)[1] - local bufnr = vim.api.nvim_get_current_buf() return { valid = true, value = entry.node, ordinal = entry.kind .. " " .. node_text, - display = entry.kind .. " " .. node_text, + display = make_display, + + node_text = node_text, filename = vim.api.nvim_buf_get_name(bufnr), -- need to add one since the previewer substacts one diff --git a/lua/telescope/opts.lua b/lua/telescope/opts.lua new file mode 100644 index 0000000..0c3de91 --- /dev/null +++ b/lua/telescope/opts.lua @@ -0,0 +1,17 @@ + +local opts_manager = {} + +-- Could use cool metatable to do this automatically +-- Idk, I have some other thoughts. +opts_manager.shorten_path = function(opts) + if opts.shorten_path ~= nil then + return opts.shorten_path + elseif config.values.shorten_path ~= nil then + return config.values.shorten_path + else + return true + end +end + + +return opts_manager diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 7819bec..075d40b 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -100,9 +100,10 @@ function Picker:new(opts) -- mappings = get_default(opts.mappings, default_mappings), attach_mappings = opts.attach_mappings, - layout_strategy = opts.layout_strategy, + selection_strategy = get_default(opts.selection_strategy, config.values.selection_strategy), + + layout_strategy = get_default(opts.layout_strategy, config.values.layout_strategy), get_window_options = opts.get_window_options, - selection_strategy = opts.selection_strategy, window = { -- TODO: This won't account for different layouts... @@ -110,13 +111,16 @@ function Picker:new(opts) -- TODO: If its's a single number, it's always that many columsn -- TODO: If it's a list, of length 2, then it's a range of min to max? height = get_default(opts.height, 0.8), - width = get_default(opts.width, config.default_window_width), - preview_width = get_default(opts.preview_width, 0.8), + width = get_default(opts.width, config.values.width), + get_preview_width = get_default(opts.preview_width, config.values.get_preview_width), results_width = get_default(opts.results_width, 0.8), -- Border config border = get_default(opts.border, {}), borderchars = get_default(opts.borderchars, { '─', '│', '─', '│', '┌', '┐', '┘', '└'}), + + -- WIP: + horizontal_config = get_default(opts.horizontal_config, config.values.horizontal_config), }, preview_cutoff = get_default(opts.preview_cutoff, 120), @@ -158,10 +162,6 @@ end function Picker:get_window_options(max_columns, max_lines, prompt_title) local layout_strategy = self.layout_strategy - if not layout_strategy then - layout_strategy = config.default_layout_strategy - end - local getter = layout_strategies[layout_strategy] if not getter then @@ -203,7 +203,7 @@ function Picker:find() -- TODO: For some reason, highlighting is kind of weird on these windows. -- It may actually be my colorscheme tho... a.nvim_win_set_option(preview_win, 'winhl', 'Normal:TelescopeNormal') - a.nvim_win_set_option(preview_win, 'winblend', 10) + a.nvim_win_set_option(preview_win, 'winblend', config.values.winblend) end -- TODO: We need to center this and make it prettier... diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index 0d66af3..ff5c681 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -18,19 +18,25 @@ layout_strategies.horizontal = function(self, max_columns, max_lines, prompt_tit local prompt = initial_options.prompt -- TODO: Test with 120 width terminal - -- TODO: Test with self.width. + -- TODO: Test with self.width local width_padding = 10 - if not self.previewer or max_columns < self.preview_cutoff then - width_padding = 2 - preview.width = 0 - elseif max_columns < 150 then - width_padding = 5 - preview.width = math.floor(max_columns * 0.4) - elseif max_columns < 200 then - preview.width = 80 + + -- TODO: Determine config settings. + if false and self.window.horizontal_config and self.window.horizontal_config.get_preview_width then + preview.width = self.window.horizontal_config.get_preview_width(max_columns, max_lines) else - preview.width = 120 + if not self.previewer or max_columns < self.preview_cutoff then + width_padding = 2 + preview.width = 0 + elseif max_columns < 150 then + width_padding = 5 + preview.width = math.floor(max_columns * 0.4) + elseif max_columns < 200 then + preview.width = 80 + else + preview.width = 120 + end end local other_width = max_columns - preview.width - (2 * width_padding)