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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/*
* KDevelop Generic Code Completion Support
*
* Copyright 2006-2008 Hamish Rodda <rodda@kde.org>
* Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "codecompletionmodel.h"
#include <QThread>
#include <KTextEditor/View>
#include <KTextEditor/Document>
#include "../duchain/declaration.h"
#include "../duchain/classfunctiondeclaration.h"
#include "../duchain/ducontext.h"
#include "../duchain/duchain.h"
#include "../duchain/namespacealiasdeclaration.h"
#include "../duchain/parsingenvironment.h"
#include "../duchain/duchainlock.h"
#include "../duchain/duchainbase.h"
#include "../duchain/topducontext.h"
#include "../duchain/duchainutils.h"
#include "../interfaces/quickopendataprovider.h"
#include "../interfaces/icore.h"
#include "../interfaces/ilanguagecontroller.h"
#include "../interfaces/icompletionsettings.h"
#include <debug.h>
#include "codecompletionworker.h"
#include "codecompletioncontext.h"
#include <duchain/specializationstore.h>
using namespace KTextEditor;
//Multi-threaded completion creates some multi-threading related crashes, and sometimes shows the completions in the wrong position if the cursor was moved
// #define SINGLE_THREADED_COMPLETION
namespace KDevelop {
class CompletionWorkerThread
: public QThread
{
Q_OBJECT
public:
explicit CompletionWorkerThread(CodeCompletionModel* model)
: QThread(model)
, m_model(model)
, m_worker(m_model->createCompletionWorker())
{
Q_ASSERT(m_worker->parent() == nullptr); // Must be null, else we cannot change the thread affinity!
m_worker->moveToThread(this);
Q_ASSERT(m_worker->thread() == this);
}
~CompletionWorkerThread() override
{
delete m_worker;
}
void run() override
{
//We connect directly, so we can do the pre-grouping within the background thread
connect(m_worker, &CodeCompletionWorker::foundDeclarationsReal, m_model,
&CodeCompletionModel::foundDeclarations, Qt::QueuedConnection);
connect(m_model, &CodeCompletionModel::completionsNeeded, m_worker,
QOverload<const DUChainPointer<KDevelop::DUContext>&, const Cursor&, View*>::of(&CodeCompletionWorker::computeCompletions),
Qt::QueuedConnection);
connect(m_model, &CodeCompletionModel::doSpecialProcessingInBackground, m_worker,
&CodeCompletionWorker::doSpecialProcessing);
exec();
}
CodeCompletionModel* m_model;
CodeCompletionWorker* m_worker;
};
bool CodeCompletionModel::forceWaitForModel()
{
return m_forceWaitForModel;
}
void CodeCompletionModel::setForceWaitForModel(bool wait)
{
m_forceWaitForModel = wait;
}
CodeCompletionModel::CodeCompletionModel(QObject* parent)
: KTextEditor::CodeCompletionModel(parent)
, m_forceWaitForModel(false)
, m_fullCompletion(true)
, m_mutex(new QMutex)
, m_thread(nullptr)
{
qRegisterMetaType<KTextEditor::Cursor>();
}
void CodeCompletionModel::initialize()
{
if (!m_thread) {
m_thread = new CompletionWorkerThread(this);
#ifdef SINGLE_THREADED_COMPLETION
m_thread->m_worker = createCompletionWorker();
#endif
m_thread->start();
}
}
CodeCompletionModel::~CodeCompletionModel()
{
if (m_thread->m_worker)
m_thread->m_worker->abortCurrentCompletion();
m_thread->quit();
m_thread->wait();
delete m_thread;
delete m_mutex;
}
bool CodeCompletionModel::fullCompletion() const
{
return m_fullCompletion;
}
KDevelop::CodeCompletionWorker* CodeCompletionModel::worker() const
{
return m_thread->m_worker;
}
void CodeCompletionModel::clear()
{
beginResetModel();
m_completionItems.clear();
m_completionContext.reset();
endResetModel();
}
void CodeCompletionModel::completionInvokedInternal(KTextEditor::View* view, const KTextEditor::Range& range,
InvocationType invocationType, const QUrl& url)
{
Q_ASSERT(m_thread == worker()->thread());
Q_UNUSED(invocationType)
DUChainReadLocker lock(DUChain::lock(), 400);
if (!lock.locked()) {
qCDebug(LANGUAGE) << "could not lock du-chain in time";
return;
}
TopDUContext* top = DUChainUtils::standardContextForUrl(url);
if (!top) {
qCDebug(LANGUAGE) << "================== NO CONTEXT FOUND =======================";
beginResetModel();
m_completionItems.clear();
endResetModel();
qCDebug(LANGUAGE) << "Completion invoked for unknown context. Document:" << url << ", Known documents:" <<
DUChain::self()->documents();
return;
}
setCurrentTopContext(TopDUContextPointer(top));
RangeInRevision rangeInRevision = top->transformToLocalRevision(KTextEditor::Range(range));
qCDebug(LANGUAGE) << "completion invoked for context" << ( DUContext* )top;
if (top->parsingEnvironmentFile() &&
top->parsingEnvironmentFile()->modificationRevision() !=
ModificationRevision::revisionForFile(IndexedString(url.toString()))) {
qCDebug(LANGUAGE) << "Found context is not current.";
}
DUContextPointer thisContext;
{
qCDebug(LANGUAGE) << "apply specialization:" << range.start();
thisContext = SpecializationStore::self().applySpecialization(top->findContextAt(rangeInRevision.start), top);
if (thisContext) {
qCDebug(LANGUAGE) << "after specialization:" << thisContext->localScopeIdentifier().toString() <<
thisContext->rangeInCurrentRevision();
} else {
thisContext = top;
}
qCDebug(LANGUAGE) << "context is set to" << thisContext.data();
}
lock.unlock();
if (m_forceWaitForModel)
emit waitForReset();
emit completionsNeeded(thisContext, range.start(), view);
}
void CodeCompletionModel::completionInvoked(KTextEditor::View* view, const KTextEditor::Range& range,
InvocationType invocationType)
{
//If this triggers, initialize() has not been called after creation.
Q_ASSERT(m_thread);
KDevelop::ICompletionSettings::CompletionLevel level =
KDevelop::ICore::self()->languageController()->completionSettings()->completionLevel();
if (level == KDevelop::ICompletionSettings::AlwaysFull ||
(invocationType != AutomaticInvocation && level == KDevelop::ICompletionSettings::MinimalWhenAutomatic))
m_fullCompletion = true;
else
m_fullCompletion = false;
//Only use grouping in full completion mode
setHasGroups(m_fullCompletion);
Q_UNUSED(invocationType)
if (!worker()) {
qCWarning(LANGUAGE) << "Completion invoked on a completion model which has no code completion worker assigned!";
}
beginResetModel();
m_completionItems.clear();
endResetModel();
worker()->abortCurrentCompletion();
worker()->setFullCompletion(m_fullCompletion);
QUrl url = view->document()->url();
completionInvokedInternal(view, range, invocationType, url);
}
void CodeCompletionModel::foundDeclarations(const QList<QExplicitlySharedDataPointer<CompletionTreeElement>>& items,
const QExplicitlySharedDataPointer<CodeCompletionContext>& completionContext)
{
m_completionContext = completionContext;
if (m_completionItems.isEmpty() && items.isEmpty()) {
if (m_forceWaitForModel) {
// TODO KF5: Check if this actually works
beginResetModel();
endResetModel(); //If we need to reset the model, reset it
}
return; //We don't need to reset, which is bad for target model
}
beginResetModel();
m_completionItems = items;
endResetModel();
if (m_completionContext) {
qCDebug(LANGUAGE) << "got completion-context with " << m_completionContext->ungroupedElements().size() <<
"ungrouped elements";
}
}
KTextEditor::CodeCompletionModelControllerInterface::MatchReaction CodeCompletionModel::matchingItem(const QModelIndex& /*matched*/)
{
return None;
}
void CodeCompletionModel::setCompletionContext(
const QExplicitlySharedDataPointer<CodeCompletionContext>& completionContext)
{
QMutexLocker lock(m_mutex);
m_completionContext = completionContext;
if (m_completionContext) {
qCDebug(LANGUAGE) << "got completion-context with " << m_completionContext->ungroupedElements().size() <<
"ungrouped elements";
}
}
QExplicitlySharedDataPointer<CodeCompletionContext> CodeCompletionModel::completionContext() const
{
QMutexLocker lock(m_mutex);
return m_completionContext;
}
void CodeCompletionModel::executeCompletionItem(View* view, const KTextEditor::Range& word,
const QModelIndex& index) const
{
//We must not lock the duchain at this place, because the items might rely on that
auto* element = static_cast<CompletionTreeElement*>(index.internalPointer());
if (!element || !element->asItem())
return;
element->asItem()->execute(view, word);
}
QExplicitlySharedDataPointer<KDevelop::CompletionTreeElement> CodeCompletionModel::itemForIndex(const QModelIndex& index)
const
{
auto* element = static_cast<CompletionTreeElement*>(index.internalPointer());
return QExplicitlySharedDataPointer<KDevelop::CompletionTreeElement>(element);
}
QVariant CodeCompletionModel::data(const QModelIndex& index, int role) const
{
if (role == Qt::TextAlignmentRole && index.column() == 0) {
return Qt::AlignRight;
}
auto element = static_cast<const CompletionTreeElement*>(index.internalPointer());
if (!element)
return QVariant();
if (role == CodeCompletionModel::GroupRole) {
if (element->asNode()) {
return QVariant(element->asNode()->role);
} else {
qCDebug(LANGUAGE) << "Requested group-role from leaf tree element";
return QVariant();
}
} else {
if (element->asNode()) {
if (role == CodeCompletionModel::InheritanceDepth) {
auto customGroupNode = dynamic_cast<const CompletionCustomGroupNode*>(element);
if (customGroupNode)
return QVariant(customGroupNode->inheritanceDepth);
}
if (role == element->asNode()->role) {
return element->asNode()->roleValue;
} else {
return QVariant();
}
}
}
if (!element->asItem()) {
qCWarning(LANGUAGE) << "Error in completion model";
return QVariant();
}
//Navigation widget interaction is done here, the other stuff is done within the tree-elements
switch (role) {
case CodeCompletionModel::InheritanceDepth:
return element->asItem()->inheritanceDepth();
case CodeCompletionModel::ArgumentHintDepth:
return element->asItem()->argumentHintDepth();
case CodeCompletionModel::ItemSelected: {
DeclarationPointer decl = element->asItem()->declaration();
if (decl) {
DUChain::self()->emitDeclarationSelected(decl);
}
break;
}
}
//In minimal completion mode, hide all columns except the "name" one
if (!m_fullCompletion && role == Qt::DisplayRole && index.column() != Name &&
(element->asItem()->argumentHintDepth() == 0 || index.column() == Prefix))
return QVariant();
//In reduced completion mode, don't show information text with the selected items
if (role == ItemSelected &&
(!m_fullCompletion ||
!ICore::self()->languageController()->completionSettings()->showMultiLineSelectionInformation()))
return QVariant();
return element->asItem()->data(index, role, this);
}
KDevelop::TopDUContextPointer CodeCompletionModel::currentTopContext() const
{
return m_currentTopContext;
}
void CodeCompletionModel::setCurrentTopContext(const KDevelop::TopDUContextPointer& topContext)
{
m_currentTopContext = topContext;
}
QModelIndex CodeCompletionModel::index(int row, int column, const QModelIndex& parent) const
{
if (parent.isValid()) {
auto* element = static_cast<CompletionTreeElement*>(parent.internalPointer());
CompletionTreeNode* node = element->asNode();
if (!node) {
qCDebug(LANGUAGE) << "Requested sub-index of leaf node";
return QModelIndex();
}
if (row < 0 || row >= node->children.count() || column < 0 || column >= ColumnCount)
return QModelIndex();
return createIndex(row, column, node->children[row].data());
} else {
if (row < 0 || row >= m_completionItems.count() || column < 0 || column >= ColumnCount)
return QModelIndex();
return createIndex(row, column, const_cast<CompletionTreeElement*>(m_completionItems[row].data()));
}
}
QModelIndex CodeCompletionModel::parent(const QModelIndex& index) const
{
if (rowCount() == 0)
return QModelIndex();
if (index.isValid()) {
auto* element = static_cast<CompletionTreeElement*>(index.internalPointer());
if (element->parent())
return createIndex(element->rowInParent(), element->columnInParent(), element->parent());
}
return QModelIndex();
}
int CodeCompletionModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
auto* element = static_cast<CompletionTreeElement*>(parent.internalPointer());
CompletionTreeNode* node = element->asNode();
if (!node)
return 0;
return node->children.count();
} else {
return m_completionItems.count();
}
}
QString CodeCompletionModel::filterString(KTextEditor::View* view, const KTextEditor::Range& range,
const KTextEditor::Cursor& position)
{
m_filterString = KTextEditor::CodeCompletionModelControllerInterface::filterString(view, range, position);
return m_filterString;
}
}
#include "moc_codecompletionmodel.cpp"
#include "codecompletionmodel.moc"
|