forked from qt-creator/qt-creator
C++: Base parsing on editor document instead of widget
This mainly takes CppEditorSupport apart.
* Parsing is now invoked by CPPEditorDocument itself by listening to
QTextDocument::contentsChanged().
* Upon construction and destruction CPPEditorDocument creates and
deletes an EditorDocumentHandle for (un)registration in the model
manager. This handle provides everything to generate the working copy
and to access the editor document processor.
* A CPPEditorDocument owns a BaseEditorDocumentProcessor instance that
controls parsing, semantic info recalculation and the semantic
highlighting for the document. This is more or less what is left from
CppEditorSupport and can be considered as the backend of a
CPPEditorDocument. CPPEditorDocument itself is quite small.
* BuiltinEditorDocumentProcessor and ClangEditorDocumentProcessor
derive from BaseEditorDocumentProcessor and implement the gaps.
* Since the semantic info calculation was bound to the widget, it
also calculated the local uses, which depend on the cursor
position. This calculation got moved into the extracted class
UseSeletionsUpdater in the cppeditor plugin, which is run once the
cursor position changes or the semantic info document is updated.
* Some more logic got extracted:
- SemanticInfoUpdater (logic was in CppEditorSupport)
- SemanticHighlighter (logic was in CppEditorSupport)
* The *Parser and *Processor classes can be easily accessed by the
static function get().
* CppHighlightingSupport is gone since it turned out to be useless.
* The editor dependency in CompletionAssistProviders is gone since we
actually only need the file path now.
Change-Id: I49d3a7bd138c5ed9620123e34480772535156508
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
@@ -32,25 +32,87 @@
|
||||
#include "cppeditorconstants.h"
|
||||
#include "cpphighlighter.h"
|
||||
|
||||
#include <cpptools/builtineditordocumentprocessor.h>
|
||||
#include <cpptools/cppcodeformatter.h>
|
||||
#include <cpptools/cppcodemodelsettings.h>
|
||||
#include <cpptools/cppmodelmanagerinterface.h>
|
||||
#include <cpptools/cppqtstyleindenter.h>
|
||||
#include <cpptools/cpptoolsconstants.h>
|
||||
#include <cpptools/cpptoolsplugin.h>
|
||||
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/runextensions.h>
|
||||
|
||||
#include <QTextDocument>
|
||||
|
||||
namespace {
|
||||
|
||||
CppTools::CppModelManagerInterface *mm()
|
||||
{
|
||||
return CppTools::CppModelManagerInterface::instance();
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace CppEditor {
|
||||
namespace Internal {
|
||||
|
||||
enum { processDocumentIntervalInMs = 150 };
|
||||
|
||||
class CppEditorDocumentHandle : public CppTools::EditorDocumentHandle
|
||||
{
|
||||
public:
|
||||
CppEditorDocumentHandle(CppEditor::Internal::CPPEditorDocument *cppEditorDocument)
|
||||
: m_cppEditorDocument(cppEditorDocument)
|
||||
, m_registrationFilePath(cppEditorDocument->filePath())
|
||||
{
|
||||
mm()->registerEditorDocument(this);
|
||||
}
|
||||
|
||||
~CppEditorDocumentHandle() { mm()->unregisterEditorDocument(m_registrationFilePath); }
|
||||
|
||||
QString filePath() const { return m_cppEditorDocument->filePath(); }
|
||||
QByteArray contents() const { return m_cppEditorDocument->contentsText(); }
|
||||
unsigned revision() const { return m_cppEditorDocument->contentsRevision(); }
|
||||
|
||||
CppTools::BaseEditorDocumentProcessor *processor()
|
||||
{ return m_cppEditorDocument->processor(); }
|
||||
|
||||
private:
|
||||
CppEditor::Internal::CPPEditorDocument * const m_cppEditorDocument;
|
||||
// The file path of the editor document can change (e.g. by "Save As..."), so make sure
|
||||
// that un-registration happens with the path the document was registered.
|
||||
const QString m_registrationFilePath;
|
||||
};
|
||||
|
||||
CPPEditorDocument::CPPEditorDocument()
|
||||
: m_fileIsBeingReloaded(false)
|
||||
, m_isObjCEnabled(false)
|
||||
, m_cachedContentsRevision(-1)
|
||||
, m_processorRevision(0)
|
||||
, m_completionAssistProvider(0)
|
||||
{
|
||||
setId(CppEditor::Constants::CPPEDITOR_ID);
|
||||
connect(this, SIGNAL(tabSettingsChanged()),
|
||||
this, SLOT(invalidateFormatterCache()));
|
||||
connect(this, SIGNAL(mimeTypeChanged()),
|
||||
this, SLOT(onMimeTypeChanged()));
|
||||
setSyntaxHighlighter(new CppHighlighter);
|
||||
setIndenter(new CppTools::CppQtStyleIndenter);
|
||||
onMimeTypeChanged();
|
||||
|
||||
connect(this, SIGNAL(tabSettingsChanged()), this, SLOT(invalidateFormatterCache()));
|
||||
connect(this, SIGNAL(mimeTypeChanged()), this, SLOT(onMimeTypeChanged()));
|
||||
|
||||
connect(this, SIGNAL(aboutToReload()), this, SLOT(onAboutToReload()));
|
||||
connect(this, SIGNAL(reloadFinished(bool)), this, SLOT(onReloadFinished()));
|
||||
connect(this, SIGNAL(filePathChanged(QString,QString)),
|
||||
this, SLOT(onFilePathChanged(QString,QString)));
|
||||
|
||||
m_processorTimer.setSingleShot(true);
|
||||
m_processorTimer.setInterval(processDocumentIntervalInMs);
|
||||
connect(&m_processorTimer, SIGNAL(timeout()), this, SLOT(processDocument()));
|
||||
|
||||
// See also onFilePathChanged() for more initialization
|
||||
}
|
||||
|
||||
CPPEditorDocument::~CPPEditorDocument()
|
||||
{
|
||||
}
|
||||
|
||||
bool CPPEditorDocument::isObjCEnabled() const
|
||||
@@ -58,6 +120,38 @@ bool CPPEditorDocument::isObjCEnabled() const
|
||||
return m_isObjCEnabled;
|
||||
}
|
||||
|
||||
CppTools::CppCompletionAssistProvider *CPPEditorDocument::completionAssistProvider() const
|
||||
{
|
||||
return m_completionAssistProvider;
|
||||
}
|
||||
|
||||
void CPPEditorDocument::semanticRehighlight()
|
||||
{
|
||||
CppTools::BaseEditorDocumentProcessor *p = processor();
|
||||
QTC_ASSERT(p, return);
|
||||
p->semanticRehighlight(true);
|
||||
}
|
||||
|
||||
CppTools::SemanticInfo CPPEditorDocument::recalculateSemanticInfo()
|
||||
{
|
||||
CppTools::BaseEditorDocumentProcessor *p = processor();
|
||||
QTC_ASSERT(p, CppTools::SemanticInfo());
|
||||
return p->recalculateSemanticInfo();
|
||||
}
|
||||
|
||||
QByteArray CPPEditorDocument::contentsText() const
|
||||
{
|
||||
QMutexLocker locker(&m_cachedContentsLock);
|
||||
|
||||
const int currentRevision = document()->revision();
|
||||
if (m_cachedContentsRevision != currentRevision && !m_fileIsBeingReloaded) {
|
||||
m_cachedContentsRevision = currentRevision;
|
||||
m_cachedContents = plainText().toUtf8();
|
||||
}
|
||||
|
||||
return m_cachedContents;
|
||||
}
|
||||
|
||||
void CPPEditorDocument::applyFontSettings()
|
||||
{
|
||||
if (TextEditor::SyntaxHighlighter *highlighter = syntaxHighlighter()) {
|
||||
@@ -82,7 +176,94 @@ void CPPEditorDocument::onMimeTypeChanged()
|
||||
{
|
||||
const QString &mt = mimeType();
|
||||
m_isObjCEnabled = (mt == QLatin1String(CppTools::Constants::OBJECTIVE_C_SOURCE_MIMETYPE)
|
||||
|| mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE));
|
||||
|| mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE));
|
||||
m_completionAssistProvider = mm()->completionAssistProvider(mt);
|
||||
}
|
||||
|
||||
void CPPEditorDocument::onAboutToReload()
|
||||
{
|
||||
QTC_CHECK(!m_fileIsBeingReloaded);
|
||||
m_fileIsBeingReloaded = true;
|
||||
}
|
||||
|
||||
void CPPEditorDocument::onReloadFinished()
|
||||
{
|
||||
QTC_CHECK(m_fileIsBeingReloaded);
|
||||
m_fileIsBeingReloaded = false;
|
||||
}
|
||||
|
||||
void CPPEditorDocument::onFilePathChanged(const QString &oldPath, const QString &newPath)
|
||||
{
|
||||
Q_UNUSED(oldPath);
|
||||
|
||||
if (!newPath.isEmpty()) {
|
||||
setMimeType(Core::MimeDatabase::findByFile(QFileInfo(newPath)).type());
|
||||
|
||||
disconnect(this, SIGNAL(contentsChanged()), this, SLOT(scheduleProcessDocument()));
|
||||
connect(this, SIGNAL(contentsChanged()), this, SLOT(scheduleProcessDocument()));
|
||||
|
||||
// Un-Register/Register in ModelManager
|
||||
m_editorDocumentHandle.reset(new CppEditorDocumentHandle(this));
|
||||
|
||||
resetProcessor();
|
||||
m_processorRevision = document()->revision();
|
||||
processDocument();
|
||||
}
|
||||
}
|
||||
|
||||
void CPPEditorDocument::scheduleProcessDocument()
|
||||
{
|
||||
m_processorRevision = document()->revision();
|
||||
m_processorTimer.start(processDocumentIntervalInMs);
|
||||
}
|
||||
|
||||
void CPPEditorDocument::processDocument()
|
||||
{
|
||||
if (processor()->isParserRunning() || m_processorRevision != contentsRevision()) {
|
||||
m_processorTimer.start();
|
||||
return;
|
||||
}
|
||||
|
||||
m_processorTimer.stop();
|
||||
if (m_fileIsBeingReloaded || filePath().isEmpty())
|
||||
return;
|
||||
|
||||
processor()->run();
|
||||
}
|
||||
|
||||
void CPPEditorDocument::resetProcessor()
|
||||
{
|
||||
releaseResources();
|
||||
processor(); // creates a new processor
|
||||
}
|
||||
|
||||
unsigned CPPEditorDocument::contentsRevision() const
|
||||
{
|
||||
return document()->revision();
|
||||
}
|
||||
|
||||
void CPPEditorDocument::releaseResources()
|
||||
{
|
||||
if (m_processor)
|
||||
disconnect(m_processor.data(), 0, this, 0);
|
||||
m_processor.reset();
|
||||
}
|
||||
|
||||
CppTools::BaseEditorDocumentProcessor *CPPEditorDocument::processor()
|
||||
{
|
||||
if (!m_processor) {
|
||||
m_processor.reset(mm()->editorDocumentProcessor(this));
|
||||
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::codeWarningsUpdated,
|
||||
this, &CPPEditorDocument::codeWarningsUpdated);
|
||||
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::ifdefedOutBlocksUpdated,
|
||||
this, &CPPEditorDocument::ifdefedOutBlocksUpdated);
|
||||
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::cppDocumentUpdated,
|
||||
this, &CPPEditorDocument::cppDocumentUpdated);
|
||||
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::semanticInfoUpdated,
|
||||
this, &CPPEditorDocument::semanticInfoUpdated);
|
||||
}
|
||||
|
||||
return m_processor.data();
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
Reference in New Issue
Block a user