CppTools: Use fallback project part if project is closed

...instead of sticking to the old one.

If a project is closed, the editor should reflect that. It also simplifies the code.

Change-Id: I9b8a94513d3b06a238cfc4ee60c11c0d0da3d93f
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Nikolai Kosjar
2017-01-19 11:08:13 +01:00
parent ec3dcfd6ad
commit 07f32ef57a
7 changed files with 44 additions and 47 deletions

View File

@@ -30,9 +30,6 @@ namespace ClangCodeModel {
ClangEditorDocumentParser::ClangEditorDocumentParser(const QString &filePath) ClangEditorDocumentParser::ClangEditorDocumentParser(const QString &filePath)
: BaseEditorDocumentParser(filePath) : BaseEditorDocumentParser(filePath)
{ {
BaseEditorDocumentParser::Configuration config = configuration();
config.stickToPreviousProjectPart = false;
setConfiguration(config);
} }
void ClangEditorDocumentParser::updateImpl(const QFutureInterface<void> &, void ClangEditorDocumentParser::updateImpl(const QFutureInterface<void> &,
@@ -41,7 +38,7 @@ void ClangEditorDocumentParser::updateImpl(const QFutureInterface<void> &,
State state_ = state(); State state_ = state();
state_.projectPartInfo = determineProjectPart(filePath(), state_.projectPartInfo = determineProjectPart(filePath(),
configuration(), configuration(),
state_, state_.projectPartInfo,
updateParams.activeProject, updateParams.activeProject,
updateParams.languagePreference, updateParams.languagePreference,
updateParams.hasActiveProjectChanged); updateParams.hasActiveProjectChanged);

View File

@@ -122,7 +122,7 @@ BaseEditorDocumentParser::Ptr BaseEditorDocumentParser::get(const QString &fileP
ProjectPartInfo BaseEditorDocumentParser::determineProjectPart( ProjectPartInfo BaseEditorDocumentParser::determineProjectPart(
const QString &filePath, const QString &filePath,
const Configuration &config, const Configuration &config,
const State &state, const ProjectPartInfo &currentProjectPartInfo,
const ProjectExplorer::Project *activeProject, const ProjectExplorer::Project *activeProject,
Language languagePreference, Language languagePreference,
bool hasActiveProjectChanged) bool hasActiveProjectChanged)
@@ -143,9 +143,8 @@ ProjectPartInfo BaseEditorDocumentParser::determineProjectPart(
const ProjectPartInfo chooserResult const ProjectPartInfo chooserResult
= chooser.choose(filePath, = chooser.choose(filePath,
state.projectPartInfo.projectPart, currentProjectPartInfo,
config.manuallySetProjectPart, config.manuallySetProjectPart,
config.stickToPreviousProjectPart,
activeProject, activeProject,
languagePreference, languagePreference,
hasActiveProjectChanged); hasActiveProjectChanged);

View File

@@ -47,7 +47,6 @@ public:
static Ptr get(const QString &filePath); static Ptr get(const QString &filePath);
struct Configuration { struct Configuration {
bool stickToPreviousProjectPart = true;
bool usePrecompiledHeaders = false; bool usePrecompiledHeaders = false;
QByteArray editorDefines; QByteArray editorDefines;
ProjectPart::Ptr manuallySetProjectPart; ProjectPart::Ptr manuallySetProjectPart;
@@ -95,10 +94,9 @@ protected:
State state() const; State state() const;
void setState(const State &state); void setState(const State &state);
static ProjectPartInfo determineProjectPart( static ProjectPartInfo determineProjectPart(const QString &filePath,
const QString &filePath,
const Configuration &config, const Configuration &config,
const State &state, const ProjectPartInfo &currentProjectPartInfo,
const ProjectExplorer::Project *activeProject, const ProjectExplorer::Project *activeProject,
Language languagePreference, Language languagePreference,
bool hasActiveProjectChanged); bool hasActiveProjectChanged);

View File

@@ -79,7 +79,7 @@ void BuiltinEditorDocumentParser::updateImpl(const QFutureInterface<void> &futur
baseState.projectPartInfo = determineProjectPart(filePath(), baseState.projectPartInfo = determineProjectPart(filePath(),
baseConfig, baseConfig,
baseState, baseState.projectPartInfo,
updateParams.activeProject, updateParams.activeProject,
updateParams.languagePreference, updateParams.languagePreference,
updateParams.hasActiveProjectChanged); updateParams.hasActiveProjectChanged);

View File

@@ -124,9 +124,8 @@ private:
ProjectPartInfo ProjectPartChooser::choose( ProjectPartInfo ProjectPartChooser::choose(
const QString &filePath, const QString &filePath,
const ProjectPart::Ptr &currentProjectPart, const ProjectPartInfo &currentProjectPartInfo,
const ProjectPart::Ptr &manuallySetProjectPart, const ProjectPart::Ptr &manuallySetProjectPart,
bool stickToPreviousProjectPart,
const ProjectExplorer::Project *activeProject, const ProjectExplorer::Project *activeProject,
Language languagePreference, Language languagePreference,
bool projectHasChanged) const bool projectHasChanged) const
@@ -138,15 +137,14 @@ ProjectPartInfo ProjectPartChooser::choose(
if (manuallySetProjectPart) if (manuallySetProjectPart)
return {manuallySetProjectPart, ProjectPartInfo::NoHint}; return {manuallySetProjectPart, ProjectPartInfo::NoHint};
ProjectPart::Ptr projectPart = currentProjectPart; ProjectPart::Ptr projectPart = currentProjectPartInfo.projectPart;
ProjectPartInfo::Hint hint = ProjectPartInfo::NoHint; ProjectPartInfo::Hint hint = ProjectPartInfo::NoHint;
QList<ProjectPart::Ptr> projectParts = m_projectPartsForFile(filePath); QList<ProjectPart::Ptr> projectParts = m_projectPartsForFile(filePath);
if (projectParts.isEmpty()) { if (projectParts.isEmpty()) {
if (projectPart && stickToPreviousProjectPart) if (projectPart && currentProjectPartInfo.hint == ProjectPartInfo::IsFallbackMatch)
// File is not directly part of any project, but we got one before. We will re-use it, // Avoid re-calculating the expensive dependency table for non-project files.
// because re-calculating this can be expensive when the dependency table is big. return {projectPart, ProjectPartInfo::IsFallbackMatch};
return {projectPart, ProjectPartInfo::NoHint};
// Fall-back step 1: Get some parts through the dependency table: // Fall-back step 1: Get some parts through the dependency table:
projectParts = m_projectPartsFromDependenciesForFile(filePath); projectParts = m_projectPartsFromDependenciesForFile(filePath);

View File

@@ -50,9 +50,8 @@ public:
ProjectPartInfo choose( ProjectPartInfo choose(
const QString &filePath, const QString &filePath,
const ProjectPart::Ptr &currentProjectPart, const ProjectPartInfo &currentProjectPartInfo,
const ProjectPart::Ptr &manuallySetProjectPart, const ProjectPart::Ptr &manuallySetProjectPart,
bool stickToPreviousProjectPart,
const ProjectExplorer::Project *activeProject, const ProjectExplorer::Project *activeProject,
Language languagePreference, Language languagePreference,
bool projectHasChanged) const; bool projectHasChanged) const;

View File

@@ -49,9 +49,9 @@ protected:
protected: protected:
QString filePath; QString filePath;
ProjectPart::Ptr currentProjectPart{new ProjectPart}; ProjectPartInfo currentProjectPartInfo{ProjectPart::Ptr(new ProjectPart),
ProjectPartInfo::NoHint};
ProjectPart::Ptr manuallySetProjectPart; ProjectPart::Ptr manuallySetProjectPart;
bool stickToPreviousProjectPart = false;
const ProjectExplorer::Project *activeProject = nullptr; const ProjectExplorer::Project *activeProject = nullptr;
bool projectHasChanged = false; bool projectHasChanged = false;
Language languagePreference = Language::Cxx; Language languagePreference = Language::Cxx;
@@ -75,11 +75,11 @@ TEST_F(ProjectPartChooser, ForMultipleChoosePrevious)
{ {
const ProjectPart::Ptr otherProjectPart; const ProjectPart::Ptr otherProjectPart;
projectPartsForFile += otherProjectPart; projectPartsForFile += otherProjectPart;
projectPartsForFile += currentProjectPart; projectPartsForFile += currentProjectPartInfo.projectPart;
const ProjectPart::Ptr chosen = choose().projectPart; const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(currentProjectPart)); ASSERT_THAT(chosen, Eq(currentProjectPartInfo.projectPart));
} }
TEST_F(ProjectPartChooser, ForMultipleChooseFromActiveProject) TEST_F(ProjectPartChooser, ForMultipleChooseFromActiveProject)
@@ -126,7 +126,7 @@ TEST_F(ProjectPartChooser, ForMultipleCheckIfActiveProjectChanged)
const ProjectPart::Ptr firstProjectPart = projectParts.at(0); const ProjectPart::Ptr firstProjectPart = projectParts.at(0);
const ProjectPart::Ptr secondProjectPart = projectParts.at(1); const ProjectPart::Ptr secondProjectPart = projectParts.at(1);
projectPartsForFile += projectParts; projectPartsForFile += projectParts;
currentProjectPart = firstProjectPart; currentProjectPartInfo.projectPart = firstProjectPart;
activeProject = secondProjectPart->project; activeProject = secondProjectPart->project;
projectHasChanged = true; projectHasChanged = true;
@@ -168,25 +168,6 @@ TEST_F(ProjectPartChooser, IndicateMultiple)
ASSERT_THAT(hint, Eq(ProjectPartInfo::Hint::IsAmbiguousMatch)); ASSERT_THAT(hint, Eq(ProjectPartInfo::Hint::IsAmbiguousMatch));
} }
TEST_F(ProjectPartChooser, IfProjectIsGoneStickToPrevious) // Built-in Code Model
{
stickToPreviousProjectPart = true;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(currentProjectPart));
}
TEST_F(ProjectPartChooser, IfProjectIsGoneDoNotStickToPrevious) // Clang Code Model
{
currentProjectPart.clear();
stickToPreviousProjectPart = true;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(ProjectPart::Ptr()));
}
TEST_F(ProjectPartChooser, ForMultipleChooseNewIfPreviousIsGone) TEST_F(ProjectPartChooser, ForMultipleChooseNewIfPreviousIsGone)
{ {
const ProjectPart::Ptr newProjectPart{new ProjectPart}; const ProjectPart::Ptr newProjectPart{new ProjectPart};
@@ -216,6 +197,32 @@ TEST_F(ProjectPartChooser, FallbackToProjectPartFromModelManager)
ASSERT_THAT(chosen, Eq(fallbackProjectPart)); ASSERT_THAT(chosen, Eq(fallbackProjectPart));
} }
TEST_F(ProjectPartChooser, ContinueUsingFallbackFromModelManagerIfProjectDoesNotChange)
{
// ...without re-calculating the dependency table.
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
currentProjectPartInfo.hint = ProjectPartInfo::IsFallbackMatch;
projectPartsFromDependenciesForFile += ProjectPart::Ptr(new ProjectPart);
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(fallbackProjectPart));
}
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges)
{
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
currentProjectPartInfo.hint = ProjectPartInfo::IsFallbackMatch;
const ProjectPart::Ptr addedProject(new ProjectPart);
projectPartsForFile += addedProject;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(addedProject));
}
TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager) TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager)
{ {
fallbackProjectPart.reset(new ProjectPart); fallbackProjectPart.reset(new ProjectPart);
@@ -241,9 +248,8 @@ void ProjectPartChooser::SetUp()
const ProjectPartInfo ProjectPartChooser::choose() const const ProjectPartInfo ProjectPartChooser::choose() const
{ {
return chooser.choose(filePath, return chooser.choose(filePath,
currentProjectPart, currentProjectPartInfo,
manuallySetProjectPart, manuallySetProjectPart,
stickToPreviousProjectPart,
activeProject, activeProject,
languagePreference, languagePreference,
projectHasChanged); projectHasChanged);