Anonymous avatar Anonymous committed b537443

Commenting blocks with a single line in code-assist

Comments (0)

Files changed (2)

rope/contrib/codeassist.py

         self.lines = lines
 
     def comment(self, lineno):
-        start = _logical_start(self.lines, lineno) - 1
+        start = _logical_start(self.lines, lineno, check_prev=True) - 1
         end = self._get_block_end(start)
-        last_indents = _get_line_indents(self.lines[start])
-        self.lines[start] = ' ' * last_indents + 'pass'
+        indents = _get_line_indents(self.lines[start])
+        if 0 < start:
+            last_lineno = self._last_non_blank(start - 1)
+            last_line = self.lines[last_lineno]
+            if last_line.rstrip().endswith(':'):
+                indents = _get_line_indents(last_line) + 4            
+        self.lines[start] = ' ' * indents + 'pass'
         for line in range(start + 1, end + 1):
             self.lines[line] = self.lines[start]
         self.lines.append('\n')
-        self._fix_incomplete_try_blocks(lineno, last_indents)
+        self._fix_incomplete_try_blocks(lineno, indents)
+
+    def _last_non_blank(self, start):
+        while start > 0 and self.lines[start].strip() == '':
+            start -= 1
+        return start
 
     def _get_block_end(self, lineno):
         end_line = lineno
     return rope.base.codeanalyze.count_line_indents(line)
 
 
-def _logical_start(lines, lineno):
+def _logical_start(lines, lineno, check_prev=False):
     logical_finder = LogicalLineFinder(ArrayLinesAdapter(lines))
+    if check_prev:
+        prev = lineno - 1
+        while prev > 0:
+            start, end = logical_finder.get_logical_line_in(prev)
+            if end is None or start <= lineno < end:
+                return start
+            if start <= prev:
+                break
+            prev -= 1
     return logical_finder.get_logical_line_in(lineno)[0]
 
 

ropetest/contrib/codeassisttest.py

         result = self._assist(code, code.rindex('f') + 1)
         self.assert_completion_not_in_result('a_var', 'global', result)
 
+    def test_codeassist_before_single_line_indents(self):
+        code = 'myvar = 1\nif True:\n    (myv\nif True:\n    pass\n'
+        result = self._assist(code, code.rindex('myv') + 3)
+        self.assert_completion_not_in_result('myvar', 'local', result)
+
+    def test_codeassist_before_line_indents_in_a_blank_line(self):
+        code = 'myvar = 1\nif True:\n    \nif True:\n    pass\n'
+        result = self._assist(code, code.rindex('    ') + 4)
+        self.assert_completion_not_in_result('myvar', 'local', result)
+
 
 class CodeAssistInProjectsTest(unittest.TestCase):
 
         self.assert_completion_in_result('nestedmod', 'global', result)
 
     def test_completing_after_dot(self):
-        code = 'class SampleClass(object):\n    def sample_method(self):\n        pass\nSampleClass.sam'
+        code = 'class SampleClass(object):\n    def sample_method(self):\n' \
+               '        pass\nSampleClass.sam'
         result = self._assist(code)
         self.assert_completion_in_result('sample_method', 'attribute', result)
 
     def test_completing_after_multiple_dots(self):
-        code = 'class Class1(object):\n    class Class2(object):\n        def sample_method(self):\n' + \
+        code = 'class Class1(object):\n    class Class2(object):\n' \
+               '        def sample_method(self):\n' \
                '            pass\nClass1.Class2.sam'
         result = self._assist(code)
         self.assert_completion_in_result('sample_method', 'attribute', result)
 
     def test_completing_after_self_dot(self):
-        code = 'class Sample(object):\n    def method1(self):\n        pass\n' + \
+        code = 'class Sample(object):\n    def method1(self):\n        pass\n' \
                '    def method2(self):\n        self.m'
         result = self._assist(code)
         self.assert_completion_in_result('method1', 'attribute', result)
 
     def test_result_start_offset_for_dotted_completions(self):
-        code = 'class Sample(object):\n    def method1(self):\n        pass\n' + \
+        code = 'class Sample(object):\n    def method1(self):\n        pass\n' \
                'Sample.me'
         self.assertEquals(len(code) - 2, starting_offset(code, len(code)))
 
     def test_backslash_after_dots(self):
-        code = 'class Sample(object):\n    def a_method(self):\n        pass\n' + \
+        code = 'class Sample(object):\n    def a_method(self):\n        pass\n' \
                'Sample.\\\n       a_m'
         result = self._assist(code)
         self.assert_completion_in_result('a_method', 'attribute', result)
 
     def test_not_proposing_global_names_after_dot(self):
-        code = 'class Sample(object):\n    def a_method(self):\n        pass\n' + \
+        code = 'class Sample(object):\n    def a_method(self):\n        pass\n' \
                'Sample.'
         result = self._assist(code)
         self.assert_completion_not_in_result('Sample', 'global', result)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.