Qt 4.8
qscriptcompletiontask.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
51 
52 #include "private/qobject_p.h"
53 
54 #include <QtCore/qset.h>
55 #include <QtCore/qdebug.h>
56 
58 
61 {
63 public:
66 
68  void emitFinished();
69 
76 };
77 
80  jobScheduler(0), console(0)
81 {
82 }
83 
85 {
86 }
87 
89 {
90 public:
95  m_frameIndex(frameIndex), m_path(path), m_task(task)
96  {}
97 
98  void start()
99  {
101  frontend.scheduleGetCompletions(m_frameIndex, m_path);
102  }
103  void handleResponse(const QScriptDebuggerResponse &response, int /*commandId*/)
104  {
105  m_task->results = response.result().toStringList();
106  m_task->emitFinished();
107  finish();
108  }
109 
110 private:
114 };
115 
116 namespace {
117 
118 static bool isIdentChar(const QChar &ch)
119 {
120  static QChar underscore = QLatin1Char('_');
121  return ch.isLetterOrNumber() || (ch == underscore);
122 }
123 
124 static bool isPrefixOf(const QString &prefix, const QString &what)
125 {
126  return ((what.length() > prefix.length())
127  && what.startsWith(prefix));
128 }
129 
130 } // namespace
131 
133 {
134 public:
138  m_prefix(prefix), m_task(task)
139  {}
140 
141  void start()
142  {
144  frontend.scheduleGetScripts();
145  }
146  void handleResponse(const QScriptDebuggerResponse &response, int /*commandId*/)
147  {
148  QScriptScriptMap scripts = response.resultAsScripts();
150  for (it = scripts.constBegin(); it != scripts.constEnd(); ++it) {
151  QString fileName = it.value().fileName();
152  if (isPrefixOf(m_prefix, fileName))
153  m_task->results.append(fileName);
154  }
155  m_task->emitFinished();
156  finish();
157  }
158 private:
161 };
162 
164 {
165  int pos = cursorPosition;
166  if ((pos > 0) && contents.at(pos-1).isNumber()) {
167  // completion of numbers is pointless
168  emitFinished();
169  return;
170  }
171 
172  while ((pos > 0) && isIdentChar(contents.at(pos-1)))
173  --pos;
174  int pos2 = cursorPosition - 1;
175  while ((pos2+1 < contents.size()) && isIdentChar(contents.at(pos2+1)))
176  ++pos2;
177  QString ident = contents.mid(pos, pos2 - pos + 1);
178  position = pos;
179 
180  QStringList path;
181  path.append(ident);
182  while ((pos > 0) && (contents.at(pos-1) == QLatin1Char('.'))) {
183  --pos;
184  pos2 = pos;
185  while ((pos > 0) && isIdentChar(contents.at(pos-1)))
186  --pos;
187  path.prepend(contents.mid(pos, pos2 - pos));
188  }
189 
190  length = path.last().length();
192 
195 }
196 
198 {
199  emit q_func()->finished();
200 }
201 
203  const QString &contents, int cursorPosition, int frameIndex,
207  QObject *parent)
209  *new QScriptCompletionTaskPrivate, parent)
210 {
212  d->contents = contents;
213  d->cursorPosition = cursorPosition;
214  if ((frameIndex == -1) && console)
215  d->frameIndex = console->currentFrameIndex();
216  else
217  d->frameIndex = frameIndex;
218  d->commandScheduler = commandScheduler;
219  d->jobScheduler = jobScheduler;
220  d->console = console;
221 }
222 
224 {
225 }
226 
228 {
230  d->type = NoCompletion;
231  // see if we're typing a command
232  // ### don't hardcode the command prefix
233  QRegExp cmdRx(QString::fromLatin1("^\\s*\\.([a-zA-Z]*)"));
234  int cmdIndex = cmdRx.indexIn(d->contents);
235  if ((cmdIndex != -1) && d->console) {
236  int len = cmdRx.matchedLength();
237  QString prefix = cmdRx.capturedTexts().at(1);
238  if ((d->cursorPosition >= cmdIndex) && (d->cursorPosition <= (cmdIndex+len))) {
239  // editing command --> get command completions
240  d->results = d->console->commandManager()->completions(prefix);
241  d->position = cmdRx.pos(1);
242  d->length = prefix.length();
243  d->type = CommandNameCompletion;
244  d->appendix = QString::fromLatin1(" ");
245  emit finished();
246  } else {
247  QScriptDebuggerConsoleCommand *cmd = d->console->commandManager()->findCommand(prefix);
248  if (!cmd) {
249  emit finished();
250  return;
251  }
252  // editing an argument
253  int argNum = 0;
254  QString arg;
255  int pos = cmdIndex + len;
256  while (pos < d->contents.size()) {
257  while ((pos < d->contents.size()) && d->contents.at(pos).isSpace())
258  ++pos;
259  if (pos < d->contents.size()) {
260  int pos2 = pos + 1;
261  while ((pos2 < d->contents.size()) && !d->contents.at(pos2).isSpace())
262  ++pos2;
263  if ((d->cursorPosition >= pos) && (d->cursorPosition <= pos2)) {
264  arg = d->contents.mid(pos, pos2 - pos);
265  break;
266  }
267  pos = pos2;
268  ++argNum;
269  }
270  }
271  QString argType = cmd->argumentTypes().value(argNum);
272  if (!argType.isEmpty()) {
273  if (argType == QLatin1String("command-or-group-name")) {
274  d->results = d->console->commandManager()->completions(arg);
275  } else if (argType == QLatin1String("script-filename")) {
276  d->position = pos;
277  d->length = arg.length();
279  QScriptDebuggerJob *job = new QScriptCompleteScriptsJob(arg, d, d->commandScheduler);
280  d->jobScheduler->scheduleJob(job);
281  } else if (argType == QLatin1String("subcommand-name")) {
282  for (int i = 0; i < cmd->subCommands().size(); ++i) {
283  QString name = cmd->subCommands().at(i);
284  if (isPrefixOf(arg, name))
285  d->results.append(name);
286  }
287  qStableSort(d->results);
288  } else if (argType == QLatin1String("script")) {
289  d->completeScriptExpression();
290  } else {
291  emit finished();
292  }
293  if ((d->type == NoCompletion) && !d->results.isEmpty()) {
294  d->position = pos;
295  d->length = arg.length();
297  emit finished();
298  }
299  }
300  }
301  } else {
302  // assume it's an eval expression
303  d->completeScriptExpression();
304  }
305 }
306 
double d
Definition: qnumeric_p.h:62
int scheduleGetCompletions(int contextIndex, const QStringList &path)
QScriptCompletionTask(const QString &contents, int cursorPosition, int frameIndex, QScriptDebuggerCommandSchedulerInterface *commandScheduler, QScriptDebuggerJobSchedulerInterface *jobScheduler, QScriptDebuggerConsole *console, QObject *parent=0)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QScriptDebuggerJobSchedulerInterface * jobScheduler
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
#define it(className, varName)
The QScriptDebuggerResponse class represents a front-end&#39;s response to a QScriptDebuggerCommand.
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
int pos(int nth=0) const
Returns the position of the nth captured text in the searched string.
Definition: qregexp.cpp:4337
int matchedLength() const
Returns the length of the last matched string, or -1 if there was no match.
Definition: qregexp.cpp:4193
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QScriptCompleteScriptsJob(const QString &prefix, QScriptCompletionTaskPrivate *task, QScriptDebuggerCommandSchedulerInterface *scheduler)
void handleResponse(const QScriptDebuggerResponse &response, int)
QScriptCompletionTaskPrivate * m_task
The QScriptDebuggerConsole class provides the core functionality of a debugger console.
QStringList toStringList() const
Returns the variant as a QStringList if the variant has type() StringList, String ...
Definition: qvariant.cpp:2259
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QScriptScriptMap resultAsScripts() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int indexIn(const QString &str, int offset=0, CaretMode caretMode=CaretAtZero) const
Attempts to find a match in str from position offset (0 by default).
Definition: qregexp.cpp:4136
QScriptDebuggerConsole * console
QScriptCompleteExpressionJob(int frameIndex, const QStringList &path, QScriptCompletionTaskPrivate *task, QScriptDebuggerCommandSchedulerInterface *scheduler)
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
QStringList capturedTexts() const
Returns a list of the captured text strings.
Definition: qregexp.cpp:4267
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QScriptCompletionTaskPrivate * m_task
T value(int i) const
Returns the value at index position i in the list.
Definition: qlist.h:661
static bool isPrefixOf(const QString &prefix, const QString &what)
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:202
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QString & append(QChar c)
Definition: qstring.cpp:1777
The QScriptDebuggerConsoleCommand class is the base class of console commands.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QObject * parent
Definition: qobject.h:92
QScriptDebuggerCommandSchedulerInterface * commandScheduler
static QString fileName(const QString &fileUrl)
QVariant result() const
Returns the result of this response.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void handleResponse(const QScriptDebuggerResponse &response, int)
bool isNumber() const
Returns true if the character is a number (Number_* categories, not just 0-9); otherwise returns fals...
Definition: qchar.cpp:669
bool isLetterOrNumber() const
Returns true if the character is a letter or number (Letter_* or Number_* categories); otherwise retu...
Definition: qchar.cpp:681
virtual int scheduleJob(QScriptDebuggerJob *job)=0