1 -local dbg_file
= io.
open(
'/tmp/cmdline_debug.log',
'a')
2 -local function dbg(
...)
5 - for i, v
in ipairs(args)
do 6 - parts[i]
= vim.inspect(v)
8 - local line
= os.
date '%H:%M:%S' .. ' ' .. table.
concat(parts,
' ')
9 - dbg_file:write(line
.. '\n')
13 1 local format
= require 'bugabinga.cmdline.format' 14 2 local position
= require 'bugabinga.cmdline.position' 15 3 local cursor
= require 'bugabinga.cmdline.cursor' 20 8 local prompt_ns
= vim.api.nvim_create_namespace
'bugabinga.cmdline.prompt' 31 19 local function get_buf()
32 - if vim.api.nvim_buf_is_valid(state.buffer)
then 35 - state.buffer
= vim.api.nvim_create_buf(
false,
true)
36 - vim.bo[state.buffer].buflisted
= false 37 - vim.bo[state.buffer].filetype
= 'bugabinga_cmdline' 20 + if vim.api.nvim_buf_is_valid( state.buffer )
then 23 + state.buffer
= vim.api.nvim_create_buf(
false,
true )
24 + vim.bo[state.buffer].buflisted
= false 25 + vim.bo[state.buffer].filetype
= 'bugabinga_cmdline' 41 -local function get_win(buf, width, fmt)
42 - if vim.api.nvim_win_is_valid(state.window)
then 45 - local screen_pos
= position.at_cursor(width
or vim.o.columns)
46 - state.window
= vim.api.nvim_open_win(buf,
false, {
47 - relative =
'editor',
48 - width = width
or vim.o.columns,
50 - row = screen_pos.row,
53 - border = vim.g.border_style,
57 - vim.wo[state.window].cursorline
= false 58 - vim.wo[state.window].winbar
= '' 59 - vim.wo[state.window].foldenable
= false 60 - vim.wo[state.window].winhighlight
= format.winhighlight(fmt.type,
'NormalFloat')
29 +local function get_win( buf, width, fmt )
30 + if vim.api.nvim_win_is_valid( state.window )
then 33 + local screen_pos
= position.at_cursor( width
or vim.o.columns )
34 + state.window
= vim.api.nvim_open_win( buf,
false, {
35 + relative =
'editor',
36 + width = width
or vim.o.columns,
38 + row = screen_pos.row,
41 + border = vim.g.border_style,
45 + vim.wo[state.window].cursorline
= false 46 + vim.wo[state.window].winbar
= '' 47 + vim.wo[state.window].foldenable
= false 48 + vim.wo[state.window].winhighlight
= format.winhighlight( fmt.type,
'NormalFloat' )
66 -local function apply_icon(buf, fmt)
67 - vim.api.nvim_buf_clear_namespace(buf, ns,
0,
-1)
68 - local icon_hl
= format.icon_highlight(fmt.type)
69 - vim.api.nvim_buf_set_extmark(buf, ns,
0,
0, {
70 - virt_text = { { fmt.icon, icon_hl } },
71 - virt_text_pos =
'overlay',
52 +local function apply_icon( buf, fmt )
53 + vim.api.nvim_buf_clear_namespace( buf, ns,
0,
-1 )
54 + local icon_hl
= format.icon_highlight( fmt.type )
55 + vim.api.nvim_buf_set_extmark( buf, ns,
0,
0, {
56 + virt_text = { { fmt.icon, icon_hl } },
57 + virt_text_pos =
'overlay',
75 -function M.
show(cmdline_state)
76 - local text
= cmdline_state:text()
77 - local firstc
= cmdline_state.firstc
78 - local prompt
= cmdline_state.prompt
80 - state.firstc
= firstc
81 - state.prompt
= prompt
82 - local fmt
= format.detect(firstc, text, prompt)
83 - local fmt_changed
= not state.current_fmt
or fmt.type
~= state.current_fmt.type
84 - state.current_fmt
= fmt
85 - local display_text
= ' ' .. text
.. ' ' 86 - local width
= vim.o.columns
87 - local buf
= get_buf()
88 - local win
= get_win(buf, width, fmt)
89 - local is_new
= not position.get_cmdline_anchor()
91 - local screen_pos
= position.at_cursor(width)
92 - vim.api.nvim_win_set_config(win, {
93 - relative =
'editor',
96 - row = screen_pos.row,
100 - screenpos = {
row = screen_pos.row,
col =
0 },
104 - bufpos = {
row =
0,
col =
0 },
106 - position.set_cmdline_anchor(anchor)
107 - vim.g.ui_cmdline_pos
= { anchor.screenpos.row, anchor.screenpos.col }
109 - if fmt_changed
then 110 - vim.wo[win].winhighlight
= format.winhighlight(fmt.type,
'NormalFloat')
112 - vim.api.nvim_buf_set_lines(buf,
0,
-1,
false, { display_text })
113 - apply_icon(buf, fmt)
114 - highlight.apply(buf, hl_ns, text, fmt.lang,
0,
2)
61 +local function show( cmdline_state )
62 + local text
= cmdline_state:text()
63 + local firstc
= cmdline_state.firstc
64 + local prompt
= cmdline_state.prompt
66 + state.firstc
= firstc
67 + state.prompt
= prompt
68 + local fmt
= format.detect( firstc, text, prompt )
69 + local fmt_changed
= not state.current_fmt
or fmt.type
~= state.current_fmt.type
70 + state.current_fmt
= fmt
71 + local display_text
= ' ' .. text
.. ' ' 72 + local width
= vim.o.columns
73 + local buf
= get_buf()
74 + local win
= get_win( buf, width, fmt )
75 + local is_new
= not position.get_cmdline_anchor()
77 + local screen_pos
= position.at_cursor( width )
78 + vim.api.nvim_win_set_config( win, {
79 + relative =
'editor',
82 + row = screen_pos.row,
86 + screenpos = {
row = screen_pos.row,
col =
0 },
90 + bufpos = {
row =
0,
col =
0 },
92 + position.set_cmdline_anchor( anchor )
93 + vim.g.ui_cmdline_pos
= { anchor.screenpos.row, anchor.screenpos.col }
96 + vim.wo[win].winhighlight
= format.winhighlight( fmt.type,
'NormalFloat' )
98 + vim.api.nvim_buf_set_lines( buf,
0,
-1,
false, { display_text } )
99 + apply_icon( buf, fmt )
100 + highlight.apply( buf, hl_ns, text, fmt.lang,
0,
2 )
116 - if fmt.type
== 'input' and prompt
and prompt
~= '' then 117 - vim.api.nvim_buf_clear_namespace(buf, prompt_ns,
0,
-1)
118 - local max_prompt_width
= math.
floor(vim.o.columns
* format.PROMPT_MAX_RATIO)
119 - local prompt_is_single_line
= not prompt:match
'\n' 120 - local prompt_fits
= vim.fn.strdisplaywidth(prompt)
<= max_prompt_width
121 - if prompt_is_single_line
and prompt_fits
then 122 - dbg(
'Path A', {
prompt = prompt,
prompt_fits = prompt_fits,
max_prompt_width = max_prompt_width })
123 - local title_text
= ' ' .. prompt
.. ' ' 124 - vim.api.nvim_win_set_config(win, {
125 - border = vim.g.border_style,
126 - title = { { title_text,
'CmdlineBorderInput' } },
127 - title_pos =
'left',
130 - local prompt_chunks
= format.prompt_lines(prompt, max_prompt_width)
131 - dbg(
'Path B debug', {
133 - prompt_chunks = prompt_chunks,
134 - max_prompt_width = max_prompt_width,
135 - prompt_width = vim.fn.strdisplaywidth(prompt),
138 - local buf_lines
= {}
139 - for _, chunk
in ipairs(prompt_chunks)
do 140 - table.
insert(buf_lines,
' ' .. chunk)
142 - table.
insert(buf_lines,
' ' .. display_text)
143 - vim.api.nvim_buf_set_lines(buf,
0,
-1,
false, buf_lines)
144 - dbg(
'buf_lines set', buf_lines)
145 - vim.api.nvim_win_set_config(win, {
146 - border = vim.g.border_style,
147 - title = { {
' ',
'Normal' } },
148 - title_pos =
'left',
149 - height =
#buf_lines,
151 - local cfg
= vim.api.nvim_win_get_config(win)
152 - dbg(
'Path B window config', {
153 - height = cfg.height,
155 - buf_line_count = vim.api.nvim_buf_line_count(buf),
102 + if fmt.type
== 'input' and prompt
and prompt
~= '' then 103 + vim.api.nvim_buf_clear_namespace( buf, prompt_ns,
0,
-1 )
104 + local max_prompt_width
= math.
floor( vim.o.columns
* format.PROMPT_MAX_RATIO )
105 + local prompt_is_single_line
= not prompt:match
'\n' 106 + local prompt_fits
= vim.fn.strdisplaywidth( prompt )
<= max_prompt_width
107 + if prompt_is_single_line
and prompt_fits
then 108 + local title_text
= ' ' .. prompt
.. ' ' 109 + vim.api.nvim_win_set_config( win, {
110 + border = vim.g.border_style,
111 + title = { { title_text,
'CmdlineBorderInput' } },
112 + title_pos =
'left',
115 + local prompt_chunks
= format.prompt_lines( prompt, max_prompt_width )
116 + local virt_line_tuples
= {}
117 + for _, chunk
in ipairs( prompt_chunks )
do 118 + vim.notify(
'adding prompt chunk', chunk )
119 + table.
insert( virt_line_tuples, { { chunk,
'NormalFloat' } } )
121 + vim.api.nvim_buf_set_extmark( buf, prompt_ns,
0,
0, {
122 + virt_lines = virt_line_tuples,
123 + virt_lines_above =
true,
125 + vim.api.nvim_win_set_config( win, {
126 + border = vim.g.border_style,
127 + title = { {
' ',
'Normal' } },
128 + title_pos =
'left',
129 + height =
#prompt_chunks
+ 1,
160 - local cursor_byte
= cmdline_state:cursor_byte()
161 - local cursor_col
= 2 + cursor_byte
162 - local anchor
= position.get_cmdline_anchor()
164 - anchor.cursor
= cursor_col
165 - anchor.bufpos.col
= cursor_col
166 - position.set_cmdline_anchor(anchor)
168 - cursor.fix(win, cursor_col)
134 + local cursor_byte
= cmdline_state:cursor_byte()
135 + local cursor_col
= 2 + cursor_byte
136 + local anchor
= position.get_cmdline_anchor()
138 + anchor.cursor
= cursor_col
139 + anchor.bufpos.col
= cursor_col
140 + position.set_cmdline_anchor( anchor )
142 + cursor.fix( win, cursor_col )
172 - if vim.api.nvim_win_is_valid(state.window)
then 173 - vim.api.nvim_buf_clear_namespace(state.buffer, prompt_ns,
0,
-1)
174 - vim.api.nvim_win_hide(state.window)
177 - state.current_fmt
= nil 181 - position.set_cmdline_anchor(
nil)
182 - vim.g.ui_cmdline_pos
= nil 145 +local function hide()
146 + if vim.api.nvim_win_is_valid( state.window )
then 147 + vim.api.nvim_buf_clear_namespace( state.buffer, prompt_ns,
0,
-1 )
148 + vim.api.nvim_win_hide( state.window )
151 + state.current_fmt
= nil 155 + position.set_cmdline_anchor(
nil )
156 + vim.g.ui_cmdline_pos
= nil 185 -function M.
update_cursor(cmdline_state)
186 - if not vim.api.nvim_win_is_valid(state.window)
then 189 - local cursor_byte
= cmdline_state:cursor_byte()
190 - local cursor_col
= 2 + cursor_byte
191 - local anchor
= position.get_cmdline_anchor()
193 - anchor.cursor
= cursor_col
194 - position.set_cmdline_anchor(anchor)
196 - cursor.fix(state.window, cursor_col)
159 +local function update_cursor( cmdline_state )
160 + if not vim.api.nvim_win_is_valid( state.window )
then 163 + local cursor_byte
= cmdline_state:cursor_byte()
164 + local cursor_col
= 2 + cursor_byte
165 + local anchor
= position.get_cmdline_anchor()
167 + anchor.cursor
= cursor_col
168 + position.set_cmdline_anchor( anchor )
170 + cursor.fix( state.window, cursor_col )
199 -function M.
get_format_type()
200 - return state.current_fmt
and state.current_fmt.type
or 'cmdline' 173 +local function get_format_type()
174 + return state.current_fmt
and state.current_fmt.type
or 'cmdline' 203 -function M.
get_text()
177 +local function get_text()
184 + update_cursor = update_cursor,
185 + get_format_type = get_format_type,
186 + get_text = get_text,