~ubuntu-branches/ubuntu/trusty/kate/trusty

« back to all changes in this revision

Viewing changes to addons/ktexteditor/lumen/completion.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell, Rohan Garg, Jonathan Riddell, Philip Muškovac
  • Date: 2014-03-19 10:38:16 UTC
  • mfrom: (1.1.42)
  • Revision ID: package-import@ubuntu.com-20140319103816-f5b5t0m6hnwclzcn
Tags: 4:4.12.90-0ubuntu1
[ Rohan Garg ]
* Update install files

[ Jonathan Riddell ]
* New upstream beta release

[ Philip Muškovac ]
* Override license-problem-json-evil for js_lint.py as the file only
  references the evil file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014  David Herberth kde@dav1d.de
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) version 3, or any
 
8
 * later version accepted by the membership of KDE e.V. (or its
 
9
 * successor approved by the membership of KDE e.V.), which shall
 
10
 * act as a proxy defined in Section 6 of version 3 of the license.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
19
**/
 
20
 
 
21
#include "completion.h"
 
22
#include <ktexteditor/view.h>
 
23
#include <ktexteditor/document.h>
 
24
#include <ktexteditor/codecompletioninterface.h>
 
25
#include <ktexteditor/codecompletionmodelcontrollerinterface.h>
 
26
#include <klocalizedstring.h>
 
27
 
 
28
 
 
29
LumenCompletionModel::LumenCompletionModel(QObject* parent, DCD* dcd): CodeCompletionModel2(parent)
 
30
{
 
31
    m_dcd = dcd;
 
32
}
 
33
 
 
34
LumenCompletionModel::~LumenCompletionModel()
 
35
{
 
36
 
 
37
}
 
38
 
 
39
bool LumenCompletionModel::shouldStartCompletion(View* view, const QString& insertedText, bool userInsertion, const Cursor& position)
 
40
{
 
41
    bool complete = KTextEditor::CodeCompletionModelControllerInterface3::shouldStartCompletion(
 
42
        view, insertedText, userInsertion, position
 
43
    );
 
44
 
 
45
    complete = complete || insertedText.endsWith("("); // calltip
 
46
    complete = complete || insertedText.endsWith("import "); // import
 
47
 
 
48
    return complete;
 
49
}
 
50
 
 
51
void LumenCompletionModel::completionInvoked(View* view, const Range& range, CodeCompletionModel::InvocationType invocationType)
 
52
{
 
53
    Q_UNUSED(invocationType);
 
54
    KTextEditor::Document* document = view->document();
 
55
 
 
56
    KTextEditor::Cursor cursor = range.end();
 
57
    KTextEditor::Cursor cursorEnd = document->documentEnd();
 
58
    KTextEditor::Range range0c = KTextEditor::Range(0, 0, cursor.line(), cursor.column());
 
59
    KTextEditor::Range rangece = KTextEditor::Range(cursor.line(), cursor.column(),
 
60
                                                    cursorEnd.line(), cursorEnd.column());
 
61
    QString text0c = document->text(range0c, false);
 
62
    QByteArray utf8 = text0c.toUtf8();
 
63
    int offset = utf8.length();
 
64
    utf8.append(document->text(rangece, false).toUtf8());
 
65
 
 
66
    m_data = m_dcd->complete(utf8, offset);
 
67
    setRowCount(m_data.completions.length());
 
68
 
 
69
    setHasGroups(false);
 
70
}
 
71
 
 
72
void LumenCompletionModel::executeCompletionItem2(Document* document, const Range& word, const QModelIndex& index) const
 
73
{
 
74
    QModelIndex sibling = index.sibling(index.row(), Name);
 
75
    KTextEditor::View* view = document->activeView();
 
76
 
 
77
    document->replaceText(word, data(sibling).toString());
 
78
 
 
79
    int crole = data(sibling, CompletionRole).toInt();
 
80
    if (crole & Function) {
 
81
        KTextEditor::Cursor cursor = document->activeView()->cursorPosition();
 
82
        document->insertText(cursor, QString("()"));
 
83
        view->setCursorPosition(Cursor(cursor.line(), cursor.column()+1));
 
84
    }
 
85
}
 
86
 
 
87
QVariant LumenCompletionModel::data(const QModelIndex& index, int role) const
 
88
{
 
89
    DCDCompletionItem item = m_data.completions[index.row()];
 
90
 
 
91
    switch (role)
 
92
    {
 
93
        case Qt::DecorationRole:
 
94
        {
 
95
            if(index.column() == Icon) {
 
96
                return item.icon();
 
97
            }
 
98
            break;
 
99
        }
 
100
        case Qt::DisplayRole:
 
101
        {
 
102
            if(item.type == DCDCompletionItemType::Calltip) {
 
103
                QRegExp funcRE("^\\s*(\\w+)\\s+(\\w+\\s*\\(.*\\))\\s*$");
 
104
                funcRE.indexIn(item.name);
 
105
                QStringList matches = funcRE.capturedTexts();
 
106
 
 
107
                switch(index.column()) {
 
108
                    case Prefix: return matches[1];
 
109
                    case Name: return matches[2];
 
110
                }
 
111
            } else {
 
112
                if(index.column() == Name) {
 
113
                    return item.name;
 
114
                }
 
115
            }
 
116
            break;
 
117
        }
 
118
        case CompletionRole:
 
119
        {
 
120
            int p = NoProperty;
 
121
            switch (item.type) {
 
122
                case DCDCompletionItemType::FunctionName: p |= Function; break;
 
123
                case DCDCompletionItemType::VariableName: p |= Variable; break;
 
124
                default: break;
 
125
            }
 
126
            return p;
 
127
        }
 
128
        case BestMatchesCount:
 
129
        {
 
130
            return 5;
 
131
        }
 
132
        case ArgumentHintDepth:
 
133
        {
 
134
            if(item.type == DCDCompletionItemType::Calltip) {
 
135
                return 1;
 
136
            }
 
137
            break;
 
138
        }
 
139
        case GroupRole:
 
140
        {
 
141
            break;
 
142
        }
 
143
        case IsExpandable:
 
144
        {
 
145
            // I like the green arrow
 
146
            return true;
 
147
        }
 
148
        case ExpandingWidget:
 
149
        {
 
150
            // TODO well implementation in DCD is missing
 
151
            return QVariant();
 
152
        }
 
153
    }
 
154
 
 
155
    return QVariant();
 
156
}
 
157
 
 
158
#include "completion.moc"
 
 
b'\\ No newline at end of file'