QmlJSEditor: Move semantic info updating to document

Change-Id: I804dbd887af1786e7554ec79f94fc8e59db1de5b
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@digia.com>
This commit is contained in:
Eike Ziller
2014-01-24 16:53:16 +01:00
parent ae5ef38e3c
commit f8461fbbdf
10 changed files with 535 additions and 507 deletions

View File

@@ -49,9 +49,9 @@ using namespace QmlJS::AST;
Information about the imports of a Document can be accessed with imports(). Information about the imports of a Document can be accessed with imports().
When dealing with a QmlJSEditor::QmlJSTextEditorWidget it is unnecessary to When dealing with a QmlJSEditor::QmlJSEditorDocument it is unnecessary to
construct a new Context manually. Instead use construct a new Context manually. Instead use
QmlJSTextEditorWidget::semanticInfo()::context. QmlJSEditorDocument::semanticInfo()::context.
*/ */
ContextPtr Context::create(const QmlJS::Snapshot &snapshot, ValueOwner *valueOwner, ContextPtr Context::create(const QmlJS::Snapshot &snapshot, ValueOwner *valueOwner,

View File

@@ -130,7 +130,7 @@ public:
Initializes a context by resolving imports. This is an expensive operation. Initializes a context by resolving imports. This is an expensive operation.
Instead of making a fresh context, consider reusing the one maintained in the Instead of making a fresh context, consider reusing the one maintained in the
\l{QmlJSEditor::SemanticInfo} of a \l{QmlJSEditor::QmlJSTextEditorWidget}. \l{QmlJSEditor::SemanticInfo} of a \l{QmlJSEditor::QmlJSEditorDocument}.
*/ */
Link::Link(const Snapshot &snapshot, const ViewerContext &vContext, const LibraryInfo &builtins) Link::Link(const Snapshot &snapshot, const ViewerContext &vContext, const LibraryInfo &builtins)

View File

@@ -50,9 +50,9 @@ using namespace QmlJS;
It is an error to use the same ScopeChain from multiple threads; use a copy. It is an error to use the same ScopeChain from multiple threads; use a copy.
Copying is cheap. Initial construction is currently expensive. Copying is cheap. Initial construction is currently expensive.
When a QmlJSEditor::QmlJSTextEditorWidget is available, there's no need to When a QmlJSEditor::QmlJSEditorDocument is available, there's no need to
construct a new ScopeChain. Instead use construct a new ScopeChain. Instead use
QmlJSTextEditorWidget::semanticInfo()::scopeChain(). QmlJSEditorDocument::semanticInfo()::scopeChain().
*/ */
QmlComponentChain::QmlComponentChain(const Document::Ptr &document) QmlComponentChain::QmlComponentChain(const Document::Ptr &document)

View File

@@ -88,7 +88,6 @@
#include <QTreeView> #include <QTreeView>
enum { enum {
UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100,
UPDATE_USES_DEFAULT_INTERVAL = 150, UPDATE_USES_DEFAULT_INTERVAL = 150,
UPDATE_OUTLINE_INTERVAL = 500 // msecs after new semantic info has been arrived / cursor has moved UPDATE_OUTLINE_INTERVAL = 500 // msecs after new semantic info has been arrived / cursor has moved
}; };
@@ -101,353 +100,6 @@ using namespace QmlJSTools;
namespace QmlJSEditor { namespace QmlJSEditor {
using namespace Internal; using namespace Internal;
namespace {
class FindIdDeclarations: protected Visitor
{
public:
typedef QHash<QString, QList<AST::SourceLocation> > Result;
Result operator()(Document::Ptr doc)
{
_ids.clear();
_maybeIds.clear();
if (doc && doc->qmlProgram())
doc->qmlProgram()->accept(this);
return _ids;
}
protected:
QString asString(AST::UiQualifiedId *id)
{
QString text;
for (; id; id = id->next) {
if (!id->name.isEmpty())
text += id->name;
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.isEmpty()) {
const QString &id = idExpr->name.toString();
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.isEmpty()) {
const QString &name = node->name.toString();
if (_ids.contains(name))
_ids[name].append(node->identifierToken);
else
_maybeIds[name].append(node->identifierToken);
}
return false;
}
private:
Result _ids;
Result _maybeIds;
};
class FindDeclarations: protected Visitor
{
QList<Declaration> _declarations;
int _depth;
public:
QList<Declaration> operator()(AST::Node *node)
{
_depth = -1;
_declarations.clear();
accept(node);
return _declarations;
}
protected:
using Visitor::visit;
using Visitor::endVisit;
QString asString(AST::UiQualifiedId *id)
{
QString text;
for (; id; id = id->next) {
if (!id->name.isEmpty())
text += id->name;
else
text += QLatin1Char('?');
if (id->next)
text += QLatin1Char('.');
}
return text;
}
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;
}
void init(Declaration *decl, AST::ExpressionNode *expressionNode)
{
const SourceLocation first = expressionNode->firstSourceLocation();
const SourceLocation last = expressionNode->lastSourceLocation();
decl->startLine = first.startLine;
decl->startColumn = first.startColumn;
decl->endLine = last.startLine;
decl->endColumn = last.startColumn + last.length;
}
virtual bool visit(AST::UiObjectDefinition *node)
{
++_depth;
Declaration decl;
init(&decl, node);
decl.text.fill(QLatin1Char(' '), _depth);
if (node->qualifiedTypeNameId)
decl.text.append(asString(node->qualifiedTypeNameId));
else
decl.text.append(QLatin1Char('?'));
_declarations.append(decl);
return true; // search for more bindings
}
virtual void endVisit(AST::UiObjectDefinition *)
{
--_depth;
}
virtual bool visit(AST::UiObjectBinding *node)
{
++_depth;
Declaration decl;
init(&decl, node);
decl.text.fill(QLatin1Char(' '), _depth);
decl.text.append(asString(node->qualifiedId));
decl.text.append(QLatin1String(": "));
if (node->qualifiedTypeNameId)
decl.text.append(asString(node->qualifiedTypeNameId));
else
decl.text.append(QLatin1Char('?'));
_declarations.append(decl);
return true; // search for more bindings
}
virtual void endVisit(AST::UiObjectBinding *)
{
--_depth;
}
virtual bool visit(AST::UiScriptBinding *)
{
++_depth;
#if 0 // ### ignore script bindings for now.
Declaration decl;
init(&decl, node);
decl.text.fill(QLatin1Char(' '), _depth);
decl.text.append(asString(node->qualifiedId));
_declarations.append(decl);
#endif
return false; // more more bindings in this subtree.
}
virtual void endVisit(AST::UiScriptBinding *)
{
--_depth;
}
virtual bool visit(AST::FunctionExpression *)
{
return false;
}
virtual bool visit(AST::FunctionDeclaration *ast)
{
if (ast->name.isEmpty())
return false;
Declaration decl;
init(&decl, ast);
decl.text.fill(QLatin1Char(' '), _depth);
decl.text += ast->name;
decl.text += QLatin1Char('(');
for (FormalParameterList *it = ast->formals; it; it = it->next) {
if (!it->name.isEmpty())
decl.text += it->name;
if (it->next)
decl.text += QLatin1String(", ");
}
decl.text += QLatin1Char(')');
_declarations.append(decl);
return false;
}
virtual bool visit(AST::VariableDeclaration *ast)
{
if (ast->name.isEmpty())
return false;
Declaration decl;
decl.text.fill(QLatin1Char(' '), _depth);
decl.text += ast->name;
const SourceLocation first = ast->identifierToken;
decl.startLine = first.startLine;
decl.startColumn = first.startColumn;
decl.endLine = first.startLine;
decl.endColumn = first.startColumn + first.length;
_declarations.append(decl);
return false;
}
};
class CreateRanges: protected AST::Visitor
{
QTextDocument *_textDocument;
QList<Range> _ranges;
public:
QList<Range> operator()(QTextDocument *textDocument, Document::Ptr doc)
{
_textDocument = textDocument;
_ranges.clear();
if (doc && doc->ast() != 0)
doc->ast()->accept(this);
return _ranges;
}
protected:
using AST::Visitor::visit;
virtual bool visit(AST::UiObjectBinding *ast)
{
if (ast->initializer && ast->initializer->lbraceToken.length)
_ranges.append(createRange(ast, ast->initializer));
return true;
}
virtual bool visit(AST::UiObjectDefinition *ast)
{
if (ast->initializer && ast->initializer->lbraceToken.length)
_ranges.append(createRange(ast, ast->initializer));
return true;
}
virtual bool visit(AST::FunctionExpression *ast)
{
_ranges.append(createRange(ast));
return true;
}
virtual bool visit(AST::FunctionDeclaration *ast)
{
_ranges.append(createRange(ast));
return true;
}
virtual bool visit(AST::UiScriptBinding *ast)
{
if (AST::Block *block = AST::cast<AST::Block *>(ast->statement))
_ranges.append(createRange(ast, block));
return true;
}
Range createRange(AST::UiObjectMember *member, AST::UiObjectInitializer *ast)
{
return createRange(member, member->firstSourceLocation(), ast->rbraceToken);
}
Range createRange(AST::FunctionExpression *ast)
{
return createRange(ast, ast->lbraceToken, ast->rbraceToken);
}
Range createRange(AST::UiScriptBinding *ast, AST::Block *block)
{
return createRange(ast, block->lbraceToken, block->rbraceToken);
}
Range createRange(AST::Node *ast, AST::SourceLocation start, AST::SourceLocation end)
{
Range range;
range.ast = ast;
range.begin = QTextCursor(_textDocument);
range.begin.setPosition(start.begin());
range.end = QTextCursor(_textDocument);
range.end.setPosition(end.end());
return range;
}
};
} // end of anonymous namespace
QmlJSTextEditorWidget::QmlJSTextEditorWidget(QWidget *parent) : QmlJSTextEditorWidget::QmlJSTextEditorWidget(QWidget *parent) :
TextEditor::BaseTextEditorWidget(new QmlJSEditorDocument, parent) TextEditor::BaseTextEditorWidget(new QmlJSEditorDocument, parent)
{ {
@@ -462,16 +114,13 @@ QmlJSTextEditorWidget::QmlJSTextEditorWidget(QmlJSTextEditorWidget *other)
void QmlJSTextEditorWidget::ctor() void QmlJSTextEditorWidget::ctor()
{ {
m_qmlJsEditorDocument = static_cast<QmlJSEditorDocument *>(baseTextDocument());
m_outlineCombo = 0; m_outlineCombo = 0;
m_outlineModel = new QmlOutlineModel(this); m_outlineModel = new QmlOutlineModel(this);
m_futureSemanticInfoRevision = 0;
m_contextPane = 0; m_contextPane = 0;
m_findReferences = new FindReferences(this); m_findReferences = new FindReferences(this);
m_semanticHighlighter = new SemanticHighlighter(this); m_semanticHighlighter = new SemanticHighlighter(this);
m_semanticInfoUpdater = new SemanticInfoUpdater(this);
m_semanticInfoUpdater->start();
setParenthesesMatchingEnabled(true); setParenthesesMatchingEnabled(true);
setMarksVisible(true); setMarksVisible(true);
setCodeFoldingSupported(true); setCodeFoldingSupported(true);
@@ -483,11 +132,6 @@ void QmlJSTextEditorWidget::ctor()
m_updateUsesTimer->setSingleShot(true); m_updateUsesTimer->setSingleShot(true);
connect(m_updateUsesTimer, SIGNAL(timeout()), this, SLOT(updateUsesNow())); connect(m_updateUsesTimer, SIGNAL(timeout()), this, SLOT(updateUsesNow()));
m_updateSemanticInfoTimer = new QTimer(this);
m_updateSemanticInfoTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
m_updateSemanticInfoTimer->setSingleShot(true);
connect(m_updateSemanticInfoTimer, SIGNAL(timeout()), this, SLOT(updateSemanticInfoNow()));
connect(this, SIGNAL(textChanged()), this, SLOT(updateUses())); connect(this, SIGNAL(textChanged()), this, SLOT(updateUses()));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateUses())); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateUses()));
@@ -518,32 +162,26 @@ void QmlJSTextEditorWidget::ctor()
} }
m_oldCursorPosition = -1; m_oldCursorPosition = -1;
if (m_modelManager) { connect(this->document(), SIGNAL(modificationChanged(bool)), this, SLOT(modificationChanged(bool)));
connect(m_modelManager, SIGNAL(documentUpdated(QmlJS::Document::Ptr)),
this, SLOT(onDocumentUpdated(QmlJS::Document::Ptr)));
connect(m_modelManager, SIGNAL(libraryInfoUpdated(QString,QmlJS::LibraryInfo)),
this, SLOT(updateSemanticInfo()));
connect(this->document(), SIGNAL(modificationChanged(bool)), this, SLOT(modificationChanged(bool)));
}
connect(m_semanticInfoUpdater, SIGNAL(updated(QmlJSTools::SemanticInfo)), connect(m_qmlJsEditorDocument, SIGNAL(semanticInfoUpdated(QmlJSTools::SemanticInfo)),
this, SLOT(acceptNewSemanticInfo(QmlJSTools::SemanticInfo))); this, SLOT(semanticInfoUpdated(QmlJSTools::SemanticInfo)));
connect(this, SIGNAL(refactorMarkerClicked(TextEditor::RefactorMarker)), connect(this, SIGNAL(refactorMarkerClicked(TextEditor::RefactorMarker)),
SLOT(onRefactorMarkerClicked(TextEditor::RefactorMarker))); SLOT(onRefactorMarkerClicked(TextEditor::RefactorMarker)));
connect(baseTextDocument(), SIGNAL(updateCodeWarnings(QmlJS::Document::Ptr)),
this, SLOT(updateCodeWarnings(QmlJS::Document::Ptr)));
setRequestMarkEnabled(true); setRequestMarkEnabled(true);
} }
QmlJSTextEditorWidget::~QmlJSTextEditorWidget() QmlJSTextEditorWidget::~QmlJSTextEditorWidget()
{ {
m_semanticInfoUpdater->abort();
m_semanticInfoUpdater->wait();
} }
SemanticInfo QmlJSTextEditorWidget::semanticInfo() const SemanticInfo QmlJSTextEditorWidget::semanticInfo() const
{ {
return m_semanticInfo; return m_qmlJsEditorDocument->semanticInfo();
} }
int QmlJSTextEditorWidget::editorRevision() const int QmlJSTextEditorWidget::editorRevision() const
@@ -562,10 +200,7 @@ QVector<QTextLayout::FormatRange> QmlJSTextEditorWidget::diagnosticRanges() cons
bool QmlJSTextEditorWidget::isSemanticInfoOutdated() const bool QmlJSTextEditorWidget::isSemanticInfoOutdated() const
{ {
if (m_semanticInfo.revision() != editorRevision()) return m_qmlJsEditorDocument->isSemanticInfoOutdated();
return true;
return false;
} }
QmlOutlineModel *QmlJSTextEditorWidget::outlineModel() const QmlOutlineModel *QmlJSTextEditorWidget::outlineModel() const
@@ -638,26 +273,9 @@ static void appendExtraSelectionsForMessages(
} }
} }
void QmlJSTextEditorWidget::onDocumentUpdated(QmlJS::Document::Ptr doc) void QmlJSTextEditorWidget::updateCodeWarnings(QmlJS::Document::Ptr doc)
{ {
if (baseTextDocument()->filePath() != doc->fileName())
return;
if (doc->editorRevision() != editorRevision()) {
// Maybe a dependency changed and our semantic info is now outdated.
// Ignore 0-revision documents though, we get them when a file is initially opened
// in an editor.
if (doc->editorRevision() != 0)
updateSemanticInfo();
return;
}
//qDebug() << doc->fileName() << "was reparsed";
if (doc->ast()) { if (doc->ast()) {
// got a correctly parsed (or recovered) file.
m_futureSemanticInfoRevision = doc->editorRevision();
m_semanticInfoUpdater->update(doc, m_modelManager->snapshot());
setExtraSelections(CodeWarningsSelection, QList<QTextEdit::ExtraSelection>()); setExtraSelections(CodeWarningsSelection, QList<QTextEdit::ExtraSelection>());
} else if (Document::isFullySupportedLanguage(doc->language())) { } else if (Document::isFullySupportedLanguage(doc->language())) {
// show parsing errors // show parsing errors
@@ -695,15 +313,15 @@ void QmlJSTextEditorWidget::jumpToOutlineElement(int /*index*/)
void QmlJSTextEditorWidget::updateOutlineNow() void QmlJSTextEditorWidget::updateOutlineNow()
{ {
if (!m_semanticInfo.document) if (!m_qmlJsEditorDocument->semanticInfo().document)
return; return;
if (m_semanticInfo.document->editorRevision() != editorRevision()) { if (m_qmlJsEditorDocument->semanticInfo().document->editorRevision() != editorRevision()) {
m_updateOutlineTimer->start(); m_updateOutlineTimer->start();
return; return;
} }
m_outlineModel->update(m_semanticInfo); m_outlineModel->update(m_qmlJsEditorDocument->semanticInfo());
QTreeView *treeView = static_cast<QTreeView*>(m_outlineCombo->view()); QTreeView *treeView = static_cast<QTreeView*>(m_outlineCombo->view());
treeView->expandAll(); treeView->expandAll();
@@ -759,15 +377,16 @@ static QList<TextEditor::RefactorMarker> removeMarkersOfType(const QList<TextEdi
void QmlJSTextEditorWidget::updateCursorPositionNow() void QmlJSTextEditorWidget::updateCursorPositionNow()
{ {
if (m_contextPane && document() && semanticInfo().isValid() const SemanticInfo info = m_qmlJsEditorDocument->semanticInfo();
&& document()->revision() == semanticInfo().document->editorRevision()) if (m_contextPane && document() && info.isValid()
&& document()->revision() == info.document->editorRevision())
{ {
Node *oldNode = m_semanticInfo.declaringMemberNoProperties(m_oldCursorPosition); Node *oldNode = info.declaringMemberNoProperties(m_oldCursorPosition);
Node *newNode = m_semanticInfo.declaringMemberNoProperties(position()); Node *newNode = info.declaringMemberNoProperties(position());
if (oldNode != newNode && m_oldCursorPosition != -1) if (oldNode != newNode && m_oldCursorPosition != -1)
m_contextPane->apply(editor(), semanticInfo().document, 0, newNode, false); m_contextPane->apply(editor(), info.document, 0, newNode, false);
if (m_contextPane->isAvailable(editor(), semanticInfo().document, newNode) && if (m_contextPane->isAvailable(editor(), info.document, newNode) &&
!m_contextPane->widget()->isVisible()) { !m_contextPane->widget()->isVisible()) {
QList<TextEditor::RefactorMarker> markers = removeMarkersOfType<QtQuickToolbarMarker>(refactorMarkers()); QList<TextEditor::RefactorMarker> markers = removeMarkersOfType<QtQuickToolbarMarker>(refactorMarkers());
if (UiObjectMember *m = newNode->uiObjectMemberCast()) { if (UiObjectMember *m = newNode->uiObjectMemberCast()) {
@@ -813,7 +432,7 @@ void QmlJSTextEditorWidget::updateUses()
void QmlJSTextEditorWidget::updateUsesNow() void QmlJSTextEditorWidget::updateUsesNow()
{ {
if (isSemanticInfoOutdated()) { if (m_qmlJsEditorDocument->isSemanticInfoOutdated()) {
updateUses(); updateUses();
return; return;
} }
@@ -821,7 +440,8 @@ void QmlJSTextEditorWidget::updateUsesNow()
m_updateUsesTimer->stop(); m_updateUsesTimer->stop();
QList<QTextEdit::ExtraSelection> selections; QList<QTextEdit::ExtraSelection> selections;
foreach (const AST::SourceLocation &loc, m_semanticInfo.idLocations.value(wordUnderCursor())) { foreach (const AST::SourceLocation &loc,
m_qmlJsEditorDocument->semanticInfo().idLocations.value(wordUnderCursor())) {
if (! loc.isValid()) if (! loc.isValid())
continue; continue;
@@ -948,10 +568,10 @@ void QmlJSTextEditorWidget::setSelectedElements()
endPos = textCursor().position(); endPos = textCursor().position();
} }
if (m_semanticInfo.isValid()) { if (m_qmlJsEditorDocument->semanticInfo().isValid()) {
SelectedElement selectedMembers; SelectedElement selectedMembers;
QList<UiObjectMember *> members = selectedMembers(m_semanticInfo.document, QList<UiObjectMember *> members
startPos, endPos); = selectedMembers(m_qmlJsEditorDocument->semanticInfo().document, startPos, endPos);
if (!members.isEmpty()) { if (!members.isEmpty()) {
foreach (UiObjectMember *m, members) { foreach (UiObjectMember *m, members) {
offsets << m; offsets << m;
@@ -1038,7 +658,7 @@ TextEditor::BaseTextEditorWidget::Link QmlJSTextEditorWidget::findLinkAt(const Q
bool /*resolveTarget*/, bool /*resolveTarget*/,
bool /*inNextSplit*/) bool /*inNextSplit*/)
{ {
const SemanticInfo semanticInfo = m_semanticInfo; const SemanticInfo semanticInfo = m_qmlJsEditorDocument->semanticInfo();
if (! semanticInfo.isValid()) if (! semanticInfo.isValid())
return Link(); return Link();
@@ -1129,10 +749,11 @@ void QmlJSTextEditorWidget::renameUsages()
void QmlJSTextEditorWidget::showContextPane() void QmlJSTextEditorWidget::showContextPane()
{ {
if (m_contextPane && m_semanticInfo.isValid()) { const SemanticInfo info = m_qmlJsEditorDocument->semanticInfo();
Node *newNode = m_semanticInfo.declaringMemberNoProperties(position()); if (m_contextPane && info.isValid()) {
ScopeChain scopeChain = m_semanticInfo.scopeChain(m_semanticInfo.rangePath(position())); Node *newNode = info.declaringMemberNoProperties(position());
m_contextPane->apply(editor(), m_semanticInfo.document, ScopeChain scopeChain = info.scopeChain(info.rangePath(position()));
m_contextPane->apply(editor(), info.document,
&scopeChain, &scopeChain,
newNode, false, true); newNode, false, true);
m_oldCursorPosition = position(); m_oldCursorPosition = position();
@@ -1154,7 +775,7 @@ void QmlJSTextEditorWidget::contextMenuEvent(QContextMenuEvent *e)
QSignalMapper mapper; QSignalMapper mapper;
connect(&mapper, SIGNAL(mapped(int)), this, SLOT(performQuickFix(int))); connect(&mapper, SIGNAL(mapped(int)), this, SLOT(performQuickFix(int)));
if (! isSemanticInfoOutdated()) { if (!m_qmlJsEditorDocument->isSemanticInfoOutdated()) {
TextEditor::IAssistInterface *interface = TextEditor::IAssistInterface *interface =
createAssistInterface(TextEditor::QuickFix, TextEditor::ExplicitlyInvoked); createAssistInterface(TextEditor::QuickFix, TextEditor::ExplicitlyInvoked);
if (interface) { if (interface) {
@@ -1188,7 +809,9 @@ void QmlJSTextEditorWidget::contextMenuEvent(QContextMenuEvent *e)
if (action->objectName() == QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT)) if (action->objectName() == QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT))
menu->addMenu(refactoringMenu); menu->addMenu(refactoringMenu);
if (action->objectName() == QLatin1String(Constants::SHOW_QT_QUICK_HELPER)) { if (action->objectName() == QLatin1String(Constants::SHOW_QT_QUICK_HELPER)) {
bool enabled = m_contextPane->isAvailable(editor(), semanticInfo().document, m_semanticInfo.declaringMemberNoProperties(position())); bool enabled = m_contextPane->isAvailable(
editor(), m_qmlJsEditorDocument->semanticInfo().document,
m_qmlJsEditorDocument->semanticInfo().declaringMemberNoProperties(position()));
action->setEnabled(enabled); action->setEnabled(enabled);
} }
} }
@@ -1231,7 +854,9 @@ void QmlJSTextEditorWidget::wheelEvent(QWheelEvent *event)
BaseTextEditorWidget::wheelEvent(event); BaseTextEditorWidget::wheelEvent(event);
if (visible) if (visible)
m_contextPane->apply(editor(), semanticInfo().document, 0, m_semanticInfo.declaringMemberNoProperties(m_oldCursorPosition), false, true); m_contextPane->apply(editor(), m_qmlJsEditorDocument->semanticInfo().document, 0,
m_qmlJsEditorDocument->semanticInfo().declaringMemberNoProperties(m_oldCursorPosition),
false, true);
} }
void QmlJSTextEditorWidget::resizeEvent(QResizeEvent *event) void QmlJSTextEditorWidget::resizeEvent(QResizeEvent *event)
@@ -1251,54 +876,10 @@ void QmlJSTextEditorWidget::unCommentSelection()
Utils::unCommentSelection(this); Utils::unCommentSelection(this);
} }
void QmlJSTextEditorWidget::updateSemanticInfo() void QmlJSTextEditorWidget::semanticInfoUpdated(const SemanticInfo &semanticInfo)
{ {
// If the editor is newer than the future semantic info, new semantic infos
// won't be accepted anyway. What we need is a reparse.
if (editorRevision() != m_futureSemanticInfoRevision)
return;
// Save time by not doing it for non-active editors.
if (EditorManager::currentEditor() != editor())
return;
m_updateSemanticInfoTimer->start();
}
void QmlJSTextEditorWidget::updateSemanticInfoNow()
{
// If the editor is newer than the future semantic info, new semantic infos
// won't be accepted anyway. What we need is a reparse.
if (editorRevision() != m_futureSemanticInfoRevision)
return;
m_updateSemanticInfoTimer->stop();
m_semanticInfoUpdater->reupdate(m_modelManager->snapshot());
}
void QmlJSTextEditorWidget::acceptNewSemanticInfo(const SemanticInfo &semanticInfo)
{
if (semanticInfo.revision() != editorRevision()) {
// ignore outdated semantic infos
return;
}
//qDebug() << file()->fileName() << "got new semantic info";
m_semanticInfo = semanticInfo;
Document::Ptr doc = semanticInfo.document;
// create the ranges
CreateRanges createRanges;
m_semanticInfo.ranges = createRanges(document(), doc);
// Refresh the ids
FindIdDeclarations updateIds;
m_semanticInfo.idLocations = updateIds(doc);
if (m_contextPane) { if (m_contextPane) {
Node *newNode = m_semanticInfo.declaringMemberNoProperties(position()); Node *newNode = semanticInfo.declaringMemberNoProperties(position());
if (newNode) { if (newNode) {
m_contextPane->apply(editor(), semanticInfo.document, 0, newNode, true); m_contextPane->apply(editor(), semanticInfo.document, 0, newNode, true);
m_cursorPositionTimer->start(); //update text marker m_cursorPositionTimer->start(); //update text marker
@@ -1309,9 +890,7 @@ void QmlJSTextEditorWidget::acceptNewSemanticInfo(const SemanticInfo &semanticIn
m_updateOutlineTimer->start(); m_updateOutlineTimer->start();
if (EditorManager::currentEditor() == editor()) if (EditorManager::currentEditor() == editor())
m_semanticHighlighter->rerun(m_semanticInfo); m_semanticHighlighter->rerun(semanticInfo);
emit semanticInfoUpdated();
} }
void QmlJSTextEditorWidget::onRefactorMarkerClicked(const TextEditor::RefactorMarker &marker) void QmlJSTextEditorWidget::onRefactorMarkerClicked(const TextEditor::RefactorMarker &marker)
@@ -1353,7 +932,7 @@ bool QmlJSTextEditorWidget::hideContextPane()
{ {
bool b = (m_contextPane) && m_contextPane->widget()->isVisible(); bool b = (m_contextPane) && m_contextPane->widget()->isVisible();
if (b) if (b)
m_contextPane->apply(editor(), semanticInfo().document, 0, 0, false); m_contextPane->apply(editor(), m_qmlJsEditorDocument->semanticInfo().document, 0, 0, false);
return b; return b;
} }
@@ -1390,7 +969,7 @@ TextEditor::IAssistInterface *QmlJSTextEditorWidget::createAssistInterface(
position(), position(),
editor()->document()->filePath(), editor()->document()->filePath(),
reason, reason,
m_semanticInfo); m_qmlJsEditorDocument->semanticInfo());
} else if (assistKind == TextEditor::QuickFix) { } else if (assistKind == TextEditor::QuickFix) {
return new QmlJSQuickFixAssistInterface(const_cast<QmlJSTextEditorWidget *>(this), reason); return new QmlJSQuickFixAssistInterface(const_cast<QmlJSTextEditorWidget *>(this), reason);
} }
@@ -1401,9 +980,9 @@ QString QmlJSTextEditorWidget::foldReplacementText(const QTextBlock &block) cons
{ {
const int curlyIndex = block.text().indexOf(QLatin1Char('{')); const int curlyIndex = block.text().indexOf(QLatin1Char('{'));
if (curlyIndex != -1 && m_semanticInfo.isValid()) { if (curlyIndex != -1 && m_qmlJsEditorDocument->semanticInfo().isValid()) {
const int pos = block.position() + curlyIndex; const int pos = block.position() + curlyIndex;
Node *node = m_semanticInfo.rangeAt(pos); Node *node = m_qmlJsEditorDocument->semanticInfo().rangeAt(pos);
const QString objectId = idOfObject(node); const QString objectId = idOfObject(node);
if (!objectId.isEmpty()) if (!objectId.isEmpty())

View File

@@ -69,6 +69,7 @@ class QmlJSEditor;
class FindReferences; class FindReferences;
namespace Internal { namespace Internal {
class QmlJSEditorDocument;
class QmlOutlineModel; class QmlOutlineModel;
class SemanticInfoUpdater; class SemanticInfoUpdater;
struct SemanticInfoUpdaterSource; struct SemanticInfoUpdaterSource;
@@ -96,9 +97,6 @@ class QMLJSEDITOR_EXPORT QmlJSTextEditorWidget : public TextEditor::BaseTextEdit
{ {
Q_OBJECT Q_OBJECT
// used e.g. in qmljsprofiler
Q_PROPERTY(QmlJSTools::SemanticInfo semanticInfo READ semanticInfo)
public: public:
QmlJSTextEditorWidget(QWidget *parent = 0); QmlJSTextEditorWidget(QWidget *parent = 0);
QmlJSTextEditorWidget(QmlJSTextEditorWidget *other); QmlJSTextEditorWidget(QmlJSTextEditorWidget *other);
@@ -106,9 +104,11 @@ public:
virtual void unCommentSelection(); virtual void unCommentSelection();
// redirecting to document
QmlJSTools::SemanticInfo semanticInfo() const; QmlJSTools::SemanticInfo semanticInfo() const;
bool isSemanticInfoOutdated() const; bool isSemanticInfoOutdated() const;
int editorRevision() const; int editorRevision() const;
QVector<QTextLayout::FormatRange> diagnosticRanges() const; QVector<QTextLayout::FormatRange> diagnosticRanges() const;
Internal::QmlOutlineModel *outlineModel() const; Internal::QmlOutlineModel *outlineModel() const;
@@ -119,8 +119,6 @@ public:
TextEditor::IAssistInterface *createAssistInterface(TextEditor::AssistKind assistKind, TextEditor::IAssistInterface *createAssistInterface(TextEditor::AssistKind assistKind,
TextEditor::AssistReason reason) const; TextEditor::AssistReason reason) const;
public slots: public slots:
void updateSemanticInfo();
void updateSemanticInfoNow();
void findUsages(); void findUsages();
void renameUsages(); void renameUsages();
void showContextPane(); void showContextPane();
@@ -132,7 +130,6 @@ signals:
void semanticInfoUpdated(); void semanticInfoUpdated();
private slots: private slots:
void onDocumentUpdated(QmlJS::Document::Ptr doc);
void modificationChanged(bool); void modificationChanged(bool);
void jumpToOutlineElement(int index); void jumpToOutlineElement(int index);
@@ -144,11 +141,12 @@ private slots:
void updateUses(); void updateUses();
void updateUsesNow(); void updateUsesNow();
void acceptNewSemanticInfo(const QmlJSTools::SemanticInfo &semanticInfo); void semanticInfoUpdated(const QmlJSTools::SemanticInfo &semanticInfo);
void onCursorPositionChanged(); void onCursorPositionChanged();
void onRefactorMarkerClicked(const TextEditor::RefactorMarker &marker); void onRefactorMarkerClicked(const TextEditor::RefactorMarker &marker);
void performQuickFix(int index); void performQuickFix(int index);
void updateCodeWarnings(QmlJS::Document::Ptr doc);
protected: protected:
void contextMenuEvent(QContextMenuEvent *e); void contextMenuEvent(QContextMenuEvent *e);
@@ -175,8 +173,8 @@ private:
QModelIndex indexForPosition(unsigned cursorPosition, const QModelIndex &rootIndex = QModelIndex()) const; QModelIndex indexForPosition(unsigned cursorPosition, const QModelIndex &rootIndex = QModelIndex()) const;
bool hideContextPane(); bool hideContextPane();
Internal::QmlJSEditorDocument *m_qmlJsEditorDocument;
QTimer *m_updateUsesTimer; QTimer *m_updateUsesTimer;
QTimer *m_updateSemanticInfoTimer;
QTimer *m_updateOutlineTimer; QTimer *m_updateOutlineTimer;
QTimer *m_updateOutlineIndexTimer; QTimer *m_updateOutlineIndexTimer;
QTimer *m_cursorPositionTimer; QTimer *m_cursorPositionTimer;
@@ -185,10 +183,6 @@ private:
QModelIndex m_outlineModelIndex; QModelIndex m_outlineModelIndex;
QmlJS::ModelManagerInterface *m_modelManager; QmlJS::ModelManagerInterface *m_modelManager;
Internal::SemanticInfoUpdater *m_semanticInfoUpdater;
QmlJSTools::SemanticInfo m_semanticInfo;
int m_futureSemanticInfoRevision;
QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes; QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes;
QVector<QTextLayout::FormatRange> m_diagnosticRanges; QVector<QTextLayout::FormatRange> m_diagnosticRanges;

View File

@@ -31,13 +31,16 @@
#include "qmljseditordocument_p.h" #include "qmljseditordocument_p.h"
#include "qmljshighlighter.h" #include "qmljshighlighter.h"
#include "qmljssemanticinfoupdater.h"
#include <qmljstools/qmljsindenter.h> #include <qmljstools/qmljsindenter.h>
#include <qmljstools/qmljsmodelmanager.h> #include <qmljstools/qmljsmodelmanager.h>
#include <qmljstools/qmljsqtstylecodeformatter.h> #include <qmljstools/qmljsqtstylecodeformatter.h>
using namespace QmlJSEditor; using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal; using namespace QmlJS;
using namespace QmlJS::AST;
using namespace QmlJSTools;
namespace { namespace {
@@ -45,30 +48,448 @@ enum {
UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100 UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100
}; };
class FindIdDeclarations: protected Visitor
{
public:
typedef QHash<QString, QList<AST::SourceLocation> > Result;
Result operator()(Document::Ptr doc)
{
_ids.clear();
_maybeIds.clear();
if (doc && doc->qmlProgram())
doc->qmlProgram()->accept(this);
return _ids;
}
protected:
QString asString(AST::UiQualifiedId *id)
{
QString text;
for (; id; id = id->next) {
if (!id->name.isEmpty())
text += id->name;
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.isEmpty()) {
const QString &id = idExpr->name.toString();
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.isEmpty()) {
const QString &name = node->name.toString();
if (_ids.contains(name))
_ids[name].append(node->identifierToken);
else
_maybeIds[name].append(node->identifierToken);
}
return false;
}
private:
Result _ids;
Result _maybeIds;
};
class FindDeclarations: protected Visitor
{
QList<Declaration> _declarations;
int _depth;
public:
QList<Declaration> operator()(AST::Node *node)
{
_depth = -1;
_declarations.clear();
accept(node);
return _declarations;
}
protected:
using Visitor::visit;
using Visitor::endVisit;
QString asString(AST::UiQualifiedId *id)
{
QString text;
for (; id; id = id->next) {
if (!id->name.isEmpty())
text += id->name;
else
text += QLatin1Char('?');
if (id->next)
text += QLatin1Char('.');
}
return text;
}
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;
}
void init(Declaration *decl, AST::ExpressionNode *expressionNode)
{
const SourceLocation first = expressionNode->firstSourceLocation();
const SourceLocation last = expressionNode->lastSourceLocation();
decl->startLine = first.startLine;
decl->startColumn = first.startColumn;
decl->endLine = last.startLine;
decl->endColumn = last.startColumn + last.length;
}
virtual bool visit(AST::UiObjectDefinition *node)
{
++_depth;
Declaration decl;
init(&decl, node);
decl.text.fill(QLatin1Char(' '), _depth);
if (node->qualifiedTypeNameId)
decl.text.append(asString(node->qualifiedTypeNameId));
else
decl.text.append(QLatin1Char('?'));
_declarations.append(decl);
return true; // search for more bindings
}
virtual void endVisit(AST::UiObjectDefinition *)
{
--_depth;
}
virtual bool visit(AST::UiObjectBinding *node)
{
++_depth;
Declaration decl;
init(&decl, node);
decl.text.fill(QLatin1Char(' '), _depth);
decl.text.append(asString(node->qualifiedId));
decl.text.append(QLatin1String(": "));
if (node->qualifiedTypeNameId)
decl.text.append(asString(node->qualifiedTypeNameId));
else
decl.text.append(QLatin1Char('?'));
_declarations.append(decl);
return true; // search for more bindings
}
virtual void endVisit(AST::UiObjectBinding *)
{
--_depth;
}
virtual bool visit(AST::UiScriptBinding *)
{
++_depth;
#if 0 // ### ignore script bindings for now.
Declaration decl;
init(&decl, node);
decl.text.fill(QLatin1Char(' '), _depth);
decl.text.append(asString(node->qualifiedId));
_declarations.append(decl);
#endif
return false; // more more bindings in this subtree.
}
virtual void endVisit(AST::UiScriptBinding *)
{
--_depth;
}
virtual bool visit(AST::FunctionExpression *)
{
return false;
}
virtual bool visit(AST::FunctionDeclaration *ast)
{
if (ast->name.isEmpty())
return false;
Declaration decl;
init(&decl, ast);
decl.text.fill(QLatin1Char(' '), _depth);
decl.text += ast->name;
decl.text += QLatin1Char('(');
for (FormalParameterList *it = ast->formals; it; it = it->next) {
if (!it->name.isEmpty())
decl.text += it->name;
if (it->next)
decl.text += QLatin1String(", ");
}
decl.text += QLatin1Char(')');
_declarations.append(decl);
return false;
}
virtual bool visit(AST::VariableDeclaration *ast)
{
if (ast->name.isEmpty())
return false;
Declaration decl;
decl.text.fill(QLatin1Char(' '), _depth);
decl.text += ast->name;
const SourceLocation first = ast->identifierToken;
decl.startLine = first.startLine;
decl.startColumn = first.startColumn;
decl.endLine = first.startLine;
decl.endColumn = first.startColumn + first.length;
_declarations.append(decl);
return false;
}
};
class CreateRanges: protected AST::Visitor
{
QTextDocument *_textDocument;
QList<Range> _ranges;
public:
QList<Range> operator()(QTextDocument *textDocument, Document::Ptr doc)
{
_textDocument = textDocument;
_ranges.clear();
if (doc && doc->ast() != 0)
doc->ast()->accept(this);
return _ranges;
}
protected:
using AST::Visitor::visit;
virtual bool visit(AST::UiObjectBinding *ast)
{
if (ast->initializer && ast->initializer->lbraceToken.length)
_ranges.append(createRange(ast, ast->initializer));
return true;
}
virtual bool visit(AST::UiObjectDefinition *ast)
{
if (ast->initializer && ast->initializer->lbraceToken.length)
_ranges.append(createRange(ast, ast->initializer));
return true;
}
virtual bool visit(AST::FunctionExpression *ast)
{
_ranges.append(createRange(ast));
return true;
}
virtual bool visit(AST::FunctionDeclaration *ast)
{
_ranges.append(createRange(ast));
return true;
}
virtual bool visit(AST::UiScriptBinding *ast)
{
if (AST::Block *block = AST::cast<AST::Block *>(ast->statement))
_ranges.append(createRange(ast, block));
return true;
}
Range createRange(AST::UiObjectMember *member, AST::UiObjectInitializer *ast)
{
return createRange(member, member->firstSourceLocation(), ast->rbraceToken);
}
Range createRange(AST::FunctionExpression *ast)
{
return createRange(ast, ast->lbraceToken, ast->rbraceToken);
}
Range createRange(AST::UiScriptBinding *ast, AST::Block *block)
{
return createRange(ast, block->lbraceToken, block->rbraceToken);
}
Range createRange(AST::Node *ast, AST::SourceLocation start, AST::SourceLocation end)
{
Range range;
range.ast = ast;
range.begin = QTextCursor(_textDocument);
range.begin.setPosition(start.begin());
range.end = QTextCursor(_textDocument);
range.end.setPosition(end.end());
return range;
}
};
} }
namespace QmlJSEditor {
namespace Internal {
QmlJSEditorDocumentPrivate::QmlJSEditorDocumentPrivate(QmlJSEditorDocument *parent) QmlJSEditorDocumentPrivate::QmlJSEditorDocumentPrivate(QmlJSEditorDocument *parent)
: m_q(parent) : m_q(parent),
m_semanticInfoDocRevision(-1)
{ {
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
// code model
m_updateDocumentTimer = new QTimer(this); m_updateDocumentTimer = new QTimer(this);
m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL); m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
m_updateDocumentTimer->setSingleShot(true); m_updateDocumentTimer->setSingleShot(true);
connect(m_q->document(), SIGNAL(contentsChanged()), m_updateDocumentTimer, SLOT(start())); connect(m_q->document(), SIGNAL(contentsChanged()), m_updateDocumentTimer, SLOT(start()));
connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(reparseDocument())); connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(reparseDocument()));
connect(modelManager, SIGNAL(documentUpdated(QmlJS::Document::Ptr)),
this, SLOT(onDocumentUpdated(QmlJS::Document::Ptr)));
// semantic info
m_semanticInfoUpdater = new SemanticInfoUpdater(this);
connect(m_semanticInfoUpdater, SIGNAL(updated(QmlJSTools::SemanticInfo)),
this, SLOT(acceptNewSemanticInfo(QmlJSTools::SemanticInfo)));
m_semanticInfoUpdater->start();
// library info changes
m_reupdateSemanticInfoTimer = new QTimer(this);
m_reupdateSemanticInfoTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
m_reupdateSemanticInfoTimer->setSingleShot(true);
connect(m_reupdateSemanticInfoTimer, SIGNAL(timeout()), this, SLOT(reupdateSemanticInfo()));
connect(modelManager, SIGNAL(libraryInfoUpdated(QString,QmlJS::LibraryInfo)),
m_reupdateSemanticInfoTimer, SLOT(start()));
}
QmlJSEditorDocumentPrivate::~QmlJSEditorDocumentPrivate()
{
m_semanticInfoUpdater->abort();
m_semanticInfoUpdater->wait();
} }
void QmlJSEditorDocumentPrivate::invalidateFormatterCache() void QmlJSEditorDocumentPrivate::invalidateFormatterCache()
{ {
QmlJSTools::CreatorCodeFormatter formatter(m_q->tabSettings()); CreatorCodeFormatter formatter(m_q->tabSettings());
formatter.invalidateCache(m_q->document()); formatter.invalidateCache(m_q->document());
} }
void QmlJSEditorDocumentPrivate::reparseDocument() void QmlJSEditorDocumentPrivate::reparseDocument()
{ {
QmlJS::ModelManagerInterface::instance()->updateSourceFiles(QStringList() << m_q->filePath(), ModelManagerInterface::instance()->updateSourceFiles(QStringList() << m_q->filePath(),
false); false);
} }
void QmlJSEditorDocumentPrivate::onDocumentUpdated(Document::Ptr doc)
{
if (m_q->filePath() != doc->fileName())
return;
// text document has changed, simply wait for the next onDocumentUpdated
if (doc->editorRevision() != m_q->document()->revision())
return;
if (doc->ast()) {
// got a correctly parsed (or recovered) file.
m_semanticInfoDocRevision = doc->editorRevision();
m_semanticInfoUpdater->update(doc, ModelManagerInterface::instance()->snapshot());
}
emit m_q->updateCodeWarnings(doc);
}
void QmlJSEditorDocumentPrivate::reupdateSemanticInfo()
{
// If the editor is newer than the semantic info (possibly with update in progress),
// new semantic infos won't be accepted anyway. We'll get a onDocumentUpdated anyhow.
if (m_q->document()->revision() != m_semanticInfoDocRevision)
return;
m_semanticInfoUpdater->reupdate(ModelManagerInterface::instance()->snapshot());
}
void QmlJSEditorDocumentPrivate::acceptNewSemanticInfo(const SemanticInfo &semanticInfo)
{
if (semanticInfo.revision() != m_q->document()->revision()) {
// ignore outdated semantic infos
return;
}
m_semanticInfo = semanticInfo;
Document::Ptr doc = semanticInfo.document;
// create the ranges
CreateRanges createRanges;
m_semanticInfo.ranges = createRanges(m_q->document(), doc);
// Refresh the ids
FindIdDeclarations updateIds;
m_semanticInfo.idLocations = updateIds(doc);
emit m_q->semanticInfoUpdated(m_semanticInfo);
}
QmlJSEditorDocument::QmlJSEditorDocument() QmlJSEditorDocument::QmlJSEditorDocument()
: m_d(new QmlJSEditorDocumentPrivate(this)) : m_d(new QmlJSEditorDocumentPrivate(this))
{ {
@@ -82,3 +503,16 @@ QmlJSEditorDocument::~QmlJSEditorDocument()
{ {
delete m_d; delete m_d;
} }
const SemanticInfo &QmlJSEditorDocument::semanticInfo() const
{
return m_d->m_semanticInfo;
}
bool QmlJSEditorDocument::isSemanticInfoOutdated() const
{
return m_d->m_semanticInfo.revision() != document()->revision();
}
} // Internal
} // QmlJSEditor

View File

@@ -30,6 +30,8 @@
#ifndef QMLJSEDITORDOCUMENT_H #ifndef QMLJSEDITORDOCUMENT_H
#define QMLJSEDITORDOCUMENT_H #define QMLJSEDITORDOCUMENT_H
#include <qmljs/qmljsdocument.h>
#include <qmljstools/qmljssemanticinfo.h>
#include <texteditor/basetextdocument.h> #include <texteditor/basetextdocument.h>
namespace QmlJSEditor { namespace QmlJSEditor {
@@ -44,7 +46,15 @@ public:
QmlJSEditorDocument(); QmlJSEditorDocument();
~QmlJSEditorDocument(); ~QmlJSEditorDocument();
const QmlJSTools::SemanticInfo &semanticInfo() const;
bool isSemanticInfoOutdated() const;
signals:
void updateCodeWarnings(QmlJS::Document::Ptr doc);
void semanticInfoUpdated(const QmlJSTools::SemanticInfo &semanticInfo);
private: private:
friend class QmlJSEditorDocumentPrivate; // sending signals
QmlJSEditorDocumentPrivate *m_d; QmlJSEditorDocumentPrivate *m_d;
}; };

View File

@@ -30,6 +30,9 @@
#ifndef QMLJSEDITORDOCUMENT_P_H #ifndef QMLJSEDITORDOCUMENT_P_H
#define QMLJSEDITORDOCUMENT_P_H #define QMLJSEDITORDOCUMENT_P_H
#include <qmljs/qmljsdocument.h>
#include <qmljstools/qmljssemanticinfo.h>
#include <QObject> #include <QObject>
#include <QTimer> #include <QTimer>
@@ -37,6 +40,7 @@ namespace QmlJSEditor {
namespace Internal { namespace Internal {
class QmlJSEditorDocument; class QmlJSEditorDocument;
class SemanticInfoUpdater;
class QmlJSEditorDocumentPrivate : public QObject class QmlJSEditorDocumentPrivate : public QObject
{ {
@@ -44,14 +48,22 @@ class QmlJSEditorDocumentPrivate : public QObject
public: public:
QmlJSEditorDocumentPrivate(QmlJSEditorDocument *parent); QmlJSEditorDocumentPrivate(QmlJSEditorDocument *parent);
~QmlJSEditorDocumentPrivate();
public slots: public slots:
void invalidateFormatterCache(); void invalidateFormatterCache();
void reparseDocument(); void reparseDocument();
void onDocumentUpdated(QmlJS::Document::Ptr doc);
void reupdateSemanticInfo();
void acceptNewSemanticInfo(const QmlJSTools::SemanticInfo &semanticInfo);
public: public:
QmlJSEditorDocument *m_q; QmlJSEditorDocument *m_q;
QTimer *m_updateDocumentTimer; QTimer *m_updateDocumentTimer; // used to compress multiple document changes
QTimer *m_reupdateSemanticInfoTimer; // used to compress multiple libraryInfo changes
int m_semanticInfoDocRevision; // document revision to which the semantic info is currently updated to
SemanticInfoUpdater *m_semanticInfoUpdater;
QmlJSTools::SemanticInfo m_semanticInfo;
}; };
} // Internal } // Internal

View File

@@ -31,6 +31,7 @@
#include "qmljshighlighter.h" #include "qmljshighlighter.h"
#include "qmljseditor.h" #include "qmljseditor.h"
#include "qmljseditorconstants.h" #include "qmljseditorconstants.h"
#include "qmljseditordocument.h"
#include "qmljseditorfactory.h" #include "qmljseditorfactory.h"
#include "qmljshoverhandler.h" #include "qmljshoverhandler.h"
#include "qmlfilewizard.h" #include "qmlfilewizard.h"
@@ -71,6 +72,7 @@
#include <QSettings> #include <QSettings>
#include <QDir> #include <QDir>
#include <QCoreApplication> #include <QCoreApplication>
#include <QTextDocument>
#include <QTimer> #include <QTimer>
#include <QMenu> #include <QMenu>
#include <QAction> #include <QAction>
@@ -95,7 +97,7 @@ QmlJSEditorPlugin::QmlJSEditorPlugin() :
m_editor(0), m_editor(0),
m_quickFixAssistProvider(0), m_quickFixAssistProvider(0),
m_reformatFileAction(0), m_reformatFileAction(0),
m_currentEditor(0), m_currentDocument(0),
m_jsonManager(new Utils::JsonSchemaManager( m_jsonManager(new Utils::JsonSchemaManager(
QStringList() << Core::ICore::userResourcePath() + QLatin1String("/json/") QStringList() << Core::ICore::userResourcePath() + QLatin1String("/json/")
<< Core::ICore::resourcePath() + QLatin1String("/json/"))) << Core::ICore::resourcePath() + QLatin1String("/json/")))
@@ -270,11 +272,11 @@ void QmlJSEditorPlugin::renameUsages()
void QmlJSEditorPlugin::reformatFile() void QmlJSEditorPlugin::reformatFile()
{ {
if (QmlJSTextEditorWidget *editor = qobject_cast<QmlJSTextEditorWidget*>(Core::EditorManager::currentEditor()->widget())) { if (m_currentDocument) {
QTC_ASSERT(!editor->isSemanticInfoOutdated(), return); QTC_ASSERT(!m_currentDocument->isSemanticInfoOutdated(), return);
const QString &newText = QmlJS::reformat(editor->semanticInfo().document); const QString &newText = QmlJS::reformat(m_currentDocument->semanticInfo().document);
QTextCursor tc(editor->textCursor()); QTextCursor tc(m_currentDocument->document());
tc.movePosition(QTextCursor::Start); tc.movePosition(QTextCursor::Start);
tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
tc.insertText(newText); tc.insertText(newText);
@@ -285,7 +287,6 @@ void QmlJSEditorPlugin::showContextPane()
{ {
if (QmlJSTextEditorWidget *editor = qobject_cast<QmlJSTextEditorWidget*>(Core::EditorManager::currentEditor()->widget())) if (QmlJSTextEditorWidget *editor = qobject_cast<QmlJSTextEditorWidget*>(Core::EditorManager::currentEditor()->widget()))
editor->showContextPane(); editor->showContextPane();
} }
Core::Command *QmlJSEditorPlugin::addToolAction(QAction *a, Core::Command *QmlJSEditorPlugin::addToolAction(QAction *a,
@@ -306,19 +307,17 @@ QmlJSQuickFixAssistProvider *QmlJSEditorPlugin::quickFixAssistProvider() const
void QmlJSEditorPlugin::currentEditorChanged(Core::IEditor *editor) void QmlJSEditorPlugin::currentEditorChanged(Core::IEditor *editor)
{ {
QmlJSTextEditorWidget *newTextEditor = 0; QmlJSEditorDocument *document = 0;
if (editor) if (editor)
newTextEditor = qobject_cast<QmlJSTextEditorWidget *>(editor->widget()); document = qobject_cast<QmlJSEditorDocument *>(editor->document());
if (m_currentEditor) { if (m_currentDocument)
m_currentEditor->baseTextDocument()->disconnect(this); m_currentDocument->disconnect(this);
m_currentEditor->disconnect(this); m_currentDocument = document;
} if (document) {
m_currentEditor = newTextEditor; connect(document->document(), SIGNAL(contentsChanged()),
if (newTextEditor) {
connect(newTextEditor->baseTextDocument(), SIGNAL(contentsChanged()),
this, SLOT(checkCurrentEditorSemanticInfoUpToDate())); this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
connect(newTextEditor, SIGNAL(semanticInfoUpdated()), connect(document, SIGNAL(semanticInfoUpdated(QmlJSTools::SemanticInfo)),
this, SLOT(checkCurrentEditorSemanticInfoUpToDate())); this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
} }
} }
@@ -332,7 +331,7 @@ void QmlJSEditorPlugin::runSemanticScan()
void QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate() void QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate()
{ {
const bool semanticInfoUpToDate = m_currentEditor && !m_currentEditor->isSemanticInfoOutdated(); const bool semanticInfoUpToDate = m_currentDocument && !m_currentDocument->isSemanticInfoOutdated();
m_reformatFileAction->setEnabled(semanticInfoUpToDate); m_reformatFileAction->setEnabled(semanticInfoUpToDate);
} }

View File

@@ -60,10 +60,10 @@ namespace QmlJS {
namespace QmlJSEditor { namespace QmlJSEditor {
class QmlFileWizard; class QmlFileWizard;
class QmlJSTextEditorWidget;
namespace Internal { namespace Internal {
class QmlJSEditorDocument;
class QmlJSEditorFactory; class QmlJSEditorFactory;
class QmlJSPreviewRunner; class QmlJSPreviewRunner;
class QmlJSQuickFixAssistProvider; class QmlJSQuickFixAssistProvider;
@@ -114,7 +114,7 @@ private:
QAction *m_reformatFileAction; QAction *m_reformatFileAction;
QPointer<QmlJSTextEditorWidget> m_currentEditor; QPointer<QmlJSEditorDocument> m_currentDocument;
QScopedPointer<Utils::JsonSchemaManager> m_jsonManager; QScopedPointer<Utils::JsonSchemaManager> m_jsonManager;
}; };