diff --git a/lua/lspsaga/highlight.lua b/lua/lspsaga/highlight.lua index d087cb0fd..a5015a7e4 100644 --- a/lua/lspsaga/highlight.lua +++ b/lua/lspsaga/highlight.lua @@ -4,6 +4,7 @@ local kind = require('lspsaga.lspkind').kind local function hi_define() return { -- general + SagaPrefix = { link = 'Prefix' }, SagaTitle = { link = 'Title' }, SagaBorder = { link = 'FloatBorder' }, SagaNormal = { link = 'NormalFloat' }, diff --git a/lua/lspsaga/symbol/winbar.lua b/lua/lspsaga/symbol/winbar.lua index 17638534e..459d1d474 100644 --- a/lua/lspsaga/symbol/winbar.lua +++ b/lua/lspsaga/symbol/winbar.lua @@ -21,9 +21,15 @@ local function path_in_bar(buf) local bar = bar_prefix() local items = {} + + local winbar_prefix = type(ui.winbar_prefix) == 'string' + and #ui.winbar_prefix > 0 + and '%#Prefix#' .. ui.winbar_prefix + or '' + local folder if ui.foldericon then - folder = ui.winbar_prefix .. get_kind_icon(302)[2] + folder = get_kind_icon(302)[2] end for item in util.path_itera(buf) do @@ -34,7 +40,7 @@ local function path_in_bar(buf) and '%#' .. (hl or 'SagaFileIcon') .. '#' .. (icon and icon .. ' ' or '') .. '%*' .. bar.prefix .. 'FileName#' .. item or bar.prefix .. 'Folder#' - .. (folder and folder or ui.winbar_prefix) + .. (folder and folder or '') .. '%*' .. bar.prefix .. 'FolderName#' @@ -47,7 +53,7 @@ local function path_in_bar(buf) end end - local barstr = '' + local barstr = winbar_prefix .. '' for i = #items, 1, -1 do barstr = barstr .. items[i] .. (i > 1 and bar.sep or '') end diff --git a/test/helper.lua b/test/helper.lua index 2d6d2f09d..832d66dd2 100644 --- a/test/helper.lua +++ b/test/helper.lua @@ -57,10 +57,17 @@ local function lspconfig_dep() lspconfig.lua_ls.setup({}) end +local function extract_winbar_value(input, arg) + local pattern = '%%#' .. arg .. '#([^%%#]+)' + local winbar_value = string.match(input, pattern) + return winbar_value or '' +end + return { test_dir = test_dir, feedkey = feedkey, treesitter_dep = treesitter_dep, lspconfig_dep = lspconfig_dep, join_paths = join_paths, + extract_winbar_value = extract_winbar_value, } diff --git a/test/winbar_spec.lua b/test/winbar_spec.lua new file mode 100644 index 000000000..d875f1a9e --- /dev/null +++ b/test/winbar_spec.lua @@ -0,0 +1,125 @@ +-- Define a table simulating LSP (Language Server Protocol) symbol responses +local lsp_symbols = { + pending_request = false, + symbols = { + { + detail = '', + kind = 19, + name = 'command', + range = { + ['end'] = { + character = 18, + line = 0, + }, + start = { + character = 6, + line = 0, + }, + }, + selectionRange = { + ['end'] = { + character = 13, + line = 0, + }, + start = { + character = 6, + line = 0, + }, + }, + }, + }, +} + +-- Function to get symbols, returns the simulated lsp_symbols table +function lsp_symbols.get_symbols(_bufnr) + return lsp_symbols +end + +-- Configuration for the lspsaga plugin +local lspsaga_opts = { + ui = { + winbar_prefix = ' ', + }, + symbol_in_winbar = { + enable = true, + separator = '|', + }, +} + +-- Require the lspsaga module and configure it with the defined options +local lspsaga = require('lspsaga') +lspsaga.setup(lspsaga_opts) + +describe('winbar', function() + local api = vim.api + local lspsaga_symbols__get_buf_symbols, lspsaga_head__get_buf_symbols + local lspsaga_symbol = require('lspsaga.symbol') + local lspsaga_head = require('lspsaga.symbol.head') + local lspsaga_winbar = require('lspsaga.symbol.winbar') + local helper = require('test.helper') + + before_each(function() + -- Create a new buffer and set it as the current buffer + local buf = api.nvim_create_buf(false, true) + api.nvim_set_current_buf(buf) + api.nvim_command('vsplit') + api.nvim_set_current_win(api.nvim_get_current_win()) + + -- Store original functions + lspsaga_symbols__get_buf_symbols = lspsaga_symbol.get_buf_symbols + lspsaga_head__get_buf_symbols = lspsaga_symbol.get_buf_symbols + + -- Replace real LSP interaction with the mock + lspsaga_symbol.get_buf_symbols = lsp_symbols.get_symbols + lspsaga_head.get_buf_symbols = lsp_symbols.get_symbols + end) + + after_each(function() + -- Close the current window and buffer + local current_win = api.nvim_get_current_win() + local current_buf = api.nvim_get_current_buf() + + -- Close the current window + if current_win ~= 0 then + api.nvim_win_close(current_win, true) + end + + -- Optionally, close the buffer if it's not the last buffer + -- This might be necessary if `vsplit` creates additional windows + -- and you want to ensure that the buffer is not left open. + if current_buf ~= 0 and api.nvim_buf_is_valid(current_buf) then + local buf_list = api.nvim_list_bufs() + if #buf_list > 1 then + -- Close the buffer if there are multiple buffers open + api.nvim_buf_delete(current_buf, { force = true }) + end + end + + -- Restore original functions after each test + lspsaga_symbol.get_buf_symbols = lspsaga_symbols__get_buf_symbols + lspsaga_head.get_buf_symbols = lspsaga_head__get_buf_symbols + end) + + it('should correctly extract components from the winbar', function() + -- Initialize the winbar for the current buffer + lspsaga_winbar.init_winbar(api.nvim_get_current_buf()) + + -- Define a winbar value large enough to exceed the window width + local winbar_value = lspsaga_winbar.get_bar() or '' + + -- Extract the components of the winbar + local saga_prefix = helper.extract_winbar_value(winbar_value, 'Prefix') + local saga_sep = helper.extract_winbar_value(winbar_value, 'SagaSep') + local saga_object = helper.extract_winbar_value(winbar_value, 'SagaObject') + + -- Verify that components were extracted correctly + assert(saga_prefix, 'Prefix not found in winbar_value') + assert(saga_sep, 'Separator not found in winbar_value') + assert(saga_object, 'Symbol not found in winbar_value') + + -- Optionally, check individual presence of prefix, separator, and symbol + assert(saga_prefix == ' ', 'Prefix does not match expected value') + assert(saga_sep == '|', 'Separator does not match expected value') + assert(saga_object == ' command', 'Symbol does not match expected value') + end) +end)