* fix(#2109): floating help window * fix(#2109): floating help window * fix(#2109): floating help window * fix(#2109): floating help window * fix(#2109): floating help window * fix(#2109): floating help window * fix(#2109): floating help window * fix(#2109): floating help window * help float no border Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com> * Update lua/nvim-tree/help.lua Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com> * Update lua/nvim-tree/help.lua Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com> * Update lua/nvim-tree/help.lua Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com> * Update lua/nvim-tree/help.lua Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com> * fix(#2109): floating help window * fix(#2109): floating help window --------- Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com>
This commit is contained in:
parent
b601b5aa25
commit
0a144ba50a
@ -690,6 +690,7 @@ function M.setup(conf)
|
||||
require("nvim-tree.live-filter").setup(opts)
|
||||
require("nvim-tree.marks").setup(opts)
|
||||
require("nvim-tree.modified").setup(opts)
|
||||
require("nvim-tree.help").setup(opts)
|
||||
if M.config.renderer.icons.show.file and pcall(require, "nvim-web-devicons") then
|
||||
require("nvim-web-devicons").setup()
|
||||
end
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
local view = require "nvim-tree.view"
|
||||
local filters = require "nvim-tree.explorer.filters"
|
||||
local renderer = require "nvim-tree.renderer"
|
||||
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
|
||||
|
||||
local M = {}
|
||||
@ -30,9 +28,4 @@ function M.dotfiles()
|
||||
return reloaders.reload_explorer()
|
||||
end
|
||||
|
||||
function M.help()
|
||||
view.toggle_help()
|
||||
renderer.draw()
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@ -91,7 +91,7 @@ Api.tree.toggle_custom_filter = require("nvim-tree.actions.tree-modifiers.toggle
|
||||
|
||||
Api.tree.toggle_hidden_filter = require("nvim-tree.actions.tree-modifiers.toggles").dotfiles
|
||||
|
||||
Api.tree.toggle_help = require("nvim-tree.actions.tree-modifiers.toggles").help
|
||||
Api.tree.toggle_help = require("nvim-tree.help").toggle
|
||||
|
||||
Api.fs.create = inject_node(require("nvim-tree.actions.fs.create-file").fn)
|
||||
Api.fs.remove = inject_node(require("nvim-tree.actions.fs.remove-file").fn)
|
||||
|
||||
197
lua/nvim-tree/help.lua
Normal file
197
lua/nvim-tree/help.lua
Normal file
@ -0,0 +1,197 @@
|
||||
local keymap = require "nvim-tree.keymap"
|
||||
|
||||
local PAT_MOUSE = "^<.*Mouse"
|
||||
local PAT_CTRL = "^<C%-"
|
||||
local PAT_SPECIAL = "^<.+"
|
||||
|
||||
local WIN_HL = table.concat({
|
||||
"Normal:NvimTreeNormal",
|
||||
"CursorLine:NvimTreeCursorLine",
|
||||
}, ",")
|
||||
|
||||
local M = {
|
||||
config = {},
|
||||
|
||||
-- one and only buf/win
|
||||
bufnr = nil,
|
||||
winnr = nil,
|
||||
}
|
||||
|
||||
--- Shorten and normalise a vim command lhs
|
||||
--- @param lhs string
|
||||
--- @return string
|
||||
local function tidy_lhs(lhs)
|
||||
-- nvim_buf_get_keymap replaces leading "<" with "<lt>" e.g. "<lt>CTRL-v>"
|
||||
lhs = lhs:gsub("^<lt>", "<")
|
||||
|
||||
-- shorten ctrls
|
||||
if lhs:lower():match "^<ctrl%-" then
|
||||
lhs = lhs:lower():gsub("^<ctrl%-", "<C%-")
|
||||
end
|
||||
|
||||
-- uppercase ctrls
|
||||
if lhs:lower():match "^<c%-" then
|
||||
lhs = lhs:upper()
|
||||
end
|
||||
|
||||
-- space is not escaped
|
||||
lhs = lhs:gsub(" ", "<Space>")
|
||||
|
||||
return lhs
|
||||
end
|
||||
|
||||
--- Remove prefix 'nvim-tree: '
|
||||
--- Hardcoded to keep default_on_attach simple
|
||||
--- @param desc string
|
||||
--- @return string
|
||||
local function tidy_desc(desc)
|
||||
return desc and desc:gsub("^nvim%-tree: ", "") or ""
|
||||
end
|
||||
|
||||
--- sort vim command lhs roughly as per :help index
|
||||
--- @param a string
|
||||
--- @param b string
|
||||
local function sort_lhs(a, b)
|
||||
-- mouse first
|
||||
if a:match(PAT_MOUSE) and not b:match(PAT_MOUSE) then
|
||||
return true
|
||||
elseif not a:match(PAT_MOUSE) and b:match(PAT_MOUSE) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- ctrl next
|
||||
if a:match(PAT_CTRL) and not b:match(PAT_CTRL) then
|
||||
return true
|
||||
elseif not a:match(PAT_CTRL) and b:match(PAT_CTRL) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- special next
|
||||
if a:match(PAT_SPECIAL) and not b:match(PAT_SPECIAL) then
|
||||
return true
|
||||
elseif not a:match(PAT_SPECIAL) and b:match(PAT_SPECIAL) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- remainder alpha
|
||||
return a:gsub("[^a-zA-Z]", "") < b:gsub("[^a-zA-Z]", "")
|
||||
end
|
||||
|
||||
--- Compute all lines for the buffer
|
||||
--- @return table strings of text
|
||||
--- @return table arrays of arguments 3-6 for nvim_buf_add_highlight()
|
||||
--- @return number maximum length of text
|
||||
local function compute()
|
||||
local hl = { { "NvimTreeRootFolder", 0, 0, 18 } }
|
||||
local width = 0
|
||||
|
||||
-- formatted lhs and desc from active keymap
|
||||
local mappings = vim.tbl_map(function(map)
|
||||
return { lhs = tidy_lhs(map.lhs), desc = tidy_desc(map.desc) }
|
||||
end, keymap.get_keymap())
|
||||
|
||||
-- sort roughly by lhs
|
||||
table.sort(mappings, function(a, b)
|
||||
return sort_lhs(a.lhs, b.lhs)
|
||||
end)
|
||||
|
||||
-- longest lhs and description
|
||||
local max_lhs = 0
|
||||
local max_desc = 0
|
||||
for _, l in pairs(mappings) do
|
||||
max_lhs = math.max(#l.lhs, max_lhs)
|
||||
max_desc = math.max(#l.desc, max_desc)
|
||||
end
|
||||
|
||||
local lines = { ("nvim-tree mappings%sexit: q"):format(string.rep(" ", max_desc + max_lhs - 23)) }
|
||||
local fmt = string.format(" %%-%ds %%-%ds", max_lhs, max_desc)
|
||||
for i, l in ipairs(mappings) do
|
||||
-- format in left aligned columns
|
||||
local line = string.format(fmt, l.lhs, l.desc)
|
||||
table.insert(lines, line)
|
||||
width = math.max(#line, width)
|
||||
|
||||
-- highlight lhs
|
||||
table.insert(hl, { "NvimTreeFolderName", i, 0, #l.lhs + 1 })
|
||||
end
|
||||
|
||||
return lines, hl, width
|
||||
end
|
||||
|
||||
--- close the window and delete the buffer, if they exist
|
||||
local function close()
|
||||
if M.winnr then
|
||||
vim.api.nvim_win_close(M.winnr, true)
|
||||
M.winnr = nil
|
||||
end
|
||||
if M.bufnr then
|
||||
vim.api.nvim_buf_delete(M.bufnr, { force = true })
|
||||
M.bufnr = nil
|
||||
end
|
||||
end
|
||||
|
||||
--- open a new window and buffer
|
||||
local function open()
|
||||
-- close existing, shouldn't be necessary
|
||||
close()
|
||||
|
||||
-- text and highlight
|
||||
local lines, hl, width = compute()
|
||||
|
||||
-- create the buffer
|
||||
M.bufnr = vim.api.nvim_create_buf(false, true)
|
||||
|
||||
-- populate it
|
||||
vim.api.nvim_buf_set_lines(M.bufnr, 0, -1, false, lines)
|
||||
vim.api.nvim_buf_set_option(M.bufnr, "modifiable", false)
|
||||
|
||||
-- highlight it
|
||||
for _, h in ipairs(hl) do
|
||||
vim.api.nvim_buf_add_highlight(M.bufnr, -1, h[1], h[2], h[3], h[4])
|
||||
end
|
||||
|
||||
-- open a very restricted window
|
||||
M.winnr = vim.api.nvim_open_win(M.bufnr, true, {
|
||||
relative = "editor",
|
||||
border = "single",
|
||||
width = width,
|
||||
height = #lines,
|
||||
row = 1,
|
||||
col = 0,
|
||||
style = "minimal",
|
||||
noautocmd = true,
|
||||
})
|
||||
|
||||
-- style it a bit like the tree
|
||||
vim.wo[M.winnr].winhl = WIN_HL
|
||||
vim.wo[M.winnr].cursorline = M.config.cursorline
|
||||
|
||||
-- quit binding
|
||||
vim.keymap.set(
|
||||
"n",
|
||||
"q",
|
||||
close,
|
||||
{ desc = "nvim-tree: exit help", buffer = M.bufnr, noremap = true, silent = true, nowait = true }
|
||||
)
|
||||
|
||||
-- close window and delete buffer on leave
|
||||
vim.api.nvim_create_autocmd({ "BufLeave", "WinLeave" }, {
|
||||
buffer = M.bufnr,
|
||||
once = true,
|
||||
callback = close,
|
||||
})
|
||||
end
|
||||
|
||||
function M.toggle()
|
||||
if M.winnr or M.bufnr then
|
||||
close()
|
||||
else
|
||||
open()
|
||||
end
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
M.config.cursorline = opts.view.cursorline
|
||||
end
|
||||
|
||||
return M
|
||||
@ -24,11 +24,6 @@ function M.get_node_at_cursor()
|
||||
|
||||
local cursor = vim.api.nvim_win_get_cursor(view.get_winnr())
|
||||
local line = cursor[1]
|
||||
if view.is_help_ui() then
|
||||
local help_lines = require("nvim-tree.renderer.help").compute_lines()
|
||||
local help_text = utils.get_nodes_by_line(help_lines, 1)[line]
|
||||
return { name = help_text }
|
||||
end
|
||||
|
||||
if line == 1 and view.is_root_folder_visible(core.get_cwd()) then
|
||||
return { name = ".." }
|
||||
|
||||
@ -1,89 +0,0 @@
|
||||
local M = {}
|
||||
|
||||
local function tidy_lhs(lhs)
|
||||
-- nvim_buf_get_keymap replaces leading "<" with "<lt>" e.g. "<lt>CTRL-v>"
|
||||
lhs = lhs:gsub("^<lt>", "<")
|
||||
|
||||
-- shorten ctrls
|
||||
if lhs:lower():match "^<ctrl%-" then
|
||||
lhs = lhs:lower():gsub("^<ctrl%-", "<C%-")
|
||||
end
|
||||
|
||||
-- uppercase ctrls
|
||||
if lhs:lower():match "^<c%-" then
|
||||
lhs = lhs:upper()
|
||||
end
|
||||
|
||||
-- space is not escaped
|
||||
lhs = lhs:gsub(" ", "<Space>")
|
||||
|
||||
return lhs
|
||||
end
|
||||
|
||||
--- Remove prefix 'nvim-tree: '
|
||||
--- Hardcoded to keep default_on_attach simple
|
||||
--- @param desc string
|
||||
--- @return string|nil
|
||||
local function tidy_desc(desc)
|
||||
return desc and desc:gsub("^nvim%-tree: ", "") or ""
|
||||
end
|
||||
|
||||
-- sort lhs roughly as per :help index
|
||||
local PAT_MOUSE = "^<.*Mouse"
|
||||
local PAT_CTRL = "^<C%-"
|
||||
local PAT_SPECIAL = "^<.+"
|
||||
local function sort_lhs(a, b)
|
||||
-- mouse last
|
||||
if a:match(PAT_MOUSE) and not b:match(PAT_MOUSE) then
|
||||
return false
|
||||
elseif not a:match(PAT_MOUSE) and b:match(PAT_MOUSE) then
|
||||
return true
|
||||
end
|
||||
|
||||
-- ctrl first
|
||||
if a:match(PAT_CTRL) and not b:match(PAT_CTRL) then
|
||||
return true
|
||||
elseif not a:match(PAT_CTRL) and b:match(PAT_CTRL) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- special next
|
||||
if a:match(PAT_SPECIAL) and not b:match(PAT_SPECIAL) then
|
||||
return true
|
||||
elseif not a:match(PAT_SPECIAL) and b:match(PAT_SPECIAL) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- lowercase alpha characters only
|
||||
return a:gsub("[^a-zA-Z]", ""):lower() < b:gsub("[^a-zA-Z]", ""):lower()
|
||||
end
|
||||
|
||||
function M.compute_lines()
|
||||
local help_lines = { "HELP" }
|
||||
local help_hl = { { "NvimTreeRootFolder", 0, 0, #help_lines[1] } }
|
||||
|
||||
local buf_keymaps = vim.api.nvim_buf_get_keymap(vim.api.nvim_get_current_buf(), "")
|
||||
|
||||
local lines = vim.tbl_map(function(bkm)
|
||||
return { lhs = tidy_lhs(bkm.lhs), desc = tidy_desc(bkm.desc) }
|
||||
end, buf_keymaps)
|
||||
|
||||
table.sort(lines, function(a, b)
|
||||
return sort_lhs(a.lhs, b.lhs)
|
||||
end)
|
||||
|
||||
local num = 0
|
||||
for _, p in pairs(lines) do
|
||||
num = num + 1
|
||||
local bind_string = string.format("%-5s %s", p.lhs, p.desc)
|
||||
local hl_len = math.max(5, string.len(p.lhs))
|
||||
table.insert(help_lines, bind_string)
|
||||
|
||||
table.insert(help_hl, { "NvimTreeFolderName", num, 0, hl_len })
|
||||
|
||||
table.insert(help_hl, { "NvimTreeFileRenamed", num, hl_len, -1 })
|
||||
end
|
||||
return help_lines, help_hl
|
||||
end
|
||||
|
||||
return M
|
||||
@ -7,7 +7,6 @@ local modified = require "nvim-tree.renderer.components.modified"
|
||||
local _padding = require "nvim-tree.renderer.components.padding"
|
||||
local icon_component = require "nvim-tree.renderer.components.icons"
|
||||
local full_name = require "nvim-tree.renderer.components.full-name"
|
||||
local help = require "nvim-tree.renderer.help"
|
||||
local git = require "nvim-tree.renderer.components.git"
|
||||
local Builder = require "nvim-tree.renderer.builder"
|
||||
local live_filter = require "nvim-tree.live-filter"
|
||||
@ -60,27 +59,21 @@ function M.draw(unloaded_bufnr)
|
||||
local cursor = vim.api.nvim_win_get_cursor(view.get_winnr())
|
||||
icon_component.reset_config()
|
||||
|
||||
local lines, hl
|
||||
local signs = {}
|
||||
if view.is_help_ui() then
|
||||
lines, hl = help.compute_lines()
|
||||
else
|
||||
lines, hl, signs = Builder.new(core.get_cwd())
|
||||
:configure_root_label(M.config.root_folder_label)
|
||||
:configure_trailing_slash(M.config.add_trailing)
|
||||
:configure_special_files(M.config.special_files)
|
||||
:configure_picture_map(picture_map)
|
||||
:configure_opened_file_highlighting(M.config.highlight_opened_files)
|
||||
:configure_modified_highlighting(M.config.highlight_modified)
|
||||
:configure_icon_padding(M.config.icons.padding)
|
||||
:configure_git_icons_placement(M.config.icons.git_placement)
|
||||
:configure_modified_placement(M.config.icons.modified_placement)
|
||||
:configure_symlink_destination(M.config.symlink_destination)
|
||||
:configure_filter(live_filter.filter, live_filter.prefix)
|
||||
:build_header(view.is_root_folder_visible(core.get_cwd()))
|
||||
:build(core.get_explorer(), unloaded_bufnr)
|
||||
:unwrap()
|
||||
end
|
||||
local lines, hl, signs = Builder.new(core.get_cwd())
|
||||
:configure_root_label(M.config.root_folder_label)
|
||||
:configure_trailing_slash(M.config.add_trailing)
|
||||
:configure_special_files(M.config.special_files)
|
||||
:configure_picture_map(picture_map)
|
||||
:configure_opened_file_highlighting(M.config.highlight_opened_files)
|
||||
:configure_modified_highlighting(M.config.highlight_modified)
|
||||
:configure_icon_padding(M.config.icons.padding)
|
||||
:configure_git_icons_placement(M.config.icons.git_placement)
|
||||
:configure_modified_placement(M.config.icons.modified_placement)
|
||||
:configure_symlink_destination(M.config.symlink_destination)
|
||||
:configure_filter(live_filter.filter, live_filter.prefix)
|
||||
:build_header(view.is_root_folder_visible(core.get_cwd()))
|
||||
:build(core.get_explorer(), unloaded_bufnr)
|
||||
:unwrap()
|
||||
|
||||
_draw(bufnr, lines, hl, signs)
|
||||
|
||||
@ -90,13 +83,8 @@ function M.draw(unloaded_bufnr)
|
||||
vim.api.nvim_win_set_cursor(view.get_winnr(), cursor)
|
||||
end
|
||||
|
||||
if view.is_help_ui() then
|
||||
diagnostics.clear()
|
||||
marks.clear()
|
||||
else
|
||||
diagnostics.update()
|
||||
marks.draw()
|
||||
end
|
||||
diagnostics.update()
|
||||
marks.draw()
|
||||
|
||||
view.grow_from_content()
|
||||
|
||||
|
||||
@ -47,8 +47,6 @@ M.View = {
|
||||
|
||||
-- The initial state of a tab
|
||||
local tabinitial = {
|
||||
-- True if help is displayed
|
||||
help = false,
|
||||
-- The position of the cursor { line, column }
|
||||
cursor = { 0, 0 },
|
||||
-- The NvimTree window number
|
||||
@ -434,22 +432,6 @@ function M.get_bufnr()
|
||||
return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
|
||||
end
|
||||
|
||||
--- Checks if nvim-tree is displaying the help ui within the tabpage specified
|
||||
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
|
||||
---@return number|nil
|
||||
function M.is_help_ui(tabpage)
|
||||
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
|
||||
local tabinfo = M.View.tabpages[tabpage]
|
||||
if tabinfo ~= nil then
|
||||
return tabinfo.help
|
||||
end
|
||||
end
|
||||
|
||||
function M.toggle_help(tabpage)
|
||||
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
|
||||
M.View.tabpages[tabpage].help = not M.View.tabpages[tabpage].help
|
||||
end
|
||||
|
||||
function M.is_buf_valid(bufnr)
|
||||
return bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user