Files
qt-creator/src/plugins/qmleditor/qmleditor.cpp

794 lines
22 KiB
C++
Raw Normal View History

2009-04-22 15:21:04 +02:00
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2009-04-22 15:21:04 +02:00
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** 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.
2009-04-22 15:21:04 +02:00
**
**************************************************************************/
2009-09-30 17:43:21 +02:00
#include "qmleditor.h"
#include "qmleditorconstants.h"
#include "qmlhighlighter.h"
#include "qmleditorplugin.h"
#include "qmldocument.h"
#include "qmlmodelmanager.h"
2009-04-22 15:21:04 +02:00
#include "qmljsastvisitor_p.h"
#include "qmljsast_p.h"
#include "qmljsengine_p.h"
#include "qmlexpressionundercursor.h"
#include "qmllookupcontext.h"
#include "qmlresolveexpression.h"
2009-09-11 14:42:50 +02:00
#include "rewriter_p.h"
#include "idcollector.h"
2009-09-11 14:42:50 +02:00
2009-04-22 15:21:04 +02:00
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/uniqueidmanager.h>
2009-09-04 16:51:11 +02:00
#include <extensionsystem/pluginmanager.h>
2009-04-22 15:21:04 +02:00
#include <texteditor/basetextdocument.h>
#include <texteditor/fontsettings.h>
#include <texteditor/textblockiterator.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <utils/uncommentselection.h>
2009-04-22 15:21:04 +02:00
#include <QtCore/QTimer>
#include <QtGui/QMenu>
#include <QtGui/QComboBox>
2009-05-05 15:33:39 +02:00
#include <QtGui/QInputDialog>
#include <QtGui/QMainWindow>
2009-07-16 17:34:04 +02:00
#include <QtGui/QToolBar>
2009-04-22 15:21:04 +02:00
enum {
UPDATE_DOCUMENT_DEFAULT_INTERVAL = 250
2009-04-22 15:21:04 +02:00
};
using namespace QmlJS;
using namespace QmlJS::AST;
2009-04-22 15:21:04 +02:00
2009-09-30 17:43:21 +02:00
namespace QmlEditor {
2009-04-22 15:21:04 +02:00
namespace Internal {
2009-05-06 17:26:29 +02:00
class FindWords: protected Visitor
{
public:
QStringList operator()(AST::Node *node)
{
_words.clear();
accept(node);
return QStringList(_words.toList());
}
2009-05-06 17:26:29 +02:00
protected:
void accept(AST::Node *node)
{ AST::Node::acceptChild(node, this); }
using Visitor::visit;
using Visitor::endVisit;
void addWords(AST::UiQualifiedId *id)
{
for (; id; id = id->next) {
if (id->name)
_words.insert(id->name->asString());
}
}
virtual bool visit(AST::UiPublicMember *node)
{
if (node->name)
_words.insert(node->name->asString());
return true;
}
virtual bool visit(AST::UiQualifiedId *node)
{
if (node->name)
_words.insert(node->name->asString());
return true;
}
virtual bool visit(AST::IdentifierExpression *node)
{
if (node->name)
_words.insert(node->name->asString());
return true;
}
virtual bool visit(AST::FieldMemberExpression *node)
{
if (node->name)
_words.insert(node->name->asString());
return true;
}
virtual bool visit(AST::FunctionExpression *node)
{
if (node->name)
_words.insert(node->name->asString());
for (AST::FormalParameterList *it = node->formals; it; it = it->next) {
if (it->name)
_words.insert(it->name->asString());
}
return true;
}
virtual bool visit(AST::FunctionDeclaration *node)
{
if (node->name)
_words.insert(node->name->asString());
for (AST::FormalParameterList *it = node->formals; it; it = it->next) {
if (it->name)
_words.insert(it->name->asString());
}
return true;
}
virtual bool visit(AST::VariableDeclaration *node)
{
if (node->name)
_words.insert(node->name->asString());
return true;
}
2009-05-06 17:26:29 +02:00
private:
QSet<QString> _words;
2009-05-06 17:26:29 +02:00
};
class FindIdDeclarations: protected Visitor
2009-05-04 18:58:36 +02:00
{
public:
typedef QMap<QString, QList<AST::SourceLocation> > Result;
Result operator()(AST::Node *node)
{
_ids.clear();
_maybeIds.clear();
accept(node);
return _ids;
}
2009-05-04 18:58:36 +02:00
protected:
QString asString(AST::UiQualifiedId *id)
{
QString text;
for (; id; id = id->next) {
if (id->name)
text += id->name->asString();
else
text += QLatin1Char('?');
if (id->next)
text += QLatin1Char('.');
}
return text;
}
void accept(AST::Node *node)
{ AST::Node::acceptChild(node, this); }
using Visitor::visit;
using Visitor::endVisit;
virtual bool visit(AST::UiScriptBinding *node)
{
if (asString(node->qualifiedId) == QLatin1String("id")) {
if (AST::ExpressionStatement *stmt = AST::cast<AST::ExpressionStatement*>(node->statement)) {
if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(stmt->expression)) {
if (idExpr->name) {
const QString id = idExpr->name->asString();
QList<AST::SourceLocation> *locs = &_ids[id];
locs->append(idExpr->firstSourceLocation());
locs->append(_maybeIds.value(id));
_maybeIds.remove(id);
return false;
}
}
}
}
accept(node->statement);
return false;
}
virtual bool visit(AST::IdentifierExpression *node)
{
if (node->name) {
const QString name = node->name->asString();
if (_ids.contains(name))
_ids[name].append(node->identifierToken);
else
_maybeIds[name].append(node->identifierToken);
}
return false;
}
2009-05-04 18:58:36 +02:00
private:
Result _ids;
Result _maybeIds;
2009-05-04 18:58:36 +02:00
};
2009-04-22 15:21:04 +02:00
class FindDeclarations: protected Visitor
{
QList<Declaration> _declarations;
int _depth;
2009-04-22 15:21:04 +02:00
public:
QList<Declaration> operator()(AST::Node *node)
{
_depth = -1;
_declarations.clear();
accept(node);
return _declarations;
}
2009-04-22 15:21:04 +02:00
protected:
using Visitor::visit;
using Visitor::endVisit;
QString asString(AST::UiQualifiedId *id)
{
QString text;
for (; id; id = id->next) {
if (id->name)
text += id->name->asString();
else
text += QLatin1Char('?');
if (id->next)
text += QLatin1Char('.');
}
2009-04-22 15:21:04 +02:00
return text;
}
2009-04-30 17:23:32 +02:00
void accept(AST::Node *node)
{ AST::Node::acceptChild(node, this); }
void init(Declaration *decl, AST::UiObjectMember *member)
{
const SourceLocation first = member->firstSourceLocation();
const SourceLocation last = member->lastSourceLocation();
decl->startLine = first.startLine;
decl->startColumn = first.startColumn;
decl->endLine = last.startLine;
decl->endColumn = last.startColumn + last.length;
}
2009-04-30 17:23:32 +02:00
virtual bool visit(AST::UiObjectDefinition *node)
{
++_depth;
2009-04-30 17:23:32 +02:00
Declaration decl;
init(&decl, node);
2009-04-30 17:23:32 +02:00
decl.text.fill(QLatin1Char(' '), _depth);
if (node->qualifiedTypeNameId)
decl.text.append(asString(node->qualifiedTypeNameId));
else
decl.text.append(QLatin1Char('?'));
2009-04-30 17:23:32 +02:00
_declarations.append(decl);
2009-04-30 17:23:32 +02:00
return true; // search for more bindings
}
2009-04-30 17:23:32 +02:00
virtual void endVisit(AST::UiObjectDefinition *)
{
--_depth;
}
2009-04-30 17:23:32 +02:00
virtual bool visit(AST::UiObjectBinding *node)
{
++_depth;
2009-04-30 17:23:32 +02:00
Declaration decl;
init(&decl, node);
2009-04-30 17:23:32 +02:00
decl.text.fill(QLatin1Char(' '), _depth);
2009-04-30 17:23:32 +02:00
decl.text.append(asString(node->qualifiedId));
decl.text.append(QLatin1String(": "));
2009-04-30 17:23:32 +02:00
if (node->qualifiedTypeNameId)
decl.text.append(asString(node->qualifiedTypeNameId));
else
decl.text.append(QLatin1Char('?'));
2009-04-30 17:23:32 +02:00
_declarations.append(decl);
2009-04-30 17:23:32 +02:00
return true; // search for more bindings
}
2009-04-30 17:23:32 +02:00
virtual void endVisit(AST::UiObjectBinding *)
{
--_depth;
}
2009-04-30 17:23:32 +02:00
#if 0 // ### ignore script bindings for now.
virtual bool visit(AST::UiScriptBinding *node)
{
++_depth;
2009-04-30 17:23:32 +02:00
Declaration decl;
init(&decl, node);
2009-04-30 17:23:32 +02:00
decl.text.fill(QLatin1Char(' '), _depth);
decl.text.append(asString(node->qualifiedId));
2009-04-30 17:23:32 +02:00
_declarations.append(decl);
2009-04-30 17:23:32 +02:00
return false; // more more bindings in this subtree.
}
2009-04-30 17:23:32 +02:00
virtual void endVisit(AST::UiScriptBinding *)
{
--_depth;
}
#endif
2009-04-22 15:21:04 +02:00
};
ScriptEditorEditable::ScriptEditorEditable(ScriptEditor *editor)
: BaseTextEditorEditable(editor)
2009-04-22 15:21:04 +02:00
{
Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
m_context << uidm->uniqueIdentifier(QmlEditor::Constants::C_QMLEDITOR);
m_context << uidm->uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR);
2009-04-22 15:21:04 +02:00
}
ScriptEditor::ScriptEditor(QWidget *parent) :
TextEditor::BaseTextEditor(parent),
2009-09-04 16:51:11 +02:00
m_methodCombo(0),
m_modelManager(0)
2009-04-22 15:21:04 +02:00
{
setParenthesesMatchingEnabled(true);
setMarksVisible(true);
setCodeFoldingSupported(true);
setCodeFoldingVisible(true);
2009-09-30 17:43:21 +02:00
setMimeType(QmlEditor::Constants::QMLEDITOR_MIMETYPE);
2009-04-22 15:21:04 +02:00
m_updateDocumentTimer = new QTimer(this);
m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
m_updateDocumentTimer->setSingleShot(true);
2009-04-22 15:21:04 +02:00
connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
2009-04-22 15:21:04 +02:00
connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
2009-04-22 15:21:04 +02:00
2009-09-30 17:43:21 +02:00
baseTextDocument()->setSyntaxHighlighter(new QmlHighlighter);
2009-09-04 16:51:11 +02:00
2009-09-30 17:43:21 +02:00
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlModelManagerInterface>();
2009-09-04 16:51:11 +02:00
if (m_modelManager) {
connect(m_modelManager, SIGNAL(documentUpdated(QmlEditor::QmlDocument::Ptr)),
this, SLOT(onDocumentUpdated(QmlEditor::QmlDocument::Ptr)));
2009-09-04 16:51:11 +02:00
}
2009-04-22 15:21:04 +02:00
}
ScriptEditor::~ScriptEditor()
{
}
QList<Declaration> ScriptEditor::declarations() const
{ return m_declarations; }
QStringList ScriptEditor::words() const
{ return m_words; }
Core::IEditor *ScriptEditorEditable::duplicate(QWidget *parent)
{
ScriptEditor *newEditor = new ScriptEditor(parent);
newEditor->duplicateFrom(editor());
2009-09-30 17:43:21 +02:00
QmlEditorPlugin::instance()->initializeEditor(newEditor);
return newEditor->editableInterface();
2009-04-22 15:21:04 +02:00
}
const char *ScriptEditorEditable::kind() const
{
2009-09-30 17:43:21 +02:00
return QmlEditor::Constants::C_QMLEDITOR;
2009-04-22 15:21:04 +02:00
}
ScriptEditor::Context ScriptEditorEditable::context() const
{
return m_context;
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::updateDocument()
{
m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::updateDocumentNow()
{
// ### move in the parser thread.
2009-04-22 15:21:04 +02:00
m_updateDocumentTimer->stop();
2009-04-22 15:21:04 +02:00
const QString fileName = file()->fileName();
2009-04-22 15:21:04 +02:00
2009-09-04 16:51:11 +02:00
m_modelManager->updateSourceFiles(QStringList() << fileName);
}
void ScriptEditor::onDocumentUpdated(QmlEditor::QmlDocument::Ptr doc)
2009-09-04 16:51:11 +02:00
{
if (file()->fileName() != doc->fileName())
return;
m_document = doc;
FindIdDeclarations updateIds;
m_ids = updateIds(doc->program());
2009-05-04 18:58:36 +02:00
2009-09-04 16:51:11 +02:00
if (doc->isParsedCorrectly()) {
FindDeclarations findDeclarations;
m_declarations = findDeclarations(doc->program());
2009-04-30 17:23:32 +02:00
FindWords findWords;
m_words = findWords(doc->program());
2009-04-22 15:21:04 +02:00
QStringList items;
items.append(tr("<Select Symbol>"));
2009-04-22 15:21:04 +02:00
foreach (Declaration decl, m_declarations)
items.append(decl.text);
2009-04-22 15:21:04 +02:00
m_methodCombo->clear();
m_methodCombo->addItems(items);
updateMethodBoxIndex();
2009-09-04 16:51:11 +02:00
}
2009-04-22 15:21:04 +02:00
QList<QTextEdit::ExtraSelection> selections;
2009-04-22 15:21:04 +02:00
QTextCharFormat errorFormat;
errorFormat.setUnderlineColor(Qt::red);
errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
2009-04-22 15:21:04 +02:00
QTextEdit::ExtraSelection sel;
2009-04-22 15:21:04 +02:00
m_diagnosticMessages = doc->diagnosticMessages();
2009-04-22 15:21:04 +02:00
foreach (const DiagnosticMessage &d, m_diagnosticMessages) {
int line = d.loc.startLine;
int column = d.loc.startColumn;
2009-04-22 15:21:04 +02:00
if (column == 0)
column = 1;
2009-04-22 15:21:04 +02:00
QTextCursor c(document()->findBlockByNumber(line - 1));
sel.cursor = c;
2009-05-06 15:45:19 +02:00
sel.cursor.setPosition(c.position() + column - 1);
if (sel.cursor.atBlockEnd())
sel.cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
else
sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
2009-05-06 15:45:19 +02:00
sel.format = errorFormat;
2009-04-22 15:21:04 +02:00
selections.append(sel);
}
2009-04-22 15:21:04 +02:00
setExtraSelections(CodeWarningsSelection, selections);
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::jumpToMethod(int index)
{
if (index) {
Declaration d = m_declarations.at(index - 1);
gotoLine(d.startLine, d.startColumn - 1);
setFocus();
}
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::updateMethodBoxIndex()
{
int line = 0, column = 0;
convertPosition(position(), &line, &column);
2009-04-22 15:21:04 +02:00
int currentSymbolIndex = 0;
2009-04-22 15:21:04 +02:00
int index = 0;
while (index < m_declarations.size()) {
const Declaration &d = m_declarations.at(index++);
2009-04-22 15:21:04 +02:00
if (line < d.startLine)
break;
else
currentSymbolIndex = index;
}
2009-04-22 15:21:04 +02:00
m_methodCombo->setCurrentIndex(currentSymbolIndex);
2009-05-04 18:58:36 +02:00
QList<QTextEdit::ExtraSelection> selections;
foreach (const AST::SourceLocation &loc, m_ids.value(wordUnderCursor())) {
if (! loc.isValid())
continue;
2009-05-05 10:46:58 +02:00
QTextEdit::ExtraSelection sel;
sel.format.setBackground(Qt::yellow);
sel.cursor = textCursor();
sel.cursor.setPosition(loc.begin());
sel.cursor.setPosition(loc.end(), QTextCursor::KeepAnchor);
selections.append(sel);
}
2009-05-04 18:58:36 +02:00
setExtraSelections(CodeSemanticsSelection, selections);
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::updateMethodBoxToolTip()
{
}
void ScriptEditor::updateFileName()
{
}
2009-05-05 15:33:39 +02:00
void ScriptEditor::renameIdUnderCursor()
{
const QString id = wordUnderCursor();
bool ok = false;
const QString newId = QInputDialog::getText(Core::ICore::instance()->mainWindow(),
tr("Rename..."),
tr("New id:"),
QLineEdit::Normal,
id, &ok);
if (ok) {
TextWriter writer;
QString code = toPlainText();
foreach (const AST::SourceLocation &loc, m_ids.value(id)) {
writer.replace(loc.offset, loc.length, newId);
}
QTextCursor tc = textCursor();
writer.write(&tc);
}
2009-05-05 15:33:39 +02:00
}
QStringList ScriptEditor::keywords() const
{
QStringList words;
2009-09-30 17:43:21 +02:00
if (QmlHighlighter *highlighter = qobject_cast<QmlHighlighter*>(baseTextDocument()->syntaxHighlighter()))
words = highlighter->keywords().toList();
return words;
}
2009-04-22 15:21:04 +02:00
void ScriptEditor::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditor::setFontSettings(fs);
2009-09-30 17:43:21 +02:00
QmlHighlighter *highlighter = qobject_cast<QmlHighlighter*>(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)
<< QLatin1String(TextEditor::Constants::C_COMMENT)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
}
highlighter->setFormats(fs.toTextCharFormats(categories));
highlighter->rehighlight();
2009-04-22 15:21:04 +02:00
}
2009-05-05 15:33:39 +02:00
QString ScriptEditor::wordUnderCursor() const
{
QTextCursor tc = textCursor();
tc.movePosition(QTextCursor::StartOfWord);
tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
const QString word= tc.selectedText();
return word;
2009-05-05 15:33:39 +02:00
}
2009-04-22 15:21:04 +02:00
bool ScriptEditor::isElectricCharacter(const QChar &ch) const
{
if (ch == QLatin1Char('}')
|| ch == QLatin1Char(']'))
return true;
return false;
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::indentBlock(QTextDocument *, QTextBlock block, QChar typedChar)
2009-04-22 15:21:04 +02:00
{
TextEditor::TabSettings ts = tabSettings();
if (typedChar == QLatin1Char('}') || typedChar == QLatin1Char(']')
|| (typedChar == QChar::Null
&& (block.text().trimmed() == "}" || block.text().trimmed() == "]"))) {
QTextCursor tc(block);
if (typedChar == QLatin1Char('}') || typedChar == QLatin1Char(']'))
tc = textCursor();
if (TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tc)) {
const QString text = tc.block().text();
int indent = ts.columnAt(text, ts.firstNonSpace(text));
ts.indentLine(block, indent);
return;
}
}
int indent = 0;
int extraIndent = 0;
if (block.previous().isValid()) {
const int braceDepth = qMax(0, block.previous().userState() >> 8);
const int previousBraceDepth = qMax(0, block.previous().previous().userState() >> 8);
if (braceDepth > previousBraceDepth)
extraIndent = ts.m_indentSize * (braceDepth - previousBraceDepth);
}
QTextBlock it = block.previous();
for (; it.isValid(); it = it.previous()) {
const QString text = it.text();
if (! text.isEmpty()) {
indent = ts.columnAt(text, ts.firstNonSpace(text));
break;
}
}
ts.indentLine(block, extraIndent + indent);
2009-04-22 15:21:04 +02:00
}
TextEditor::BaseTextEditorEditable *ScriptEditor::createEditableInterface()
{
ScriptEditorEditable *editable = new ScriptEditorEditable(this);
createToolBar(editable);
return editable;
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::createToolBar(ScriptEditorEditable *editable)
{
m_methodCombo = new QComboBox;
m_methodCombo->setMinimumContentsLength(22);
//m_methodCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
2009-04-22 15:21:04 +02:00
// Make the combo box prefer to expand
QSizePolicy policy = m_methodCombo->sizePolicy();
policy.setHorizontalPolicy(QSizePolicy::Expanding);
m_methodCombo->setSizePolicy(policy);
2009-04-22 15:21:04 +02:00
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()));
2009-04-22 15:21:04 +02:00
connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
2009-04-22 15:21:04 +02:00
QToolBar *toolBar = static_cast<QToolBar*>(editable->toolBar());
2009-04-22 15:21:04 +02:00
QList<QAction*> actions = toolBar->actions();
toolBar->insertWidget(actions.first(), m_methodCombo);
2009-04-22 15:21:04 +02:00
}
TextEditor::BaseTextEditor::Link ScriptEditor::findLinkAt(const QTextCursor &cursor, bool /*resolveTarget*/)
2009-09-11 14:42:50 +02:00
{
Link link;
if (!m_modelManager)
return link;
const Snapshot snapshot = m_modelManager->snapshot();
2009-09-30 17:43:21 +02:00
QmlDocument::Ptr doc = snapshot.document(file()->fileName());
2009-09-11 14:42:50 +02:00
if (!doc)
return link;
QTextCursor expressionCursor(cursor);
{
// correct the position by moving to the end of an identifier (if we're hovering over one):
const QString txt = cursor.block().text();
int pos = cursor.position();
forever {
const QChar ch = characterAt(pos);
if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
++pos;
else
break;
}
expressionCursor.setPosition(pos);
}
QmlExpressionUnderCursor expressionUnderCursor;
expressionUnderCursor(expressionCursor, doc);
2009-09-22 17:43:05 +02:00
QmlLookupContext context(expressionUnderCursor.expressionScopes(), doc, snapshot);
QmlResolveExpression resolver(context);
QmlSymbol *symbol = resolver.typeOf(expressionUnderCursor.expressionNode());
if (!symbol)
return link;
if (const QmlSymbolFromFile *target = symbol->asSymbolFromFile()) {
link.pos = expressionUnderCursor.expressionOffset();
link.length = expressionUnderCursor.expressionLength();
link.fileName = target->fileName();
link.line = target->line();
link.column = target->column();
if (link.column > 0)
--link.column;
}
2009-09-11 14:42:50 +02:00
return link;
}
2009-04-22 15:21:04 +02:00
void ScriptEditor::contextMenuEvent(QContextMenuEvent *e)
{
QMenu *menu = new QMenu();
2009-09-30 17:43:21 +02:00
if (Core::ActionContainer *mcontext = Core::ICore::instance()->actionManager()->actionContainer(QmlEditor::Constants::M_CONTEXT)) {
QMenu *contextMenu = mcontext->menu();
foreach (QAction *action, contextMenu->actions())
menu->addAction(action);
}
const QString id = wordUnderCursor();
const QList<AST::SourceLocation> &locations = m_ids.value(id);
if (! locations.isEmpty()) {
menu->addSeparator();
QAction *a = menu->addAction(tr("Rename id '%1'...").arg(id));
connect(a, SIGNAL(triggered()), this, SLOT(renameIdUnderCursor()));
}
appendStandardContextMenuActions(menu);
menu->exec(e->globalPos());
menu->deleteLater();
2009-04-22 15:21:04 +02:00
}
void ScriptEditor::unCommentSelection()
{
Utils::unCommentSelection(this);
}
2009-04-22 15:21:04 +02:00
} // namespace Internal
2009-09-30 17:43:21 +02:00
} // namespace QmlEditor