Anonymous avatar Anonymous committed a3c2ac0

Ignoring the contents of a triple quote string file finding current statement range

Comments (0)

Files changed (3)

docs/workingon.txt

 - More tests for explicit and implicit continuations
 ? Transform big unextractible methods into method objects
 - Not proposing variables defined after current line in the same scope
+- Ignoring """ or ''' string content
 
-* Ignoring """ or ''' string content
 
 ! scopes_count does not work because there might be loops and other blocks
 ? Add rope.scopes module

rope/codeassist.py

     def __init__(self, lines, lineno):
         self.lines = lines
         self.lineno = lineno
+        self.in_string = ''
+        self.open_parens = 0
+        self.explicit_continuation = False
 
-    # TODO: extract methods to shorten
+    def _analyze_line(self, current_line):
+        for i in range(len(current_line)):
+            char = current_line[i]
+            if char in '\'"':
+                if self.in_string == '':
+                    self.in_string = char
+                    if char * 3 == current_line[i:i + 3]:
+                        self.in_string = char * 3
+                elif self.in_string == current_line[i:i + len(self.in_string)] and \
+                     not (i > 0 and current_line[i - 1] == '\\' and
+                          not (i > 1 and current_line[i - 2:i] == '\\\\')):
+                    self.in_string = ''
+            if self.in_string != '':
+                continue
+            if char == '#':
+                break
+            if char in '([{':
+                self.open_parens += 1
+            if char in ')]}':
+                self.open_parens -= 1
+        if current_line.rstrip().endswith('\\'):
+            self.explicit_continuation = True
+        else:
+            self.explicit_continuation = False
+
+
     def get_range(self):
         last_statement = 0
-        open_parens = 0
-        explicit_continuation = False
-        in_string = ''
         for current_line_number in range(0, self.lineno + 1):
-            if not explicit_continuation and open_parens == 0 and in_string == '':
+            if not self.explicit_continuation and self.open_parens == 0 and self.in_string == '':
                 last_statement = current_line_number
             current_line = self.lines[current_line_number]
-            for i in range(len(current_line)):
-                char = current_line[i]
-                if char in '\'"':
-                    if in_string == '':
-                        in_string = char
-                    elif in_string == char and \
-                         not (i > 0 and current_line[i - 1] == '\\' and
-                              not (i > 1 and current_line[i - 2:i] == '\\\\')):
-                        in_string = ''
-                if in_string != '':
-                    continue
-                if char == '#':
-                    break
-                if char in '([{':
-                    open_parens += 1
-                if char in ')]}':
-                    open_parens -= 1
-            if current_line.rstrip().endswith('\\'):
-                explicit_continuation = True
-            else:
-                explicit_continuation = False
+            self._analyze_line(current_line)
         last_indents = self.get_line_indents(last_statement)
         end_line = self.lineno
         if True or self.lines[self.lineno].rstrip().endswith(':'):
                     break
         return (last_statement, end_line)
 
-    def get_line_indents(self, line):
+    def get_line_indents(self, line_number):
         indents = 0
-        for char in self.lines[line]:
+        for char in self.lines[line_number]:
             if char == ' ':
                 indents += 1
             else:
                 break
         return indents
 
+
 class CodeAssist(ICodeAssist):
     def __init__(self, indentation_length=4):
         self.indentation_length = indentation_length

ropetest/codeassisttest.py

         result = self.assist.complete_code(code, 16)
         self.assert_proposal_not_in_result('my_var', 'local_variable', result)
 
+    def test_ignoring_string_contents_with_triple_quotes(self):
+        code = "my_var = '''(\n'('''\nmy_"
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('my_var', 'global_variable', result)
+
+    def test_ignoring_string_contents_with_triple_quotes_and_backslash(self):
+        code = 'my_var = """\\"""("""\nmy_'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('my_var', 'global_variable', result)
+
+    def test_ignoring_string_contents_with_triple_quotes_and_double_backslash(self):
+        code = 'my_var = """\\\\"""\nmy_'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('my_var', 'global_variable', result)
+
 
 if __name__ == '__main__':
     unittest.main()
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.