fix(comment): fall back to using trimmed comment markers (#28950)

fix(comment): fall back to using trimmed comment markers (#28938)

Problem: Currently comment detection, addition, and removal are done
  by matching 'commentstring' exactly. This has the downside when users
  want to add comment markers with space (like with `-- %s`
  commentstring) but also be able to uncomment lines that do not contain
  space (like `--aaa`).

Solution: Use the following approach:
  - Line is commented if it matches 'commentstring' with trimmed parts.
  - Adding comment is 100% relying on 'commentstring' parts (as is now).
  - Removing comment is first trying exact 'commentstring' parts with
    fallback on trying its trimmed parts.
(cherry picked from commit 0a2218f965)

Co-authored-by: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
This commit is contained in:
github-actions[bot]
2024-05-23 16:02:13 -05:00
committed by GitHub
parent bdd5871dc5
commit 21b21b94e6
3 changed files with 33 additions and 25 deletions

View File

@@ -301,27 +301,34 @@ describe('commenting', function()
eq(get_lines(), { 'aa', '', ' ', '\t', 'aa' })
end)
it('matches comment parts strictly when detecting comment/uncomment', function()
it('correctly matches comment parts during checking and uncommenting', function()
local validate = function(from, to, ref_lines)
set_lines({ '#aa', '# aa', '# aa' })
set_lines({ '/*aa*/', '/* aa */', '/* aa */' })
toggle_lines(from, to)
eq(get_lines(), ref_lines)
end
set_commentstring('#%s')
validate(1, 3, { 'aa', ' aa', ' aa' })
validate(2, 3, { '#aa', ' aa', ' aa' })
validate(3, 3, { '#aa', '# aa', ' aa' })
-- Should first try to match 'commentstring' parts exactly with their
-- whitespace, with fallback on trimmed parts
set_commentstring('/*%s*/')
validate(1, 3, { 'aa', ' aa ', ' aa ' })
validate(2, 3, { '/*aa*/', ' aa ', ' aa ' })
validate(3, 3, { '/*aa*/', '/* aa */', ' aa ' })
set_commentstring('# %s')
validate(1, 3, { '# #aa', '# # aa', '# # aa' })
validate(2, 3, { '#aa', 'aa', ' aa' })
validate(3, 3, { '#aa', '# aa', ' aa' })
set_commentstring('/* %s */')
validate(1, 3, { 'aa', 'aa', ' aa ' })
validate(2, 3, { '/*aa*/', 'aa', ' aa ' })
validate(3, 3, { '/*aa*/', '/* aa */', ' aa ' })
set_commentstring('# %s')
validate(1, 3, { '# #aa', '# # aa', '# # aa' })
validate(2, 3, { '#aa', '# # aa', '# # aa' })
validate(3, 3, { '#aa', '# aa', 'aa' })
set_commentstring('/* %s */')
validate(1, 3, { 'aa', ' aa ', 'aa' })
validate(2, 3, { '/*aa*/', ' aa ', 'aa' })
validate(3, 3, { '/*aa*/', '/* aa */', 'aa' })
set_commentstring(' /*%s*/ ')
validate(1, 3, { 'aa', ' aa ', ' aa ' })
validate(2, 3, { '/*aa*/', ' aa ', ' aa ' })
validate(3, 3, { '/*aa*/', '/* aa */', ' aa ' })
end)
it('uncomments on inconsistent indent levels', function()