~ubuntu-branches/ubuntu/utopic/kdevelop-php/utopic

« back to all changes in this revision

Viewing changes to completion/helpers.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2010-01-17 17:10:22 UTC
  • Revision ID: james.westby@ubuntu.com-20100117171022-q2xlgd9ekewo2ijx
Tags: upstream-1.0.0~beta2
ImportĀ upstreamĀ versionĀ 1.0.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * KDevelop Php Code Completion Support
 
3
 *
 
4
 * Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
 
5
 * Copyright 2008 Niko Sams <niko.sams@gmail.com>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 */
 
22
 
 
23
#include "completion/helpers.h"
 
24
#include "completion/item.h"
 
25
#include <QList>
 
26
#include <QVariant>
 
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>
 
35
 
 
36
using namespace KDevelop;
 
37
namespace Php
 
38
{
 
39
 
 
40
void createArgumentList(const NormalDeclarationCompletionItem& item, QString& ret, QList<QVariant>* highlighting,
 
41
                        bool phpTypeHinting)
 
42
{
 
43
    ///@todo also highlight the matches of the previous arguments, they are given by ViableFunction
 
44
    Declaration* dec(item.declaration().data());
 
45
 
 
46
    int textFormatStart = 0;
 
47
    QTextFormat normalFormat(QTextFormat::CharFormat);
 
48
    QTextFormat highlightFormat; //highlightFormat is invalid, so kate uses the match-quality dependent color.
 
49
 
 
50
    AbstractFunctionDeclaration* decl = dynamic_cast<AbstractFunctionDeclaration*>(dec);
 
51
    FunctionType::Ptr functionType = dec->type<FunctionType>();
 
52
 
 
53
    if (functionType && decl) {
 
54
 
 
55
        QVector<Declaration*> parameters;
 
56
        if (DUChainUtils::getArgumentContext(dec))
 
57
            parameters = DUChainUtils::getArgumentContext(dec)->localDeclarations();
 
58
 
 
59
        uint defaultParamNum = 0;
 
60
 
 
61
        int firstDefaultParam = parameters.count() - decl->defaultParametersSize();
 
62
 
 
63
        ret = '(';
 
64
        bool first = true;
 
65
        int num = 0;
 
66
 
 
67
        foreach(Declaration* dec, parameters) {
 
68
            if (first)
 
69
                first = false;
 
70
            else
 
71
                ret += ", ";
 
72
 
 
73
            bool doHighlight = false;
 
74
            QTextFormat doFormat = normalFormat;
 
75
 
 
76
//       if( num < f.matchedArguments )
 
77
//       {
 
78
            doHighlight = true;
 
79
            doFormat = QTextFormat(QTextFormat::CharFormat);
 
80
 
 
81
//         if( parameterConversion != conversions.end() ) {
 
82
//           //Interpolate the color
 
83
//           quint64 badMatchColor = 0xff7777ff; //Full blue
 
84
//           quint64 goodMatchColor = 0xff77ff77; //Full green
 
85
//
 
86
//           uint totalColor = (badMatchColor*(Cpp::MaximumConversionResult-(*parameterConversion).rank) + goodMatchColor*(*parameterConversion).rank)/Cpp::MaximumConversionResult;
 
87
//
 
88
//           doFormat.setBackground( QBrush(totalColor) );
 
89
//
 
90
//           ++parameterConversion;
 
91
//         }
 
92
//       }
 
93
 
 
94
            if (doHighlight) {
 
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();
 
101
                }
 
102
            }
 
103
 
 
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
 
107
                    if (!phpTypeHinting
 
108
                            || (type->whichType() == AbstractType::TypeIntegral
 
109
                                && type.cast<IntegralType>()->dataType() == IntegralType::TypeArray)
 
110
                            || type->whichType() == AbstractType::TypeStructure) {
 
111
                        ret += type->toString() + ' ';
 
112
                    }
 
113
                }
 
114
            }
 
115
 
 
116
            ret += '$' + dec->identifier().toString();
 
117
 
 
118
            if (doHighlight) {
 
119
                if (highlighting && ret.length() != textFormatStart) {
 
120
                    *highlighting <<  QVariant(textFormatStart);
 
121
                    *highlighting << QVariant(ret.length() - textFormatStart);
 
122
                    *highlighting << doFormat;
 
123
                    textFormatStart = ret.length();
 
124
                }
 
125
            }
 
126
 
 
127
 
 
128
            if (num >= firstDefaultParam) {
 
129
                ret += " = " + decl->defaultParameters()[defaultParamNum].str();
 
130
                ++defaultParamNum;
 
131
            }
 
132
 
 
133
            ++num;
 
134
        }
 
135
        ret += ')';
 
136
 
 
137
        if (highlighting && ret.length() != textFormatStart) {
 
138
            *highlighting <<  QVariant(textFormatStart);
 
139
            *highlighting << QVariant(ret.length());
 
140
            *highlighting << normalFormat;
 
141
            textFormatStart = ret.length();
 
142
        }
 
143
        return;
 
144
    }
 
145
}
 
146
 
 
147
QStringList getMethodTokens(QString text)
 
148
{
 
149
    QStringList tokens;
 
150
 
 
151
 
 
152
    text = text.trimmed();
 
153
    if (text.endsWith(QString("function"), Qt::CaseInsensitive)) {
 
154
        tokens << "function";
 
155
        text = text.left(text.length() - 8);
 
156
    }
 
157
 
 
158
    QStringList possibleTokens;
 
159
    possibleTokens << "private";
 
160
    possibleTokens << "public";
 
161
    possibleTokens << "protected";
 
162
    possibleTokens << "static";
 
163
    possibleTokens << "abstract";
 
164
    possibleTokens << "final";
 
165
 
 
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)) {
 
171
                tokens << token;
 
172
                text = text.left(text.length() - token.length());
 
173
                foundAnything = true;
 
174
                possibleTokens.removeOne(token);
 
175
                break;
 
176
            }
 
177
        }
 
178
        if (!foundAnything) {
 
179
            break;
 
180
        }
 
181
    }
 
182
 
 
183
    return tokens;
 
184
}
 
185
 
 
186
QString getIndendation( const QString &line ) {
 
187
    return line.left(line.indexOf(QRegExp("\\S"), 0));
 
188
}
 
189
 
 
190
}