2
* Copyright 2014 David Herberth kde@dav1d.de
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.
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.
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/>.
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>
29
LumenCompletionModel::LumenCompletionModel(QObject* parent, DCD* dcd): CodeCompletionModel2(parent)
34
LumenCompletionModel::~LumenCompletionModel()
39
bool LumenCompletionModel::shouldStartCompletion(View* view, const QString& insertedText, bool userInsertion, const Cursor& position)
41
bool complete = KTextEditor::CodeCompletionModelControllerInterface3::shouldStartCompletion(
42
view, insertedText, userInsertion, position
45
complete = complete || insertedText.endsWith("("); // calltip
46
complete = complete || insertedText.endsWith("import "); // import
51
void LumenCompletionModel::completionInvoked(View* view, const Range& range, CodeCompletionModel::InvocationType invocationType)
53
Q_UNUSED(invocationType);
54
KTextEditor::Document* document = view->document();
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());
66
m_data = m_dcd->complete(utf8, offset);
67
setRowCount(m_data.completions.length());
72
void LumenCompletionModel::executeCompletionItem2(Document* document, const Range& word, const QModelIndex& index) const
74
QModelIndex sibling = index.sibling(index.row(), Name);
75
KTextEditor::View* view = document->activeView();
77
document->replaceText(word, data(sibling).toString());
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));
87
QVariant LumenCompletionModel::data(const QModelIndex& index, int role) const
89
DCDCompletionItem item = m_data.completions[index.row()];
93
case Qt::DecorationRole:
95
if(index.column() == Icon) {
100
case Qt::DisplayRole:
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();
107
switch(index.column()) {
108
case Prefix: return matches[1];
109
case Name: return matches[2];
112
if(index.column() == Name) {
122
case DCDCompletionItemType::FunctionName: p |= Function; break;
123
case DCDCompletionItemType::VariableName: p |= Variable; break;
128
case BestMatchesCount:
132
case ArgumentHintDepth:
134
if(item.type == DCDCompletionItemType::Calltip) {
145
// I like the green arrow
148
case ExpandingWidget:
150
// TODO well implementation in DCD is missing
158
#include "completion.moc"
b'\\ No newline at end of file'