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
|
/*
* 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 "codecompletionworker.h"
#include <KTextEditor/View>
#include <KTextEditor/Document>
#include <KLocalizedString>
#include "../duchain/ducontext.h"
#include "../duchain/duchainlock.h"
#include "../duchain/duchain.h"
#include <debug.h>
#include "codecompletion.h"
#include "codecompletionitem.h"
#include "codecompletionmodel.h"
#include "codecompletionitemgrouper.h"
#include <util/foregroundlock.h>
using namespace KTextEditor;
using namespace KDevelop;
CodeCompletionWorker::CodeCompletionWorker(KDevelop::CodeCompletionModel* model) :
m_hasFoundDeclarations(false)
, m_mutex(new QMutex())
, m_abort(false)
, m_fullCompletion(true)
, m_model(model)
{
}
CodeCompletionWorker::~CodeCompletionWorker()
{
delete m_mutex;
}
void CodeCompletionWorker::setFullCompletion(bool full)
{
m_fullCompletion = full;
}
bool CodeCompletionWorker::fullCompletion() const
{
return m_fullCompletion;
}
void CodeCompletionWorker::failed()
{
foundDeclarations({}, {});
}
void CodeCompletionWorker::foundDeclarations(const QList<CompletionTreeElementPointer>& items,
const CodeCompletionContext::Ptr& completionContext)
{
m_hasFoundDeclarations = true;
emit foundDeclarationsReal(items, completionContext);
}
void CodeCompletionWorker::computeCompletions(const KDevelop::DUContextPointer& context,
const KTextEditor::Cursor& position, KTextEditor::View* view)
{
{
QMutexLocker lock(m_mutex);
m_abort = false;
}
///@todo It's not entirely safe to pass KTextEditor::View* through a queued connection
// We access the view/document which is not thread-safe, so we need the foreground lock
ForegroundLock foreground;
//Compute the text we should complete on
KTextEditor::Document* doc = view->document();
if (!doc) {
qCDebug(LANGUAGE) << "No document for completion";
failed();
return;
}
KTextEditor::Range range;
QString text;
{
QMutexLocker lock(m_mutex);
DUChainReadLocker lockDUChain;
if (context) {
qCDebug(LANGUAGE) << context->localScopeIdentifier().toString();
range = KTextEditor::Range(context->rangeInCurrentRevision().start(), position);
} else
range = KTextEditor::Range(KTextEditor::Cursor(position.line(), 0), position);
updateContextRange(range, view, context);
text = doc->text(range);
}
if (position.column() == 0) //Seems like when the cursor is a the beginning of a line, kate does not give the \n
text += QLatin1Char('\n');
if (aborting()) {
failed();
return;
}
m_hasFoundDeclarations = false;
KTextEditor::Cursor cursorPosition = view->cursorPosition();
QString followingText; //followingText may contain additional text that stands for the current item. For example in the case "QString|", QString is in addedText.
if (position < cursorPosition)
followingText = view->document()->text(KTextEditor::Range(position, cursorPosition));
foreground.unlock();
computeCompletions(context, position, followingText, range, text);
if (!m_hasFoundDeclarations)
failed();
}
void KDevelop::CodeCompletionWorker::doSpecialProcessing(uint)
{
}
CodeCompletionContext* CodeCompletionWorker::createCompletionContext(const KDevelop::DUContextPointer& context,
const QString& contextText,
const QString& followingText,
const KDevelop::CursorInRevision& position) const
{
Q_UNUSED(context);
Q_UNUSED(contextText);
Q_UNUSED(followingText);
Q_UNUSED(position);
return nullptr;
}
void CodeCompletionWorker::computeCompletions(const KDevelop::DUContextPointer& context,
const KTextEditor::Cursor& position, const QString& followingText,
const KTextEditor::Range& contextRange, const QString& contextText)
{
Q_UNUSED(contextRange);
qCDebug(LANGUAGE) << "added text:" << followingText;
CodeCompletionContext::Ptr completionContext(createCompletionContext(context, contextText, followingText, CursorInRevision::castFromSimpleCursor(KTextEditor::Cursor(
position))));
if (KDevelop::CodeCompletionModel* m = model())
m->setCompletionContext(completionContext);
if (completionContext && completionContext->isValid()) {
{
DUChainReadLocker lock(DUChain::lock());
if (!context) {
failed();
qCDebug(LANGUAGE) << "Completion context disappeared before completions could be calculated";
return;
}
}
QList<CompletionTreeItemPointer> items = completionContext->completionItems(aborting(), fullCompletion());
if (aborting()) {
failed();
return;
}
QList<QExplicitlySharedDataPointer<CompletionTreeElement>> tree = computeGroups(items, completionContext);
if (aborting()) {
failed();
return;
}
tree += completionContext->ungroupedElements();
foundDeclarations(tree, completionContext);
} else {
qCDebug(LANGUAGE) << "setContext: Invalid code-completion context";
}
}
QList<QExplicitlySharedDataPointer<CompletionTreeElement>> CodeCompletionWorker::computeGroups(
const QList<CompletionTreeItemPointer>& items,
const QExplicitlySharedDataPointer<CodeCompletionContext>& completionContext)
{
Q_UNUSED(completionContext);
QList<QExplicitlySharedDataPointer<CompletionTreeElement>> tree;
/**
* 1. Group by argument-hint depth
* 2. Group by inheritance depth
* 3. Group by simplified attributes
* */
CodeCompletionItemGrouper<ArgumentHintDepthExtractor, CodeCompletionItemGrouper<InheritanceDepthExtractor,
CodeCompletionItemGrouper<SimplifiedAttributesExtractor>>> argumentHintDepthGrouper(tree, nullptr, std::move(
items));
return tree;
}
void CodeCompletionWorker::abortCurrentCompletion()
{
QMutexLocker lock(m_mutex);
m_abort = true;
}
bool& CodeCompletionWorker::aborting()
{
return m_abort;
}
KDevelop::CodeCompletionModel* CodeCompletionWorker::model() const
{
return m_model;
}
void CodeCompletionWorker::updateContextRange(Range& contextRange, View* view, const DUContextPointer& context) const
{
Q_UNUSED(contextRange);
Q_UNUSED(view);
Q_UNUSED(context);
}
|