Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty line indentation when using tabs #228

Merged
merged 1 commit into from Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions lua/Comment/opfunc.lua
Expand Up @@ -140,8 +140,7 @@ function Op.linewise(param)
---When commenting multiple line, it is to be expected that indentation should be preserved
---So, When looping over multiple lines we need to store the indentation of the mininum length (except empty line)
---Which will be used to semantically comment rest of the lines
---@type integer
local min_indent = -1
local min_indent, tabbed = -1, false

-- If the given cmode is uncomment then we actually don't want to compute the cmode or min_indent
if param.cmode ~= U.cmode.uncomment then
Expand All @@ -155,9 +154,9 @@ function Op.linewise(param)
-- If local `cmode` == comment or the given cmode ~= uncomment, then only calculate min_indent
-- As calculating min_indent only makes sense when we actually want to comment the lines
if not U.is_empty(line) and (cmode == U.cmode.comment or param.cmode == U.cmode.comment) then
local len = U.indent_len(line)
local _, len = string.find(line, '^%s*')
if min_indent == -1 or min_indent > len then
min_indent = len
min_indent, tabbed = len, string.find(line, '^\t') ~= nil
end
end
end
Expand All @@ -177,7 +176,7 @@ function Op.linewise(param)
end
end
else
local comment = U.commenter(param.lcs, param.rcs, padding, min_indent)
local comment = U.commenter(param.lcs, param.rcs, padding, min_indent, nil, tabbed)
for i, line in ipairs(param.lines) do
if not U.ignore(line, pattern) then
param.lines[i] = comment(line)
Expand Down
14 changes: 3 additions & 11 deletions lua/Comment/utils.lua
Expand Up @@ -65,15 +65,6 @@ function U.is_empty(iter)
return #iter == 0
end

---@private
---Get the length of the indentation
---@param str string
---@return integer integer Length of indent chars
function U.indent_len(str)
local _, len = string.find(str, '^%s*')
return len
end

---@private
---Helper to get padding character
---@param flag boolean
Expand Down Expand Up @@ -193,12 +184,13 @@ end
---@param padding boolean Is padding enabled?
---@param scol? integer Starting column
---@param ecol? integer Ending column
---@param tabbed? boolean Using tab indentation
---@return fun(line:string|string[]):string
function U.commenter(left, right, padding, scol, ecol)
function U.commenter(left, right, padding, scol, ecol, tabbed)
local pad = U.get_pad(padding)
local ll = U.is_empty(left) and left or (left .. pad)
local rr = U.is_empty(right) and right or (pad .. right)
local empty = string.rep(' ', scol or 0) .. left .. right
local empty = string.rep(tabbed and '\t' or ' ', scol or 0) .. left .. right
local is_lw = scol and not ecol

return function(line)
Expand Down