source: trunk/src/scripttools/debugging/qscriptdebuggerconsole.cpp@ 506

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

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

File size: 11.7 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 "qscriptdebuggerconsole_p.h"
43#include "qscriptdebuggerconsolecommandjob_p.h"
44#include "qscriptdebuggerconsolecommandmanager_p.h"
45#include "qscriptdebuggerscriptedconsolecommand_p.h"
46#include "qscriptmessagehandlerinterface_p.h"
47
48#include <QtCore/qdir.h>
49#include <QtCore/qfileinfo.h>
50#include <QtCore/qstring.h>
51#include <QtCore/qstringlist.h>
52#include <QtCore/qdebug.h>
53#include <QtScript/qscriptengine.h>
54
55QT_BEGIN_NAMESPACE
56
57/*!
58 \since 4.5
59 \class QScriptDebuggerConsole
60 \internal
61
62 \brief The QScriptDebuggerConsole class provides the core functionality of a debugger console.
63*/
64
65class QScriptDebuggerConsolePrivate
66{
67 Q_DECLARE_PUBLIC(QScriptDebuggerConsole)
68public:
69 QScriptDebuggerConsolePrivate(QScriptDebuggerConsole*);
70 ~QScriptDebuggerConsolePrivate();
71
72 void loadScriptedCommands(const QString &scriptsPath,
73 QScriptMessageHandlerInterface *messageHandler);
74 QScriptDebuggerConsoleCommandJob *createJob(
75 const QString &command,
76 QScriptMessageHandlerInterface *messageHandler,
77 QScriptDebuggerCommandSchedulerInterface *commandScheduler);
78
79 QScriptDebuggerConsoleCommandManager *commandManager;
80 QString commandPrefix;
81 QString input;
82 QStringList commandHistory;
83 int currentFrameIndex;
84 qint64 currentScriptId;
85 int currentLineNumber;
86 int evaluateAction;
87 qint64 sessionId;
88
89 QScriptDebuggerConsole *q_ptr;
90};
91
92QScriptDebuggerConsolePrivate::QScriptDebuggerConsolePrivate(QScriptDebuggerConsole* parent)
93 : q_ptr(parent)
94{
95 sessionId = 0;
96 currentFrameIndex = 0;
97 currentScriptId = -1;
98 currentLineNumber = -1;
99 evaluateAction = 0;
100 commandPrefix = QLatin1String(".");
101 commandManager = new QScriptDebuggerConsoleCommandManager();
102}
103
104QScriptDebuggerConsolePrivate::~QScriptDebuggerConsolePrivate()
105{
106 delete commandManager;
107}
108
109/*!
110 Loads command definitions from scripts located in the given \a scriptsPath.
111*/
112void QScriptDebuggerConsolePrivate::loadScriptedCommands(
113 const QString &scriptsPath,
114 QScriptMessageHandlerInterface *messageHandler)
115{
116 QDir dir(scriptsPath);
117 QFileInfoList entries = dir.entryInfoList(QStringList()
118 << QLatin1String("*.qs"));
119 for (int i = 0; i < entries.size(); ++i) {
120 const QFileInfo &fi = entries.at(i);
121 QString fileName = fi.fileName();
122 QFile file(scriptsPath + QLatin1Char('/') + fileName);
123 if (!file.open(QIODevice::ReadOnly))
124 continue;
125 QTextStream stream(&file);
126 QString program = stream.readAll();
127 QScriptDebuggerScriptedConsoleCommand *command;
128 command = QScriptDebuggerScriptedConsoleCommand::parse(
129 program, fileName, messageHandler);
130 if (!command)
131 continue;
132 commandManager->addCommand(command);
133 }
134}
135
136
137/*!
138 Creates a job that will execute the given debugger \a command.
139 Returns the new job, or 0 if the command is undefined.
140*/
141QScriptDebuggerConsoleCommandJob *QScriptDebuggerConsolePrivate::createJob(
142 const QString &command, QScriptMessageHandlerInterface *messageHandler,
143 QScriptDebuggerCommandSchedulerInterface *commandScheduler)
144{
145 QString name;
146 int i = command.indexOf(QLatin1Char(' '));
147 if (i == -1) {
148 name = command;
149 i = name.size();
150 } else {
151 name = command.left(i);
152 }
153 if (name.isEmpty())
154 return 0;
155 QScriptDebuggerConsoleCommand *cmd = commandManager->findCommand(name);
156 if (!cmd) {
157 // try to auto-complete
158 QStringList completions = commandManager->completions(name);
159 if (!completions.isEmpty()) {
160 if (completions.size() > 1) {
161 QString msg;
162 msg.append(QString::fromLatin1("Ambiguous command \"%0\": ")
163 .arg(name));
164 for (int j = 0; j < completions.size(); ++j) {
165 if (j > 0)
166 msg.append(QString::fromLatin1(", "));
167 msg.append(completions.at(j));
168 }
169 msg.append(QString::fromLatin1("."));
170 messageHandler->message(QtWarningMsg, msg);