@@ -12,17 +12,13 @@ local function clamp(value, min, max)
12
12
return math.min (math.max (value , min ), max )
13
13
end
14
14
15
- --- Checks if a row is skippable for `opts.col_wrap` .
15
+ --- Returns whether the row at the specified index has a field .
16
16
--- @param metrics CsvView.Metrics
17
17
--- @param row_idx integer
18
18
--- @return boolean
19
- local function is_skippable_row (metrics , row_idx )
19
+ local function has_field (metrics , row_idx )
20
20
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
26
22
end
27
23
28
24
--- Wraps around columns when moving beyond the last column
@@ -35,15 +31,16 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
35
31
local rest = math.abs (relative_col )
36
32
local direction = (relative_col > 0 ) and 1 or - 1
37
33
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
39
37
while rest > 0 do
40
38
local row = metrics .rows [row_idx ]
41
39
if not row then
42
- -- We've gone beyond valid rows, so break out
43
40
break
44
41
end
45
42
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
47
44
if col_idx + direction < 1 then
48
45
if row_idx == 1 then
49
46
-- Already at the first row, clamp to first col
@@ -55,14 +52,14 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
55
52
-- Move to the previous row
56
53
row_idx = row_idx - 1
57
54
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
61
58
col_idx = # metrics .rows [row_idx ].fields
62
59
rest = rest - 1
63
60
end
64
61
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
66
63
elseif col_idx + direction > # row .fields then
67
64
if row_idx == # metrics .rows then
68
65
-- Already at the last row, clamp to last col
@@ -74,9 +71,9 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
74
71
-- Move to the next row
75
72
row_idx = row_idx + 1
76
73
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
80
77
col_idx = 1
81
78
rest = rest - 1
82
79
end
@@ -87,8 +84,12 @@ local function wrap_column(metrics, row_idx, col_idx, relative_col)
87
84
end
88
85
end
89
86
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
+ --
92
93
if col_idx ~= 0 and metrics :is_last_col (row_idx , col_idx ) and metrics :is_empty_field (row_idx , col_idx ) then
93
94
row_idx , col_idx = wrap_column (metrics , row_idx , col_idx , direction )
94
95
end
0 commit comments