Exemple #1
0
def test_nested_namedtuples(Script):
    """
    From issue #730.
    """
    s = Script(dedent('''
        import collections
        Dataset = collections.namedtuple('Dataset', ['data'])
        Datasets = collections.namedtuple('Datasets', ['train'])
        train_x = Datasets(train=Dataset('data_value'))
        train_x.train.'''))
    assert 'data' in [c.name for c in s.complete()]
class JediCompletionModel(CodeCompletionBase):
    TITLE_AUTOCOMPLETION = i18nc('@label:listbox', 'Python Jedi autocomplete')
    MIMETYPES = ['text/x-python']
    """Code Completion model using Jedi.

    I chose not to use libkatepate’s AbstractCodeCompletionModel due to it being optimized
    for several traits that we don’t need, especially line and word extraction
    """

    def __init__(self, parent):
        """Script and completion list, the only properties we need"""
        super(JediCompletionModel, self).__init__(parent)
        self.script = None


    def completionInvoked(self, view, range_, invocation_type):
        """For the lack of a better event, we create a script here and remember its completions"""
        # TODO Check if cursor positioned in a comment or string and
        # DO NOT EVEN TRY TO COMPLETE SOMETHING IN THAT CASE!
        # Unfortunately, due a (still opened) BUG https://bugs.kde.org/show_bug.cgi?id=247896#c5
        # it is impossible to get that info nowadays :(
        # Fortunately, pate plugin can be hacked to provide required method...
        doc = view.document()
        if not doc.mimeType() in self.MIMETYPES:
            # Reset the model (and loose all completions!) if current document
            # is not suitable!!! (so data() will return nothing)
            self.resultList.clear()
            return

        cursor = view.cursorPosition()
        self.script = Script(doc.text(), cursor.line() + 1, cursor.column(), doc.url().toLocalFile())

        self.resultList = self.script.complete()


    def data(self, index, role):
        """Basically a 2D-lookup-table for all the things a code completion model can do"""
        if not index.parent().isValid():
            return self.TITLE_AUTOCOMPLETION

        item = self.resultList[index.row()]
        col = index.column()

        if role == Qt.DecorationRole and col == CCM.Icon:
            return KIcon(TYPE2ICON[item.type.lower()]).pixmap(16, 16)

        if role == Qt.DisplayRole:
            call_def = self.script.get_in_function_call()
            if call_def:
                before, after = _func_param_strings(call_def)

            if col == CCM.Prefix:
                return before if call_def else None
            elif col == CCM.Name:
                return item.word
            elif col == CCM.Postfix:
                return after if call_def else item.description
            #elif col == CCM.Arguments:
                # TODO: what could we use it for?
        elif col == CCM.Name:
            return self.roles.get(role)