Skip to content

Commit 48062d8

Browse files
committed
fix: #16
1 parent 7ea93eb commit 48062d8

File tree

2 files changed

+96
-77
lines changed

2 files changed

+96
-77
lines changed

lua/convert/init.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ M.find_next = function()
2121

2222
for row = next_row, #lines, 1 do
2323
local line = lines[row]
24-
local found_unit = utils.find_unit_in_line(line, row)
24+
local found_units = utils.find_all_units_in_line(line, row)
2525

2626

27-
if found_unit ~= nil then
28-
vim.api.nvim_win_set_cursor(current_win, { row, found_unit.pos.start_col - 1 })
29-
ui.open_win(found_unit)
27+
if found_units ~= nil then
28+
vim.api.nvim_win_set_cursor(current_win, { row, found_units[1].pos.start_col - 1 })
29+
ui.open_win(found_units)
3030
return
3131
end
3232
end
@@ -42,11 +42,11 @@ M.find_current = function()
4242

4343
line = string.rep(" ", #current_line - #line) .. line
4444

45-
local found_unit = utils.find_unit_in_line(line, cursor_pos.row)
45+
local found_units = utils.find_all_units_in_line(line, cursor_pos.row)
4646

47-
if found_unit ~= nil then
48-
vim.api.nvim_win_set_cursor(current_win, { cursor_pos.row, found_unit.pos.start_col - 1 })
49-
ui.open_win(found_unit)
47+
if found_units ~= nil and #found_units > 0 then
48+
vim.api.nvim_win_set_cursor(current_win, { cursor_pos.row, found_units[1].pos.start_col - 1 })
49+
ui.open_win(found_units)
5050
end
5151
end
5252

lua/convert/ui/open_popup.lua

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,113 @@ local utils = require("convert.utils")
44
local config = require("convert.config")
55

66
local size_units = {
7-
'px',
8-
'rem',
9-
'cm',
10-
'in',
11-
'mm',
12-
'pt',
13-
'pc'
7+
'px',
8+
'rem',
9+
'cm',
10+
'in',
11+
'mm',
12+
'pt',
13+
'pc'
1414
}
1515

1616
local color_units = {
17-
'rgb',
18-
'hex',
19-
'hsl'
17+
'rgb',
18+
'hex',
19+
'hsl'
2020
}
2121

2222
local color_menu = {
23-
Menu.item('rgb'),
24-
Menu.item('hex'),
25-
Menu.item('hsl'),
23+
Menu.item('rgb'),
24+
Menu.item('hex'),
25+
Menu.item('hsl'),
2626
}
2727

2828
local size_menu = {
29-
Menu.item('px'),
30-
Menu.item('rem'),
31-
Menu.item('cm'),
32-
Menu.item('in'),
33-
Menu.item('mm'),
34-
Menu.item('pt'),
35-
Menu.item('pc'),
29+
Menu.item('px'),
30+
Menu.item('rem'),
31+
Menu.item('cm'),
32+
Menu.item('in'),
33+
Menu.item('mm'),
34+
Menu.item('pt'),
35+
Menu.item('pc'),
3636
}
3737

3838
local M = {}
3939

40+
--- Opens popup window for convert in a single line
41+
---@param found_units matched[]
42+
M.open_win = function(found_units)
43+
if not found_units or #found_units == 0 then return end
4044

41-
---@param found_unit matched
42-
M.open_win = function(found_unit)
43-
local lines = nil
45+
local from_unit = found_units[1].unit
46+
local from_val = found_units[1].val
4447

45-
if utils.contains(color_units, found_unit.unit) then
46-
lines = color_menu
47-
end
48+
local lines = nil
4849

49-
if utils.contains(size_units, found_unit.unit) then
50-
lines = size_menu
51-
end
50+
if utils.contains(color_units, from_unit) then
51+
lines = color_menu
52+
elseif utils.contains(size_units, from_unit) then
53+
lines = size_menu
54+
else
55+
return
56+
end
5257

53-
local popup_opts = {
54-
relative = "cursor",
55-
position = {
56-
row = 2,
57-
col = 1,
58-
},
59-
size = {
60-
width = 40,
61-
height = #lines,
62-
},
63-
border = {
64-
style = "rounded",
65-
text = {
66-
top = "[Convert " .. found_unit.val .. " To]",
67-
top_align = "center"
68-
},
69-
},
70-
buf_options = {
71-
modifiable = false,
72-
readonly = true,
73-
},
74-
win_options = {
75-
winhighlight = "Normal:Normal"
76-
}
77-
}
78-
local menu = Menu(popup_opts, {
79-
lines = lines,
58+
local popup_opts = {
59+
relative = "cursor",
60+
position = {
61+
row = 2,
62+
col = 1,
63+
},
64+
size = {
65+
width = 40,
66+
height = #lines,
67+
},
68+
border = {
69+
style = "rounded",
70+
text = {
71+
top = "[Convert " .. from_val .. " To]",
72+
top_align = "center"
73+
},
74+
},
75+
buf_options = {
76+
modifiable = false,
77+
readonly = true,
78+
},
79+
win_options = {
80+
winhighlight = "Normal:Normal"
81+
}
82+
}
83+
local menu = Menu(popup_opts, {
84+
lines = lines,
8085

81-
max_width = 100,
82-
keymap = config.keymaps,
83-
on_submit = function(item)
84-
local from_unit = found_unit.unit
85-
local to_unit = item.text
86-
local from_val = found_unit.val
87-
local converted = calculator.convert(from_unit, to_unit, from_val)
88-
vim.api.nvim_buf_set_text(0, found_unit.pos.row - 1, found_unit.pos.start_col - 1, found_unit.pos.row - 1, found_unit.pos.end_col,
89-
{ converted })
90-
vim.api.nvim_win_set_cursor(0, { found_unit.pos.row, found_unit.pos.end_col + #to_unit })
91-
end
92-
})
86+
max_width = 100,
87+
keymap = config.keymaps,
88+
on_submit = function(item)
89+
local to_unit = item.text
90+
local bufnr = 0
9391

94-
menu:mount()
92+
for i = #found_units, 1, -1 do
93+
local match = found_units[i]
94+
if match.unit == from_unit then
95+
local converted = calculator.convert(from_unit, to_unit, match.val)
96+
vim.api.nvim_buf_set_text(
97+
bufnr,
98+
match.pos.row - 1,
99+
match.pos.start_col - 1,
100+
match.pos.row - 1,
101+
match.pos.end_col,
102+
{ converted }
103+
)
104+
end
105+
end
106+
107+
-- Move cursor to end of last converted unit
108+
local last = found_units[#found_units]
109+
vim.api.nvim_win_set_cursor(0, { last.pos.row, last.pos.end_col + #to_unit })
110+
end
111+
})
112+
113+
menu:mount()
95114
end
96115

97116
return M

0 commit comments

Comments
 (0)