2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "qtscripteditor.h"
|
|
|
|
|
#include "qtscripteditorconstants.h"
|
|
|
|
|
#include "qtscripthighlighter.h"
|
|
|
|
|
#include "qtscripteditorplugin.h"
|
2010-01-08 14:55:11 +01:00
|
|
|
|
|
|
|
|
#include <qscripthighlighter/qscriptindenter.h>
|
2010-01-12 14:42:45 +01:00
|
|
|
#include <qscripthighlighter/qscriptincrementalscanner.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-03-30 23:32:38 +02:00
|
|
|
#include "parser/javascriptengine_p.h"
|
|
|
|
|
#include "parser/javascriptparser_p.h"
|
|
|
|
|
#include "parser/javascriptlexer_p.h"
|
|
|
|
|
#include "parser/javascriptnodepool_p.h"
|
|
|
|
|
#include "parser/javascriptastvisitor_p.h"
|
|
|
|
|
#include "parser/javascriptast_p.h"
|
|
|
|
|
|
2009-11-09 17:35:20 +01:00
|
|
|
#include <utils/uncommentselection.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2009-01-13 13:39:31 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <texteditor/basetextdocument.h>
|
|
|
|
|
#include <texteditor/fontsettings.h>
|
|
|
|
|
#include <texteditor/textblockiterator.h>
|
|
|
|
|
#include <texteditor/texteditorconstants.h>
|
2009-03-30 23:59:31 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-03-30 23:32:38 +02:00
|
|
|
#include <QtCore/QTimer>
|
2009-03-31 00:57:31 +02:00
|
|
|
#include <QtCore/QtDebug>
|
|
|
|
|
|
|
|
|
|
#include <QtGui/QComboBox>
|
2009-06-16 13:42:33 +02:00
|
|
|
#include <QtGui/QHBoxLayout>
|
|
|
|
|
#include <QtGui/QMenu>
|
2009-07-16 17:34:04 +02:00
|
|
|
#include <QtGui/QToolBar>
|
2009-03-30 23:32:38 +02:00
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100
|
|
|
|
|
};
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-03-31 00:57:31 +02:00
|
|
|
using namespace JavaScript::AST;
|
2010-01-12 14:42:45 +01:00
|
|
|
using namespace SharedTools;
|
2009-03-31 00:57:31 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace QtScriptEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2009-03-31 00:57:31 +02:00
|
|
|
class FindDeclarations: protected Visitor
|
|
|
|
|
{
|
|
|
|
|
QList<Declaration> declarations;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
QList<Declaration> accept(JavaScript::AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
JavaScript::AST::Node::acceptChild(node, this);
|
|
|
|
|
return declarations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
using Visitor::visit;
|
|
|
|
|
|
|
|
|
|
virtual bool visit(FunctionExpression *)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(FunctionDeclaration *ast)
|
|
|
|
|
{
|
2009-04-02 09:50:17 +02:00
|
|
|
if (! ast->name)
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-03-31 00:57:31 +02:00
|
|
|
QString text = ast->name->asString();
|
|
|
|
|
|
|
|
|
|
text += QLatin1Char('(');
|
|
|
|
|
for (FormalParameterList *it = ast->formals; it; it = it->next) {
|
2009-04-02 09:50:17 +02:00
|
|
|
if (it->name)
|
|
|
|
|
text += it->name->asString();
|
2009-03-31 00:57:31 +02:00
|
|
|
|
|
|
|
|
if (it->next)
|
|
|
|
|
text += QLatin1String(", ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text += QLatin1Char(')');
|
|
|
|
|
|
|
|
|
|
Declaration d;
|
|
|
|
|
d.text = text;
|
2009-04-02 09:50:17 +02:00
|
|
|
d.startLine = ast->startLine;
|
2009-03-31 00:57:31 +02:00
|
|
|
d.startColumn = ast->startColumn;
|
|
|
|
|
d.endLine = ast->endLine;
|
|
|
|
|
d.endColumn = ast->endColumn;
|
|
|
|
|
|
|
|
|
|
declarations.append(d);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(VariableDeclaration *ast)
|
|
|
|
|
{
|
2009-04-02 09:50:17 +02:00
|
|
|
if (! ast->name)
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-03-31 00:57:31 +02:00
|
|
|
Declaration d;
|
|
|
|
|
d.text = ast->name->asString();
|
|
|
|
|
d.startLine= ast->startLine;
|
|
|
|
|
d.startColumn = ast->startColumn;
|
|
|
|
|
d.endLine = ast->endLine;
|
|
|
|
|
d.endColumn = ast->endColumn;
|
|
|
|
|
|
|
|
|
|
declarations.append(d);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
ScriptEditorEditable::ScriptEditorEditable(ScriptEditor *editor, const QList<int>& context)
|
|
|
|
|
: BaseTextEditorEditable(editor), m_context(context)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptEditor::ScriptEditor(const Context &context,
|
|
|
|
|
QWidget *parent) :
|
|
|
|
|
TextEditor::BaseTextEditor(parent),
|
2009-03-31 00:57:31 +02:00
|
|
|
m_context(context),
|
|
|
|
|
m_methodCombo(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
setParenthesesMatchingEnabled(true);
|
|
|
|
|
setMarksVisible(true);
|
2009-03-16 17:23:50 +01:00
|
|
|
setCodeFoldingSupported(true);
|
2008-12-02 12:01:29 +01:00
|
|
|
setCodeFoldingVisible(true);
|
|
|
|
|
setMimeType(QtScriptEditor::Constants::C_QTSCRIPTEDITOR_MIMETYPE);
|
|
|
|
|
|
2009-03-30 23:32:38 +02:00
|
|
|
m_updateDocumentTimer = new QTimer(this);
|
|
|
|
|
m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
|
|
|
|
|
m_updateDocumentTimer->setSingleShot(true);
|
|
|
|
|
|
|
|
|
|
connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
|
|
|
|
|
|
|
|
|
|
connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
baseTextDocument()->setSyntaxHighlighter(new QtScriptHighlighter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptEditor::~ScriptEditor()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-01 16:25:16 +02:00
|
|
|
QList<Declaration> ScriptEditor::declarations() const
|
|
|
|
|
{ return m_declarations; }
|
|
|
|
|
|
2009-04-02 10:41:08 +02:00
|
|
|
QStringList ScriptEditor::words() const
|
|
|
|
|
{ return m_words; }
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Core::IEditor *ScriptEditorEditable::duplicate(QWidget *parent)
|
|
|
|
|
{
|
2009-03-30 23:59:31 +02:00
|
|
|
ScriptEditor *newEditor = new ScriptEditor(m_context, parent);
|
|
|
|
|
newEditor->duplicateFrom(editor());
|
|
|
|
|
QtScriptEditorPlugin::instance()->initializeEditor(newEditor);
|
|
|
|
|
return newEditor->editableInterface();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-07 18:17:24 +01:00
|
|
|
QString ScriptEditorEditable::id() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-07 18:17:24 +01:00
|
|
|
return QtScriptEditor::Constants::C_QTSCRIPTEDITOR_ID;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptEditor::Context ScriptEditorEditable::context() const
|
|
|
|
|
{
|
|
|
|
|
return m_context;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-30 23:32:38 +02:00
|
|
|
void ScriptEditor::updateDocument()
|
|
|
|
|
{
|
|
|
|
|
m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEditor::updateDocumentNow()
|
|
|
|
|
{
|
|
|
|
|
// ### move in the parser thread.
|
|
|
|
|
|
|
|
|
|
m_updateDocumentTimer->stop();
|
|
|
|
|
|
|
|
|
|
const QString fileName = file()->fileName();
|
|
|
|
|
const QString code = toPlainText();
|
|
|
|
|
|
|
|
|
|
JavaScriptParser parser;
|
|
|
|
|
JavaScriptEnginePrivate driver;
|
|
|
|
|
|
|
|
|
|
JavaScript::NodePool nodePool(fileName, &driver);
|
|
|
|
|
driver.setNodePool(&nodePool);
|
|
|
|
|
|
|
|
|
|
JavaScript::Lexer lexer(&driver);
|
|
|
|
|
lexer.setCode(code, /*line = */ 1);
|
|
|
|
|
driver.setLexer(&lexer);
|
|
|
|
|
|
2009-04-02 09:50:17 +02:00
|
|
|
if (parser.parse(&driver)) {
|
|
|
|
|
JavaScript::AST::Visitor v;
|
|
|
|
|
driver.ast()->accept(&v);
|
2009-03-30 23:32:38 +02:00
|
|
|
|
2009-04-02 09:50:17 +02:00
|
|
|
FindDeclarations decls;
|
|
|
|
|
m_declarations = decls.accept(driver.ast());
|
2009-03-31 00:57:31 +02:00
|
|
|
|
2009-04-02 10:41:08 +02:00
|
|
|
m_words.clear();
|
|
|
|
|
foreach (const JavaScriptNameIdImpl &id, driver.literals())
|
|
|
|
|
m_words.append(id.asString());
|
|
|
|
|
|
2009-04-02 09:50:17 +02:00
|
|
|
QStringList items;
|
|
|
|
|
items.append(tr("<Select Symbol>"));
|
2009-03-31 00:57:31 +02:00
|
|
|
|
2009-04-02 09:50:17 +02:00
|
|
|
foreach (Declaration decl, m_declarations)
|
|
|
|
|
items.append(decl.text);
|
2009-03-31 00:57:31 +02:00
|
|
|
|
2009-04-02 09:50:17 +02:00
|
|
|
m_methodCombo->clear();
|
|
|
|
|
m_methodCombo->addItems(items);
|
|
|
|
|
updateMethodBoxIndex();
|
|
|
|
|
}
|
2009-04-01 16:25:16 +02:00
|
|
|
|
|
|
|
|
QList<QTextEdit::ExtraSelection> selections;
|
2009-03-31 00:57:31 +02:00
|
|
|
|
2009-04-01 16:25:16 +02:00
|
|
|
QTextCharFormat errorFormat;
|
|
|
|
|
errorFormat.setUnderlineColor(Qt::red);
|
|
|
|
|
errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
2009-03-31 00:57:31 +02:00
|
|
|
|
2009-04-01 16:25:16 +02:00
|
|
|
QTextCharFormat warningFormat;
|
2009-04-06 17:35:29 +02:00
|
|
|
warningFormat.setUnderlineColor(Qt::darkYellow);
|
2009-04-01 16:25:16 +02:00
|
|
|
warningFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
2009-03-30 23:32:38 +02:00
|
|
|
|
2009-04-01 16:25:16 +02:00
|
|
|
QTextEdit::ExtraSelection sel;
|
|
|
|
|
|
|
|
|
|
foreach (const JavaScriptParser::DiagnosticMessage &d, parser.diagnosticMessages()) {
|
2009-04-02 09:50:17 +02:00
|
|
|
int line = d.line;
|
|
|
|
|
int column = d.column;
|
2009-03-30 23:32:38 +02:00
|
|
|
|
2009-04-02 09:50:17 +02:00
|
|
|
if (column == 0)
|
|
|
|
|
column = 1;
|
2009-04-01 16:25:16 +02:00
|
|
|
|
2009-04-02 19:53:26 +02:00
|
|
|
if (d.isWarning())
|
|
|
|
|
sel.format = warningFormat;
|
|
|
|
|
else
|
|
|
|
|
sel.format = errorFormat;
|
2009-03-30 23:32:38 +02:00
|
|
|
|
2009-04-02 09:50:17 +02:00
|
|
|
QTextCursor c(document()->findBlockByNumber(line - 1));
|
|
|
|
|
sel.cursor = c;
|
|
|
|
|
sel.cursor.setPosition(c.position() + column - 1);
|
2009-03-30 23:32:38 +02:00
|
|
|
sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
|
|
selections.append(sel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setExtraSelections(CodeWarningsSelection, selections);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-31 00:57:31 +02:00
|
|
|
void ScriptEditor::jumpToMethod(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index) {
|
|
|
|
|
Declaration d = m_declarations.at(index - 1);
|
|
|
|
|
gotoLine(d.startLine, d.startColumn - 1);
|
|
|
|
|
setFocus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEditor::updateMethodBoxIndex()
|
|
|
|
|
{
|
|
|
|
|
int line = 0, column = 0;
|
|
|
|
|
convertPosition(position(), &line, &column);
|
|
|
|
|
|
|
|
|
|
int currentSymbolIndex = 0;
|
|
|
|
|
|
2009-03-31 11:11:05 +02:00
|
|
|
int index = 0;
|
|
|
|
|
while (index < m_declarations.size()) {
|
|
|
|
|
const Declaration &d = m_declarations.at(index++);
|
|
|
|
|
|
|
|
|
|
if (line < d.startLine)
|
|
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
currentSymbolIndex = index;
|
2009-03-31 00:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_methodCombo->setCurrentIndex(currentSymbolIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEditor::updateMethodBoxToolTip()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEditor::updateFileName()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void ScriptEditor::setFontSettings(const TextEditor::FontSettings &fs)
|
|
|
|
|
{
|
|
|
|
|
TextEditor::BaseTextEditor::setFontSettings(fs);
|
|
|
|
|
QtScriptHighlighter *highlighter = qobject_cast<QtScriptHighlighter*>(baseTextDocument()->syntaxHighlighter());
|
|
|
|
|
if (!highlighter)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
static QVector<QString> categories;
|
|
|
|
|
if (categories.isEmpty()) {
|
|
|
|
|
categories << QLatin1String(TextEditor::Constants::C_NUMBER)
|
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_STRING)
|
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_TYPE)
|
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_KEYWORD)
|
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
|
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_LABEL)
|
2009-10-01 13:34:02 +02:00
|
|
|
<< QLatin1String(TextEditor::Constants::C_COMMENT)
|
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
highlighter->setFormats(fs.toTextCharFormats(categories));
|
|
|
|
|
highlighter->rehighlight();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ScriptEditor::isElectricCharacter(const QChar &ch) const
|
|
|
|
|
{
|
|
|
|
|
if (ch == QLatin1Char('{') || ch == QLatin1Char('}'))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
|
|
|
|
|
{
|
2010-01-07 16:23:43 +01:00
|
|
|
TextEditor::TabSettings ts = tabSettings();
|
2010-01-08 14:55:11 +01:00
|
|
|
SharedTools::QScriptIndenter indenter;
|
2010-01-07 16:23:43 +01:00
|
|
|
indenter.setTabSize(ts.m_tabSize);
|
|
|
|
|
indenter.setIndentSize(ts.m_indentSize);
|
|
|
|
|
|
2010-01-08 14:55:11 +01:00
|
|
|
const int indent = indenter.indentForBottomLine(doc->begin(), block.next(), typedChar);
|
2010-01-07 16:23:43 +01:00
|
|
|
ts.indentLine(block, indent);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-31 00:57:31 +02:00
|
|
|
TextEditor::BaseTextEditorEditable *ScriptEditor::createEditableInterface()
|
|
|
|
|
{
|
|
|
|
|
ScriptEditorEditable *editable = new ScriptEditorEditable(this, m_context);
|
|
|
|
|
createToolBar(editable);
|
|
|
|
|
return editable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEditor::createToolBar(ScriptEditorEditable *editable)
|
|
|
|
|
{
|
|
|
|
|
m_methodCombo = new QComboBox;
|
|
|
|
|
m_methodCombo->setMinimumContentsLength(22);
|
|
|
|
|
//m_methodCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
|
|
|
|
|
|
|
|
|
|
// Make the combo box prefer to expand
|
|
|
|
|
QSizePolicy policy = m_methodCombo->sizePolicy();
|
|
|
|
|
policy.setHorizontalPolicy(QSizePolicy::Expanding);
|
|
|
|
|
m_methodCombo->setSizePolicy(policy);
|
|
|
|
|
|
|
|
|
|
connect(m_methodCombo, SIGNAL(activated(int)), this, SLOT(jumpToMethod(int)));
|
|
|
|
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateMethodBoxIndex()));
|
|
|
|
|
connect(m_methodCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMethodBoxToolTip()));
|
|
|
|
|
|
|
|
|
|
connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
|
|
|
|
|
|
2009-07-15 16:23:07 +02:00
|
|
|
QToolBar *toolBar = static_cast<QToolBar*>(editable->toolBar());
|
2009-03-31 00:57:31 +02:00
|
|
|
|
|
|
|
|
QList<QAction*> actions = toolBar->actions();
|
2009-06-16 13:42:33 +02:00
|
|
|
QWidget *w = toolBar->widgetForAction(actions.first());
|
|
|
|
|
static_cast<QHBoxLayout*>(w->layout())->insertWidget(0, m_methodCombo, 1);
|
2009-03-31 00:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void ScriptEditor::contextMenuEvent(QContextMenuEvent *e)
|
|
|
|
|
{
|
2009-11-09 16:47:48 +01:00
|
|
|
QMenu *menu = new QMenu();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-20 15:31:33 +01:00
|
|
|
if (Core::ActionContainer *mcontext = Core::ICore::instance()->actionManager()->actionContainer(QtScriptEditor::Constants::M_CONTEXT)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
QMenu *contextMenu = mcontext->menu();
|
2008-12-09 11:07:24 +01:00
|
|
|
foreach (QAction *action, contextMenu->actions())
|
2008-12-02 12:01:29 +01:00
|
|
|
menu->addAction(action);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-09 16:47:48 +01:00
|
|
|
appendStandardContextMenuActions(menu);
|
2008-12-02 12:01:29 +01:00
|
|
|
menu->exec(e->globalPos());
|
|
|
|
|
delete menu;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-09 17:35:20 +01:00
|
|
|
void ScriptEditor::unCommentSelection()
|
|
|
|
|
{
|
|
|
|
|
Utils::unCommentSelection(this);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-13 13:51:01 +01:00
|
|
|
static bool isCompleteStringLiteral(const QStringRef &text)
|
|
|
|
|
{
|
|
|
|
|
if (text.length() < 2)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const QChar quote = text.at(0);
|
|
|
|
|
|
|
|
|
|
if (text.at(text.length() - 1) == quote)
|
|
|
|
|
return text.at(text.length() - 2) != QLatin1Char('\\'); // ### not exactly.
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-12 14:42:45 +01:00
|
|
|
bool ScriptEditor::contextAllowsAutoParentheses(const QTextCursor &cursor, const QString &textToInsert) const
|
|
|
|
|
{
|
|
|
|
|
QChar ch;
|
|
|
|
|
|
|
|
|
|
if (! textToInsert.isEmpty())
|
|
|
|
|
ch = textToInsert.at(0);
|
|
|
|
|
|
2010-01-12 15:59:22 +01:00
|
|
|
switch (ch.unicode()) {
|
|
|
|
|
case '\'':
|
|
|
|
|
case '"':
|
|
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
|
case '[':
|
|
|
|
|
case '{':
|
|
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
|
case ']':
|
|
|
|
|
case '}':
|
|
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (ch.isNull())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} // end of switch
|
|
|
|
|
|
2010-01-12 14:42:45 +01:00
|
|
|
const QString blockText = cursor.block().text();
|
2010-01-12 15:59:22 +01:00
|
|
|
int blockState = cursor.block().userState();
|
|
|
|
|
if (blockState == -1)
|
|
|
|
|
blockState = 0;
|
|
|
|
|
else
|
|
|
|
|
blockState = blockState & 0xFF;
|
2010-01-12 14:42:45 +01:00
|
|
|
|
|
|
|
|
QScriptIncrementalScanner tokenize;
|
|
|
|
|
const QList<QScriptIncrementalScanner::Token> tokens = tokenize(blockText, blockState);
|
|
|
|
|
const int pos = cursor.columnNumber();
|
|
|
|
|
|
2010-01-13 13:51:01 +01:00
|
|
|
int tokenIndex = 0;
|
|
|
|
|
for (; tokenIndex < tokens.size(); ++tokenIndex) {
|
2010-01-12 14:42:45 +01:00
|
|
|
const QScriptIncrementalScanner::Token &token = tokens.at(tokenIndex);
|
2010-01-13 13:51:01 +01:00
|
|
|
|
2010-01-12 15:59:22 +01:00
|
|
|
if (pos >= token.begin()) {
|
|
|
|
|
if (pos < token.end())
|
|
|
|
|
break;
|
2010-01-13 13:51:01 +01:00
|
|
|
|
|
|
|
|
else if (pos == token.end() && (token.is(QScriptIncrementalScanner::Token::Comment) ||
|
|
|
|
|
token.is(QScriptIncrementalScanner::Token::String)))
|
2010-01-12 15:59:22 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2010-01-12 14:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-13 13:51:01 +01:00
|
|
|
if (tokenIndex != tokens.size()) {
|
2010-01-12 14:42:45 +01:00
|
|
|
const QScriptIncrementalScanner::Token &token = tokens.at(tokenIndex);
|
|
|
|
|
|
|
|
|
|
switch (token.kind) {
|
|
|
|
|
case QScriptIncrementalScanner::Token::Comment:
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case QScriptIncrementalScanner::Token::String: {
|
2010-01-13 13:51:01 +01:00
|
|
|
const QStringRef tokenText = blockText.midRef(token.offset, token.length);
|
|
|
|
|
const QChar quote = tokenText.at(0);
|
|
|
|
|
|
|
|
|
|
if (ch == quote && isCompleteStringLiteral(tokenText))
|
|
|
|
|
break;
|
|
|
|
|
|
2010-01-12 14:42:45 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
} // end of switch
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-12 15:59:22 +01:00
|
|
|
return true;
|
2010-01-12 14:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ScriptEditor::isInComment(const QTextCursor &) const
|
|
|
|
|
{
|
|
|
|
|
// ### implement me
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ScriptEditor::insertMatchingBrace(const QTextCursor &tc, const QString &text, const QChar &, int *skippedChars) const
|
|
|
|
|
{
|
|
|
|
|
if (text.length() != 1)
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
const QChar la = characterAt(tc.position());
|
|
|
|
|
|
|
|
|
|
const QChar ch = text.at(0);
|
|
|
|
|
switch (ch.unicode()) {
|
|
|
|
|
case '\'':
|
|
|
|
|
if (la != ch)
|
|
|
|
|
return QString(ch);
|
|
|
|
|
++*skippedChars;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
|
if (la != ch)
|
|
|
|
|
return QString(ch);
|
|
|
|
|
++*skippedChars;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
|
return QString(QLatin1Char(')'));
|
|
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
|
return QString(QLatin1Char(']'));
|
|
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
|
return QString(); // nothing to do.
|
|
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
|
case ']':
|
|
|
|
|
case '}':
|
|
|
|
|
case ';':
|
|
|
|
|
if (la == ch)
|
|
|
|
|
++*skippedChars;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
} // end of switch
|
|
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ScriptEditor::insertParagraphSeparator(const QTextCursor &) const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String("}\n");
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QtScriptEditor
|