Unverified Commit 00ad12ee authored by Gemma Guest's avatar Gemma Guest Committed by GitHub
Browse files

Merge pull request #33077 from mantidproject/33069_preserve_indent_when_commenting

Preserve indent when commenting
parents 3d43e38f 0c4cc0d9
......@@ -41,5 +41,6 @@ Bugfixes
- Fixed an issue to plot negative values with logarithm scaling in slice view.
- Workbench will no longer hang if an algorithm was running when workbench was closed.
- Fixed a bug in the editor where uncommenting using 'ctrl+/' wasn't working correctly for lines of the form '<optional whitespace>#code_here # inline comment'.
- Commenting code in the editor using 'ctrl+/' will preserve indenting (i.e. `# ` will be inserted at the position of the first non-whitespace character in the line).
:ref:`Release 6.3.0 <v6.3.0>`
......@@ -62,8 +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)):
lines[i] = '# ' + lines[i]
lines[i] = lines[i][:istart] + '# ' + lines[i][istart:]
return lines
def _uncomment_lines(self, lines):
......
......@@ -31,7 +31,9 @@ class CodeCommenterTest(unittest.TestCase):
"",
"import numpy",
"# import mantid",
"do_something()"]
"for ii in range(2):",
" do_something()",
"do_something_else()"]
self.editor = CodeEditor("AlternateCSPython", QFont())
self.editor.setText('\n'.join(self.lines))
......@@ -75,19 +77,35 @@ class CodeCommenterTest(unittest.TestCase):
self.editor.setSelection(0, 2, 3, 2)
self.commenter.toggle_comment()
expected_lines = copy(self.lines)
expected_lines[start_line:end_line+1] = [
line.replace('# ', '') for line in expected_lines[0:end_line+1]]
expected_lines[start_line:end_line + 1] = [
line.replace('# ', '') for line in expected_lines[0:end_line + 1]]
self.assertEqual(self.editor.text(), '\n'.join(expected_lines))
def test_multiline_comment(self):
def test_multiline_comment_for_mix_of_commented_and_uncommented_lines(self):
start_line, end_line = 5, 8
self.editor.setSelection(start_line, 5, end_line, 5)
self.commenter.toggle_comment()
expected_lines = copy(self.lines)
expected_lines[start_line:end_line+1] = [
'# ' + line for line in expected_lines[start_line:end_line+1]]
expected_lines[start_line:end_line + 1] = [
'# ' + line for line in expected_lines[start_line:end_line + 1]]
self.assertEqual(self.editor.text(), '\n'.join(expected_lines))
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_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