Begin work on larger global config, to tailor defaults

This commit is contained in:
TJ DeVries
2020-09-07 00:20:08 -04:00
parent 2592586533
commit 11a3c70609
7 changed files with 165 additions and 56 deletions

View File

@@ -313,6 +313,8 @@ builtin.builtin = function(opts)
end 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) builtin.fd = function(opts)
opts = opts or {} opts = opts or {}
@@ -328,8 +330,6 @@ builtin.fd = function(opts)
return return
end 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 local cwd = opts.cwd
if cwd then if cwd then
cwd = vim.fn.expand(cwd) cwd = vim.fn.expand(cwd)
@@ -388,7 +388,9 @@ end
builtin.treesitter = function(opts) builtin.treesitter = function(opts)
opts = opts or {} 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 if not has_nvim_treesitter then
print('You need to install nvim-treesitter') print('You need to install nvim-treesitter')
return return

View File

@@ -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. -- TODO: Add other major configuration points here.
-- border -- border
-- borderchars -- borderchars
-- selection_strategy -- selection_strategy
-- TODO: use `require('telescope').setup { }` local config = {}
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {} config.values = _TelescopeConfigurationValues
_TelescopeConfigurationValues.default_layout_strategy = get_default( function config.set_defaults(defaults)
_TelescopeConfigurationValues.default_layout_strategy, defaults = defaults or {}
'horizontal'
)
-- TODO: this should probably be more complicated than just a number. local function get(name, default_val)
-- If you're going to allow a bunch of layout strats, they should have nested info or something return first_non_null(defaults[name], config.values[name], default_val)
_TelescopeConfigurationValues.default_window_width = get_default( end
_TelescopeConfigurationValues.default_window_width,
0.75
)
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

View File

@@ -1,25 +1,50 @@
-- TODO: Debounce preview window maybe local telescope = {}
-- TODO: Make filters
-- "fzf --filter"
-- jobstart() -> | fzf --filter "input on prompt"
local finders = require('telescope.finders') --[[
local pickers = require('telescope.pickers') local actions = require('telescope.actions')
local previewers = require('telescope.previewers')
local sorters = require('telescope.sorters')
local state = require('telescope.state')
local builtin = require('telescope.builtin')
local telescope = { require('telescope').setup {
-- -- <module>.new { } defaults = {
-- finders = finders, -- Picker Configuration
-- pickers = pickers, border = {},
-- previewers = previewers, borderchars = { '─', '│', '─', '│', '┌', '┐', '┘', '└'},
-- sorters = sorters, 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 = {
["<C-n>"] = actions.move_selection_next,
["<C-p>"] = actions.move_selection_previous,
},
n = {
["<esc>"] = actions.close,
["<CR>"] = 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 return telescope

View File

@@ -193,17 +193,40 @@ end
function make_entry.gen_from_treesitter(opts) function make_entry.gen_from_treesitter(opts)
opts = opts or {} 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) return function(entry)
local ts_utils = require('nvim-treesitter.ts_utils') 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 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 node_text = ts_utils.get_node_text(entry.node)[1]
local bufnr = vim.api.nvim_get_current_buf()
return { return {
valid = true, valid = true,
value = entry.node, value = entry.node,
ordinal = entry.kind .. " " .. node_text, 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), filename = vim.api.nvim_buf_get_name(bufnr),
-- need to add one since the previewer substacts one -- need to add one since the previewer substacts one

17
lua/telescope/opts.lua Normal file
View File

@@ -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

View File

@@ -100,9 +100,10 @@ function Picker:new(opts)
-- mappings = get_default(opts.mappings, default_mappings), -- mappings = get_default(opts.mappings, default_mappings),
attach_mappings = opts.attach_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, get_window_options = opts.get_window_options,
selection_strategy = opts.selection_strategy,
window = { window = {
-- TODO: This won't account for different layouts... -- 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 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? -- 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), height = get_default(opts.height, 0.8),
width = get_default(opts.width, config.default_window_width), width = get_default(opts.width, config.values.width),
preview_width = get_default(opts.preview_width, 0.8), get_preview_width = get_default(opts.preview_width, config.values.get_preview_width),
results_width = get_default(opts.results_width, 0.8), results_width = get_default(opts.results_width, 0.8),
-- Border config -- Border config
border = get_default(opts.border, {}), border = get_default(opts.border, {}),
borderchars = get_default(opts.borderchars, { '', '', '', '', '', '', '', ''}), 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), preview_cutoff = get_default(opts.preview_cutoff, 120),
@@ -158,10 +162,6 @@ end
function Picker:get_window_options(max_columns, max_lines, prompt_title) function Picker:get_window_options(max_columns, max_lines, prompt_title)
local layout_strategy = self.layout_strategy local layout_strategy = self.layout_strategy
if not layout_strategy then
layout_strategy = config.default_layout_strategy
end
local getter = layout_strategies[layout_strategy] local getter = layout_strategies[layout_strategy]
if not getter then if not getter then
@@ -203,7 +203,7 @@ function Picker:find()
-- TODO: For some reason, highlighting is kind of weird on these windows. -- TODO: For some reason, highlighting is kind of weird on these windows.
-- It may actually be my colorscheme tho... -- It may actually be my colorscheme tho...
a.nvim_win_set_option(preview_win, 'winhl', 'Normal:TelescopeNormal') 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 end
-- TODO: We need to center this and make it prettier... -- TODO: We need to center this and make it prettier...

View File

@@ -18,19 +18,25 @@ layout_strategies.horizontal = function(self, max_columns, max_lines, prompt_tit
local prompt = initial_options.prompt local prompt = initial_options.prompt
-- TODO: Test with 120 width terminal -- TODO: Test with 120 width terminal
-- TODO: Test with self.width. -- TODO: Test with self.width
local width_padding = 10 local width_padding = 10
if not self.previewer or max_columns < self.preview_cutoff then
width_padding = 2 -- TODO: Determine config settings.
preview.width = 0 if false and self.window.horizontal_config and self.window.horizontal_config.get_preview_width then
elseif max_columns < 150 then preview.width = self.window.horizontal_config.get_preview_width(max_columns, max_lines)
width_padding = 5
preview.width = math.floor(max_columns * 0.4)
elseif max_columns < 200 then
preview.width = 80
else 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 end
local other_width = max_columns - preview.width - (2 * width_padding) local other_width = max_columns - preview.width - (2 * width_padding)