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
|
/*
* This file is part of qmljs, the QML/JS language support plugin for KDevelop
* Copyright (c) 2014 Denis Steckelmacher <steckdenis@yahoo.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "completionitem.h"
#include "context.h"
#include <language/codecompletion/codecompletionmodel.h>
#include <language/duchain/declaration.h>
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/classdeclaration.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/types/abstracttype.h>
#include <language/duchain/types/structuretype.h>
#include <ktexteditor/document.h>
#include <ktexteditor/view.h>
#include "../../duchain/functiontype.h"
using namespace QmlJS;
using namespace KDevelop;
CompletionItem::CompletionItem(DeclarationPointer decl, int inheritanceDepth, Decoration decoration)
: NormalDeclarationCompletionItem(decl, QExplicitlySharedDataPointer<KDevelop::CodeCompletionContext>(), inheritanceDepth),
m_decoration(decoration)
{
}
QVariant CompletionItem::data(const QModelIndex& index, int role, const CodeCompletionModel* model) const
{
DUChainReadLocker lock;
Declaration* decl = declaration().data();
if (!decl) {
return QVariant();
}
ClassDeclaration* classDecl = dynamic_cast<ClassDeclaration *>(decl);
StructureType::Ptr declType = StructureType::Ptr::dynamicCast(decl->abstractType());
auto funcType = QmlJS::FunctionType::Ptr::dynamicCast(decl->abstractType());
if (role == CodeCompletionModel::BestMatchesCount) {
return 5;
} else if (role == CodeCompletionModel::MatchQuality) {
AbstractType::Ptr referenceType =
static_cast<QmlJS::CodeCompletionContext*>(model->completionContext().data())->typeToMatch();
if (!referenceType) {
return QVariant();
}
AbstractType::Ptr declType = decl->abstractType();
if (!declType) {
return QVariant();
}
QmlJS::FunctionType::Ptr declFunc = QmlJS::FunctionType::Ptr::dynamicCast(declType);
if (declType->equals(referenceType.constData())) {
// Perfect type match
return QVariant(10);
} else if (declFunc && declFunc->returnType() &&
declFunc->returnType()->equals(referenceType.constData())) {
// Also very nice: a function returning the proper type
return QVariant(9);
} else {
// Completely different types, no luck
return QVariant();
}
} else if (role == Qt::DisplayRole && funcType) {
// Functions are displayed using the "type funcName(arg, arg, arg...)" format
Declaration* funcDecl = funcType->declaration(decl->topContext());
if (funcDecl) {
switch (index.column()) {
case CodeCompletionModel::Prefix:
return funcType->returnType()->toString();
case CodeCompletionModel::Name:
// Return the identifier of the declaration being listed, not of its
// function declaration (because the function may have been declared
// anonymously, even if it has been assigned to a variable)
return decl->identifier().toString();
case CodeCompletionModel::Arguments:
{
QStringList args;
for (auto arg : funcDecl->internalContext()->localDeclarations()) {
args.append(arg->toString());
}
return QString("(%1)").arg(args.join(QLatin1String(", ")));
}
}
}
} else if (role == Qt::DisplayRole && index.column() == CodeCompletionModel::Prefix) {
if (classDecl) {
if (classDecl->classType() == ClassDeclarationData::Class) {
// QML component
return QString("component");
} else if (classDecl->classType() == ClassDeclarationData::Interface) {
// C++-ish QML component
return QString("wrapper");
}
}
if (decl && (
decl->kind() == Declaration::NamespaceAlias ||
decl->kind() == Declaration::Namespace
)) {
// Display namespaces and namespace aliases as modules
return QString("module");
}
if (decl && decl->abstractType() &&
decl->kind() == Declaration::Type &&
decl->abstractType()->whichType() == AbstractType::TypeEnumeration) {
// Enum
return QString("enum");
}
if (declType &&
decl->kind() == Declaration::Instance &&
declType->declarationId().qualifiedIdentifier().isEmpty()) {
// QML component instance. The type that should be displayed is the
// base class of its anonymous class
ClassDeclaration* anonymousClass = dynamic_cast<ClassDeclaration *>(declType->declaration(decl->topContext()));
if (anonymousClass && anonymousClass->baseClassesSize() > 0) {
return anonymousClass->baseClasses()[0].baseClass.abstractType()->toString();
}
}
}
return NormalDeclarationCompletionItem::data(index, role, model);
}
QString CompletionItem::declarationName() const
{
ClassFunctionDeclaration* classFuncDecl = dynamic_cast<ClassFunctionDeclaration *>(declaration().data());
if (classFuncDecl && classFuncDecl->isSignal() && m_decoration == QmlJS::CompletionItem::ColonOrBracket) {
// Signals, when completed in a QML component context, are transformed into slots
QString signal = classFuncDecl->identifier().toString();
if (signal.size() > 0) {
return QLatin1String("on") + signal.at(0).toUpper() + signal.mid(1);
}
}
return NormalDeclarationCompletionItem::declarationName();
}
CodeCompletionModel::CompletionProperties CompletionItem::completionProperties() const
{
DUChainReadLocker lock;
// Variables having a function type should have a function icon. FunctionDeclarations
// are skipped here because they are already handled properly by completionProperties()
if (declaration() && declaration()->abstractType() &&
!declaration()->isFunctionDeclaration() &&
declaration()->abstractType()->whichType() == AbstractType::TypeFunction) {
return CodeCompletionModel::Function;
}
// Put declarations in a context owned by a namespace in the namespace scope
auto properties = NormalDeclarationCompletionItem::completionProperties();
if (declaration() && declaration()->context() && declaration()->context()->owner() && (
declaration()->context()->owner()->kind() == Declaration::Namespace ||
declaration()->context()->type() == DUContext::Enum
)) {
properties &= ~(CodeCompletionModel::LocalScope | CodeCompletionModel::GlobalScope | CodeCompletionModel::Public);
properties |= CodeCompletionModel::NamespaceScope;
}
return properties;
}
void CompletionItem::execute(KTextEditor::View* view, const KTextEditor::Range& word)
{
KTextEditor::Document* document = view->document();
QString base = declarationName();
switch (m_decoration)
{
case QmlJS::CompletionItem::NoDecoration:
document->replaceText(word, base);
break;
case QmlJS::CompletionItem::Quotes:
document->replaceText(word, "\"" + base + "\"");
break;
case QmlJS::CompletionItem::QuotesAndBracket:
document->replaceText(word, "\"" + base + "\"]");
break;
case QmlJS::CompletionItem::ColonOrBracket:
if (declaration() && declaration()->abstractType() &&
declaration()->abstractType()->whichType() == AbstractType::TypeStructure) {
document->replaceText(word, base + " {}");
} else {
document->replaceText(word, base + ": ");
}
break;
case QmlJS::CompletionItem::Brackets:
document->replaceText(word, base + "()");
}
}
|