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 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 "normaldeclarationcompletionitem.h"
#include "codecompletionmodel.h"
#include "../duchain/duchainlock.h"
#include "../duchain/duchain.h"
#include "../duchain/classfunctiondeclaration.h"
#include "../duchain/types/functiontype.h"
#include "../duchain/types/enumeratortype.h"
#include "../duchain/duchainutils.h"
#include "../duchain/navigation/abstractdeclarationnavigationcontext.h"
#include <debug.h>
#include <KTextEditor/Document>
#include <KTextEditor/View>
namespace KDevelop {
const int NormalDeclarationCompletionItem::normalBestMatchesCount = 5;
//If this is true, the return-values of argument-hints will be just written as "..." if they are too long
const bool NormalDeclarationCompletionItem::shortenArgumentHintReturnValues = true;
const int NormalDeclarationCompletionItem::maximumArgumentHintReturnValueLength = 30;
const int NormalDeclarationCompletionItem::desiredTypeLength = 20;
NormalDeclarationCompletionItem::NormalDeclarationCompletionItem(const KDevelop::DeclarationPointer& decl,
const QExplicitlySharedDataPointer<CodeCompletionContext>& context,
int inheritanceDepth)
: m_completionContext(context)
, m_declaration(decl)
, m_inheritanceDepth(inheritanceDepth)
{
}
KDevelop::DeclarationPointer NormalDeclarationCompletionItem::declaration() const
{
return m_declaration;
}
QExplicitlySharedDataPointer<KDevelop::CodeCompletionContext> NormalDeclarationCompletionItem::completionContext() const
{
return m_completionContext;
}
int NormalDeclarationCompletionItem::inheritanceDepth() const
{
return m_inheritanceDepth;
}
int NormalDeclarationCompletionItem::argumentHintDepth() const
{
if (m_completionContext)
return m_completionContext->depth();
else
return 0;
}
QString NormalDeclarationCompletionItem::declarationName() const
{
if (!m_declaration) {
return QStringLiteral("<unknown>");
}
QString ret = m_declaration->identifier().toString();
if (ret.isEmpty())
return QStringLiteral("<unknown>");
else
return ret;
}
void NormalDeclarationCompletionItem::execute(KTextEditor::View* view, const KTextEditor::Range& word)
{
if (m_completionContext && m_completionContext->depth() != 0)
return; //Do not replace any text when it is an argument-hint
KTextEditor::Document* document = view->document();
QString newText;
{
KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock());
if (m_declaration) {
newText = declarationName();
} else {
qCDebug(LANGUAGE) << "Declaration disappeared";
return;
}
}
document->replaceText(word, newText);
KTextEditor::Range newRange = word;
newRange.setEnd(KTextEditor::Cursor(newRange.end().line(), newRange.start().column() + newText.length()));
executed(view, newRange);
}
QWidget* NormalDeclarationCompletionItem::createExpandingWidget(const KDevelop::CodeCompletionModel* model) const
{
Q_UNUSED(model);
return nullptr;
}
bool NormalDeclarationCompletionItem::createsExpandingWidget() const
{
return false;
}
QString NormalDeclarationCompletionItem::shortenedTypeString(const KDevelop::DeclarationPointer& decl,
int desiredTypeLength) const
{
Q_UNUSED(desiredTypeLength);
return decl->abstractType()->toString();
}
void NormalDeclarationCompletionItem::executed(KTextEditor::View* view, const KTextEditor::Range& word)
{
Q_UNUSED(view);
Q_UNUSED(word);
}
QVariant NormalDeclarationCompletionItem::data(const QModelIndex& index, int role,
const KDevelop::CodeCompletionModel* model) const
{
DUChainReadLocker lock(DUChain::lock(), 500);
if (!lock.locked()) {
qCDebug(LANGUAGE) << "Failed to lock the du-chain in time";
return QVariant();
}
if (!m_declaration)
return QVariant();
switch (role) {
case Qt::DisplayRole:
if (index.column() == CodeCompletionModel::Name) {
return declarationName();
} else if (index.column() == CodeCompletionModel::Postfix) {
if (FunctionType::Ptr functionType = m_declaration->type<FunctionType>()) {
// Retrieve const/volatile string
return functionType->AbstractType::toString();
}
} else if (index.column() == CodeCompletionModel::Prefix) {
if (m_declaration->kind() == Declaration::Namespace)
return QStringLiteral("namespace");
if (m_declaration->abstractType()) {
if (EnumeratorType::Ptr enumerator = m_declaration->type<EnumeratorType>()) {
if (m_declaration->context()->owner() && m_declaration->context()->owner()->abstractType()) {
if (!m_declaration->context()->owner()->identifier().isEmpty())
return shortenedTypeString(DeclarationPointer(
m_declaration->context()->owner()), desiredTypeLength);
else
return QStringLiteral("enum");
}
}
if (FunctionType::Ptr functionType = m_declaration->type<FunctionType>()) {
auto* funDecl = dynamic_cast<ClassFunctionDeclaration*>(m_declaration.data());
if (functionType->returnType()) {
QString ret = shortenedTypeString(m_declaration, desiredTypeLength);
if (shortenArgumentHintReturnValues && argumentHintDepth() &&
ret.length() > maximumArgumentHintReturnValueLength)
return QStringLiteral("...");
else
return ret;
} else if (argumentHintDepth()) {
return QString();//Don't show useless prefixes in the argument-hints
} else if (funDecl && funDecl->isConstructor())
return QStringLiteral("<constructor>");
else if (funDecl && funDecl->isDestructor())
return QStringLiteral("<destructor>");
else
return QStringLiteral("<incomplete type>");
} else {
return shortenedTypeString(m_declaration, desiredTypeLength);
}
} else {
return QStringLiteral("<incomplete type>");
}
} else if (index.column() == CodeCompletionModel::Arguments) {
if (m_declaration->isFunctionDeclaration()) {
auto functionType = declaration()->type<FunctionType>();
return functionType ? functionType->partToString(FunctionType::SignatureArguments) : QVariant();
}
}
break;
case CodeCompletionModel::BestMatchesCount:
return QVariant(normalBestMatchesCount);
case CodeCompletionModel::IsExpandable:
return QVariant(createsExpandingWidget());
case CodeCompletionModel::ExpandingWidget: {
QWidget* nav = createExpandingWidget(model);
Q_ASSERT(nav);
QVariant v;
v.setValue<QWidget*>(nav);
return v;
}
case CodeCompletionModel::ScopeIndex:
return static_cast<int>(reinterpret_cast<quintptr>(m_declaration->context()));
case CodeCompletionModel::CompletionRole:
return ( int )completionProperties();
case CodeCompletionModel::ItemSelected: {
NavigationContextPointer ctx(new AbstractDeclarationNavigationContext(DeclarationPointer(
m_declaration),
TopDUContextPointer()));
return ctx->html(true);
}
case Qt::DecorationRole:
{
if (index.column() == CodeCompletionModel::Icon) {
CodeCompletionModel::CompletionProperties p = completionProperties();
lock.unlock();
return DUChainUtils::iconForProperties(p);
}
break;
}
}
return QVariant();
}
}
|