cpptoolseditorsupport.cpp 3.88 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
con's avatar
con committed
32 33 34 35

#include "cpptoolseditorsupport.h"
#include "cppmodelmanager.h"

36 37
#include <coreplugin/ifile.h>

con's avatar
con committed
38
#include <texteditor/itexteditor.h>
Roberto Raggi's avatar
Roberto Raggi committed
39
#include <texteditor/basetexteditor.h>
hjk's avatar
hjk committed
40

Roberto Raggi's avatar
Roberto Raggi committed
41 42 43 44 45
#include <AST.h>
#include <ASTVisitor.h>
#include <TranslationUnit.h>

#include <QtCore/QTimer>
con's avatar
con committed
46 47

using namespace CppTools::Internal;
Roberto Raggi's avatar
Roberto Raggi committed
48 49
using namespace CPlusPlus;

con's avatar
con committed
50 51 52 53 54
CppEditorSupport::CppEditorSupport(CppModelManager *modelManager)
    : QObject(modelManager),
      _modelManager(modelManager),
      _updateDocumentInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL)
{
55 56
    _revision = 0;

con's avatar
con committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    _updateDocumentTimer = new QTimer(this);
    _updateDocumentTimer->setSingleShot(true);
    _updateDocumentTimer->setInterval(_updateDocumentInterval);
    connect(_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
}

CppEditorSupport::~CppEditorSupport()
{ }

TextEditor::ITextEditor *CppEditorSupport::textEditor() const
{ return _textEditor; }

void CppEditorSupport::setTextEditor(TextEditor::ITextEditor *textEditor)
{
    _textEditor = textEditor;

73
    if (_textEditor) {
Erik Verbruggen's avatar
Erik Verbruggen committed
74 75
        connect(_textEditor, SIGNAL(contentsChanged()), this, SIGNAL(contentsChanged()));
        connect(this, SIGNAL(contentsChanged()), this, SLOT(updateDocument()));
Roberto Raggi's avatar
Roberto Raggi committed
76

Erik Verbruggen's avatar
Erik Verbruggen committed
77 78
        updateDocument();
    }
con's avatar
con committed
79 80
}

81
QString CppEditorSupport::contents()
con's avatar
con committed
82 83
{
    if (! _textEditor)
84
        return QString();
Roberto Raggi's avatar
Roberto Raggi committed
85
    else if (! _cachedContents.isEmpty())
86
        _cachedContents = _textEditor->contents();
con's avatar
con committed
87

Roberto Raggi's avatar
Roberto Raggi committed
88
    return _cachedContents;
con's avatar
con committed
89 90
}

91 92 93
unsigned CppEditorSupport::editorRevision() const
{
    if (_textEditor) {
94
        if (TextEditor::BaseTextEditorWidget *ed = qobject_cast<TextEditor::BaseTextEditorWidget *>(_textEditor->widget()))
95 96 97 98 99 100
            return ed->document()->revision();
    }

    return 0;
}

con's avatar
con committed
101 102 103 104 105 106 107
int CppEditorSupport::updateDocumentInterval() const
{ return _updateDocumentInterval; }

void CppEditorSupport::setUpdateDocumentInterval(int updateDocumentInterval)
{ _updateDocumentInterval = updateDocumentInterval; }

void CppEditorSupport::updateDocument()
Roberto Raggi's avatar
Roberto Raggi committed
108
{
109 110
    _revision = editorRevision();

111
    if (qobject_cast<TextEditor::BaseTextEditorWidget*>(_textEditor->widget()) != 0) {
Roberto Raggi's avatar
Roberto Raggi committed
112 113 114 115 116
        _modelManager->stopEditorSelectionsUpdate();
    }

    _updateDocumentTimer->start(_updateDocumentInterval);
}
con's avatar
con committed
117 118 119

void CppEditorSupport::updateDocumentNow()
{
120
    if (_documentParser.isRunning() || _revision != editorRevision()) {
con's avatar
con committed
121 122 123
        _updateDocumentTimer->start(_updateDocumentInterval);
    } else {
        _updateDocumentTimer->stop();
Roberto Raggi's avatar
Roberto Raggi committed
124

con's avatar
con committed
125
        QStringList sourceFiles(_textEditor->file()->fileName());
126
        _cachedContents = _textEditor->contents().toUtf8();
con's avatar
con committed
127 128 129 130
        _documentParser = _modelManager->refreshSourceFiles(sourceFiles);
    }
}