cpptoolseditorsupport.cpp 4.32 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@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
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
con's avatar
con committed
32

33 34
#include "cppcompletionsupport.h"
#include "cpphighlightingsupport.h"
con's avatar
con committed
35 36 37
#include "cpptoolseditorsupport.h"
#include "cppmodelmanager.h"

38 39
#include <coreplugin/ifile.h>

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

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

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

49
using namespace CppTools;
con's avatar
con committed
50
using namespace CppTools::Internal;
Roberto Raggi's avatar
Roberto Raggi committed
51 52
using namespace CPlusPlus;

con's avatar
con committed
53 54 55
CppEditorSupport::CppEditorSupport(CppModelManager *modelManager)
    : QObject(modelManager),
      _modelManager(modelManager),
56 57 58
      _updateDocumentInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL),
      m_completionSupport(new CppCompletionSupport(this)),
      m_highlightingSupport(new CppHighlightingSupport)
con's avatar
con committed
59
{
60 61
    _revision = 0;

con's avatar
con committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    _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;

78
    if (_textEditor) {
Erik Verbruggen's avatar
Erik Verbruggen committed
79 80
        connect(_textEditor, SIGNAL(contentsChanged()), this, SIGNAL(contentsChanged()));
        connect(this, SIGNAL(contentsChanged()), this, SLOT(updateDocument()));
Roberto Raggi's avatar
Roberto Raggi committed
81

Erik Verbruggen's avatar
Erik Verbruggen committed
82 83
        updateDocument();
    }
con's avatar
con committed
84 85
}

86
QString CppEditorSupport::contents()
con's avatar
con committed
87 88
{
    if (! _textEditor)
89
        return QString();
Roberto Raggi's avatar
Roberto Raggi committed
90
    else if (! _cachedContents.isEmpty())
91
        _cachedContents = _textEditor->contents();
con's avatar
con committed
92

Roberto Raggi's avatar
Roberto Raggi committed
93
    return _cachedContents;
con's avatar
con committed
94 95
}

96 97 98
unsigned CppEditorSupport::editorRevision() const
{
    if (_textEditor) {
99
        if (TextEditor::BaseTextEditorWidget *ed = qobject_cast<TextEditor::BaseTextEditorWidget *>(_textEditor->widget()))
100 101 102 103 104 105
            return ed->document()->revision();
    }

    return 0;
}

106 107 108 109 110 111 112 113 114 115
CppTools::CppCompletionSupport *CppEditorSupport::completionSupport() const
{
    return m_completionSupport.data();
}

CppHighlightingSupport *CppEditorSupport::highlightingSupport() const
{
    return m_highlightingSupport.data();
}

con's avatar
con committed
116 117 118 119 120 121 122
int CppEditorSupport::updateDocumentInterval() const
{ return _updateDocumentInterval; }

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

void CppEditorSupport::updateDocument()
Roberto Raggi's avatar
Roberto Raggi committed
123
{
124 125
    _revision = editorRevision();

126
    if (qobject_cast<TextEditor::BaseTextEditorWidget*>(_textEditor->widget()) != 0) {
Roberto Raggi's avatar
Roberto Raggi committed
127 128 129 130 131
        _modelManager->stopEditorSelectionsUpdate();
    }

    _updateDocumentTimer->start(_updateDocumentInterval);
}
con's avatar
con committed
132 133 134

void CppEditorSupport::updateDocumentNow()
{
135
    if (_documentParser.isRunning() || _revision != editorRevision()) {
con's avatar
con committed
136 137 138
        _updateDocumentTimer->start(_updateDocumentInterval);
    } else {
        _updateDocumentTimer->stop();
Roberto Raggi's avatar
Roberto Raggi committed
139

con's avatar
con committed
140
        QStringList sourceFiles(_textEditor->file()->fileName());
141
        _cachedContents = _textEditor->contents().toUtf8();
con's avatar
con committed
142 143 144 145
        _documentParser = _modelManager->refreshSourceFiles(sourceFiles);
    }
}