2015-10-01 12:45:06 +02:00
|
|
|
/****************************************************************************
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:57:14 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-06-01 18:51:55 +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:14 +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.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:57:14 +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.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "unsavedfiles.h"
|
|
|
|
|
|
2016-01-28 18:31:05 +01:00
|
|
|
#include "unsavedfile.h"
|
|
|
|
|
|
2015-08-13 11:48:42 +02:00
|
|
|
#include <algorithm>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
namespace ClangBackEnd {
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
class UnsavedFilesData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
UnsavedFilesData();
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
time_point lastChangeTimePoint;
|
2016-01-28 18:31:05 +01:00
|
|
|
std::vector<UnsavedFile> unsavedFiles;
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UnsavedFilesData::UnsavedFilesData()
|
|
|
|
|
: lastChangeTimePoint(std::chrono::steady_clock::now())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnsavedFiles::UnsavedFiles()
|
|
|
|
|
: d(std::make_shared<UnsavedFilesData>())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnsavedFiles::~UnsavedFiles() = default;
|
|
|
|
|
|
|
|
|
|
UnsavedFiles::UnsavedFiles(const UnsavedFiles &) = default;
|
2015-06-10 13:52:45 +02:00
|
|
|
UnsavedFiles &UnsavedFiles::operator=(const UnsavedFiles &) = default;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
UnsavedFiles::UnsavedFiles(UnsavedFiles &&other)
|
|
|
|
|
: d(std::move(other.d))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-10 13:52:45 +02:00
|
|
|
UnsavedFiles &UnsavedFiles::operator=(UnsavedFiles &&other)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
d = std::move(other.d);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UnsavedFiles::createOrUpdate(const QVector<FileContainer> &fileContainers)
|
|
|
|
|
{
|
|
|
|
|
for (const FileContainer &fileContainer : fileContainers)
|
2016-01-28 18:31:05 +01:00
|
|
|
updateUnsavedFileWithFileContainer(fileContainer);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
updateLastChangeTimePoint();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UnsavedFiles::remove(const QVector<FileContainer> &fileContainers)
|
|
|
|
|
{
|
|
|
|
|
for (const FileContainer &fileContainer : fileContainers)
|
2016-01-28 18:31:05 +01:00
|
|
|
removeUnsavedFile(fileContainer);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
updateLastChangeTimePoint();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 18:31:05 +01:00
|
|
|
UnsavedFile &UnsavedFiles::unsavedFile(const Utf8String &filePath)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-01-28 18:31:05 +01:00
|
|
|
const auto isMatchingFile = [filePath] (const UnsavedFile &unsavedFile) {
|
|
|
|
|
return unsavedFile.filePath() == filePath;
|
|
|
|
|
};
|
|
|
|
|
const auto unsavedFileIterator = std::find_if(d->unsavedFiles.begin(),
|
|
|
|
|
d->unsavedFiles.end(),
|
|
|
|
|
isMatchingFile);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-01-28 18:31:05 +01:00
|
|
|
if (unsavedFileIterator != d->unsavedFiles.end())
|
|
|
|
|
return *unsavedFileIterator;
|
|
|
|
|
|
|
|
|
|
static UnsavedFile defaultUnsavedFile = UnsavedFile(Utf8String(), Utf8String());
|
|
|
|
|
return defaultUnsavedFile;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-01 12:00:21 +02:00
|
|
|
uint UnsavedFiles::count() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-01-28 18:31:05 +01:00
|
|
|
return uint(d->unsavedFiles.size());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CXUnsavedFile *UnsavedFiles::cxUnsavedFiles() const
|
|
|
|
|
{
|
2016-01-28 18:31:05 +01:00
|
|
|
return d->unsavedFiles.data()->data();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const time_point &UnsavedFiles::lastChangeTimePoint() const
|
|
|
|
|
{
|
|
|
|
|
return d->lastChangeTimePoint;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 18:31:05 +01:00
|
|
|
void UnsavedFiles::updateUnsavedFileWithFileContainer(const FileContainer &fileContainer)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-01-28 18:31:05 +01:00
|
|
|
if (fileContainer.hasUnsavedFileContent())
|
|
|
|
|
addOrUpdateUnsavedFile(fileContainer);
|
|
|
|
|
else
|
|
|
|
|
removeUnsavedFile(fileContainer);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 18:31:05 +01:00
|
|
|
void UnsavedFiles::removeUnsavedFile(const FileContainer &fileContainer)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
const Utf8String filePath = fileContainer.filePath();
|
2016-01-28 18:31:05 +01:00
|
|
|
auto removeBeginIterator = std::partition(d->unsavedFiles.begin(),
|
|
|
|
|
d->unsavedFiles.end(),
|
|
|
|
|
[filePath] (const UnsavedFile &unsavedFile) { return filePath != unsavedFile.filePath(); });
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-01-28 18:31:05 +01:00
|
|
|
d->unsavedFiles.erase(removeBeginIterator, d->unsavedFiles.end());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 18:31:05 +01:00
|
|
|
void UnsavedFiles::addOrUpdateUnsavedFile(const FileContainer &fileContainer)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
const Utf8String filePath = fileContainer.filePath();
|
|
|
|
|
const Utf8String fileContent = fileContainer.unsavedFileContent();
|
2016-01-28 18:31:05 +01:00
|
|
|
auto isSameFile = [filePath] (const UnsavedFile &unsavedFile) { return filePath == unsavedFile.filePath(); };
|
|
|
|
|
|
|
|
|
|
auto unsavedFileIterator = std::find_if(d->unsavedFiles.begin(), d->unsavedFiles.end(), isSameFile);
|
|
|
|
|
if (unsavedFileIterator == d->unsavedFiles.end())
|
|
|
|
|
d->unsavedFiles.emplace_back(filePath, fileContent);
|
|
|
|
|
else
|
|
|
|
|
*unsavedFileIterator = UnsavedFile(filePath, fileContent);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UnsavedFiles::updateLastChangeTimePoint()
|
|
|
|
|
{
|
|
|
|
|
d->lastChangeTimePoint = std::chrono::steady_clock::now();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
} // namespace ClangBackEnd
|