Source

rope_py3k / rope / contrib / codeassist.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import keyword
import re
import sys

import rope.base.codeanalyze
from rope.base import pyobjects, pynames, taskhandle, builtins
from rope.base.codeanalyze import (StatementRangeFinder, ArrayLinesAdapter,
                                   WordRangeFinder, ScopeNameFinder,
                                   SourceLinesAdapter, BadIdentifierError)
from rope.refactor import occurrences, functionutils


def code_assist(project, source_code, offset, resource=None, templates={}):
    """Return python code completions as a list of `CodeAssistProposal`\s

    `resource` is a `rope.base.resources.Resource` object.  If
    provided things relative imports are handled.

    `templates` should be a dictionary of template name to `Template`
    objects.  The matches are returned as `TemplateProposal`
    instances.

    """
    assist = _PythonCodeAssist(project, templates=templates)
    return assist.assist(source_code, offset, resource)


def starting_offset(source_code, offset):
    """Return the offset in which the completion should be inserted

    Usually code assist proposals should be inserted like::

        completion = proposal.name # not for templates
        result = (source_code[:starting_offset] +
                  completion + source_code[offset:])

    Where starting_offset is the offset returned by this function.

    """
    word_finder = WordRangeFinder(source_code)
    expression, starting, starting_offset = \
        word_finder.get_splitted_primary_before(offset)
    return starting_offset


def get_doc(project, source_code, offset, resource=None):
    """Get the pydoc"""
    pymodule = _get_pymodule(project.pycore, source_code, resource)
    scope_finder = ScopeNameFinder(pymodule)
    element = scope_finder.get_pyname_at(offset)
    if element is None:
        return None
    pyobject = element.get_object()
    return PyDocExtractor().get_doc(pyobject)


def get_definition_location(project, source_code, offset, resource=None):
    """Return a (`rope.base.resources.Resource`, lineno) tuple"""
    pymodule = project.pycore.get_string_module(source_code, resource)
    scope_finder = ScopeNameFinder(pymodule)
    element = scope_finder.get_pyname_at(offset)
    if element is not None:
        module, lineno = element.get_definition_location()
        if module is not None:
            return module.get_module().get_resource(), lineno
    return (None, None)


def find_occurrences(project, resource, offset, unsure=False,
                     task_handle=taskhandle.NullTaskHandle()):
    """Return a list of `Location`\s

    if `unsure` is `True`, possible matches are returned, too.

    """
    name = rope.base.codeanalyze.get_name_at(resource, offset)
    pyname = rope.base.codeanalyze.get_pyname_at(project.get_pycore(),
                                                 resource, offset)
    def is_match(occurrence):
        return unsure
    finder = occurrences.FilteredFinder(
        project.get_pycore(), name, [pyname], unsure=is_match)
    files = project.get_pycore().get_python_files()
    job_set = task_handle.create_jobset('Finding Occurrences',
                                        count=len(files))
    result = []
    for resource in files:
        job_set.started_job('Working On <%s>' % resource.path)
        for occurrence in finder.find_occurrences(resource):
            location = Location()
            location.resource = resource
            location.offset = occurrence.get_word_range()[0]
            location.unsure = occurrence.is_unsure()
            result.append(location)
        job_set.finished_job()
    return result


class Location(object):

    resource = None
    offset = None
    unsure = False


class CodeAssistProposal(object):
    """The base class for proposals reported by `code_assist`

    The `kind` instance variable shows the kind of the proposal and
    can be 'global', 'local', 'builtin', 'attribute', 'keyword',
    'parameter_keyword' and 'template'.

    """

    def __init__(self, name, kind):
        self.name = name
        self.kind = kind


class CompletionProposal(CodeAssistProposal):
    """A completion proposal

    The `type` instance variable shows the type of the proposal and
    can be 'variable', 'class', 'function', 'imported' , 'paramter'
    and `None`.

    """

    def __init__(self, name, kind, type=None):
        super(CompletionProposal, self).__init__(name, kind)
        self.type = type

    def __str__(self):
        return '%s (%s, %s)' % (self.name, self.kind, self.type)

    def __repr__(self):
        return str(self)


class TemplateProposal(CodeAssistProposal):
    """A template proposal

    The `template` attribute is a `Template` object.
    """

    def __init__(self, name, template):
        super(TemplateProposal, self).__init__(name, 'template')
        self.template = template


class Template(object):
    """Templates that are used in code assists

    Variables in templates are in ``${variable}`` format. To put a
    dollar sign in the template put $$.  To specify cursor position
    use ${cursor}.

    """

    def __init__(self, template):
        self.template = template

    var_pattern = re.compile(r'((?<=[^\$])|^)\${(?P<variable>\w+)}')

    def variables(self):
        """Get template variables

        Return the list of variables sorted by their order of
        occurence in the template.

        """
        result = []
        for match in self.var_pattern.finditer(self.template):
            new_var = match.group('variable')
            if new_var not in result and new_var != 'cursor':
                result.append(new_var)
        return result

    def _substitute(self, input_string, mapping):
        import string
        single_dollar = re.compile('((?<=[^\$])|^)\$((?=[^{\$])|$)')
        template = single_dollar.sub('$$', input_string)
        t = string.Template(template)
        return t.substitute(mapping, cursor='')

    def substitute(self, mapping):
        return self._substitute(self.template, mapping)

    def get_cursor_location(self, mapping):
        cursor_index = len(self.template)
        for match in self.var_pattern.finditer(self.template):
            new_var = match.group('variable')
            if new_var == 'cursor':
                cursor_index = match.start('variable') - 2
        new_template = self.template[0:cursor_index]
        start = len(self._substitute(new_template, mapping))
        return start


def sorted_proposals(proposals, kindpref=None, typepref=None):
    """Sort a list of proposals

    Return a sorted list of the given `CodeAssistProposal`\s.

    `typepref` can be a list of proposal kinds.  Defaults to ``['local',
    'parameter_keyword', 'global', 'attribute', 'template', 'builtin',
    'keyword']``.

    `typepref` can be a list of proposal types.  Defaults to
    ``['class', 'function', 'variable', 'parameter', 'imported',
    None]``.  (`None` stands for completions with no type like
    keywords.)

    """
    sorter = _ProposalSorter(proposals, kindpref, typepref)
    return sorter.get_sorted_proposal_list()


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


class _PythonCodeAssist(object):

    def __init__(self, project, templates={}):
        self.project = project
        self.keywords = keyword.kwlist
        self.templates = templates

    def _find_starting_offset(self, source_code, offset):
        current_offset = offset - 1
        while current_offset >= 0 and (source_code[current_offset].isalnum() or
                                       source_code[current_offset] in '_'):
            current_offset -= 1;
        return current_offset + 1

    def _get_matching_keywords(self, starting):
        result = []
        for kw in self.keywords:
            if kw.startswith(starting):
                result.append(CompletionProposal(kw, 'keyword'))
        return result

    def add_template(self, name, definition):
        self.templates.append(TemplateProposal(name, Template(definition)))

    def _get_template_proposals(self, starting):
        result = []
        for name, template in self.templates.items():
            if name.startswith(starting):
                result.append(TemplateProposal(name, template))
        return result

    def _get_code_completions(self, source_code, offset,
                              expression, starting, resource):
        collector = _CodeCompletionCollector(self.project, source_code, offset,
                                             expression, starting, resource)
        return collector.get_code_completions()

    def assist(self, source_code, offset, resource=None):
        if offset > len(source_code):
            return []
        word_finder = WordRangeFinder(source_code)
        expression, starting, starting_offset = \
            word_finder.get_splitted_primary_before(offset)
        completions = list(
            self._get_code_completions(source_code, offset, expression,
                                       starting, resource).values())
        if expression.strip() == '' and starting.strip() != '':
            completions.extend(self._get_matching_keywords(starting))
            completions.extend(self._get_template_proposals(starting))
        return completions


class _ProposalSorter(object):
    """Sort a list of code assist proposals"""

    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', 'builtin', 'keyword']
        self.kindpref = kindpref
        if typepref is None:
            typepref = ['class', 'function', 'variable',
                        'parameter', 'imported', None]
        self.typerank = dict((type, index)
                              for index, type in enumerate(typepref))

    def get_sorted_proposal_list(self):
        """Return a list of `CodeAssistProposal`"""
        proposals = {}
        for proposal in self.proposals:
            proposals.setdefault(proposal.kind, []).append(proposal)
        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.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:
            return cmp(self.typerank.get(proposal1.type, 100),
                       self.typerank.get(proposal2.type, 100))
        return self._compare_underlined_names(proposal1.name,
                                              proposal2.name)

    def _compare_underlined_names(self, name1, name2):
        def underline_count(name):
            result = 0
            while result < len(name) and name[result] == '_':
                result += 1
            return result
        underline_count1 = underline_count(name1)
        underline_count2 = underline_count(name2)
        if underline_count1 != underline_count2:
            return cmp(underline_count1, underline_count2)
        return cmp(name1, name2)


class _CodeCompletionCollector(object):

    def __init__(self, project, source_code,
                 offset, expression, starting, resource):
        self.project = project
        self.expression = expression
        self.starting = starting
        self.offset = offset
        self.pycore = self.project.get_pycore()
        self.unchanged_source = source_code
        self.lines = source_code.split('\n')
        self.source_code = source_code
        self.resource = resource
        source_lines = SourceLinesAdapter(source_code)
        self.lineno = source_lines.get_line_number(offset)
        self.current_indents = self._get_line_indents(
            source_lines.get_line(self.lineno))
        self._comment_current_statement()
        self.source_code = '\n'.join(self.lines)

    def _get_line_indents(self, line):
        return rope.base.codeanalyze.count_line_indents(line)

    def _comment_current_statement(self):
        range_finder = StatementRangeFinder(ArrayLinesAdapter(self.lines),
                                            self.lineno)
        start = range_finder.get_statement_start() - 1
        end = range_finder.get_block_end() - 1
        last_indents = self._get_line_indents(self.lines[start])
        self.lines[start] = ' ' * last_indents + 'pass'
        for line in range(start + 1, end + 1):
            #self.lines[line] = '#' # + lines[line]
            self.lines[line] = self.lines[start]
        self.lines.append('\n')
        self._fix_incomplete_try_blocks()

    def _fix_incomplete_try_blocks(self):
        block_start = self.lineno
        last_indents = self.current_indents
        while block_start > 0:
            block_start = rope.base.codeanalyze.get_block_start(
                ArrayLinesAdapter(self.lines), block_start) - 1
            if self.lines[block_start].strip().startswith('try:'):
                indents = self._get_line_indents(self.lines[block_start])
                if indents > last_indents:
                    continue
                last_indents = indents
                block_end = self._find_matching_deindent(block_start)
                if not self.lines[block_end].strip().startswith('finally:') and \
                   not self.lines[block_end].strip().startswith('except '):
                    self.lines.insert(block_end, ' ' * indents + 'finally:')
                    self.lines.insert(block_end + 1, ' ' * indents + '    pass')

    def _find_matching_deindent(self, line_number):
        indents = self._get_line_indents(self.lines[line_number])
        current_line = line_number + 1
        while current_line < len(self.lines):
            line = self.lines[current_line]
            if not line.strip().startswith('#') and not line.strip() == '':
                # HACK: We should have used logical lines here
                if self._get_line_indents(self.lines[current_line]) <= indents:
                    return current_line
            current_line += 1
        return len(self.lines) - 1

    def _get_dotted_completions(self, module_scope, holding_scope):
        result = {}
        pyname_finder = ScopeNameFinder(module_scope.pyobject)
        found_pyname = pyname_finder.get_pyname_in_scope(holding_scope,
                                                         self.expression)
        if found_pyname is not None:
            element = found_pyname.get_object()
            for name, pyname in element.get_attributes().items():
                if name.startswith(self.starting) or self.starting == '':
                    result[name] = CompletionProposal(
                        name, 'attribute', self._get_pyname_type(pyname))
        return result

    def _get_undotted_completions(self, scope, result, propagated=False):
        if scope.parent != None:
            self._get_undotted_completions(scope.parent, result,
                                           propagated=True)
        if propagated:
            names = scope.get_propagated_names()
        else:
            names = scope.get_names()
        for name, pyname in names.items():
            if name.startswith(self.starting):
                kind = 'local'
                if isinstance(pyname, builtins.BuiltinName):
                    kind = 'builtin'
                elif scope.get_kind() == 'Module':
                    kind = 'global'
                result[name] = CompletionProposal(
                    name, kind, self._get_pyname_type(pyname))

    def _get_pyname_type(self, pyname):
        if isinstance(pyname, (pynames.AssignedName, pynames.UnboundName)):
            return 'variable'
        if isinstance(pyname, pynames.ImportedName) or \
           isinstance(pyname, pynames.ImportedModule):
            return 'imported'
        if isinstance(pyname, pynames.ParameterName):
            return 'parameter'
        if isinstance(pyname, builtins.BuiltinName) or \
           isinstance(pyname, pynames.DefinedName):
            pyobject = pyname.get_object()
            if isinstance(pyobject, pyobjects.AbstractFunction):
                return 'function'
            if isinstance(pyobject, pyobjects.AbstractClass):
                return 'class'

    def get_code_completions(self):
        module_scope = _get_pymodule(self.pycore, self.source_code,
                                    self.resource).get_scope()
        result = {}
        inner_scope = module_scope.get_inner_scope_for_line(self.lineno,
                                                            self.current_indents)
        if self.expression.strip() != '':
            result.update(self._get_dotted_completions(module_scope,
                                                       inner_scope))
        else:
            result.update(self._get_keyword_parameters(module_scope.pyobject,
                                                       inner_scope))
            self._get_undotted_completions(inner_scope, result)
        return result

    def _get_keyword_parameters(self, pymodule, scope):
        offset = self.offset
        if offset == 0:
            return {}
        word_finder = WordRangeFinder(self.unchanged_source)
        lines = SourceLinesAdapter(self.unchanged_source)
        lineno = lines.get_line_number(offset)
        stop_line = StatementRangeFinder(lines, lineno).get_statement_start()
        stop = lines.get_line_start(stop_line)
        if word_finder.is_on_function_call_keyword(offset - 1, stop):
            name_finder = ScopeNameFinder(pymodule)
            function_parens = word_finder.find_parens_start_from_inside(offset - 1, stop)
            primary = word_finder.get_primary_at(function_parens - 1)
            try:
                function_pyname = ScopeNameFinder.get_pyname_in_scope(scope,
                                                                      primary)
            except BadIdentifierError, e:
                return {}
            if function_pyname is not None:
                pyobject = function_pyname.get_object()
                if isinstance(pyobject, pyobjects.AbstractFunction):
                    pass
                elif isinstance(pyobject, pyobjects.AbstractClass) and \
                     '__init__' in pyobject.get_attributes():
                    pyobject = pyobject.get_attribute('__init__').get_object()
                elif '__call__' in pyobject.get_attributes():
                    pyobject = pyobject.get_attribute('__call__').get_object()
                if isinstance(pyobject, pyobjects.AbstractFunction):
                    param_names = []
                    param_names.extend(
                        pyobject.get_param_names(special_args=False))
                    result = {}
                    for name in param_names:
                        if name.startswith(self.starting):
                            result[name + '='] = CompletionProposal(
                                name + '=', 'parameter_keyword')
                    return result
        return {}


def _get_pymodule(pycore, source_code, resource):
    if resource and resource.exists() and source_code == resource.read():
        return pycore.resource_to_pyobject(resource)
    return pycore.get_string_module(source_code, resource=resource)


class PyDocExtractor(object):

    def get_doc(self, pyobject):
        if isinstance(pyobject, pyobjects.AbstractFunction):
            return self._get_function_docstring(pyobject)
        elif isinstance(pyobject, pyobjects.AbstractClass):
            return self._get_class_docstring(pyobject)
        elif isinstance(pyobject, pyobjects.AbstractModule):
            return self._trim_docstring(pyobject.get_doc())
        return None

    def _get_class_docstring(self, pyclass):
        contents = self._trim_docstring(pyclass.get_doc(), 2)
        supers = [super.get_name() for super in pyclass.get_superclasses()]
        doc = 'class %s(%s):\n\n' % (pyclass.get_name(), ', '.join(supers)) + contents

        if '__init__' in pyclass.get_attributes():
            init = pyclass.get_attribute('__init__').get_object()
            if isinstance(init, pyobjects.AbstractFunction):
                doc += '\n\n' + self._get_single_function_docstring(init)
        return doc

    def _get_function_docstring(self, pyfunction):
        functions = [pyfunction]
        if self._is_method(pyfunction):
            functions.extend(self._get_super_methods(pyfunction.parent,
                                                     pyfunction.get_name()))
        return '\n\n'.join([self._get_single_function_docstring(function)
                            for function in functions])

    def _is_method(self, pyfunction):
        return isinstance(pyfunction, pyobjects.PyFunction) and \
               isinstance(pyfunction.parent, pyobjects.PyClass)

    def _get_single_function_docstring(self, pyfunction):
        signature = self._get_function_signature(pyfunction)
        if self._is_method(pyfunction):
            signature = pyfunction.parent.get_name() + '.' + signature
            self._get_super_methods(pyfunction.parent, pyfunction.get_name())
        return signature + ':\n\n' + self._trim_docstring(pyfunction.get_doc(),
                                                         indents=2)

    def _get_super_methods(self, pyclass, name):
        result = []
        for super_class in pyclass.get_superclasses():
            if name in super_class.get_attributes():
                function = super_class.get_attribute(name).get_object()
                if isinstance(function, pyobjects.AbstractFunction):
                    result.append(function)
            result.extend(self._get_super_methods(super_class, name))
        return result

    def _get_function_signature(self, pyfunction):
        if isinstance(pyfunction, pyobjects.PyFunction):
            info = functionutils.DefinitionInfo.read(pyfunction)
            return info.to_string()
        else:
            return '%s(%s)' % (pyfunction.get_name(),
                               ', '.join(pyfunction.get_param_names()))

    def _trim_docstring(self, docstring, indents=0):
        """The sample code from :PEP:`257`"""
        if not docstring:
            return ''
        # Convert tabs to spaces (following normal Python rules)
        # and split into a list of lines:
        lines = docstring.expandtabs().splitlines()
        # Determine minimum indentation (first line doesn't count):
        indent = sys.maxint
        for line in lines[1:]:
            stripped = line.lstrip()
            if stripped:
                indent = min(indent, len(line) - len(stripped))
        # Remove indentation (first line is special):
        trimmed = [lines[0].strip()]
        if indent < sys.maxint:
            for line in lines[1:]:
                trimmed.append(line[indent:].rstrip())
        # Strip off trailing and leading blank lines:
        while trimmed and not trimmed[-1]:
            trimmed.pop()
        while trimmed and not trimmed[0]:
            trimmed.pop(0)
        # Return a single string:
        return '\n'.join((' ' * indents + line for line in trimmed))