Skip to content

Commit 2b600d9

Browse files
committed
fix:fix #107, for not handle '\t' properly when get specfied pos char
1 parent 1754bb9 commit 2b600d9

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

lua/hlchunk/mods/chunk/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function ChunkMod:get_chunk_data(range, virt_text_list, row_list, virt_text_win_
9898
chars = utf8Split(mid)
9999
-- when use click `<<` or `>>` to indent, we should make sure the line would not encounter the indent char
100100
for i = 1, mid_char_nums do
101-
local char = Pos.get_char_at_pos(Pos(range.bufnr, range.start + i, start_col))
101+
local char = Pos.get_char_at_pos(Pos(range.bufnr, range.start + i, start_col), self.meta.shiftwidth)
102102
if not char:match("%s") and #char ~= 0 then
103103
chars[i] = ""
104104
end

lua/hlchunk/utils/cFunc.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local M = {}
2323
---@return number
2424
function M.get_indent(bufnr, lnum)
2525
local line_cnt = vim.api.nvim_buf_line_count(bufnr)
26-
if lnum >= line_cnt then
26+
if lnum >= line_cnt or lnum <= 0 then
2727
return -1
2828
end
2929
local handler = C.find_buffer_by_handle(bufnr, ffi.new("Error"))
@@ -40,19 +40,19 @@ end
4040
---@return string
4141
function M.get_line(bufnr, lnum)
4242
local line_cnt = vim.api.nvim_buf_line_count(bufnr)
43-
if lnum >= line_cnt then
43+
if lnum >= line_cnt or lnum <= 0 then
4444
return ""
4545
end
4646
local handler = C.find_buffer_by_handle(bufnr, ffi.new("Error"))
47-
return C.ml_get_buf(handler, lnum + 1)
47+
return ffi.string(C.ml_get_buf(handler, lnum + 1))
4848
end
4949

5050
---@param bufnr number
5151
---@param lnum number 0-index
5252
---@return number
5353
function M.get_line_len(bufnr, lnum)
5454
local line_cnt = vim.api.nvim_buf_line_count(bufnr)
55-
if lnum >= line_cnt then
55+
if lnum >= line_cnt or lnum <= 0 then
5656
return 0
5757
end
5858
local handler = C.find_buffer_by_handle(bufnr, ffi.new("Error"))

lua/hlchunk/utils/position.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local class = require("hlchunk.utils.class")
2+
local cFunc = require("hlchunk.utils.cFunc")
23

34
---@class Pos
45
---@field bufnr number
@@ -14,12 +15,15 @@ local Pos = class(function(self, bufnr, row, col)
1415
end)
1516

1617
---@param pos Pos
18+
---@param expand_tab_width? number when the field is given, the tab will be expand to blank with the width, like "\t\t" -> " " when the width is 2
1719
---@return string
18-
function Pos.get_char_at_pos(pos)
19-
local row = pos.row
20-
local col = pos.col
21-
local char = vim.api.nvim_buf_get_text(pos.bufnr, row, col, row, col + 1, {})[1]
22-
return char
20+
function Pos.get_char_at_pos(pos, expand_tab_width)
21+
local line = cFunc.get_line(pos.bufnr, pos.row)
22+
if expand_tab_width then
23+
local expanded_tab = string.rep(" ", expand_tab_width)
24+
line = line:gsub("\t", expanded_tab)
25+
end
26+
return line:sub(pos.col + 1, pos.col + 1)
2327
end
2428

2529
return Pos

0 commit comments

Comments
 (0)