2014-07-30 17:37:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-07-30 17:37:17 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2014-07-30 17:37:17 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2014-07-30 17:37:17 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "baseeditordocumentparser.h"
|
2015-03-05 08:22:48 +01:00
|
|
|
#include "baseeditordocumentprocessor.h"
|
2014-07-30 17:37:17 +02:00
|
|
|
|
2015-12-15 11:59:48 +01:00
|
|
|
#include "cppmodelmanager.h"
|
2014-08-19 15:59:29 +02:00
|
|
|
#include "editordocumenthandle.h"
|
|
|
|
|
|
2014-07-30 17:37:17 +02:00
|
|
|
namespace CppTools {
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\class CppTools::BaseEditorDocumentParser
|
|
|
|
|
|
|
|
|
|
\brief The BaseEditorDocumentParser class parses a source text as
|
|
|
|
|
precisely as possible.
|
|
|
|
|
|
|
|
|
|
It's meant to be used in the C++ editor to get precise results by using
|
|
|
|
|
the "best" project part for a file.
|
|
|
|
|
|
2015-07-10 12:34:40 +02:00
|
|
|
Derived classes are expected to implement updateHelper() this way:
|
2015-07-07 15:02:14 +02:00
|
|
|
|
|
|
|
|
\list
|
|
|
|
|
\li Get a copy of the configuration and the last state.
|
|
|
|
|
\li Work on the data and do whatever is necessary. At least, update
|
|
|
|
|
the project part with the help of determineProjectPart().
|
2015-07-10 12:34:40 +02:00
|
|
|
\li Ensure the new state is set before updateHelper() returns.
|
2015-07-07 15:02:14 +02:00
|
|
|
\endlist
|
2014-07-30 17:37:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
BaseEditorDocumentParser::BaseEditorDocumentParser(const QString &filePath)
|
2015-07-07 15:02:14 +02:00
|
|
|
: m_filePath(filePath)
|
2014-07-30 17:37:17 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseEditorDocumentParser::~BaseEditorDocumentParser()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BaseEditorDocumentParser::filePath() const
|
|
|
|
|
{
|
|
|
|
|
return m_filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
BaseEditorDocumentParser::Configuration BaseEditorDocumentParser::configuration() const
|
2014-07-30 17:37:17 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
QMutexLocker locker(&m_stateAndConfigurationMutex);
|
|
|
|
|
return m_configuration;
|
2014-07-30 17:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
void BaseEditorDocumentParser::setConfiguration(const Configuration &configuration)
|
2014-07-30 17:37:17 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
QMutexLocker locker(&m_stateAndConfigurationMutex);
|
|
|
|
|
m_configuration = configuration;
|
2014-07-30 17:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-15 12:32:01 +01:00
|
|
|
void BaseEditorDocumentParser::update(const WorkingCopy &workingCopy)
|
2016-07-27 14:48:42 +02:00
|
|
|
{
|
|
|
|
|
QFutureInterface<void> dummy;
|
|
|
|
|
update(dummy, workingCopy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseEditorDocumentParser::update(const QFutureInterface<void> &future,
|
|
|
|
|
const WorkingCopy &workingCopy)
|
2015-07-10 12:34:40 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_updateIsRunning);
|
2016-07-27 14:48:42 +02:00
|
|
|
updateHelper(future, workingCopy);
|
2015-07-10 12:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
BaseEditorDocumentParser::State BaseEditorDocumentParser::state() const
|
2014-07-30 17:37:17 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
QMutexLocker locker(&m_stateAndConfigurationMutex);
|
|
|
|
|
return m_state;
|
2014-07-30 17:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
void BaseEditorDocumentParser::setState(const State &state)
|
2014-07-30 17:37:17 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
QMutexLocker locker(&m_stateAndConfigurationMutex);
|
|
|
|
|
m_state = state;
|
2014-07-30 17:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
ProjectPart::Ptr BaseEditorDocumentParser::projectPart() const
|
2014-07-30 17:37:17 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
return state().projectPart;
|
2014-07-30 17:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-01 17:34:07 +02:00
|
|
|
BaseEditorDocumentParser::Ptr BaseEditorDocumentParser::get(const QString &filePath)
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2014-09-15 00:12:27 +02:00
|
|
|
CppModelManager *cmmi = CppModelManager::instance();
|
2014-11-24 11:19:41 +01:00
|
|
|
if (CppEditorDocumentHandle *cppEditorDocument = cmmi->cppEditorDocument(filePath)) {
|
|
|
|
|
if (BaseEditorDocumentProcessor *processor = cppEditorDocument->processor())
|
2014-08-19 15:59:29 +02:00
|
|
|
return processor->parser();
|
|
|
|
|
}
|
2015-09-01 17:34:07 +02:00
|
|
|
return BaseEditorDocumentParser::Ptr();
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart(const QString &filePath,
|
|
|
|
|
const Configuration &config,
|
|
|
|
|
const State &state)
|
2014-07-30 17:37:17 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
if (config.manuallySetProjectPart)
|
|
|
|
|
return config.manuallySetProjectPart;
|
2015-07-07 15:17:58 +02:00
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
ProjectPart::Ptr projectPart = state.projectPart;
|
2014-07-30 17:37:17 +02:00
|
|
|
|
2014-09-15 00:12:27 +02:00
|
|
|
CppModelManager *cmm = CppModelManager::instance();
|
2015-07-07 15:02:14 +02:00
|
|
|
QList<ProjectPart::Ptr> projectParts = cmm->projectPart(filePath);
|
2014-07-30 17:37:17 +02:00
|
|
|
if (projectParts.isEmpty()) {
|
2015-07-14 19:01:32 +02:00
|
|
|
if (projectPart && config.stickToPreviousProjectPart)
|
2014-07-30 17:37:17 +02:00
|
|
|
// File is not directly part of any project, but we got one before. We will re-use it,
|
|
|
|
|
// because re-calculating this can be expensive when the dependency table is big.
|
2015-07-07 15:17:58 +02:00
|
|
|
return projectPart;
|
2014-07-30 17:37:17 +02:00
|
|
|
|
|
|
|
|
// Fall-back step 1: Get some parts through the dependency table:
|
2015-07-07 15:02:14 +02:00
|
|
|
projectParts = cmm->projectPartFromDependencies(Utils::FileName::fromString(filePath));
|
2014-07-30 17:37:17 +02:00
|
|
|
if (projectParts.isEmpty())
|
|
|
|
|
// Fall-back step 2: Use fall-back part from the model manager:
|
2015-07-07 15:17:58 +02:00
|
|
|
projectPart = cmm->fallbackProjectPart();
|
2014-07-30 17:37:17 +02:00
|
|
|
else
|
2015-07-07 15:17:58 +02:00
|
|
|
projectPart = projectParts.first();
|
2014-07-30 17:37:17 +02:00
|
|
|
} else {
|
2015-07-07 15:17:58 +02:00
|
|
|
if (!projectParts.contains(projectPart))
|
2014-07-30 17:37:17 +02:00
|
|
|
// Apparently the project file changed, so update our project part.
|
2015-07-07 15:17:58 +02:00
|
|
|
projectPart = projectParts.first();
|
2014-07-30 17:37:17 +02:00
|
|
|
}
|
2015-07-07 15:17:58 +02:00
|
|
|
|
|
|
|
|
return projectPart;
|
2014-07-30 17:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace CppTools
|