Anonymous avatar Anonymous committed 64f2296

codeassist: removed template support

Comments (0)

Files changed (2)

rope/contrib/codeassist.py

 import keyword
 import re
 import sys
+import warnings
 
 import rope.base.codeanalyze
 import rope.base.evaluate
 
 
 def code_assist(project, source_code, offset, resource=None,
-                templates={}, maxfixes=1, later_locals=True):
+                templates=None, maxfixes=1, later_locals=True):
     """Return python code completions as a list of `CodeAssistProposal`\s
 
     `resource` is a `rope.base.resources.Resource` object.  If
     provided, relative imports are handled.
 
-    `templates` should be a dictionary of template name to `Template`
-    objects.  The matches are returned as `TemplateProposal`
-    instances.
-
     `maxfixes` is the maximum number of errors to fix if the code has
     errors in it.
 
     this line is ignored.
 
     """
+    if templates is not None:
+        warnings.warn('Codeassist no longer supports templates',
+                      DeprecationWarning, stacklevel=2)
     assist = _PythonCodeAssist(
         project, source_code, offset, resource=resource,
-        templates=templates, maxfixes=maxfixes, later_locals=later_locals)
+        maxfixes=maxfixes, later_locals=later_locals)
     return assist()
 
 
 
     Usually code assist proposals should be inserted like::
 
-        completion = proposal.name # not for templates
+        completion = proposal.name
         result = (source_code[:starting_offset] +
                   completion + source_code[offset:])
 
 
     The `kind` instance variable shows the kind of the proposal and
     can be 'global', 'local', 'builtin', 'attribute', 'keyword',
-    'parameter_keyword' and 'template'.
+    'parameter_keyword'.
 
     """
 
 
     `kindpref` can be a list of proposal kinds.  Defaults to
     ``['local', 'parameter_keyword', 'global', 'attribute',
-    'template', 'keyword']``.
+    'keyword']``.
 
     `typepref` can be a list of proposal types.  Defaults to
     ``['class', 'function', 'variable', 'parameter', 'imported',
     'builtin', None]``.  (`None` stands for completions with no type
     like keywords.)
-
     """
     sorter = _ProposalSorter(proposals, kindpref, typepref)
     return sorter.get_sorted_proposal_list()
 
 
 def starting_expression(source_code, offset):
-    """Return the expression to complete
-
-    For instance completing
-
-    """
+    """Return the expression to complete"""
     word_finder = WordRangeFinder(source_code)
     expression, starting, starting_offset = \
         word_finder.get_splitted_primary_before(offset)
 
 
 def default_templates():
-    templates = {}
-    templates['main'] = Template("if __name__ == '__main__':\n    ${cursor}\n")
-    test_case_template = \
-        ('import unittest\n\n\n'
-         'class ${TestClass}(unittest.TestCase):\n\n'
-         '    def setUp(self):\n        super(${TestClass}, self).setUp()\n\n'
-         '    def tearDown(self):\n        super(${TestClass}, self).tearDown()\n\n'
-         '    def test_trivial_case${cursor}(self):\n        pass\n\n\n'
-         'if __name__ == \'__main__\':\n'
-         '    unittest.main()\n')
-    templates['testcase'] = Template(test_case_template)
-    templates['hash'] = Template('\n    def __hash__(self):\n' +
-                                 '        return 1${cursor}\n')
-    templates['eq'] = Template('\n    def __eq__(self, obj):\n' +
-                               '        ${cursor}return obj is self\n')
-    templates['super'] = Template('super(${class}, self)')
-    return templates
+    warnings.warn('default_templates() is deprecated.',
+                  DeprecationWarning, stacklevel=2)
+    return {}
 
 
 class _PythonCodeAssist(object):
 
     def __init__(self, project, source_code, offset, resource=None,
-                 templates={}, maxfixes=1, later_locals=True):
+                 maxfixes=1, later_locals=True):
         self.project = project
         self.pycore = self.project.get_pycore()
         self.code = source_code
         self.resource = resource
         self.maxfixes = maxfixes
         self.later_locals = later_locals
-        self.templates = templates
         self.word_finder = WordRangeFinder(source_code)
         self.expression, self.starting, self.offset = \
             self.word_finder.get_splitted_primary_before(offset)
                 result.append(CompletionProposal(kw, 'keyword'))
         return result
 
-    def _template_proposals(self, starting):
-        result = []
-        for name, template in self.templates.items():
-            if name.startswith(starting):
-                result.append(TemplateProposal(name, template))
-        return result
-
     def __call__(self):
         if self.offset > len(self.code):
             return []
         completions = list(self._code_completions().values())
         if self.expression.strip() == '' and self.starting.strip() != '':
             completions.extend(self._matching_keywords(self.starting))
-            completions.extend(self._template_proposals(self.starting))
         return completions
 
     def _dotted_completions(self, module_scope, holding_scope):
     def __init__(self, code_assist_proposals, kindpref=None, typepref=None):
         self.proposals = code_assist_proposals
         if kindpref is None:
-            kindpref = ['local', 'parameter_keyword', 'global', 'attribute',
-                        'template', 'keyword']
+            kindpref = ['local', 'parameter_keyword', 'global',
+                        'attribute', 'keyword']
         self.kindpref = kindpref
         if typepref is None:
             typepref = ['class', 'function', 'variable',
         result = []
         for kind in self.kindpref:
             kind_proposals = proposals.get(kind, [])
-            if kind != 'template':
-                kind_proposals = [proposal for proposal in kind_proposals
-                                  if proposal.type in self.typerank]
+            kind_proposals = [proposal for proposal in kind_proposals
+                              if proposal.type in self.typerank]
             kind_proposals.sort(self._proposal_cmp)
             result.extend(kind_proposals)
         return result
 
     def _proposal_cmp(self, proposal1, proposal2):
-        if 'template' not in (proposal1.kind, proposal2.kind) and \
-           proposal1.type != proposal2.type:
+        if proposal1.type != proposal2.type:
             return cmp(self.typerank.get(proposal1.type, 100),
                        self.typerank.get(proposal2.type, 100))
         return self._compare_underlined_names(proposal1.name,

ropetest/contrib/codeassisttest.py

 
 from rope.base import exceptions
 from rope.contrib.codeassist import \
-     (Template, get_definition_location, get_doc, starting_expression,
+     (get_definition_location, get_doc, starting_expression,
       find_occurrences, code_assist, sorted_proposals, starting_offset)
 from ropetest import testutils
 
         testutils.remove_project(self.project)
         super(CodeAssistTest, self).tearDown()
 
-    def _assist(self, code, offset=None, templates={}, **args):
+    def _assist(self, code, offset=None, **args):
         if offset is None:
             offset = len(code)
-        return code_assist(self.project, code, offset,
-                           templates=templates, **args)
+        return code_assist(self.project, code, offset,  **args)
 
     def test_simple_assist(self):
         self._assist('', 0)
         result = self._assist(code)
         self.assert_completion_in_result('param', 'local', result)
 
-    def assert_template_in_result(self, name, result):
-        for template in result:
-            if template.name == name:
-                break
-        else:
-            self.fail('template <%s> was not proposed' % name)
-
-    def assert_template_not_in_result(self, name, result):
-        for template in result:
-            if template.name == name:
-                self.fail('template <%s> was proposed' % name)
-
-    def test_proposing_basic_templates(self):
-        templates = {'my_template': Template('print("hello")')}
-        code = 'my_te'
-        result = self._assist(code, templates=templates)
-        self.assert_template_in_result('my_template', result)
-
     def test_code_assist_when_having_a_two_line_function_header(self):
         code = 'def f(param1,\n      param2):\n    para'
         result = self._assist(code)
         self.assert_completion_in_result('a_method', 'attribute', result)
 
     def test_proposals_sorter(self):
-        templates = {'my_sample_template': Template('')}
         code = 'def my_sample_function(self):\n' + \
                '    my_sample_var = 20\n' + \
                '    my_sample_'
-        proposals = sorted_proposals(self._assist(code, templates=templates))
+        proposals = sorted_proposals(self._assist(code))
         self.assertEquals('my_sample_var', proposals[0].name)
         self.assertEquals('my_sample_function', proposals[1].name)
-        self.assertEquals('my_sample_template', proposals[2].name)
 
     def test_proposals_sorter_for_methods_and_attributes(self):
-        templates = {'my_sample_template': Template('')}
         code = 'class A(object):\n' + \
                '    def __init__(self):\n' + \
                '        self.my_a_var = 10\n' + \
                '        pass\n' + \
                'a_var = A()\n' + \
                'a_var.my_'
-        proposals = sorted_proposals(self._assist(code, templates=templates))
+        proposals = sorted_proposals(self._assist(code))
         self.assertEquals('my_b_func', proposals[0].name)
         self.assertEquals('my_c_func', proposals[1].name)
         self.assertEquals('my_a_var', proposals[2].name)
         self.assertEquals('my_a_var', proposals[1].name)
 
     def test_proposals_sorter_underlined_methods(self):
-        templates = {'my_sample_template': Template('')}
         code = 'class A(object):\n' + \
                '    def _my_func(self):\n' + \
                '        self.my_a_var = 10\n' + \
                '        pass\n' + \
                'a_var = A()\n' + \
                'a_var.'
-        proposals = sorted_proposals(self._assist(code, templates=templates))
+        proposals = sorted_proposals(self._assist(code))
         self.assertEquals('my_func', proposals[0].name)
         self.assertEquals('_my_func', proposals[1].name)
 
         self.assertEquals('my_global_var', proposals[0].name)
         self.assertEquals('my_global_func', proposals[1].name)
 
-    def test_proposals_sorter_sorting_templates(self):
-        templates = {'my_first_template': Template(''),
-                     'my_second_template': Template('')}
-        code = 'my_'
-        result = self._assist(code, templates=templates)
-        proposals = sorted_proposals(result, kindpref=['template'])
-        self.assertEquals('my_first_template', proposals[0].name)
-        self.assertEquals('my_second_template', proposals[1].name)
-
     def test_proposals_sorter_and_missing_type_in_typepref(self):
         code = 'my_global_var = 1\n' \
                'def my_global_func():\n    pass\n' \
         self.assertEquals('l.app', starting_expression(code, len(code)))
 
 
-class TemplateTest(unittest.TestCase):
-
-    def test_template_get_variables(self):
-        template = Template('Name = ${name}')
-        self.assertEquals(['name'], template.variables())
-
-    def test_template_get_variables_multiple_variables(self):
-        template = Template('Name = ${name}\nAge = ${age}\n')
-        self.assertEquals(['name', 'age'], template.variables())
-
-    def test_substitution(self):
-        template = Template('Name = ${name}\nAge = ${age}\n')
-        self.assertEquals('Name = Ali\nAge = 20\n',
-                          template.substitute({'name': 'Ali', 'age': '20'}))
-
-    def test_underlined_variables(self):
-        template = Template('Name = ${name_var}')
-        self.assertEquals(['name_var'], template.variables())
-        self.assertEquals('Name = Ali', template.substitute({'name_var': 'Ali'}))
-
-    @testutils.assert_raises(KeyError)
-    def test_unmapped_variable(self):
-        template = Template('Name = ${name}')
-        template.substitute({})
-
-    def test_double_dollar_sign(self):
-        template = Template('Name = $${name}')
-        self.assertEquals([], template.variables())
-        self.assertEquals('Name = ${name}', template.substitute({'name': 'Ali'}))
-
-    def test_untemplate_dollar_signs(self):
-        template = Template('$name = ${value}')
-        self.assertEquals(['value'], template.variables())
-        self.assertEquals('$name = Ali', template.substitute({'value': 'Ali'}))
-
-    def test_template_get_variables_multiple_variables2(self):
-        template = Template('Name = ${name}${age}\n')
-        self.assertEquals(['name', 'age'], template.variables())
-
-    def test_template_get_variables_start_of_the_string(self):
-        template = Template('${name}\n')
-        self.assertEquals(['name'], template.variables())
-
-    def test_the_same_variable_many_times(self):
-        template = Template("Today is ${today}, the day after ${today} is ${tomorrow}")
-        self.assertEquals(['today', 'tomorrow'], template.variables())
-        self.assertEquals("Today is 26th, the day after 26th is 27th",
-                         template.substitute({'today': '26th', 'tomorrow': '27th'}))
-
-    def test_cursor_in_templates(self):
-        template = Template('My name is ${name}${cursor}.')
-        self.assertEquals(['name'], template.variables())
-        self.assertEquals('My name is Ali.', template.substitute({'name': 'Ali'}))
-
-    def test_get_cursor_location(self):
-        template = Template('My name is ${name}${cursor}.')
-        self.assertEquals(14, template.get_cursor_location({'name': 'Ali'}))
-
-    def test_get_cursor_location_with_no_cursor(self):
-        template = Template('My name is ${name}.')
-        self.assertEquals(15, template.get_cursor_location({'name': 'Ali'}))
-
-
 def suite():
     result = unittest.TestSuite()
     result.addTests(unittest.makeSuite(CodeAssistTest))
     result.addTests(unittest.makeSuite(CodeAssistInProjectsTest))
-    result.addTests(unittest.makeSuite(TemplateTest))
     return result
 
 if __name__ == '__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.