aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qt4projectmanager/profilecompletion.cpp
blob: 9d3f706d8a3ad0d1a75e66b820a243287ec6da5b (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "profilecompletion.h"
#include "profileeditor.h"
#include "profilekeywords.h"
#include <texteditor/itexteditor.h>
#include <texteditor/completionsettings.h>
#include <cplusplus/Icons.h>
#include <QDebug>

using namespace Qt4ProjectManager::Internal;

ProFileCompletion::ProFileCompletion(QObject *parent) :
    TextEditor::ICompletionCollector(parent),
    m_editor(0),
    m_startPosition(-1),
    m_variableIcon(CPlusPlus::Icons().iconForType(CPlusPlus::Icons::VarPublicIconType)),
    m_functionIcon(CPlusPlus::Icons().iconForType(CPlusPlus::Icons::FuncPublicIconType))
{
}

ProFileCompletion::~ProFileCompletion()
{

}

QList<TextEditor::CompletionItem> ProFileCompletion::getCompletions()
{
    QList<TextEditor::CompletionItem> completionItems;
    completions(&completionItems);

    return completionItems;
}

bool ProFileCompletion::shouldRestartCompletion()
{
    return false;
}

TextEditor::ITextEditable *ProFileCompletion::editor() const
{
    return m_editor;
}

int ProFileCompletion::startPosition() const
{
    return m_startPosition;
}

bool ProFileCompletion::supportsEditor(TextEditor::ITextEditable *editor)
{
    if (qobject_cast<ProFileEditorEditable *>(editor))
        return true;
    return false;
}

bool ProFileCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
{
    m_editor = editor;
    const int pos = editor->position();

    if (completionSettings().m_completionTrigger == TextEditor::AutomaticCompletion) {
        QChar characterUnderCursor = editor->characterAt(pos);
        if (!characterUnderCursor.isLetterOrNumber()) {
            m_startPosition = findStartOfName();
            if (pos - m_startPosition >= 3 && !isInComment())
                return true;
        }
    }
    return false;
}

int ProFileCompletion::findStartOfName(int pos) const
{
    if (pos == -1)
        pos = m_editor->position();
    QChar chr;

    // Skip to the start of a name
    do {
        chr = m_editor->characterAt(--pos);
    } while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));

    return pos + 1;
}

bool ProFileCompletion::isInComment() const
{
    const int beginOfLinePosition = m_editor->position(TextEditor::ITextEditor::StartOfLine);
    const QString lineBeginning = m_editor->textAt(beginOfLinePosition,
                                  m_startPosition - beginOfLinePosition);
    if (lineBeginning.contains(QLatin1Char('#')))
        return true;
    return false;
}

int ProFileCompletion::startCompletion(TextEditor::ITextEditable *editor)
{
    m_editor = editor;
    m_startPosition = findStartOfName();

    return m_startPosition;
}

void ProFileCompletion::completions(QList<TextEditor::CompletionItem> *completions)
{
    const int length = m_editor->position() - m_startPosition;
    if (length < 0)
        return;

    if (isInComment())
        return;

    const QString key = m_editor->textAt(m_startPosition, length);

    QList<TextEditor::CompletionItem> items;
    QStringList keywords = ProFileKeywords::variables()
            + ProFileKeywords::functions();
//    qSort(keywords);
    for (int i = 0; i < keywords.count(); i++) {
        TextEditor::CompletionItem item(this);
        item.text = keywords[i];
        item.data = QVariant::fromValue(item.text);
        item.icon = ProFileKeywords::isFunction(item.text)
                ? m_functionIcon : m_variableIcon;
        items.append(item);
    }

    filter(items, completions, key);
}

bool ProFileCompletion::typedCharCompletes(const TextEditor::CompletionItem &item, QChar typedChar)
{
    // only '(' in case of a function
    if (typedChar == QLatin1Char('(') && ProFileKeywords::isFunction(item.text))
        return true;
    return false;
}

void ProFileCompletion::complete(const TextEditor::CompletionItem &item, QChar typedChar)
{
    Q_UNUSED(typedChar)

    int replaceLength = m_editor->position() - m_startPosition;
    if (replaceLength < 0)
        return;

    QString toInsert = item.text;
    int cursorOffset = 0;
    if (ProFileKeywords::isFunction(toInsert)
            && completionSettings().m_autoInsertBrackets) {
        if (completionSettings().m_spaceAfterFunctionName) {
            if (m_editor->textAt(m_editor->position(), 2) == QLatin1String(" (")) {
                cursorOffset = 2;
            } else if (m_editor->characterAt(m_editor->position()) == QLatin1Char('(')
                       || m_editor->characterAt(m_editor->position()) == QLatin1Char(' ')) {
                replaceLength += 1;
                toInsert += QLatin1String(" (");
            } else {
                toInsert += QLatin1String(" ()");
                cursorOffset = -1;
            }
        } else {
            if (m_editor->characterAt(m_editor->position()) == QLatin1Char('(')) {
                cursorOffset = 1;
            } else {
                toInsert += QLatin1String("()");
                cursorOffset = -1;
            }
        }
    }

    m_editor->setCurPos(m_startPosition);
    m_editor->replace(replaceLength, toInsert);
    if (cursorOffset)
        m_editor->setCurPos(m_editor->position() + cursorOffset);
}

bool ProFileCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems)
{
    if (completionItems.count() == 1) {
        complete(completionItems.first(), QChar());
        return true;
    }

    return TextEditor::ICompletionCollector::partiallyComplete(completionItems);
}

void ProFileCompletion::cleanup()
{
}