Skip to content

Commit

Permalink
fix:fix #107, for not handle '\t' properly when get specfied pos char
Browse files Browse the repository at this point in the history
  • Loading branch information
shellRaining committed Jun 10, 2024
1 parent 1754bb9 commit 2b600d9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lua/hlchunk/mods/chunk/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function ChunkMod:get_chunk_data(range, virt_text_list, row_list, virt_text_win_
chars = utf8Split(mid)
-- when use click `<<` or `>>` to indent, we should make sure the line would not encounter the indent char
for i = 1, mid_char_nums do
local char = Pos.get_char_at_pos(Pos(range.bufnr, range.start + i, start_col))
local char = Pos.get_char_at_pos(Pos(range.bufnr, range.start + i, start_col), self.meta.shiftwidth)
if not char:match("%s") and #char ~= 0 then
chars[i] = ""
end
Expand Down
8 changes: 4 additions & 4 deletions lua/hlchunk/utils/cFunc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local M = {}
---@return number
function M.get_indent(bufnr, lnum)
local line_cnt = vim.api.nvim_buf_line_count(bufnr)
if lnum >= line_cnt then
if lnum >= line_cnt or lnum <= 0 then
return -1
end
local handler = C.find_buffer_by_handle(bufnr, ffi.new("Error"))
Expand All @@ -40,19 +40,19 @@ end
---@return string
function M.get_line(bufnr, lnum)
local line_cnt = vim.api.nvim_buf_line_count(bufnr)
if lnum >= line_cnt then
if lnum >= line_cnt or lnum <= 0 then
return ""
end
local handler = C.find_buffer_by_handle(bufnr, ffi.new("Error"))
return C.ml_get_buf(handler, lnum + 1)
return ffi.string(C.ml_get_buf(handler, lnum + 1))
end

---@param bufnr number
---@param lnum number 0-index
---@return number
function M.get_line_len(bufnr, lnum)
local line_cnt = vim.api.nvim_buf_line_count(bufnr)
if lnum >= line_cnt then
if lnum >= line_cnt or lnum <= 0 then
return 0
end
local handler = C.find_buffer_by_handle(bufnr, ffi.new("Error"))
Expand Down
14 changes: 9 additions & 5 deletions lua/hlchunk/utils/position.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local class = require("hlchunk.utils.class")
local cFunc = require("hlchunk.utils.cFunc")

---@class Pos
---@field bufnr number
Expand All @@ -14,12 +15,15 @@ local Pos = class(function(self, bufnr, row, col)
end)

---@param pos Pos
---@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
---@return string
function Pos.get_char_at_pos(pos)
local row = pos.row
local col = pos.col
local char = vim.api.nvim_buf_get_text(pos.bufnr, row, col, row, col + 1, {})[1]
return char
function Pos.get_char_at_pos(pos, expand_tab_width)
local line = cFunc.get_line(pos.bufnr, pos.row)
if expand_tab_width then
local expanded_tab = string.rep(" ", expand_tab_width)
line = line:gsub("\t", expanded_tab)
end
return line:sub(pos.col + 1, pos.col + 1)
end

return Pos

0 comments on commit 2b600d9

Please sign in to comment.