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
|
/*
KDevelop Php Code Completion Support
SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-FileCopyrightText: 2008 Hamish Rodda <rodda@kde.org>
SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams@gmail.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "item.h"
#include <KTextEditor/Range>
#include <KTextEditor/View>
#include <KTextEditor/Document>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/declaration.h>
#include <language/duchain/namespacealiasdeclaration.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/types/functiontype.h>
#include <language/codecompletion/codecompletionmodel.h>
#include <language/codecompletion/codecompletionhelper.h>
#include "../duchain/declarations/classdeclaration.h"
#include "../duchain/declarations/classmethoddeclaration.h"
#include "../duchain/declarations/variabledeclaration.h"
#include "../duchain/types/structuretype.h"
#include "../duchain/navigation/navigationwidget.h"
#include "../duchain/helper.h"
#include "helpers.h"
#include "context.h"
#include "completiondebug.h"
using namespace KDevelop;
namespace Php
{
QString NormalDeclarationCompletionItem::declarationName() const
{
QString ret = prettyName(m_declaration.data());
if ( ret.isEmpty() ) {
return QStringLiteral("<unknown>");
}
bool isStatic = false;
if (!m_declaration->isFunctionDeclaration()) {
if (dynamic_cast<VariableDeclaration*>(m_declaration.data())) {
ret = '$' + ret;
} else if (ClassMemberDeclaration* memberDec = dynamic_cast<ClassMemberDeclaration*>(m_declaration.data())) {
isStatic = memberDec->isStatic();
if (memberDec->isStatic() && memberDec->abstractType() && !(memberDec->abstractType()->modifiers() & AbstractType::ConstModifier)) {
// PHP is strange, $obj->asdf, class::const but class::$static ...
ret = '$' + ret;
}
}
} else if ( ClassFunctionDeclaration* funDec = dynamic_cast<ClassFunctionDeclaration*>(m_declaration.data()) ) {
isStatic = funDec->isStatic();
}
const QExplicitlySharedDataPointer<CodeCompletionContext>& ctx = completionContext();
if ( ctx->memberAccessOperation() == CodeCompletionContext::NoMemberAccess ) {
// if we complete a class member or method (inside a method)
// we might have to add "self::", "parent::" or "$this->"
if ( ctx->duContext() && ctx->duContext()->parentContext()
&& ctx->duContext()->parentContext()->type() == DUContext::Class )
{
if ( m_declaration->context() && m_declaration->context()->type() == DUContext::Class ) {
if ( isStatic ) {
ret = "self::" + ret;
} else {
ret = "$this->" + ret;
}
}
}
}
return ret;
}
void NormalDeclarationCompletionItem::executed(KTextEditor::View* view, const KTextEditor::Range& word)
{
if (m_declaration && dynamic_cast<AbstractFunctionDeclaration*>(m_declaration.data())) {
//Do some intelligent stuff for functions with the parens:
insertFunctionParenText(view, word.end(), m_declaration);
}
}
QVariant NormalDeclarationCompletionItem::data(const QModelIndex& index, int role, const KDevelop::CodeCompletionModel* model) const
{
DUChainReadLocker lock(DUChain::lock(), 500);
if (!lock.locked()) {
qCDebug(COMPLETION) << "Failed to lock the du-chain in time";
return QVariant();
}
if (!m_declaration) {
return QVariant();
}
Declaration* dec = const_cast<Declaration*>(m_declaration.data());
switch (role) {
case CodeCompletionModel::ItemSelected:
return QVariant(NavigationWidget::shortDescription(dec));
case Qt::DisplayRole:
switch (index.column()) {
case CodeCompletionModel::Postfix:
return QVariant();
case CodeCompletionModel::Prefix:
if (dec->kind() == Declaration::Type && !dec->isTypeAlias()) {
if (dec->isFunctionDeclaration()) {
FunctionType::Ptr funcType = dec->type<FunctionType>();
if ( funcType && funcType->returnType() ) {
return funcType->returnType()->toString();
} else {
return "<notype>";
}
} else if (dec->internalContext() && dec->internalContext()->type() == DUContext::Class) {
ClassDeclaration* classDec = dynamic_cast<ClassDeclaration*>(dec);
if (classDec) {
if (classDec->classType() == ClassDeclarationData::Interface) {
return "interface";
} else {
return "class";
}
}
}
return QVariant();
} else if (dec->kind() == Declaration::Namespace) {
return "namespace";
}
break;
case CodeCompletionModel::Arguments:
if (FunctionType::Ptr functionType = dec->type<FunctionType>()) {
QString ret;
createArgumentList(*this, ret, 0);
return ret;
}
break;
}
break;
case CodeCompletionModel::HighlightingMethod:
if (index.column() == CodeCompletionModel::Arguments) {
if (completionContext()->memberAccessOperation() == CodeCompletionContext::FunctionCallAccess) {
return QVariant(CodeCompletionModel::CustomHighlighting);
} else {
return QVariant();
}
break;
}
break;
case CodeCompletionModel::CustomHighlight:
if (index.column() == CodeCompletionModel::Arguments && completionContext()->memberAccessOperation() == CodeCompletionContext::FunctionCallAccess) {
QString ret;
QList<QVariant> highlight;
createArgumentList(*this, ret, &highlight);
return QVariant(highlight);
}
break;
}
lock.unlock();
return KDevelop::NormalDeclarationCompletionItem::data(index, role, model);
}
QWidget* NormalDeclarationCompletionItem::createExpandingWidget(const KDevelop::CodeCompletionModel* model) const
{
return new NavigationWidget(m_declaration, model->currentTopContext());
}
bool NormalDeclarationCompletionItem::createsExpandingWidget() const
{
return true;
}
QExplicitlySharedDataPointer<Php::CodeCompletionContext> NormalDeclarationCompletionItem::completionContext() const
{
auto context = dynamic_cast<Php::CodeCompletionContext*>(m_completionContext.data());
return QExplicitlySharedDataPointer<Php::CodeCompletionContext>(context);
}
}
|