source: trunk/src/scripttools/debugging/qscriptcompletiontask.cpp@ 345

Last change on this file since 345 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 10.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSCriptTools module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qscriptcompletiontask_p.h"
43#include "qscriptcompletiontaskinterface_p_p.h"
44#include "qscriptdebuggerconsole_p.h"
45#include "qscriptdebuggerconsolecommand_p.h"
46#include "qscriptdebuggerconsolecommandmanager_p.h"
47
48#include "qscriptenginedebuggerfrontend_p.h" // ### kill
49#include "qscriptdebuggerbackend_p.h" // ### kill
50#include <QtScript/qscriptcontext.h>
51#include <QtScript/qscriptvalue.h>
52#include <QtScript/qscriptvalueiterator.h>
53
54#include "private/qobject_p.h"
55
56#include <QtCore/qset.h>
57#include <QtCore/qdebug.h>
58
59QT_BEGIN_NAMESPACE
60
61class QScriptCompletionTaskPrivate
62 : public QScriptCompletionTaskInterfacePrivate
63{
64 Q_DECLARE_PUBLIC(QScriptCompletionTask)
65public:
66 QScriptCompletionTaskPrivate();
67 ~QScriptCompletionTaskPrivate();
68
69 void completeScriptExpression();
70
71 QString contents;
72 int cursorPosition;
73 int frameIndex;
74 QScriptDebuggerFrontend *frontend;
75 QScriptDebuggerConsole *console;
76};
77
78QScriptCompletionTaskPrivate::QScriptCompletionTaskPrivate()
79{
80}
81
82QScriptCompletionTaskPrivate::~QScriptCompletionTaskPrivate()
83{
84}
85
86QScriptCompletionTask::QScriptCompletionTask(
87 const QString &contents, int cursorPosition,
88 int frameIndex, QScriptDebuggerFrontend *frontend,
89 QScriptDebuggerConsole *console,
90 QObject *parent)
91 : QScriptCompletionTaskInterface(
92 *new QScriptCompletionTaskPrivate, parent)
93{
94 Q_D(QScriptCompletionTask);
95 d->contents = contents;
96 d->cursorPosition = cursorPosition;
97 if ((frameIndex == -1) && console)
98 d->frameIndex = console->currentFrameIndex();
99 else
100 d->frameIndex = frameIndex;
101 d->frontend = frontend;
102 d->console = console;
103}
104
105QScriptCompletionTask::~QScriptCompletionTask()
106{
107}
108
109namespace {
110
111static bool isIdentChar(const QChar &ch)
112{
113 static QChar underscore = QLatin1Char('_');
114 return ch.isLetter() || (ch == underscore);
115}
116
117static bool isPrefixOf(const QString &prefix, const QString &what)
118{
119 return ((what.length() > prefix.length())
120 && what.startsWith(prefix));
121}
122
123} // namespace
124
125void QScriptCompletionTaskPrivate::completeScriptExpression()
126{
127 int pos = cursorPosition;
128 if ((pos > 0) && contents.at(pos-1).isNumber()) {
129 // completion of numbers is pointless
130 return;
131 }
132
133 while ((pos > 0) && isIdentChar(contents.at(pos-1)))
134 --pos;
135 int pos2 = cursorPosition;
136 while ((pos2 < contents.size()-1) && isIdentChar(contents.at(pos2+1)))
137 ++pos2;
138 QString ident = contents.mid(pos, pos2 - pos + 1);
139 position = pos;
140
141 QStringList path;
142 path.append(ident);
143 while ((pos > 0) && (contents.at(pos-1) == QLatin1Char('.'))) {
144 --pos;
145 pos2 = pos;
146 while ((pos > 0) && isIdentChar(contents.at(pos-1)))
147 --pos;
148 path.prepend(contents.mid(pos, pos2 - pos));
149 }
150
151 // ### super-cheating for now; have to use the async API
152 QScriptEngineDebuggerFrontend *edf = static_cast<QScriptEngineDebuggerFrontend*>(frontend);
153 QScriptDebuggerBackend *backend = edf->backend();
154 QScriptContext *ctx = backend->context(frameIndex);
155 QScriptValueList objects;
156 QString prefix = path.last();
157 QSet<QString> matches;
158 if (path.size() > 1) {
159 const QString &topLevelIdent = path.at(0);
160 QScriptValue obj;
161 if (topLevelIdent == QString::fromLatin1("this")) {
162 obj = ctx->thisObject();
163 } else {
164 QScriptValueList scopeChain;
165#if QT_VERSION >= 0x040500
166 scopeChain = ctx->scopeChain();
167#else
168 scopeChain.append(ctx->activationObject());
169#endif
170 for (int i = 0; i < scopeChain.size(); ++i) {
171 QScriptValue oo = scopeChain.at(i).property(topLevelIdent);
172 if (oo.isObject()) {
173 obj = oo;
174 break;
175 }
176 }
177 }
178 for (int i = 1; obj.isObject() && (i < path.size()-1); ++i)
179 obj = obj.property(path.at(i));
180 if (obj.isValid())
181 objects.append(obj);
182 } else {
183#if QT_VERSION >= 0x040500
184 objects << ctx->scopeChain();
185#else
186 objects.append(ctx->activationObject());
187#endif
188 QStringList keywords;
189 keywords.append(QString::fromLatin1("this"));
190 keywords.append(QString::fromLatin1("true"));
191 keywords.append(QString::fromLatin1("false"));
192 keywords.append(QString::fromLatin1("null"));
193 for (int i = 0; i < keywords.size(); ++i) {
194 const QString &kwd = keywords.at(i);
195 if (isPrefixOf(prefix, kwd))
196 matches.insert(kwd);
197 }
198 }
199
200 for (int i = 0; i < objects.size(); ++i) {
201 QScriptValue obj = objects.at(i);
202 while (obj.isObject()) {
203 QScriptValueIterator it(obj);
204 while (it.hasNext()) {
205 it.next();
206 QString propertyName = it.name();
207 if (isPrefixOf(prefix, propertyName))
208 matches.insert(propertyName);
209 }
210 obj = obj.prototype();
211 }
212 }
213 results = matches.toList();
214 qStableSort(results);
215
216 length = prefix.length();
217 type = QScriptCompletionTask::ScriptIdentifierCompletion;
218}
219
220void QScriptCompletionTask::start()
221{
222 Q_D(QScriptCompletionTask);
223 d->type = NoCompletion;
224 // see if we're typing a command
225 // ### don't hardcode the command prefix
226 QRegExp cmdRx(QString::fromLatin1("^\\s*\\.([a-zA-Z]*)"));
227 int cmdIndex = cmdRx.indexIn(d->contents);
228 if ((cmdIndex != -1) && d->console) {
229 int len = cmdRx.matchedLength();
230 QString prefix = cmdRx.capturedTexts().at(1);
231 if ((d->cursorPosition >= cmdIndex) && (d->cursorPosition <= (cmdIndex+len))) {
232 // editing command --> get command completions
233 d->results = d->console->commandManager()->completions(prefix);
234 qStableSort(d->results);
235 d->position = cmdRx.pos(1);
236 d->length = prefix.length();
237 d->type = CommandNameCompletion;
238 d->appendix = QString::fromLatin1(" ");
239 emit finished();
240 } else {
241 QScriptDebuggerConsoleCommand *cmd = d->console->commandManager()->findCommand(prefix);
242 if (!cmd) {
243 emit finished();
244 return;
245 }
246 // editing an argument
247 int argNum = 0;
248 QString arg;
249 int pos = cmdIndex + len;
250 while (pos < d->contents.size()) {
251 while ((pos < d->contents.size()) && d->contents.at(pos).isSpace())
252 ++pos;
253 if (pos < d->contents.size()) {
254 int pos2 = pos + 1;
255 while ((pos2 < d->contents.size()) && !d->contents.at(pos2).isSpace())
256 ++pos2;
257 if ((d->cursorPosition >= pos) && (d->cursorPosition <= pos2)) {
258 arg = d->contents.mid(pos, pos2 - pos);
259 break;
260 }
261 pos = pos2;
262 ++argNum;
263 }
264 }
265 QString argType = cmd->argumentTypes().value(argNum);
266 if (!argType.isEmpty()) {
267 if (argType == QString::fromLatin1("command-or-group-name")) {
268 d->results = d->console->commandManager()->completions(arg);
269 } else if (argType == QString::fromLatin1("script-filename")) {
270 // ### super-cheating for now; have to use the async API
271 QScriptEngineDebuggerFrontend *edf = static_cast<QScriptEngineDebuggerFrontend*>(d->frontend);
272 QScriptDebuggerBackend *backend = edf->backend();
273 QScriptScriptMap scripts = backend->scripts();
274 QScriptScriptMap::const_iterator it;
275 for (it = scripts.constBegin(); it != scripts.constEnd(); ++it) {
276 QString fileName = it.value().fileName();
277 if (isPrefixOf(arg, fileName))
278 d->results.append(fileName);
279 }
280 } else if (argType == QString::fromLatin1("subcommand-name")) {
281 for (int i = 0; i < cmd->subCommands().size(); ++i) {
282 QString name = cmd->subCommands().at(i);
283 if (isPrefixOf(arg, name))
284 d->results.append(name);
285 }
286 } else if (argType == QString::fromLatin1("script")) {
287 d->completeScriptExpression();
288 }
289 if ((d->type == NoCompletion) && !d->results.isEmpty()) {
290 qStableSort(d->results);
291 d->position = pos;
292 d->length = arg.length();
293 d->type = CommandArgumentCompletion;
294 }
295 }
296 emit finished();
297 }
298 } else {
299 // assume it's an eval expression
300 d->completeScriptExpression();
301 emit finished();
302 }
303}
304
305QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.