示例#1
0
文件: index.py 项目: nicwest/indj
 def _get_definitions_from_file(self, path):
     with open(path, 'r') as fh:
         defs = jedi.defined_names(fh.read())
     module_import_path = self._get_module_import_path(path)
     items = [
         (d.name, self._get_import_path(d.full_name, module_import_path), )
         for d in defs]
     return items
示例#2
0
文件: index.py 项目: nicwest/indj
 def _get_definitions_from_file(self, path):
     with open(path, 'r') as fh:
         defs = jedi.defined_names(fh.read())
     module_import_path = self._get_module_import_path(path)
     items = [(
         d.name,
         self._get_import_path(d.full_name, module_import_path),
     ) for d in defs]
     return items
示例#3
0
 def names(self):
     namelist = jedi.defined_names(self.source_path)
     for name in namelist:
         yield {
             "name": name.name,
             "type": name.type,
             "line": name.line,
             "pos": name.start_pos,
             "doc": name.doc
         }
示例#4
0
def test_extract_def():
    code = """
    import pyqode.python.widgets
    import PyQt5.QtWidgets as QtWidgets
    app = QtWidgets.QApplication([])
    editor = pyqode.python.widgets.PyCyodeEdit()
    editor.file.open(__file__)
    editor.show()
    app.exec()
    """
    for definition in jedi.defined_names(code):
        result = workers._extract_def(definition, "")
        assert result
示例#5
0
def defined_names(request_data):
    """
    Returns the list of defined names for the document.
    """
    global _old_definitions
    ret_val = []
    path = request_data['path']
    toplvl_definitions = jedi.defined_names(request_data['code'], path,
                                            'utf-8')
    for d in toplvl_definitions:
        definition = _extract_def(d, path)
        if d.type != 'import':
            ret_val.append(definition)
    ret_val = [d.to_dict() for d in ret_val]
    return ret_val
示例#6
0
def defined_names(request_data):
    """
    Returns the list of defined names for the document.
    """
    global _old_definitions
    ret_val = []
    path = request_data['path']
    toplvl_definitions = jedi.defined_names(
        request_data['code'], path, 'utf-8')
    for d in toplvl_definitions:
        definition = _extract_def(d, path)
        if d.type != 'import':
            ret_val.append(definition)
    ret_val = [d.to_dict() for d in ret_val]
    return ret_val
示例#7
0
def make_definitions():
    """
    Return a list of definitions for parametrized tests.

    :rtype: [jedi.api_classes.BaseDefinition]
    """
    source = dedent(
        """
    import sys

    class C:
        pass

    x = C()

    def f():
        pass

    def g():
        yield

    h = lambda: None
    """
    )

    definitions = []
    definitions += defined_names(source)

    source += dedent(
        """
    variable = sys or C or x or f or g or g() or h"""
    )
    lines = source.splitlines()
    script = Script(source, len(lines), len("variable"), None)
    definitions += script.goto_definitions()

    script2 = Script(source, 4, len("class C"), None)
    definitions += script2.usages()

    source_param = "def f(a): return a"
    script_param = Script(source_param, 1, len(source_param), None)
    definitions += script_param.goto_assignments()

    return definitions
示例#8
0
def make_definitions():
    """
    Return a list of definitions for parametrized tests.

    :rtype: [jedi.api_classes.BaseDefinition]
    """
    source = dedent("""
    import sys

    class C:
        pass

    x = C()

    def f():
        pass

    def g():
        yield

    h = lambda: None
    """)

    definitions = []
    definitions += defined_names(source)

    source += dedent("""
    variable = sys or C or x or f or g or g() or h""")
    lines = source.splitlines()
    script = Script(source, len(lines), len('variable'), None)
    definitions += script.goto_definitions()

    script2 = Script(source, 4, len('class C'), None)
    definitions += script2.usages()

    source_param = "def f(a): return a"
    script_param = Script(source_param, 1, len(source_param), None)
    definitions += script_param.goto_assignments()

    return definitions
示例#9
0
    def __call__(self, client, caller_id, **kwargs):
        import jedi
        ret_val = []
        toplvl_definitions = jedi.defined_names(self.code, self.path,
                                                self.encoding)
        for d in toplvl_definitions:
            d_line, d_column = d.start_pos
            definition = Definition(d.name, iconFromType(d.name, d.type),
                                    d_line, d_column, d.full_name)
            if d.type.upper() == "IMPORT":
                definition.name = definition.full_name
            if d.type == "class":
                sub_definitions = d.defined_names()
                for sub_d in sub_definitions:
                    icon = iconFromType(sub_d.name, sub_d.type)
                    line, column = sub_d.start_pos
                    sub_definition = Definition(
                        sub_d.name, icon, line, column, sub_d.full_name)
                    if sub_definition.full_name == "":
                        sub_definition.full_name = sub_d.name
                    definition.add_child(sub_definition)
            ret_val.append(definition)

        try:
            old_definitions = self.slotDict["%d_definitions" % caller_id]
        except KeyError:
            old_definitions = []

        if ret_val:
            if not _compare_definitions(ret_val, old_definitions):
                ret_val = None
                logger.debug("No changes detected")
            else:
                self.slotDict["%d_definitions" % caller_id] = ret_val
                logger.debug("Document structure %r" % ret_val)
        return ret_val
示例#10
0
 def check(self, source, desired):
     definitions = jedi.defined_names(textwrap.dedent(source))
     full_names = [d.full_name for d in definitions]
     self.assertEqual(full_names, desired)
示例#11
0
def test_follow_imports():
    # github issue #344
    imp = defined_names('import datetime')[0]
    assert imp.name == 'datetime'
    datetime_names = [str(d.name) for d in imp.defined_names()]
    assert 'timedelta' in datetime_names
示例#12
0
def defined_names(request_data):
    """
    Returns the list of defined names for the document.
    """
    def _compare_definitions(a, b):
        """
        Compare two definition lists.

        Returns True if they are different.
        """
        if len(a) != len(b):
            return True
        else:
            # compare every definition. If one is different, break
            for def_a, def_b in zip(a, b):
                if def_a != def_b:
                    return True
            return False

    global _old_definitions
    code = request_data['code']
    path = request_data['path']
    # encoding = request_data['encoding']
    encoding = 'utf-8'
    ret_val = []
    toplvl_definitions = jedi.defined_names(code, path, encoding)
    for d in toplvl_definitions:
        d_line, d_column = d.start_pos
        # use full name for import type
        definition = Definition(d.name, icon_from_typename(d.name, d.type),
                                d_line, d_column, d.full_name)
        # check for methods in class
        if d.type == "class" or d.type == 'function':
            try:
                sub_definitions = d.defined_names()
                for sub_d in sub_definitions:
                    icon = icon_from_typename(sub_d.name, sub_d.type)
                    line, column = sub_d.start_pos
                    if sub_d.full_name == "":
                        sub_d.full_name = sub_d.name
                    sub_definition = Definition(sub_d.name, icon, line, column,
                                                sub_d.full_name)
                    definition.add_child(sub_definition)
            except AttributeError:
                pass
        if d.type != 'import':
            ret_val.append(definition)
    try:
        old_definitions = _old_definitions["%s_definitions" % path]
    except KeyError:
        old_definitions = []
    status = False
    if ret_val:
        if not _compare_definitions(ret_val, old_definitions):
            ret_val = None
            _logger().debug("No changes detected")
        else:
            _old_definitions["%s_definitions" % path] = ret_val
            _logger().debug("Document structure changed")
            status = True
            ret_val = [d.to_dict() for d in ret_val]
    return status, ret_val
示例#13
0
 def check(self, source, desired):
     definitions = jedi.defined_names(textwrap.dedent(source))
     full_names = [d.full_name for d in definitions]
     self.assertEqual(full_names, desired)
示例#14
0
def test_follow_imports():
    # github issue #344
    imp = defined_names('import datetime')[0]
    assert imp.name == 'datetime'
    datetime_names = [str(d.name) for d in imp.defined_names()]
    assert 'timedelta' in datetime_names