Skip to content

Commit ecd5bf4

Browse files
committed
wat
1 parent 6b43602 commit ecd5bf4

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

lua/treewalker/lines.lua

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,14 @@ end
5050
---@param line string
5151
---@return integer
5252
function M.get_start_col(line)
53-
local tabwidth = vim.opt.tabstop:get()
54-
local count = 1 -- 1 indexed
55-
for i = 1, #line do
56-
local c = line:sub(i, i)
57-
if c == "\t" then
58-
count = math.floor((count + tabwidth) / tabwidth) * tabwidth
59-
elseif c == " " then
60-
count = count + 1
61-
else
62-
break
63-
end
53+
if not line or line == "" then
54+
return 1
6455
end
65-
return count
56+
local first = line:find("%S")
57+
if not first then
58+
return 1
59+
end
60+
return first
6661
end
6762

6863
return M

tests/treewalker/lines_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,15 @@ describe("lines", function()
7474
local indent = lines.get_start_col(" local line = lines.get_indent()")
7575
assert.equals(3, indent)
7676
end)
77+
78+
it("treats tabs as single characters", function()
79+
local indent = lines.get_start_col("\tlocal line = lines.get_indent()")
80+
assert.equals(2, indent)
81+
end)
82+
83+
it("falls back to the first column when the line is blank", function()
84+
local indent = lines.get_start_col(" ")
85+
assert.equals(1, indent)
86+
end)
7787
end)
7888
end)

0 commit comments

Comments
 (0)