CompilationDatabase: enable the listing of non-cpp files

Files listed in an optional "compile_database.json.files" are also
visible in the project, similar to the GenericProjectManager.

Change-Id: I1250b674298079bab90a0ffe036107a09b4ac6e5
Reviewed-by: Thomas Otto <totto@zbh.uni-hamburg.de>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Thomas Otto
2019-05-01 16:59:52 +02:00
parent de8827b53f
commit b32c1453b4
3 changed files with 56 additions and 3 deletions

View File

@@ -30,6 +30,7 @@ namespace Constants {
const char COMPILATIONDATABASEMIMETYPE[] = "text/x-compilation-database-project";
const char COMPILATIONDATABASEPROJECT_ID[] = "CompilationDatabase.CompilationDatabaseEditor";
const char COMPILATIONDATABASEPROJECT_FILES_SUFFIX[] = ".files";
} // Constants
} // CompilationDatabaseProjectManager

View File

@@ -405,12 +405,38 @@ std::vector<Entry> readJsonObjects(const QString &filePath)
return result;
}
QStringList readExtraFiles(const QString &filePath)
{
QStringList result;
QFile file(filePath);
if (file.open(QFile::ReadOnly)) {
QTextStream stream(&file);
while (!stream.atEnd()) {
QString line = stream.readLine();
line = line.trimmed();
if (line.isEmpty() || line.startsWith('#'))
continue;
result.push_back(line);
}
}
return result;
}
} // anonymous namespace
void CompilationDatabaseProject::buildTreeAndProjectParts(const Utils::FileName &projectFile)
{
std::vector<Entry> array = readJsonObjects(projectFilePath().toString());
if (array.empty()) {
const QString jsonExtraFilename = projectFilePath().toString() +
Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX;
const QStringList &extras = readExtraFiles(jsonExtraFilename);
if (array.empty() && extras.empty()) {
emitParsingFinished(false);
return;
}
@@ -448,6 +474,21 @@ void CompilationDatabaseProject::buildTreeAndProjectParts(const Utils::FileName
rpps.append(rpp);
}
if (!extras.empty()) {
const QString baseDir = projectFile.parentDir().toString();
QStringList extraFiles;
for (const QString &extra : extras) {
auto extraFile = Utils::FileName::fromString(baseDir);
extraFile.appendPath(extra);
extraFiles.append(extraFile.toString());
}
CppTools::RawProjectPart rppExtra;
rppExtra.setFiles(extraFiles);
rpps.append(rppExtra);
}
m_treeScanner.future().waitForFinished();
QCoreApplication::processEvents();
@@ -458,6 +499,10 @@ void CompilationDatabaseProject::buildTreeAndProjectParts(const Utils::FileName
root->addNode(std::make_unique<FileNode>(projectFile, FileType::Project));
if (QFile::exists(jsonExtraFilename))
root->addNode(std::make_unique<FileNode>(Utils::FileName::fromString(jsonExtraFilename),
FileType::Project));
setRootProjectNode(std::move(root));
m_cppCodeModelUpdater->update({this, kitInfo, rpps});
@@ -511,6 +556,8 @@ CompilationDatabaseProject::CompilationDatabaseProject(const Utils::FileName &pr
&CompilationDatabaseProject::reparseProject);
m_fileSystemWatcher.addFile(projectFile.toString(), Utils::FileSystemWatcher::WatchModifiedDate);
m_fileSystemWatcher.addFile(projectFile.toString() + Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX,
Utils::FileSystemWatcher::WatchModifiedDate);
connect(&m_fileSystemWatcher,
&Utils::FileSystemWatcher::fileChanged,
this,

View File

@@ -43,13 +43,18 @@ namespace CompilationDatabaseProjectManager {
namespace Internal {
const char CHANGEROOTDIR[] = "CompilationDatabaseProjectManager.ChangeRootDirectory";
const char COMPILE_COMMANDS_JSON[] = "compile_commands.json";
bool CompilationDatabaseProjectManagerPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{
Q_UNUSED(arguments);
Q_UNUSED(errorMessage);
Core::FileIconProvider::registerIconOverlayForFilename(Utils::Icons::PROJECT.imageFileName(),
"compile_commands.json");
Core::FileIconProvider::registerIconOverlayForFilename(
Utils::Icons::PROJECT.imageFileName(),
COMPILE_COMMANDS_JSON);
Core::FileIconProvider::registerIconOverlayForFilename(
Utils::Icons::PROJECT.imageFileName(),
QString(COMPILE_COMMANDS_JSON) + Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX);
ProjectExplorer::ProjectManager::registerProjectType<CompilationDatabaseProject>(
Constants::COMPILATIONDATABASEMIMETYPE);