2
* KDevelop Php Code Completion Support
4
* Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
5
* Copyright 2008 Niko Sams <niko.sams@gmail.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU Library General Public License as
9
* published by the Free Software Foundation; either version 2 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public
18
* License along with this program; if not, write to the
19
* Free Software Foundation, Inc.,
20
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
#include "completion/helpers.h"
24
#include "completion/item.h"
27
#include <language/duchain/declaration.h>
28
#include <language/duchain/abstractfunctiondeclaration.h>
29
#include <language/duchain/duchainutils.h>
30
#include <language/duchain/ducontext.h>
31
#include <language/duchain/types/functiontype.h>
32
#include <QTextFormat>
33
#include <QStringList>
34
#include <language/duchain/types/integraltype.h>
36
using namespace KDevelop;
40
void createArgumentList(const NormalDeclarationCompletionItem& item, QString& ret, QList<QVariant>* highlighting,
43
///@todo also highlight the matches of the previous arguments, they are given by ViableFunction
44
Declaration* dec(item.declaration().data());
46
int textFormatStart = 0;
47
QTextFormat normalFormat(QTextFormat::CharFormat);
48
QTextFormat highlightFormat; //highlightFormat is invalid, so kate uses the match-quality dependent color.
50
AbstractFunctionDeclaration* decl = dynamic_cast<AbstractFunctionDeclaration*>(dec);
51
FunctionType::Ptr functionType = dec->type<FunctionType>();
53
if (functionType && decl) {
55
QVector<Declaration*> parameters;
56
if (DUChainUtils::getArgumentContext(dec))
57
parameters = DUChainUtils::getArgumentContext(dec)->localDeclarations();
59
uint defaultParamNum = 0;
61
int firstDefaultParam = parameters.count() - decl->defaultParametersSize();
67
foreach(Declaration* dec, parameters) {
73
bool doHighlight = false;
74
QTextFormat doFormat = normalFormat;
76
// if( num < f.matchedArguments )
79
doFormat = QTextFormat(QTextFormat::CharFormat);
81
// if( parameterConversion != conversions.end() ) {
82
// //Interpolate the color
83
// quint64 badMatchColor = 0xff7777ff; //Full blue
84
// quint64 goodMatchColor = 0xff77ff77; //Full green
86
// uint totalColor = (badMatchColor*(Cpp::MaximumConversionResult-(*parameterConversion).rank) + goodMatchColor*(*parameterConversion).rank)/Cpp::MaximumConversionResult;
88
// doFormat.setBackground( QBrush(totalColor) );
90
// ++parameterConversion;
95
if (highlighting && ret.length() != textFormatStart) {
96
//Add a default-highlighting for the passed text
97
*highlighting << QVariant(textFormatStart);
98
*highlighting << QVariant(ret.length() - textFormatStart);
99
*highlighting << QVariant(normalFormat);
100
textFormatStart = ret.length();
104
if (num < functionType->arguments().count()) {
105
if (AbstractType::Ptr type = functionType->arguments().at(num)) {
106
// when php-like type hinting is requested only add types for arrays and classes
108
|| (type->whichType() == AbstractType::TypeIntegral
109
&& type.cast<IntegralType>()->dataType() == IntegralType::TypeArray)
110
|| type->whichType() == AbstractType::TypeStructure) {
111
ret += type->toString() + ' ';
116
ret += '$' + dec->identifier().toString();
119
if (highlighting && ret.length() != textFormatStart) {
120
*highlighting << QVariant(textFormatStart);
121
*highlighting << QVariant(ret.length() - textFormatStart);
122
*highlighting << doFormat;
123
textFormatStart = ret.length();
128
if (num >= firstDefaultParam) {
129
ret += " = " + decl->defaultParameters()[defaultParamNum].str();
137
if (highlighting && ret.length() != textFormatStart) {
138
*highlighting << QVariant(textFormatStart);
139
*highlighting << QVariant(ret.length());
140
*highlighting << normalFormat;
141
textFormatStart = ret.length();
147
QStringList getMethodTokens(QString text)
152
text = text.trimmed();
153
if (text.endsWith(QString("function"), Qt::CaseInsensitive)) {
154
tokens << "function";
155
text = text.left(text.length() - 8);
158
QStringList possibleTokens;
159
possibleTokens << "private";
160
possibleTokens << "public";
161
possibleTokens << "protected";
162
possibleTokens << "static";
163
possibleTokens << "abstract";
164
possibleTokens << "final";
166
while (!possibleTokens.isEmpty()) {
167
bool foundAnything = false;
168
text = text.trimmed();
169
foreach(const QString &token, possibleTokens) {
170
if (text.endsWith(token, Qt::CaseInsensitive)) {
172
text = text.left(text.length() - token.length());
173
foundAnything = true;
174
possibleTokens.removeOne(token);
178
if (!foundAnything) {
186
QString getIndendation( const QString &line ) {
187
return line.left(line.indexOf(QRegExp("\\S"), 0));