Skip to content

Commit 85119fe

Browse files
authored
fix(insert_eol): don't add space in front of comment on empty lines (#131)
also set the correct indentation with on empty lines
1 parent 8a2b2f3 commit 85119fe

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lua/Comment/extra.lua

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,27 @@ function E.insert_eol(ctype, cfg)
6666
local line = A.nvim_get_current_line()
6767
local padding = U.get_padding(cfg.padding)
6868

69-
-- NOTE:
70-
-- 1. Python is the only language that recommends 2 spaces between the statement and the comment
71-
-- 2. Other than that, I am assuming that the users wants a space b/w the end of line and start of the comment
72-
local space = vim.bo.filetype == 'python' and ' ' or ' '
73-
local ll = line .. space .. lcs .. padding
74-
7569
-- We need RHS of cstr, if we are doing block comments or if RHS exists
7670
-- because even in line comment RHS do exists for some filetypes like jsx_element, ocaml
7771
local if_rcs = (ctype == U.ctype.block or rcs) and padding .. rcs or ''
7872

79-
local erow, ecol = srow - 1, #ll - 1
80-
A.nvim_buf_set_lines(0, erow, srow, false, { ll .. if_rcs })
73+
local ecol
74+
if line:match('^$') then
75+
-- If line is empty, start comment at the correct indentation level
76+
A.nvim_buf_set_lines(0, srow - 1, srow, false, { lcs .. padding .. if_rcs})
77+
vim.cmd('normal! ==')
78+
ecol = A.nvim_get_current_line():len() - if_rcs:len() - 1
79+
else
80+
-- NOTE:
81+
-- 1. Python is the only language that recommends 2 spaces between the statement and the comment
82+
-- 2. Other than that, I am assuming that the users wants a space b/w the end of line and start of the comment
83+
local space = vim.bo.filetype == 'python' and ' ' or ' '
84+
local ll = line .. space .. lcs .. padding
85+
86+
local erow = srow - 1
87+
ecol = #ll - 1
88+
A.nvim_buf_set_lines(0, erow, srow, false, { ll .. if_rcs })
89+
end
8190
U.move_n_insert(srow, ecol)
8291
U.is_fn(cfg.post_hook, ctx)
8392
end

0 commit comments

Comments
 (0)