Commit d40a167c authored by Richard Waite's avatar Richard Waite
Browse files

Put comment at index of least indented line in selection

parent ff4b3a6f
......@@ -62,13 +62,10 @@ class CodeCommenter:
return True
def _comment_lines(self, lines):
# find least indented code in selection
istart = min(re.match(r"\s*", line).end() for line in lines) # use of * means will return 0 if no leading \s
for i in range(len(lines)):
str_match = re.match(r"\s+", lines[i])
if str_match:
i_non_space = str_match.end()
lines[i] = lines[i][:i_non_space] + '# ' + lines[i][i_non_space:]
else:
lines[i] = '# ' + lines[i]
lines[i] = lines[i][:istart] + '# ' + lines[i][istart:]
return lines
def _uncomment_lines(self, lines):
......
......@@ -90,15 +90,22 @@ class CodeCommenterTest(unittest.TestCase):
'# ' + line for line in expected_lines[start_line:end_line + 1]]
self.assertEqual(self.editor.text(), '\n'.join(expected_lines))
def test_multiline_comment_preserves_indenting(self):
def test_multiline_comment_uses_top_level_indentation(self):
start_line, end_line = 10, 13
self.editor.setSelection(start_line, 5, end_line, 5)
self.commenter.toggle_comment()
expected_lines = ["# for ii in range(2):",
" # do_something()",
"# do_something()",
"# do_something_else()"]
self.assertEqual(self.editor.text().split('\n')[start_line:end_line], expected_lines)
def test_comment_preserves_indenting_on_single_line(self):
iline = 11
self.editor.setSelection(iline, 5, iline, 6)
self.commenter.toggle_comment()
# check commented at indented position
self.assertEqual(self.editor.text().split('\n')[iline], " # do_something()")
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment