aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/codeassist/textdocumentmanipulator.cpp
blob: 793885d7c3a2435ec54bc7332aa7d29a8dbb6950 (plain)
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "textdocumentmanipulator.h"

#include <texteditor/texteditor.h>
#include <texteditor/textdocument.h>

namespace TextEditor {

TextDocumentManipulator::TextDocumentManipulator(TextEditorWidget *textEditorWidget)
    : m_textEditorWidget(textEditorWidget)
{
}

int TextDocumentManipulator::currentPosition() const
{
    return m_textEditorWidget->position();
}

int TextDocumentManipulator::positionAt(TextPositionOperation textPositionOperation) const
{
    return m_textEditorWidget->position(textPositionOperation);
}

QChar TextDocumentManipulator::characterAt(int position) const
{
    return m_textEditorWidget->characterAt(position);
}

QString TextDocumentManipulator::textAt(int position, int length) const
{
    return m_textEditorWidget->textAt(position, length);
}

QTextCursor TextDocumentManipulator::textCursorAt(int position) const
{
    auto cursor = m_textEditorWidget->textCursor();
    cursor.setPosition(position);

    return cursor;
}

void TextDocumentManipulator::setCursorPosition(int position)
{
    m_textEditorWidget->setCursorPosition(position);
}

void TextDocumentManipulator::setAutoCompleteSkipPosition(int position)
{
    QTextCursor cursor = m_textEditorWidget->textCursor();
    cursor.setPosition(position);
    m_textEditorWidget->setAutoCompleteSkipPosition(cursor);
}

bool TextDocumentManipulator::replace(int position, int length, const QString &text)
{
    bool textWillBeReplaced = textIsDifferentAt(position, length, text);

    if (textWillBeReplaced)
        replaceWithoutCheck(position, length, text);

    return textWillBeReplaced;
}

void TextDocumentManipulator::insertCodeSnippet(int position,
                                                const QString &text,
                                                const SnippetParser &parse)
{
    auto cursor = m_textEditorWidget->textCursor();
    cursor.setPosition(position, QTextCursor::KeepAnchor);
    m_textEditorWidget->insertCodeSnippet(cursor, text, parse);
}

void TextDocumentManipulator::paste()
{
    m_textEditorWidget->paste();
}

void TextDocumentManipulator::encourageApply()
{
    m_textEditorWidget->encourageApply();
}

namespace {

bool hasOnlyBlanksBeforeCursorInLine(QTextCursor textCursor)
{
    textCursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);

    const auto textBeforeCursor = textCursor.selectedText();

    const auto nonSpace = std::find_if(textBeforeCursor.cbegin(),
                                       textBeforeCursor.cend(),
                                       [] (const QChar &signBeforeCursor) {
        return !signBeforeCursor.isSpace();
    });

    return nonSpace == textBeforeCursor.cend();
}

}

void TextDocumentManipulator::autoIndent(int position, int length)
{
    auto cursor = m_textEditorWidget->textCursor();
    cursor.setPosition(position);
    if (hasOnlyBlanksBeforeCursorInLine(cursor)) {
        cursor.setPosition(position + length, QTextCursor::KeepAnchor);

        m_textEditorWidget->textDocument()->autoIndent(cursor);
    }
}

bool TextDocumentManipulator::textIsDifferentAt(int position, int length, const QString &text) const
{
    const auto textToBeReplaced = m_textEditorWidget->textAt(position, length);

    return text != textToBeReplaced;
}

void TextDocumentManipulator::replaceWithoutCheck(int position, int length, const QString &text)
{
    auto cursor = m_textEditorWidget->textCursor();
    cursor.setPosition(position);
    cursor.setPosition(position + length, QTextCursor::KeepAnchor);
    cursor.insertText(text);
}

} // namespace TextEditor