1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmljssemantichighlighter.h"
36 #include <qmljs/qmljsmodelmanagerinterface.h>
37 #include <qmljs/qmljsdocument.h>
38 #include <qmljs/qmljscheck.h>
39 #include <qmljs/qmljsinterpreter.h>
40 #include <qmljs/qmljslink.h>
42 namespace QmlJSEditor {
45 SemanticHighlighter::SemanticHighlighter(QObject *parent)
52 SemanticHighlighter::~SemanticHighlighter()
56 void SemanticHighlighter::abort()
58 QMutexLocker locker(&m_mutex);
60 m_condition.wakeOne();
63 void SemanticHighlighter::rehighlight(const SemanticHighlighterSource &source)
65 QMutexLocker locker(&m_mutex);
67 m_condition.wakeOne();
70 bool SemanticHighlighter::isOutdated()
72 QMutexLocker locker(&m_mutex);
73 const bool outdated = ! m_source.fileName.isEmpty() || m_done;
77 void SemanticHighlighter::run()
79 setPriority(QThread::LowestPriority);
84 while (! (m_done || ! m_source.fileName.isEmpty()))
85 m_condition.wait(&m_mutex);
87 const bool done = m_done;
88 const SemanticHighlighterSource source = m_source;
96 const SemanticInfo info = semanticInfo(source);
100 m_lastSemanticInfo = info;
108 SemanticInfo SemanticHighlighter::semanticInfo(const SemanticHighlighterSource &source)
111 const int revision = m_lastSemanticInfo.revision();
114 QmlJS::Snapshot snapshot;
115 QmlJS::Document::Ptr doc;
117 if (! source.force && revision == source.revision) {
119 snapshot = m_lastSemanticInfo.snapshot;
120 doc = m_lastSemanticInfo.document;
125 snapshot = source.snapshot;
126 doc = snapshot.documentFromSource(source.code, source.fileName);
127 doc->setEditorRevision(source.revision);
129 snapshot.insert(doc);
132 SemanticInfo semanticInfo;
133 semanticInfo.snapshot = snapshot;
134 semanticInfo.document = doc;
136 QmlJS::Interpreter::Context *ctx = new QmlJS::Interpreter::Context;
137 QmlJS::Link link(ctx, doc, snapshot, QmlJS::ModelManagerInterface::instance()->importPaths());
138 semanticInfo.m_context = QSharedPointer<const QmlJS::Interpreter::Context>(ctx);
139 semanticInfo.semanticMessages = link.diagnosticMessages();
141 QStringList importPaths;
143 importPaths = m_modelManager->importPaths();
144 QmlJS::Check checker(doc, snapshot, ctx);
145 semanticInfo.semanticMessages.append(checker());
150 void SemanticHighlighter::setModelManager(QmlJS::ModelManagerInterface *modelManager)
152 m_modelManager = modelManager;
155 } // namespace Internal
156 } // namespace QmlJSEditor