File: autocomplete.py

package info (click to toggle)
turing 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,340 kB
  • sloc: python: 106,582; xml: 101; makefile: 53; sh: 29
file content (51 lines) | stat: -rw-r--r-- 1,925 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8 -*-
""" Contains the python autocomplete mode """
from pyqode.core.api import TextHelper
from pyqode.core.modes import AutoCompleteMode


class PyAutoCompleteMode(AutoCompleteMode):
    """ Auto complete parentheses and method definitions.

    Extends :class:`pyqode.core.modes.AutoCompleteMode` to add
    support for method/function call:

        - function completion adds "):" to the function definition.
        - method completion adds "self):" to the method definition.
    """
    def _in_method_call(self):
        helper = TextHelper(self.editor)
        line_nbr = helper.current_line_nbr() - 1
        expected_indent = helper.line_indent() - 4
        while line_nbr >= 0:
            text = helper.line_text(line_nbr)
            indent = len(text) - len(text.lstrip())
            if indent == expected_indent and 'class' in text:
                return True
            line_nbr -= 1
        return False

    def _handle_fct_def(self):
        if self._in_method_call():
            th = TextHelper(self.editor)
            if '@classmethod' in th.line_text(th.current_line_nbr() - 1):
                txt = "cls):"
            else:
                txt = "self):"
        else:
            txt = "):"
        cursor = self.editor.textCursor()
        cursor.insertText(txt)
        cursor.movePosition(cursor.Left, cursor.MoveAnchor, 2)
        self.editor.setTextCursor(cursor)

    def _on_post_key_pressed(self, event):
        # if we are in disabled cc, use the parent implementation
        helper = TextHelper(self.editor)
        if (event.text() == "(" and
                helper.current_line_text().lstrip().startswith("def ")):
            self._handle_fct_def()
        else:
            line = TextHelper(self.editor).current_line_text().strip()
            if not line.endswith(('"""', "'''")):
                super(PyAutoCompleteMode, self)._on_post_key_pressed(event)