Skip to content

Commit d6652a8

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

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
-- Restore original functions after each test
79+
lspsaga_symbol.get_buf_symbols = lspsaga_symbols__get_buf_symbols
80+
lspsaga_head.get_buf_symbols = lspsaga_head__get_buf_symbols
81+
end)
82+
83+
it('should correctly extract components from the winbar', function()
84+
-- Initialize the winbar for the current buffer
85+
lspsaga_winbar.init_winbar(api.nvim_get_current_buf())
86+
87+
-- Simulate a situation with many items in the winbar
88+
-- (Adapt as necessary to create a long list of symbols)
89+
local cur_win = api.nvim_get_current_win()
90+
local max_width = math.floor(api.nvim_win_get_width(cur_win) * 0.9)
91+
api.nvim_set_option_value('winbar', string.rep('Item|', max_width), {
92+
scope = 'local',
93+
win = api.nvim_get_current_win(),
94+
})
95+
96+
-- Define a winbar value large enough to exceed the window width
97+
local winbar_value = lspsaga_winbar.get_bar() or ''
98+
99+
-- Extract the components of the winbar
100+
local saga_prefix = helper.extract_winbar_value(winbar_value, 'Prefix')
101+
local saga_sep = helper.extract_winbar_value(winbar_value, 'SagaSep')
102+
local saga_object = helper.extract_winbar_value(winbar_value, 'SagaObject')
103+
104+
-- Verify that components were extracted correctly
105+
assert(saga_prefix, 'Prefix not found in winbar_value')
106+
assert(saga_sep, 'Separator not found in winbar_value')
107+
assert(saga_object, 'Symbol not found in winbar_value')
108+
109+
-- Optionally, check individual presence of prefix, separator, and symbol
110+
assert(saga_prefix == ' ', 'Prefix does not match expected value')
111+
assert(saga_sep == '|', 'Separator does not match expected value')
112+
assert(saga_object == ' command', 'Symbol does not match expected value')
113+
end)
114+
end)

0 commit comments

Comments
 (0)