Skip to content

Commit a408e5e

Browse files
fix(#1482): adds the ability to set a prefix to winbar
1 parent 4ce44df commit a408e5e

File tree

4 files changed

+142
-3
lines changed

4 files changed

+142
-3
lines changed

lua/lspsaga/highlight.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local kind = require('lspsaga.lspkind').kind
44
local function hi_define()
55
return {
66
-- general
7+
SagaPrefix = { link = 'Prefix' },
78
SagaTitle = { link = 'Title' },
89
SagaBorder = { link = 'FloatBorder' },
910
SagaNormal = { link = 'NormalFloat' },

lua/lspsaga/symbol/winbar.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ local function path_in_bar(buf)
2121

2222
local bar = bar_prefix()
2323
local items = {}
24+
25+
local winbar_prefix = type(ui.winbar_prefix) == 'string'
26+
and #ui.winbar_prefix > 0
27+
and '%#Prefix#' .. ui.winbar_prefix
28+
or ''
29+
2430
local folder
2531
if ui.foldericon then
26-
folder = ui.winbar_prefix .. get_kind_icon(302)[2]
32+
folder = get_kind_icon(302)[2]
2733
end
2834

2935
for item in util.path_itera(buf) do
@@ -34,7 +40,7 @@ local function path_in_bar(buf)
3440
and '%#' .. (hl or 'SagaFileIcon') .. '#' .. (icon and icon .. ' ' or '') .. '%*' .. bar.prefix .. 'FileName#' .. item
3541
or bar.prefix
3642
.. 'Folder#'
37-
.. (folder and folder or ui.winbar_prefix)
43+
.. (folder and folder or '')
3844
.. '%*'
3945
.. bar.prefix
4046
.. 'FolderName#'
@@ -47,7 +53,7 @@ local function path_in_bar(buf)
4753
end
4854
end
4955

50-
local barstr = ''
56+
local barstr = winbar_prefix .. ''
5157
for i = #items, 1, -1 do
5258
barstr = barstr .. items[i] .. (i > 1 and bar.sep or '')
5359
end

test/helper.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ local function lspconfig_dep()
5757
lspconfig.lua_ls.setup({})
5858
end
5959

60+
local function extract_winbar_value(input, arg)
61+
local pattern = '%%#' .. arg .. '#([^%%#]+)'
62+
local winbar_value = string.match(input, pattern)
63+
return winbar_value or ''
64+
end
65+
6066
return {
6167
test_dir = test_dir,
6268
feedkey = feedkey,
6369
treesitter_dep = treesitter_dep,
6470
lspconfig_dep = lspconfig_dep,
6571
join_paths = join_paths,
72+
extract_winbar_value = extract_winbar_value,
6673
}

test/winbar_spec.lua

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
-- Define a table simulating LSP (Language Server Protocol) symbol responses
2+
local lsp_symbols = {
3+
pending_request = false,
4+
symbols = {
5+
{
6+
detail = '',
7+
kind = 19,
8+
name = 'command',
9+
range = {
10+
['end'] = {
11+
character = 18,
12+
line = 0,
13+
},
14+
start = {
15+
character = 6,
16+
line = 0,
17+
},
18+
},
19+
selectionRange = {
20+
['end'] = {
21+
character = 13,
22+
line = 0,
23+
},
24+
start = {
25+
character = 6,
26+
line = 0,
27+
},
28+
},
29+
},
30+
},
31+
}
32+
33+
-- Function to get symbols, returns the simulated lsp_symbols table
34+
function lsp_symbols.get_symbols(_bufnr)
35+
return lsp_symbols
36+
end
37+
38+
-- Configuration for the lspsaga plugin
39+
local lspsaga_opts = {
40+
ui = {
41+
winbar_prefix = ' ',
42+
},
43+
symbol_in_winbar = {
44+
enable = true,
45+
separator = '|',
46+
},
47+
}
48+
49+
-- Require the lspsaga module and configure it with the defined options
50+
local lspsaga = require('lspsaga')
51+
lspsaga.setup(lspsaga_opts)
52+
53+
describe('winbar', function()
54+
local api = vim.api
55+
local lspsaga_symbols__get_buf_symbols, lspsaga_head__get_buf_symbols
56+
local lspsaga_symbol = require('lspsaga.symbol')
57+
local lspsaga_head = require('lspsaga.symbol.head')
58+
local lspsaga_winbar = require('lspsaga.symbol.winbar')
59+
local helper = require('test.helper')
60+
61+
before_each(function()
62+
-- Create a new buffer and set it as the current buffer
63+
local buf = api.nvim_create_buf(false, true)
64+
api.nvim_set_current_buf(buf)
65+
api.nvim_command('vsplit')
66+
api.nvim_set_current_win(api.nvim_get_current_win())
67+
68+
-- Store original functions
69+
lspsaga_symbols__get_buf_symbols = lspsaga_symbol.get_buf_symbols
70+
lspsaga_head__get_buf_symbols = lspsaga_symbol.get_buf_symbols
71+
72+
-- Replace real LSP interaction with the mock
73+
lspsaga_symbol.get_buf_symbols = lsp_symbols.get_symbols
74+
lspsaga_head.get_buf_symbols = lsp_symbols.get_symbols
75+
end)
76+
77+
after_each(function()
78+
-- Close the current window and buffer
79+
local current_win = api.nvim_get_current_win()
80+
local current_buf = api.nvim_get_current_buf()
81+
82+
-- Close the current window
83+
if current_win ~= 0 then
84+
api.nvim_win_close(current_win, true)
85+
end
86+
87+
-- Optionally, close the buffer if it's not the last buffer
88+
-- This might be necessary if `vsplit` creates additional windows
89+
-- and you want to ensure that the buffer is not left open.
90+
if current_buf ~= 0 and api.nvim_buf_is_valid(current_buf) then
91+
local buf_list = api.nvim_list_bufs()
92+
if #buf_list > 1 then
93+
-- Close the buffer if there are multiple buffers open
94+
api.nvim_buf_delete(current_buf, { force = true })
95+
end
96+
end
97+
98+
-- Restore original functions after each test
99+
lspsaga_symbol.get_buf_symbols = lspsaga_symbols__get_buf_symbols
100+
lspsaga_head.get_buf_symbols = lspsaga_head__get_buf_symbols
101+
end)
102+
103+
it('should correctly extract components from the winbar', function()
104+
-- Initialize the winbar for the current buffer
105+
lspsaga_winbar.init_winbar(api.nvim_get_current_buf())
106+
107+
-- Define a winbar value large enough to exceed the window width
108+
local winbar_value = lspsaga_winbar.get_bar() or ''
109+
110+
-- Extract the components of the winbar
111+
local saga_prefix = helper.extract_winbar_value(winbar_value, 'Prefix')
112+
local saga_sep = helper.extract_winbar_value(winbar_value, 'SagaSep')
113+
local saga_object = helper.extract_winbar_value(winbar_value, 'SagaObject')
114+
115+
-- Verify that components were extracted correctly
116+
assert(saga_prefix, 'Prefix not found in winbar_value')
117+
assert(saga_sep, 'Separator not found in winbar_value')
118+
assert(saga_object, 'Symbol not found in winbar_value')
119+
120+
-- Optionally, check individual presence of prefix, separator, and symbol
121+
assert(saga_prefix == ' ', 'Prefix does not match expected value')
122+
assert(saga_sep == '|', 'Separator does not match expected value')
123+
assert(saga_object == ' command', 'Symbol does not match expected value')
124+
end)
125+
end)

0 commit comments

Comments
 (0)