diff --git a/README.md b/README.md index 93395ca..7fe1a20 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ On versions prior to 0.6, this plugin will act as a polyfill for `vim.ui` `vim.select` (built-in) -![Screenshot from 2021-12-02 19-50-50](https://user-images.githubusercontent.com/506791/144542327-c7420501-c75d-40de-9fc8-70204ed16589.png) +![Screenshot from 2021-12-04 17-14-32](https://user-images.githubusercontent.com/506791/144729527-ede0d7ba-a6e6-41e0-be5a-1a5f16d35b05.png) ## Installation dressing.nvim supports all the usual plugin managers diff --git a/lua/dressing/select/builtin.lua b/lua/dressing/select/builtin.lua index c9ec2d5..aceaee5 100644 --- a/lua/dressing/select/builtin.lua +++ b/lua/dressing/select/builtin.lua @@ -27,13 +27,14 @@ M.select = function(config, items, opts, on_choice) end vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines) vim.api.nvim_buf_set_option(bufnr, "modifiable", false) + local width = util.calculate_width(max_width, config) local winopt = { relative = config.relative, anchor = config.anchor, row = config.row, col = config.col, border = config.border, - width = util.calculate_width(max_width, config), + width = width, height = util.calculate_height(#lines, config), zindex = 150, style = "minimal", @@ -43,6 +44,29 @@ M.select = function(config, items, opts, on_choice) vim.api.nvim_win_set_option(winnr, "cursorline", true) pcall(vim.api.nvim_win_set_option, winnr, "cursorlineopt", "both") + -- Create the title window once the main window is placed. + -- Have to defer here or the title will be in the wrong location + vim.defer_fn(function() + local titlebuf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(titlebuf, 0, -1, true, { " " .. opts.prompt }) + vim.api.nvim_buf_set_option(titlebuf, "bufhidden", "wipe") + local prompt_width = math.min(width, 2 + vim.api.nvim_strwidth(opts.prompt)) + local titlewin = vim.api.nvim_open_win(titlebuf, false, { + relative = "win", + win = winnr, + width = prompt_width, + height = 1, + row = -1, + col = (width - prompt_width) / 2, + focusable = false, + zindex = 151, + style = "minimal", + noautocmd = true, + }) + vim.api.nvim_buf_set_var(bufnr, "dressing_title_window", titlewin) + vim.api.nvim_win_set_option(titlewin, "winblend", config.winblend) + end, 5) + local function map(lhs, rhs) vim.api.nvim_buf_set_keymap(bufnr, "n", lhs, rhs, { silent = true, noremap = true }) end @@ -51,24 +75,32 @@ M.select = function(config, items, opts, on_choice) map("", [[lua require('dressing.select.builtin').cancel()]]) map("", [[lua require('dressing.select.builtin').cancel()]]) vim.cmd([[ - autocmd BufLeave ++nested ++once lua require('dressing.select.builtin').cancel() + autocmd BufLeave ++nested ++once lua require('dressing.select.builtin').cancel() ]]) end +local function close_window() + local callback = _callback + local items = _items + clear_callback() + local ok, titlewin = pcall(vim.api.nvim_buf_get_var, 0, "dressing_title_window") + if ok and vim.api.nvim_win_is_valid(titlewin) then + vim.api.nvim_win_close(titlewin, true) + end + vim.api.nvim_win_close(0, true) + return callback, items +end + M.choose = function() local cursor = vim.api.nvim_win_get_cursor(0) local idx = cursor[1] - local item = _items[idx] - local callback = _callback - clear_callback() - vim.api.nvim_win_close(0, true) + local callback, items = close_window() + local item = items[idx] callback(item, idx) end M.cancel = function() - local callback = _callback - clear_callback() - vim.api.nvim_win_close(0, true) + local callback = close_window() callback(nil, nil) end