summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Cullmann <cullmann@kde.org>2014-01-18 21:12:29 (GMT)
committerChristoph Cullmann <cullmann@kde.org>2014-01-18 21:12:36 (GMT)
commit59b824fded498cc6860f2ded302e71e5255b6af0 (patch)
treed54dd5cb07427f10bf6d0bbd20691304c3580cf7
parent1cd86b59bf1189506b252ff71c02f4bfa93b4ee0 (diff)
fold code completion model2
-rw-r--r--autotests/src/vimode_test.cpp19
-rw-r--r--autotests/src/vimode_test.h2
-rw-r--r--src/completion/katecompletionwidget.cpp12
-rw-r--r--src/completion/katewordcompletion.cpp16
-rw-r--r--src/completion/katewordcompletion.h4
-rw-r--r--src/include/ktexteditor/codecompletionmodel.h48
-rw-r--r--src/src/codecompletionmodel.cpp14
7 files changed, 37 insertions, 78 deletions
diff --git a/autotests/src/vimode_test.cpp b/autotests/src/vimode_test.cpp
index 38f4581..871506d 100644
--- a/autotests/src/vimode_test.cpp
+++ b/autotests/src/vimode_test.cpp
@@ -170,13 +170,14 @@ QVariant FakeCodeCompletionTestModel::data(const QModelIndex &index, int role) c
}
return QVariant();
}
-void FakeCodeCompletionTestModel::executeCompletionItem(Document *document, const Range &word, int row) const
+
+void FakeCodeCompletionTestModel::executeCompletionItem (KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const
{
- qDebug() << "word: " << word << "(" << document->text(word) << ")";
+ qDebug() << "word: " << word << "(" << view->document()->text(word) << ")";
const Cursor origCursorPos = m_kateView->cursorPosition();
- const QString textToInsert = m_completions[row];
- const QString textAfterCursor = document->text(Range(word.end(), Cursor(word.end().line(), document->lineLength(word.end().line()))));
- document->removeText(Range(word.start(), origCursorPos));
+ const QString textToInsert = m_completions[index.row()];
+ const QString textAfterCursor = view->document()->text(Range(word.end(), Cursor(word.end().line(), view->document()->lineLength(word.end().line()))));
+ view->document()->removeText(Range(word.start(), origCursorPos));
const int lengthStillToRemove = word.end().column() - origCursorPos.column();
QString actualTextInserted = textToInsert;
// Merge brackets?
@@ -196,7 +197,7 @@ void FakeCodeCompletionTestModel::executeCompletionItem(Document *document, cons
}
const bool mergeOpenBracketWithExisting = (openingBracketPos != -1) && !endedWithSemiColon;
// Add the function name, for now: we don't yet know if we'll be adding the "()", too.
- document->insertText(word.start(), justFunctionName);
+ view->document()->insertText(word.start(), justFunctionName);
if (mergeOpenBracketWithExisting) {
// Merge with opening bracket.
actualTextInserted = justFunctionName;
@@ -204,7 +205,7 @@ void FakeCodeCompletionTestModel::executeCompletionItem(Document *document, cons
} else {
// Don't merge.
const QString afterFunctionName = endedWithSemiColon ? "();" : "()";
- document->insertText(Cursor(word.start().line(), word.start().column() + justFunctionName.length()), afterFunctionName);
+ view->document()->insertText(Cursor(word.start().line(), word.start().column() + justFunctionName.length()), afterFunctionName);
if (takesArgument) {
// Place the cursor immediately after the opening "(" we just added.
m_kateView->setCursorPosition(Cursor(word.start().line(), word.start().column() + justFunctionName.length() + 1));
@@ -212,13 +213,13 @@ void FakeCodeCompletionTestModel::executeCompletionItem(Document *document, cons
}
} else {
// Plain text.
- document->insertText(word.start(), textToInsert);
+ view->document()->insertText(word.start(), textToInsert);
}
if (m_removeTailOnCompletion) {
const int tailLength = word.end().column() - origCursorPos.column();
const Cursor tailStart = Cursor(word.start().line(), word.start().column() + actualTextInserted.length());
const Cursor tailEnd = Cursor(tailStart.line(), tailStart.column() + tailLength);
- document->removeText(Range(tailStart, tailEnd));
+ view->document()->removeText(Range(tailStart, tailEnd));
}
}
diff --git a/autotests/src/vimode_test.h b/autotests/src/vimode_test.h
index 6117201..6210eff 100644
--- a/autotests/src/vimode_test.h
+++ b/autotests/src/vimode_test.h
@@ -118,7 +118,7 @@ public:
void forceInvocationIfDocTextIs(const QString &desiredDocText);
void doNotForceInvocation();;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
- virtual void executeCompletionItem(KTextEditor::Document *document, const KTextEditor::Range &word, int row) const;
+ virtual void executeCompletionItem (KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const;
KTextEditor::CodeCompletionInterface *cc() const;
private:
void failTest() const;
diff --git a/src/completion/katecompletionwidget.cpp b/src/completion/katecompletionwidget.cpp
index 3228365..1d2db31 100644
--- a/src/completion/katecompletionwidget.cpp
+++ b/src/completion/katecompletionwidget.cpp
@@ -870,20 +870,10 @@ void KateCompletionWidget::execute()
KTextEditor::CodeCompletionModel *model = static_cast<KTextEditor::CodeCompletionModel *>(const_cast<QAbstractItemModel *>(toExecute.model()));
Q_ASSERT(model);
- KTextEditor::CodeCompletionModel2 *model2 = qobject_cast<KTextEditor::CodeCompletionModel2 *>(model);
-
Q_ASSERT(m_completionRanges.contains(model));
KTextEditor::Cursor start = m_completionRanges[model].range->start();
- if (model2) {
- model2->executeCompletionItem2(view()->document(), *m_completionRanges[model].range, toExecute);
- } else if (toExecute.parent().isValid())
- //The normale CodeCompletionInterface cannot handle feedback for hierarchical models, so just do the replacement
- {
- view()->document()->replaceText(*m_completionRanges[model].range, model->data(toExecute.sibling(toExecute.row(), KTextEditor::CodeCompletionModel::Name)).toString());
- } else {
- model->executeCompletionItem(view()->document(), *m_completionRanges[model].range, toExecute.row());
- }
+ model->executeCompletionItem(view(), *m_completionRanges[model].range, toExecute);
view()->doc()->editEnd();
m_completionEditRunning = false;
diff --git a/src/completion/katewordcompletion.cpp b/src/completion/katewordcompletion.cpp
index c91dc03..b7b60cc 100644
--- a/src/completion/katewordcompletion.cpp
+++ b/src/completion/katewordcompletion.cpp
@@ -60,7 +60,8 @@ static const int autoInvocationMaxFilesize = 1000000;
//BEGIN KateWordCompletionModel
KateWordCompletionModel::KateWordCompletionModel(QObject *parent)
- : CodeCompletionModel2(parent), m_automatic(false)
+ : CodeCompletionModel (parent)
+ , m_automatic(false)
{
setHasGroups(false);
}
@@ -237,16 +238,15 @@ QStringList KateWordCompletionModel::allMatches(KTextEditor::View *view, const K
return result.values();
}
-void KateWordCompletionModel::executeCompletionItem2(
- KTextEditor::Document *document
+void KateWordCompletionModel::executeCompletionItem (KTextEditor::View *view
, const KTextEditor::Range &word
, const QModelIndex &index
) const
{
- KateView *v = qobject_cast<KateView *> (document->activeView());
+ KateView *v = qobject_cast<KateView *> (view);
if (v->config()->wordCompletionRemoveTail()) {
int tailStart = word.end().column();
- const QString &line = document->line(word.end().line());
+ const QString &line = view->document()->line(word.end().line());
int tailEnd = line.length();
for (int i = word.end().column(); i < tailEnd; ++i) {
// Letters, numbers and underscore are part of a word!
@@ -263,12 +263,12 @@ void KateWordCompletionModel::executeCompletionItem2(
KTextEditor::Range tail(KTextEditor::Cursor(word.start().line(), tailStart), KTextEditor::Cursor(word.end().line(), tailEnd));
- document->replaceText(word, m_matches.at(index.row()));
+ view->document()->replaceText(word, m_matches.at(index.row()));
v->doc()->editEnd();
v->doc()->editStart();
- document->replaceText(tail, QString());
+ view->document()->replaceText(tail, QString());
} else {
- document->replaceText(word, m_matches.at(index.row()));
+ view->document()->replaceText(word, m_matches.at(index.row()));
}
}
diff --git a/src/completion/katewordcompletion.h b/src/completion/katewordcompletion.h
index 6998fac..9ac39e8 100644
--- a/src/completion/katewordcompletion.h
+++ b/src/completion/katewordcompletion.h
@@ -34,7 +34,7 @@
#include "katepartdebug.h"
#include <ktexteditor_export.h>
-class KTEXTEDITOR_EXPORT KateWordCompletionModel : public KTextEditor::CodeCompletionModel2, public KTextEditor::CodeCompletionModelControllerInterface
+class KTEXTEDITOR_EXPORT KateWordCompletionModel : public KTextEditor::CodeCompletionModel, public KTextEditor::CodeCompletionModelControllerInterface
{
Q_OBJECT
Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
@@ -73,7 +73,7 @@ public:
QStringList allMatches(KTextEditor::View *view, const KTextEditor::Range &range) const;
- virtual void executeCompletionItem2(KTextEditor::Document *document, const KTextEditor::Range &word, const QModelIndex &index) const;
+ virtual void executeCompletionItem (KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const;
private:
QStringList m_matches;
diff --git a/src/include/ktexteditor/codecompletionmodel.h b/src/include/ktexteditor/codecompletionmodel.h
index aa321a2..8477ecd 100644
--- a/src/include/ktexteditor/codecompletionmodel.h
+++ b/src/include/ktexteditor/codecompletionmodel.h
@@ -384,20 +384,19 @@ public:
* @param invocationType How the code completion was triggered
* */
virtual void completionInvoked(KTextEditor::View *view, const KTextEditor::Range &range, InvocationType invocationType);
+
/**
- * @deprecated This does not work if your model is hierarchical(@see GroupRole). Use CodeCompletionModel2::executeCompletionItem2 instead.
- *
- * This function is responsible for inserting a selected completion into the
- * document. The default implementation replaces the text that the completions
- * were based on with the Qt::DisplayRole of the Name column of the given match.
- *
- * @param document The document to insert the completion into
- * @param word The Range that the completions are based on (what the user entered
- * so far)
- * @param row The row of the completion match to insert
- * */
- virtual void executeCompletionItem(Document *document, const Range &word, int row) const;
-
+ * This function is responsible for inserting a selected completion into the
+ * view. The default implementation replaces the text that the completions
+ * were based on with the Qt::DisplayRole of the Name column of the given match.
+ *
+ * @param view the view to insert the completion into
+ * @param word the Range that the completions are based on (what the user entered
+ * so far)
+ * @param index identifies the completion match to insert
+ * */
+ virtual void executeCompletionItem (KTextEditor::View *view, const Range &word, const QModelIndex &index) const;
+
// Reimplementations
/**
* Reimplemented from QAbstractItemModel::columnCount(). The default implementation
@@ -471,29 +470,6 @@ private:
class CodeCompletionModelPrivate *const d;
};
-/**
- * You must inherit your completion-model from CodeCompletionModel2 if you want to
- * use a hierarchical structure and want to receive execution-feedback.
- * @see CodeCompletionModel::GroupRole
- * */
-class KTEXTEDITOR_EXPORT CodeCompletionModel2 : public CodeCompletionModel
-{
- Q_OBJECT
-public:
- CodeCompletionModel2(QObject *parent);
- /**
- * This function is responsible for inserting a selected completion into the
- * document. The default implementation replaces the text that the completions
- * were based on with the Qt::DisplayRole of the Name column of the given match.
- *
- * @param document the document to insert the completion into
- * @param word the Range that the completions are based on (what the user entered
- * so far)
- * @param index identifies the completion match to insert
- * */
- virtual void executeCompletionItem2(Document *document, const Range &word, const QModelIndex &index) const;
-};
-
Q_DECLARE_OPERATORS_FOR_FLAGS(CodeCompletionModel::CompletionProperties)
Q_DECLARE_OPERATORS_FOR_FLAGS(CodeCompletionModel::HighlightMethods)
diff --git a/src/src/codecompletionmodel.cpp b/src/src/codecompletionmodel.cpp
index 4a211c8..f7519e3 100644
--- a/src/src/codecompletionmodel.cpp
+++ b/src/src/codecompletionmodel.cpp
@@ -20,6 +20,7 @@
#include "codecompletionmodel.h"
#include "document.h"
+#include "view.h"
using namespace KTextEditor;
@@ -99,9 +100,9 @@ void CodeCompletionModel::completionInvoked(KTextEditor::View *view, const Range
Q_UNUSED(invocationType)
}
-void CodeCompletionModel::executeCompletionItem(Document *document, const Range &word, int row) const
+void CodeCompletionModel::executeCompletionItem (KTextEditor::View *view, const Range &word, const QModelIndex &index) const
{
- document->replaceText(word, data(index(row, Name, QModelIndex())).toString());
+ view->document()->replaceText(word, data(index.sibling(index.row(), Name)).toString());
}
bool CodeCompletionModel::hasGroups() const
@@ -117,13 +118,4 @@ void CodeCompletionModel::setHasGroups(bool hasGroups)
}
}
-CodeCompletionModel2::CodeCompletionModel2(QObject *parent) : CodeCompletionModel(parent)
-{
-}
-
-void CodeCompletionModel2::executeCompletionItem2(Document *document, const Range &word, const QModelIndex &index) const
-{
- document->replaceText(word, data(index.sibling(index.row(), Name)).toString());
-}
-
#include "codecompletionmodel.moc"