OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / qmljssemantichighlighter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmljssemantichighlighter.h"
35
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>
41
42 namespace QmlJSEditor {
43 namespace Internal {
44
45 SemanticHighlighter::SemanticHighlighter(QObject *parent)
46         : QThread(parent),
47           m_done(false),
48           m_modelManager(0)
49 {
50 }
51
52 SemanticHighlighter::~SemanticHighlighter()
53 {
54 }
55
56 void SemanticHighlighter::abort()
57 {
58     QMutexLocker locker(&m_mutex);
59     m_done = true;
60     m_condition.wakeOne();
61 }
62
63 void SemanticHighlighter::rehighlight(const SemanticHighlighterSource &source)
64 {
65     QMutexLocker locker(&m_mutex);
66     m_source = source;
67     m_condition.wakeOne();
68 }
69
70 bool SemanticHighlighter::isOutdated()
71 {
72     QMutexLocker locker(&m_mutex);
73     const bool outdated = ! m_source.fileName.isEmpty() || m_done;
74     return outdated;
75 }
76
77 void SemanticHighlighter::run()
78 {
79     setPriority(QThread::LowestPriority);
80
81     forever {
82         m_mutex.lock();
83
84         while (! (m_done || ! m_source.fileName.isEmpty()))
85             m_condition.wait(&m_mutex);
86
87         const bool done = m_done;
88         const SemanticHighlighterSource source = m_source;
89         m_source.clear();
90
91         m_mutex.unlock();
92
93         if (done)
94             break;
95
96         const SemanticInfo info = semanticInfo(source);
97
98         if (! isOutdated()) {
99             m_mutex.lock();
100             m_lastSemanticInfo = info;
101             m_mutex.unlock();
102
103             emit changed(info);
104         }
105     }
106 }
107
108 SemanticInfo SemanticHighlighter::semanticInfo(const SemanticHighlighterSource &source)
109 {
110     m_mutex.lock();
111     const int revision = m_lastSemanticInfo.revision();
112     m_mutex.unlock();
113
114     QmlJS::Snapshot snapshot;
115     QmlJS::Document::Ptr doc;
116
117     if (! source.force && revision == source.revision) {
118         m_mutex.lock();
119         snapshot = m_lastSemanticInfo.snapshot;
120         doc = m_lastSemanticInfo.document;
121         m_mutex.unlock();
122     }
123
124     if (! doc) {
125         snapshot = source.snapshot;
126         doc = snapshot.documentFromSource(source.code, source.fileName);
127         doc->setEditorRevision(source.revision);
128         doc->parse();
129         snapshot.insert(doc);
130     }
131
132     SemanticInfo semanticInfo;
133     semanticInfo.snapshot = snapshot;
134     semanticInfo.document = doc;
135
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();
140
141     QStringList importPaths;
142     if (m_modelManager)
143         importPaths = m_modelManager->importPaths();
144     QmlJS::Check checker(doc, snapshot, ctx);
145     semanticInfo.semanticMessages.append(checker());
146
147     return semanticInfo;
148 }
149
150 void SemanticHighlighter::setModelManager(QmlJS::ModelManagerInterface *modelManager)
151 {
152     m_modelManager = modelManager;
153 }
154
155 } // namespace Internal
156 } // namespace QmlJSEditor