editor_utils.py 2.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
import re;

# places the cursor inside the given editor into the given line
# (leading and trailing whitespaces are ignored!)
# and goes to the end of the line
# line can be a regex - but if so, remember to set isRegex to True
# the function returns True if this went fine, False on error
def placeCursorToLine(editor,line,isRegex=False):
    cursor = editor.textCursor()
    oldPosition = 0
    cursor.setPosition(oldPosition)
12
    editor.setTextCursor(cursor)
13
14
15
    found = False
    if isRegex:
        regex = re.compile(line)
16
    editorRealName = objectMap.realName(editor)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    while not found:
        currentLine = str(lineUnderCursor(editor)).strip()
        if isRegex:
            if regex.match(currentLine):
                found = True
            else:
                type(editor, "<Down>")
                if oldPosition==editor.textCursor().position():
                    break
                oldPosition = editor.textCursor().position()
        else:
            if currentLine==line:
                found = True
            else:
31
                type(editorRealName, "<Down>")
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
                if oldPosition==editor.textCursor().position():
                    break
                oldPosition = editor.textCursor().position()
    if not found:
        test.fatal("Couldn't find line matching\n\n%s\n\nLeaving test..." % line)
        return False
    cursor=editor.textCursor()
    cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.MoveAnchor)
    editor.setTextCursor(cursor)
    return True

# this function simply opens the context menu inside the given editor
# at the same position where the text cursor is located at
def openContextMenuOnTextCursorPosition(editor):
    rect = editor.cursorRect(editor.textCursor())
    openContextMenu(editor, rect.x+rect.width/2, rect.y+rect.height/2, 0)

# this function marks/selects the text inside the given editor from position
# startPosition to endPosition (both inclusive)
def markText(editor, startPosition, endPosition):
    cursor = editor.textCursor()
    cursor.setPosition(startPosition)
    cursor.movePosition(QTextCursor.StartOfLine)
    editor.setTextCursor(cursor)
    cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor, endPosition-startPosition)
    cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
    cursor.setPosition(endPosition, QTextCursor.KeepAnchor)
    editor.setTextCursor(cursor)

61
62
63
64
65
# works for all standard editors
def replaceEditorContent(editor, newcontent):
    type(editor, "<Ctrl+A>")
    type(editor, "<Delete>")
    type(editor, newcontent)
66
67
68
69
70
71
72
73
74
75

def typeLines(editor, lines):
    if isinstance(lines, (str, unicode)):
        lines = [lines]
    if isinstance(lines, (list, tuple)):
        for line in lines:
            type(editor, line)
            type(editor, "<Enter>")
    else:
        test.warning("Illegal parameter passed to typeLines()")