Commit 51de95c4 authored by Roberto Raggi's avatar Roberto Raggi

Use the text editor revision to ignore outdated parse results.

parent aa8a6706
......@@ -111,7 +111,8 @@ private:
Document::Document(const QString &fileName)
: _fileName(QDir::cleanPath(fileName)),
_globalNamespace(0),
_revision(0)
_revision(0),
_editorRevision(0)
{
_control = new Control();
......@@ -148,6 +149,16 @@ void Document::setRevision(unsigned revision)
_revision = revision;
}
unsigned Document::editorRevision() const
{
return _editorRevision;
}
void Document::setEditorRevision(unsigned editorRevision)
{
_editorRevision = editorRevision;
}
QDateTime Document::lastModified() const
{
return _lastModified;
......@@ -489,6 +500,7 @@ Document::Ptr Snapshot::documentFromSource(const QByteArray &preprocessedCode,
if (Document::Ptr thisDocument = document(fileName)) {
newDoc->_revision = thisDocument->_revision;
newDoc->_editorRevision = thisDocument->_editorRevision;
newDoc->_lastModified = thisDocument->_lastModified;
newDoc->_includes = thisDocument->_includes;
newDoc->_definedMacros = thisDocument->_definedMacros;
......
......@@ -60,6 +60,9 @@ public:
unsigned revision() const;
void setRevision(unsigned revision);
unsigned editorRevision() const;
void setEditorRevision(unsigned editorRevision);
QDateTime lastModified() const;
void setLastModified(const QDateTime &lastModified);
......@@ -312,6 +315,7 @@ private:
QByteArray _source;
QDateTime _lastModified;
unsigned _revision;
unsigned _editorRevision;
friend class Snapshot;
};
......
......@@ -786,6 +786,9 @@ void CPPEditor::onDocumentUpdated(Document::Ptr doc)
if (doc->fileName() != file()->fileName())
return;
if (doc->editorRevision() != editorRevision())
return;
if (! m_initialized) {
m_initialized = true;
......@@ -1059,6 +1062,16 @@ void CPPEditor::highlightUses(const QList<SemanticInfo::Use> &uses,
void CPPEditor::updateMethodBoxIndexNow()
{
if (! m_overviewModel->document())
return;
if (m_overviewModel->document()->editorRevision() != editorRevision()) {
m_updateMethodBoxTimer->start();
return;
}
m_updateMethodBoxTimer->stop();
int line = 0, column = 0;
convertPosition(position(), &line, &column);
......@@ -1409,9 +1422,14 @@ Symbol *CPPEditor::findDefinition(Symbol *symbol)
return 0;
}
unsigned CPPEditor::editorRevision() const
{
return document()->revision();
}
bool CPPEditor::isOutdated() const
{
if (m_lastSemanticInfo.revision != document()->revision())
if (m_lastSemanticInfo.revision != editorRevision())
return true;
return false;
......@@ -2026,7 +2044,7 @@ void CPPEditor::semanticRehighlight()
void CPPEditor::updateSemanticInfo(const SemanticInfo &semanticInfo)
{
if (semanticInfo.revision != document()->revision()) {
if (semanticInfo.revision != editorRevision()) {
// got outdated semantic info
semanticRehighlight();
return;
......@@ -2077,10 +2095,10 @@ SemanticHighlighter::Source CPPEditor::currentSource(bool force)
const QString fileName = file()->fileName();
QString code;
if (force || m_lastSemanticInfo.revision != document()->revision())
if (force || m_lastSemanticInfo.revision != editorRevision())
code = toPlainText(); // get the source code only when needed.
const int revision = document()->revision();
const unsigned revision = editorRevision();
SemanticHighlighter::Source source(snapshot, fileName, code,
line, column, revision);
source.force = force;
......
......@@ -79,10 +79,10 @@ public:
typedef QHashIterator<CPlusPlus::Symbol *, QList<Use> > LocalUseIterator;
SemanticInfo()
: revision(-1), hasQ(false), hasD(false)
: revision(0), hasQ(false), hasD(false)
{ }
int revision;
unsigned revision;
bool hasQ: 1;
bool hasD: 1;
CPlusPlus::Snapshot snapshot;
......@@ -188,6 +188,7 @@ public:
void indentInsertedText(const QTextCursor &tc);
unsigned editorRevision() const;
bool isOutdated() const;
SemanticInfo semanticInfo() const;
......
......@@ -1012,7 +1012,7 @@ int CPPQuickFixCollector::startCompletion(TextEditor::ITextEditable *editable)
const SemanticInfo info = _editor->semanticInfo();
if (info.revision != _editor->document()->revision()) {
if (info.revision != _editor->editorRevision()) {
// outdated
qWarning() << "TODO: outdated semantic info, force a reparse.";
return -1;
......
......@@ -190,8 +190,8 @@ public: // attributes
protected:
CPlusPlus::Document::Ptr switchDocument(CPlusPlus::Document::Ptr doc);
bool includeFile(const QString &absoluteFilePath, QString *result);
QString tryIncludeFile(QString &fileName, IncludeType type);
bool includeFile(const QString &absoluteFilePath, QString *result, unsigned *revision);
QString tryIncludeFile(QString &fileName, IncludeType type, unsigned *revision);
void mergeEnvironment(CPlusPlus::Document::Ptr doc);
......@@ -328,14 +328,16 @@ void CppPreprocessor::resetEnvironment()
m_processed.clear();
}
bool CppPreprocessor::includeFile(const QString &absoluteFilePath, QString *result)
bool CppPreprocessor::includeFile(const QString &absoluteFilePath, QString *result, unsigned *revision)
{
if (absoluteFilePath.isEmpty() || m_included.contains(absoluteFilePath))
return true;
if (m_workingCopy.contains(absoluteFilePath)) {
m_included.insert(absoluteFilePath);
*result = m_workingCopy.source(absoluteFilePath);
const QPair<QString, unsigned> r = m_workingCopy.get(absoluteFilePath);
*result = r.first;
*revision = r.second;
return true;
}
......@@ -356,12 +358,12 @@ bool CppPreprocessor::includeFile(const QString &absoluteFilePath, QString *resu
return false;
}
QString CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type)
QString CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type, unsigned *revision)
{
QFileInfo fileInfo(fileName);
if (fileName == QLatin1String(pp_configuration_file) || fileInfo.isAbsolute()) {
QString contents;
includeFile(fileName, &contents);
includeFile(fileName, &contents, revision);
return contents;
}
......@@ -372,7 +374,7 @@ QString CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type)
path += fileName;
path = QDir::cleanPath(path);
QString contents;
if (includeFile(path, &contents)) {
if (includeFile(path, &contents, revision)) {
fileName = path;
return contents;
}
......@@ -384,7 +386,7 @@ QString CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type)
path += fileName;
path = QDir::cleanPath(path);
QString contents;
if (includeFile(path, &contents)) {
if (includeFile(path, &contents, revision)) {
fileName = path;
return contents;
}
......@@ -397,7 +399,7 @@ QString CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type)
path += fileName;
path = QDir::cleanPath(path);
QString contents;
if (includeFile(path, &contents)) {
if (includeFile(path, &contents, revision)) {
fileName = path;
return contents;
}
......@@ -416,7 +418,7 @@ QString CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type)
path += name;
path = QDir::cleanPath(path);
QString contents;
if (includeFile(path, &contents)) {
if (includeFile(path, &contents, revision)) {
fileName = path;
return contents;
}
......@@ -431,7 +433,7 @@ QString CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type)
if (projectFile.endsWith(path)) {
fileName = projectFile;
QString contents;
includeFile(fileName, &contents);
includeFile(fileName, &contents, revision);
return contents;
}
}
......@@ -524,13 +526,13 @@ void CppPreprocessor::stopSkippingBlocks(unsigned offset)
m_currentDoc->stopSkippingBlocks(offset);
}
void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type,
unsigned line)
void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type, unsigned line)
{
if (fileName.isEmpty())
return;
QString contents = tryIncludeFile(fileName, type);
unsigned editorRevision = 0;
QString contents = tryIncludeFile(fileName, type, &editorRevision);
fileName = QDir::cleanPath(fileName);
if (m_currentDoc) {
m_currentDoc->addIncludeFile(fileName, line);
......@@ -560,6 +562,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type,
doc = Document::create(fileName);
doc->setRevision(m_revision);
doc->setEditorRevision(editorRevision);
QFileInfo info(fileName);
if (info.exists())
......@@ -797,7 +800,7 @@ CppModelManager::WorkingCopy CppModelManager::buildWorkingCopyList()
TextEditor::ITextEditor *textEditor = it.key();
CppEditorSupport *editorSupport = it.value();
QString fileName = textEditor->file()->fileName();
workingCopy.insert(fileName, editorSupport->contents());
workingCopy.insert(fileName, editorSupport->contents(), editorSupport->editorRevision());
}
QSetIterator<AbstractEditorSupport *> jt(m_addtionalEditorSupport);
......
......@@ -81,25 +81,20 @@ public:
class WorkingCopy
{
public:
typedef QHash<QString, QString> Table;
typedef Table::const_iterator iterator;
typedef Table::const_iterator const_iterator;
public:
const_iterator begin() const { return _elements.begin(); }
const_iterator end() const { return _elements.end(); }
void insert(const QString &fileName, const QString &source)
{ _elements.insert(fileName, source); }
void insert(const QString &fileName, const QString &source, unsigned revision = 0)
{ _elements.insert(fileName, qMakePair(source, revision)); }
bool contains(const QString &fileName) const
{ return _elements.contains(fileName); }
QString source(const QString &fileName) const
{ return _elements.value(fileName).first; }
QPair<QString, unsigned> get(const QString &fileName) const
{ return _elements.value(fileName); }
private:
typedef QHash<QString, QPair<QString, unsigned> > Table;
Table _elements;
};
......
......@@ -84,6 +84,16 @@ QString CppEditorSupport::contents()
return _cachedContents;
}
unsigned CppEditorSupport::editorRevision() const
{
if (_textEditor) {
if (TextEditor::BaseTextEditor *ed = qobject_cast<TextEditor::BaseTextEditor *>(_textEditor->widget()))
return ed->document()->revision();
}
return 0;
}
int CppEditorSupport::updateDocumentInterval() const
{ return _updateDocumentInterval; }
......
......@@ -70,6 +70,7 @@ public:
void setUpdateDocumentInterval(int updateDocumentInterval);
QString contents();
unsigned editorRevision() const;
Q_SIGNALS:
void contentsChanged();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment