cpptoolseditorsupport.cpp 4.14 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
con's avatar
con committed
29

30 31
#include "cppcompletionsupport.h"
#include "cpphighlightingsupport.h"
con's avatar
con committed
32 33 34
#include "cpptoolseditorsupport.h"
#include "cppmodelmanager.h"

35
#include <coreplugin/idocument.h>
36

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

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

44
#include <QTimer>
con's avatar
con committed
45

46
using namespace CppTools;
con's avatar
con committed
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
CppEditorSupport::CppEditorSupport(CppModelManager *modelManager)
    : QObject(modelManager),
      _modelManager(modelManager),
53
      _updateDocumentInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL)
con's avatar
con committed
54
{
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

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