Skip to content

Commit c16f6a3

Browse files
committed
refactor: improve comments
1 parent 7614787 commit c16f6a3

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

lua/csvview/jump.lua

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ local function clamp(value, min, max)
1212
return math.min(math.max(value, min), max)
1313
end
1414

15-
--- Checks if a row is skippable for `opts.col_wrap`.
15+
--- Returns whether the row at the specified index has a field.
1616
---@param metrics CsvView.Metrics
1717
---@param row_idx integer
1818
---@return boolean
19-
local function is_skippable_row(metrics, row_idx)
19+
local function has_field(metrics, row_idx)
2020
local row = metrics.rows[row_idx]
21-
if row.is_comment or #row.fields == 0 then
22-
return true
23-
end
24-
25-
return false
21+
return not row.is_comment and #row.fields > 0
2622
end
2723

2824
--- Wraps around columns when moving beyond the last column
@@ -35,15 +31,16 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
3531
local rest = math.abs(relative_col)
3632
local direction = (relative_col > 0) and 1 or -1
3733

38-
-- While we still have columns to move (rest)...
34+
-- Move horizontally by the amount of relative_col
35+
-- When moving to the right, if the end of the line is reached, move to the first column of the next line
36+
-- When moving to the left, if the start of the line is reached, move to the last column of the previous line
3937
while rest > 0 do
4038
local row = metrics.rows[row_idx]
4139
if not row then
42-
-- We've gone beyond valid rows, so break out
4340
break
4441
end
4542

46-
-- Check if we're trying to move before the first column
43+
-- When moving to the left and trying to move before the first column
4744
if col_idx + direction < 1 then
4845
if row_idx == 1 then
4946
-- Already at the first row, clamp to first col
@@ -55,14 +52,14 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
5552
-- Move to the previous row
5653
row_idx = row_idx - 1
5754

58-
-- If the previous row is non-empty, move to the last field
59-
-- Otherwise, you might want to decrement rest or skip further empty rows
60-
if not is_skippable_row(metrics, row_idx) then
55+
-- If there is at least one field in the previous row, move to the last column of that row
56+
-- If there are no fields, do not decrease the number of moves, and check the previous row again in the next loop.
57+
if has_field(metrics, row_idx) then
6158
col_idx = #metrics.rows[row_idx].fields
6259
rest = rest - 1
6360
end
6461

65-
-- Check if we're trying to move beyond the last column
62+
-- When moving to the right and trying to move beyond the last column
6663
elseif col_idx + direction > #row.fields then
6764
if row_idx == #metrics.rows then
6865
-- Already at the last row, clamp to last col
@@ -74,9 +71,9 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
7471
-- Move to the next row
7572
row_idx = row_idx + 1
7673

77-
-- If the next row is non-empty, move to the first field
78-
-- Otherwise, you might want to decrement rest or skip further empty rows
79-
if not is_skippable_row(metrics, row_idx) then
74+
-- If there is at least one field in the next row, move to the first column of that row
75+
-- If there are no fields, do not decrease the number of moves, and check the next row again in the next loop.
76+
if has_field(metrics, row_idx) then
8077
col_idx = 1
8178
rest = rest - 1
8279
end
@@ -87,8 +84,12 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
8784
end
8885
end
8986

90-
-- If we land exactly on an empty field at the end of a line,
91-
-- we might need to move to the next row. This recursion is a neat trick but watch for infinite loops!
87+
--
88+
-- Additional processing when the destination is the last column of the row and is empty data
89+
--
90+
-- If the destination is empty data, generally move to the delimiter behind it,
91+
-- but if the end of the line is empty, you cannot jump because there is no delimiter behind it. Move one more column.
92+
--
9293
if col_idx ~= 0 and metrics:is_last_col(row_idx, col_idx) and metrics:is_empty_field(row_idx, col_idx) then
9394
row_idx, col_idx = wrap_column(metrics, row_idx, col_idx, direction)
9495
end

lua/csvview/metrics.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,14 @@ end
333333
---@param start integer
334334
---@param num integer
335335
function CsvViewMetrics:_add_row_placeholders(start, num)
336-
-- `table.insert` is inefficient when editing large buffers
336+
--
337+
-- This function is equivalent to the following code, but is more efficient when editing large buffers.
338+
--
339+
-- for i = 1, num do
340+
-- table.insert( self.rows, start, <placeholder> )
341+
-- end
342+
--
343+
337344
local len = #self.rows
338345
for i = len, start, -1 do
339346
self.rows[i + num] = self.rows[i]
@@ -347,7 +354,14 @@ end
347354
---@param start integer
348355
---@param num integer
349356
function CsvViewMetrics:_remove_rows(start, num)
350-
-- `table.remove` is inefficient when editing large buffers
357+
--
358+
-- This function is equivalent to the following code, but is more efficient when editing large buffers.
359+
--
360+
-- for i = 1, num do
361+
-- table.remove( self.rows, start )
362+
-- end
363+
--
364+
351365
local len = #self.rows
352366
for i = start, len do
353367
self.rows[i] = self.rows[i + num]

lua/csvview/view.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function View:render_column_index_header(lnum)
9999
local virt = {} --- @type string[][]
100100
for i, column in ipairs(self.metrics.columns) do
101101
local char = tostring(i)
102-
virt[#virt + 1] = { string.rep(" ", column.max_width - #char) }
102+
virt[#virt + 1] = { string.rep(" ", column.max_width - #char + self.opts.view.spacing) }
103103
virt[#virt + 1] = { char }
104104
if i < #self.metrics.columns then
105105
virt[#virt + 1] = { "," }
@@ -202,6 +202,7 @@ end
202202
---@param bot_lnum integer 1-indexed
203203
---@param winid integer window id
204204
function View:render(top_lnum, bot_lnum, winid)
205+
-- https://github.com/neovim/neovim/issues/16166
205206
-- self:render_column_index_header(top_lnum)
206207

207208
--- render all fields in ranges

0 commit comments

Comments
 (0)