forked from qt-creator/qt-creator
Utils: Make TextFileFormat::ReadResult a class
... containing the previous enum plus an error string, remove the *errorString out-parameter from the static functions and adjust all callers. Change-Id: I9eab8b40cd28492906a1336fc6bab55cf1ae1122 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -198,7 +198,7 @@ bool TextFileFormat::decode(const QByteArray &data, QStringList *target) const
|
||||
// Read text file contents to string or stringlist.
|
||||
template <class Target>
|
||||
TextFileFormat::ReadResult readTextFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
Target *target, TextFileFormat *format, QString *errorString,
|
||||
Target *target, TextFileFormat *format,
|
||||
QByteArray *decodingErrorSampleIn = nullptr)
|
||||
{
|
||||
if (decodingErrorSampleIn)
|
||||
@@ -207,15 +207,11 @@ TextFileFormat::ReadResult readTextFile(const FilePath &filePath, const QTextCod
|
||||
QByteArray data;
|
||||
try {
|
||||
FileReader reader;
|
||||
if (!reader.fetch(filePath, errorString))
|
||||
return TextFileFormat::ReadIOError;
|
||||
if (const Result<> res = reader.fetch(filePath); !res)
|
||||
return {TextFileFormat::ReadIOError, res.error()};
|
||||
data = reader.data();
|
||||
} catch (const std::bad_alloc &) {
|
||||
if (errorString)
|
||||
*errorString = Tr::tr("Out of memory.");
|
||||
else
|
||||
qWarning() << Q_FUNC_INFO << "Out of memory in" << filePath;
|
||||
return TextFileFormat::ReadMemoryAllocationError;
|
||||
return {TextFileFormat::ReadMemoryAllocationError, Tr::tr("Out of memory.")};
|
||||
}
|
||||
|
||||
if (!data.isEmpty())
|
||||
@@ -225,13 +221,9 @@ TextFileFormat::ReadResult readTextFile(const FilePath &filePath, const QTextCod
|
||||
format->setCodec(defaultCodec ? defaultCodec : QTextCodec::codecForLocale());
|
||||
|
||||
if (!format->decode(data, target)) {
|
||||
if (errorString)
|
||||
*errorString = Tr::tr("An encoding error was encountered.");
|
||||
else
|
||||
qWarning() << Q_FUNC_INFO << "An encoding error was encountered in" << filePath;
|
||||
if (decodingErrorSampleIn)
|
||||
*decodingErrorSampleIn = TextFileFormat::decodingErrorSample(data);
|
||||
return TextFileFormat::ReadEncodingError;
|
||||
return {TextFileFormat::ReadEncodingError, Tr::tr("An encoding error was encountered.")};
|
||||
}
|
||||
return TextFileFormat::ReadSuccess;
|
||||
}
|
||||
@@ -247,15 +239,14 @@ TextFileFormat::ReadResult readTextFile(const FilePath &filePath, const QTextCod
|
||||
|
||||
TextFileFormat::ReadResult
|
||||
TextFileFormat::readFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QStringList *plainTextList, TextFileFormat *format, QString *errorString,
|
||||
QStringList *plainTextList, TextFileFormat *format,
|
||||
QByteArray *decodingErrorSample /* = 0 */)
|
||||
{
|
||||
const TextFileFormat::ReadResult result =
|
||||
readTextFile(filePath, defaultCodec,
|
||||
plainTextList, format, errorString, decodingErrorSample);
|
||||
readTextFile(filePath, defaultCodec, plainTextList, format, decodingErrorSample);
|
||||
if (debug)
|
||||
qDebug().nospace() << Q_FUNC_INFO << filePath << ' ' << *format
|
||||
<< " returns " << result << '/' << plainTextList->size() << " chunks";
|
||||
<< " returns " << result.code << '/' << plainTextList->size() << " chunks";
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -270,31 +261,30 @@ TextFileFormat::ReadResult
|
||||
|
||||
TextFileFormat::ReadResult
|
||||
TextFileFormat::readFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QString *plainText, TextFileFormat *format, QString *errorString,
|
||||
QString *plainText, TextFileFormat *format,
|
||||
QByteArray *decodingErrorSample /* = 0 */)
|
||||
{
|
||||
const TextFileFormat::ReadResult result =
|
||||
readTextFile(filePath, defaultCodec,
|
||||
plainText, format, errorString, decodingErrorSample);
|
||||
plainText, format, decodingErrorSample);
|
||||
if (debug)
|
||||
qDebug().nospace() << Q_FUNC_INFO << filePath << ' ' << *format
|
||||
<< " returns " << result << '/' << plainText->size() << " characters";
|
||||
<< " returns " << result.code << '/' << plainText->size() << " characters";
|
||||
return result;
|
||||
}
|
||||
|
||||
TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath,
|
||||
const QTextCodec *defaultCodec,
|
||||
QByteArray *plainText, QString *errorString)
|
||||
QByteArray *plainText)
|
||||
{
|
||||
QByteArray data;
|
||||
try {
|
||||
FileReader reader;
|
||||
if (!reader.fetch(filePath, errorString))
|
||||
return TextFileFormat::ReadIOError;
|
||||
if (const Result<> res = reader.fetch(filePath); !res)
|
||||
return {TextFileFormat::ReadIOError, res.error()};
|
||||
data = reader.data();
|
||||
} catch (const std::bad_alloc &) {
|
||||
*errorString = Tr::tr("Out of memory.");
|
||||
return TextFileFormat::ReadMemoryAllocationError;
|
||||
return {TextFileFormat::ReadMemoryAllocationError, Tr::tr("Out of memory.")};
|
||||
}
|
||||
|
||||
TextFileFormat format = TextFileFormat::detect(data);
|
||||
@@ -313,16 +303,15 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath
|
||||
return TextFileFormat::ReadSuccess;
|
||||
}
|
||||
|
||||
tl::expected<QString, std::pair<TextFileFormat::ReadResult, QString>>
|
||||
tl::expected<QString, TextFileFormat::ReadResult>
|
||||
TextFileFormat::readFile(const FilePath &filePath, const QTextCodec *defaultCodec)
|
||||
{
|
||||
QString plainText;
|
||||
TextFileFormat format;
|
||||
QString errorString;
|
||||
const TextFileFormat::ReadResult result =
|
||||
readTextFile(filePath, defaultCodec, &plainText, &format, &errorString, nullptr);
|
||||
if (result != TextFileFormat::ReadSuccess)
|
||||
return tl::unexpected(std::make_pair(result, errorString));
|
||||
readTextFile(filePath, defaultCodec, &plainText, &format, nullptr);
|
||||
if (result.code != TextFileFormat::ReadSuccess)
|
||||
return tl::unexpected(result);
|
||||
return plainText;
|
||||
}
|
||||
|
||||
|
@@ -8,8 +8,6 @@
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
#include <utility>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextCodec;
|
||||
class QByteArray;
|
||||
@@ -34,14 +32,24 @@ public:
|
||||
#endif
|
||||
};
|
||||
|
||||
enum ReadResult
|
||||
{
|
||||
enum ReadResultCode {
|
||||
ReadSuccess,
|
||||
ReadEncodingError,
|
||||
ReadMemoryAllocationError,
|
||||
ReadIOError
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT ReadResult
|
||||
{
|
||||
public:
|
||||
ReadResult() = default;
|
||||
ReadResult(ReadResultCode code) : code(code) {}
|
||||
ReadResult(ReadResultCode code, const QString &error) : code(code), error(error) {}
|
||||
|
||||
ReadResultCode code;
|
||||
QString error;
|
||||
};
|
||||
|
||||
TextFileFormat();
|
||||
|
||||
static TextFileFormat detect(const QByteArray &data);
|
||||
@@ -50,15 +58,16 @@ public:
|
||||
bool decode(const QByteArray &data, QStringList *target) const;
|
||||
|
||||
static ReadResult readFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QStringList *plainText, TextFileFormat *format, QString *errorString,
|
||||
QStringList *plainText, TextFileFormat *format,
|
||||
QByteArray *decodingErrorSample = nullptr);
|
||||
static ReadResult readFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QString *plainText, TextFileFormat *format, QString *errorString,
|
||||
QString *plainText, TextFileFormat *format,
|
||||
QByteArray *decodingErrorSample = nullptr);
|
||||
static ReadResult readFileUTF8(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QByteArray *plainText, QString *errorString);
|
||||
static tl::expected<QString, std::pair<ReadResult, QString>>
|
||||
readFile(const FilePath &filePath, const QTextCodec *defaultCodec);
|
||||
QByteArray *plainText);
|
||||
|
||||
static tl::expected<QString, ReadResult> readFile(const FilePath &filePath,
|
||||
const QTextCodec *defaultCodec);
|
||||
|
||||
Utils::Result<> writeFile(const FilePath &filePath, QString plainText) const;
|
||||
|
||||
|
@@ -47,12 +47,11 @@ QByteArray CppParser::getFileContent(const FilePath &filePath) const
|
||||
if (const auto source = m_workingCopy.source(filePath)) {
|
||||
fileContent = *source;
|
||||
} else {
|
||||
QString error;
|
||||
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
||||
if (TextFileFormat::readFileUTF8(filePath, codec, &fileContent, &error)
|
||||
!= TextFileFormat::ReadSuccess) {
|
||||
qDebug() << "Failed to read file" << filePath << ":" << error;
|
||||
}
|
||||
const TextFileFormat::ReadResult result =
|
||||
TextFileFormat::readFileUTF8(filePath, codec, &fileContent);
|
||||
if (result.code != TextFileFormat::ReadSuccess)
|
||||
qDebug() << "Failed to read file" << filePath << ":" << result.error;
|
||||
}
|
||||
fileContent.replace("\r\n", "\n");
|
||||
return fileContent;
|
||||
|
@@ -140,16 +140,14 @@ QTextDocument *FixitsRefactoringFile::document(const FilePath &filePath) const
|
||||
if (m_documents.find(filePath) == m_documents.end()) {
|
||||
QString fileContents;
|
||||
if (!filePath.isEmpty()) {
|
||||
QString error;
|
||||
QTextCodec *defaultCodec = Core::EditorManager::defaultTextCodec();
|
||||
TextFileFormat::ReadResult result = TextFileFormat::readFile(filePath,
|
||||
defaultCodec,
|
||||
&fileContents,
|
||||
&m_textFileFormat,
|
||||
&error);
|
||||
if (result != TextFileFormat::ReadSuccess) {
|
||||
&m_textFileFormat);
|
||||
if (result.code != TextFileFormat::ReadSuccess) {
|
||||
qCDebug(fixitsLog)
|
||||
<< "ERROR: Could not read " << filePath.toUserOutput() << ":" << error;
|
||||
<< "ERROR: Could not read " << filePath.toUserOutput() << ":" << result.error;
|
||||
m_textFileFormat.setCodec(nullptr);
|
||||
}
|
||||
}
|
||||
|
@@ -55,7 +55,7 @@ BaseTextDocument::~BaseTextDocument()
|
||||
|
||||
bool BaseTextDocument::hasDecodingError() const
|
||||
{
|
||||
return d->m_readResult == TextFileFormat::ReadEncodingError;
|
||||
return d->m_readResult.code == TextFileFormat::ReadEncodingError;
|
||||
}
|
||||
|
||||
QByteArray BaseTextDocument::decodingErrorSample() const
|
||||
@@ -129,15 +129,13 @@ bool BaseTextDocument::isUtf8Codec(const QByteArray &name)
|
||||
Returns whether the operation was successful.
|
||||
*/
|
||||
|
||||
BaseTextDocument::ReadResult BaseTextDocument::read(const Utils::FilePath &filePath,
|
||||
QStringList *plainTextList,
|
||||
QString *errorString)
|
||||
BaseTextDocument::ReadResult BaseTextDocument::read(const FilePath &filePath,
|
||||
QStringList *plainTextList)
|
||||
{
|
||||
d->m_readResult = Utils::TextFileFormat::readFile(filePath,
|
||||
d->m_readResult = TextFileFormat::readFile(filePath,
|
||||
codec(),
|
||||
plainTextList,
|
||||
&d->m_format,
|
||||
errorString,
|
||||
&d->m_decodingErrorSample);
|
||||
return d->m_readResult;
|
||||
}
|
||||
@@ -152,15 +150,13 @@ BaseTextDocument::ReadResult BaseTextDocument::read(const Utils::FilePath &fileP
|
||||
Returns whether the operation was successful.
|
||||
*/
|
||||
|
||||
BaseTextDocument::ReadResult BaseTextDocument::read(const Utils::FilePath &filePath,
|
||||
QString *plainText,
|
||||
QString *errorString)
|
||||
BaseTextDocument::ReadResult BaseTextDocument::read(const FilePath &filePath,
|
||||
QString *plainText)
|
||||
{
|
||||
d->m_readResult = Utils::TextFileFormat::readFile(filePath,
|
||||
d->m_readResult = TextFileFormat::readFile(filePath,
|
||||
codec(),
|
||||
plainText,
|
||||
&d->m_format,
|
||||
errorString,
|
||||
&d->m_decodingErrorSample);
|
||||
return d->m_readResult;
|
||||
}
|
||||
|
@@ -31,8 +31,8 @@ public:
|
||||
bool supportsUtf8Bom() const;
|
||||
Utils::TextFileFormat::LineTerminationMode lineTerminationMode() const;
|
||||
|
||||
ReadResult read(const Utils::FilePath &filePath, QStringList *plainTextList, QString *errorString);
|
||||
ReadResult read(const Utils::FilePath &filePath, QString *plainText, QString *errorString);
|
||||
ReadResult read(const Utils::FilePath &filePath, QStringList *plainTextList);
|
||||
ReadResult read(const Utils::FilePath &filePath, QString *plainText);
|
||||
|
||||
bool hasDecodingError() const;
|
||||
QByteArray decodingErrorSample() const;
|
||||
|
@@ -101,25 +101,22 @@ void CppSearchResultFilter::setValue(bool &member, bool value)
|
||||
|
||||
namespace Internal {
|
||||
|
||||
|
||||
static QByteArray getSource(const Utils::FilePath &fileName,
|
||||
const WorkingCopy &workingCopy)
|
||||
static QByteArray getSource(const FilePath &fileName, const WorkingCopy &workingCopy)
|
||||
{
|
||||
if (const auto source = workingCopy.source(fileName)) {
|
||||
if (const auto source = workingCopy.source(fileName))
|
||||
return *source;
|
||||
} else {
|
||||
|
||||
QString fileContents;
|
||||
Utils::TextFileFormat format;
|
||||
TextFileFormat format;
|
||||
QString error;
|
||||
QTextCodec *defaultCodec = EditorManager::defaultTextCodec();
|
||||
Utils::TextFileFormat::ReadResult result = Utils::TextFileFormat::readFile(
|
||||
fileName, defaultCodec, &fileContents, &format, &error);
|
||||
if (result != Utils::TextFileFormat::ReadSuccess)
|
||||
qWarning() << "Could not read " << fileName << ". Error: " << error;
|
||||
TextFileFormat::ReadResult result = TextFileFormat::readFile(
|
||||
fileName, defaultCodec, &fileContents, &format);
|
||||
if (result.code != TextFileFormat::ReadSuccess)
|
||||
qWarning() << "Could not read " << fileName << ". Error: " << result.error;
|
||||
|
||||
return fileContents.toUtf8();
|
||||
}
|
||||
}
|
||||
|
||||
static QByteArray typeId(CPlusPlus::Symbol *symbol)
|
||||
{
|
||||
|
@@ -192,14 +192,11 @@ bool CppSourceProcessor::getFileContents(const FilePath &absoluteFilePath,
|
||||
|
||||
// Get from file
|
||||
*revision = 0;
|
||||
QString error;
|
||||
if (Utils::TextFileFormat::readFileUTF8(absoluteFilePath,
|
||||
m_defaultCodec,
|
||||
contents,
|
||||
&error)
|
||||
!= Utils::TextFileFormat::ReadSuccess) {
|
||||
const TextFileFormat::ReadResult result =
|
||||
TextFileFormat::readFileUTF8(absoluteFilePath, m_defaultCodec, contents);
|
||||
if (result.code != TextFileFormat::ReadSuccess) {
|
||||
qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath.toUrlishString()),
|
||||
qPrintable(error));
|
||||
qPrintable(result.error));
|
||||
return false;
|
||||
}
|
||||
contents->replace("\r\n", "\n");
|
||||
|
@@ -55,20 +55,18 @@ Result<> FormWindowFile::open(const FilePath &filePath, const FilePath &realFile
|
||||
return ResultError("File name is empty"); // FIXME: Use something better
|
||||
|
||||
QString contents;
|
||||
QString errorString;
|
||||
TextFileFormat::ReadResult readResult = read(filePath.absoluteFilePath(),
|
||||
&contents,
|
||||
&errorString);
|
||||
if (readResult == TextFileFormat::ReadEncodingError)
|
||||
return ResultError(errorString);
|
||||
if (readResult != TextFileFormat::ReadSuccess)
|
||||
return ResultError(errorString);
|
||||
TextFileFormat::ReadResult readResult = read(filePath.absoluteFilePath(), &contents);
|
||||
if (readResult.code == TextFileFormat::ReadEncodingError)
|
||||
return ResultError(readResult.error);
|
||||
if (readResult.code != TextFileFormat::ReadSuccess)
|
||||
return ResultError(readResult.error);
|
||||
|
||||
form->setFileName(filePath.absoluteFilePath().toUrlishString());
|
||||
const QByteArray contentsBA = contents.toUtf8();
|
||||
QBuffer str;
|
||||
str.setData(contentsBA);
|
||||
str.open(QIODevice::ReadOnly);
|
||||
QString errorString;
|
||||
if (!form->setContents(&str, &errorString))
|
||||
return ResultError(errorString);
|
||||
form->setDirty(filePath != realFilePath);
|
||||
|
@@ -270,17 +270,16 @@ Result<> DiffEditorDocument::open(const FilePath &filePath, const FilePath &real
|
||||
QTC_CHECK(filePath == realFilePath); // does not support autosave
|
||||
beginReload();
|
||||
QString patch;
|
||||
QString errorString;
|
||||
ReadResult readResult = read(filePath, &patch, &errorString);
|
||||
if (readResult == TextFileFormat::ReadIOError
|
||||
|| readResult == TextFileFormat::ReadMemoryAllocationError) {
|
||||
return ResultError(errorString);
|
||||
ReadResult readResult = read(filePath, &patch);
|
||||
if (readResult.code == TextFileFormat::ReadIOError
|
||||
|| readResult.code == TextFileFormat::ReadMemoryAllocationError) {
|
||||
return ResultError(readResult.error);
|
||||
}
|
||||
|
||||
const std::optional<QList<FileData>> fileDataList = DiffUtils::readPatch(patch);
|
||||
bool ok = fileDataList.has_value();
|
||||
if (!ok) {
|
||||
errorString = Tr::tr("Could not parse patch file \"%1\". "
|
||||
readResult.error = Tr::tr("Could not parse patch file \"%1\". "
|
||||
"The content is not of unified diff format.")
|
||||
.arg(filePath.toUserOutput());
|
||||
} else {
|
||||
@@ -291,10 +290,10 @@ Result<> DiffEditorDocument::open(const FilePath &filePath, const FilePath &real
|
||||
setDiffFiles(*fileDataList);
|
||||
}
|
||||
endReload(ok);
|
||||
if (!ok && readResult == TextFileFormat::ReadEncodingError)
|
||||
if (!ok && readResult.code == TextFileFormat::ReadEncodingError)
|
||||
ok = selectEncoding();
|
||||
if (!ok)
|
||||
return ResultError(errorString);
|
||||
return ResultError(readResult.error);
|
||||
return ResultOk;
|
||||
}
|
||||
|
||||
|
@@ -176,12 +176,11 @@ QList<ReloadInput> DiffCurrentFileController::reloadInputList() const
|
||||
DocumentModel::documentForFilePath(FilePath::fromString(m_fileName)));
|
||||
|
||||
if (textDocument && textDocument->isModified()) {
|
||||
QString errorString;
|
||||
TextFileFormat format = textDocument->format();
|
||||
|
||||
QString leftText;
|
||||
const TextFileFormat::ReadResult leftResult = TextFileFormat::readFile(
|
||||
FilePath::fromString(m_fileName), format.codec(), &leftText, &format, &errorString);
|
||||
FilePath::fromString(m_fileName), format.codec(), &leftText, &format);
|
||||
|
||||
const QString rightText = textDocument->plainText();
|
||||
|
||||
@@ -190,9 +189,9 @@ QList<ReloadInput> DiffCurrentFileController::reloadInputList() const
|
||||
reloadInput.fileInfo = {DiffFileInfo(m_fileName, Tr::tr("Saved")),
|
||||
DiffFileInfo(m_fileName, Tr::tr("Modified"))};
|
||||
reloadInput.fileInfo[RightSide].patchBehaviour = DiffFileInfo::PatchEditor;
|
||||
reloadInput.binaryFiles = (leftResult == TextFileFormat::ReadEncodingError);
|
||||
reloadInput.binaryFiles = (leftResult.code == TextFileFormat::ReadEncodingError);
|
||||
|
||||
if (leftResult == TextFileFormat::ReadIOError)
|
||||
if (leftResult.code == TextFileFormat::ReadIOError)
|
||||
reloadInput.fileOperation = FileData::NewFile;
|
||||
|
||||
result << reloadInput;
|
||||
@@ -229,7 +228,7 @@ QList<ReloadInput> DiffOpenFilesController::reloadInputList() const
|
||||
QString leftText;
|
||||
const QString fileName = textDocument->filePath().toUrlishString();
|
||||
const TextFileFormat::ReadResult leftResult = TextFileFormat::readFile(
|
||||
FilePath::fromString(fileName), format.codec(), &leftText, &format, &errorString);
|
||||
FilePath::fromString(fileName), format.codec(), &leftText, &format);
|
||||
|
||||
const QString rightText = textDocument->plainText();
|
||||
|
||||
@@ -238,9 +237,9 @@ QList<ReloadInput> DiffOpenFilesController::reloadInputList() const
|
||||
reloadInput.fileInfo = {DiffFileInfo(fileName, Tr::tr("Saved")),
|
||||
DiffFileInfo(fileName, Tr::tr("Modified"))};
|
||||
reloadInput.fileInfo[RightSide].patchBehaviour = DiffFileInfo::PatchEditor;
|
||||
reloadInput.binaryFiles = (leftResult == TextFileFormat::ReadEncodingError);
|
||||
reloadInput.binaryFiles = (leftResult.code == TextFileFormat::ReadEncodingError);
|
||||
|
||||
if (leftResult == TextFileFormat::ReadIOError)
|
||||
if (leftResult.code == TextFileFormat::ReadIOError)
|
||||
reloadInput.fileOperation = FileData::NewFile;
|
||||
|
||||
result << reloadInput;
|
||||
@@ -281,7 +280,7 @@ QList<ReloadInput> DiffModifiedFilesController::reloadInputList() const
|
||||
QString leftText;
|
||||
const QString fileName = textDocument->filePath().toUrlishString();
|
||||
const TextFileFormat::ReadResult leftResult = TextFileFormat::readFile(
|
||||
FilePath::fromString(fileName), format.codec(), &leftText, &format, &errorString);
|
||||
FilePath::fromString(fileName), format.codec(), &leftText, &format);
|
||||
|
||||
const QString rightText = textDocument->plainText();
|
||||
|
||||
@@ -290,9 +289,9 @@ QList<ReloadInput> DiffModifiedFilesController::reloadInputList() const
|
||||
reloadInput.fileInfo = {DiffFileInfo(fileName, Tr::tr("Saved")),
|
||||
DiffFileInfo(fileName, Tr::tr("Modified"))};
|
||||
reloadInput.fileInfo[RightSide].patchBehaviour = DiffFileInfo::PatchEditor;
|
||||
reloadInput.binaryFiles = (leftResult == TextFileFormat::ReadEncodingError);
|
||||
reloadInput.binaryFiles = (leftResult.code == TextFileFormat::ReadEncodingError);
|
||||
|
||||
if (leftResult == TextFileFormat::ReadIOError)
|
||||
if (leftResult.code == TextFileFormat::ReadIOError)
|
||||
reloadInput.fileOperation = FileData::NewFile;
|
||||
|
||||
result << reloadInput;
|
||||
@@ -332,19 +331,19 @@ QList<ReloadInput> DiffExternalFilesController::reloadInputList() const
|
||||
QString rightText;
|
||||
|
||||
const TextFileFormat::ReadResult leftResult = TextFileFormat::readFile(
|
||||
m_leftFilePath, format.codec(), &leftText, &format, &errorString);
|
||||
m_leftFilePath, format.codec(), &leftText, &format);
|
||||
const TextFileFormat::ReadResult rightResult = TextFileFormat::readFile(
|
||||
m_rightFilePath, format.codec(), &rightText, &format, &errorString);
|
||||
m_rightFilePath, format.codec(), &rightText, &format);
|
||||
|
||||
ReloadInput reloadInput;
|
||||
reloadInput.text = {leftText, rightText};
|
||||
reloadInput.fileInfo[LeftSide].fileName = m_leftFilePath.toUrlishString();
|
||||
reloadInput.fileInfo[RightSide].fileName = m_rightFilePath.toUrlishString();
|
||||
reloadInput.binaryFiles = (leftResult == TextFileFormat::ReadEncodingError
|
||||
|| rightResult == TextFileFormat::ReadEncodingError);
|
||||
reloadInput.binaryFiles = leftResult.code == TextFileFormat::ReadEncodingError
|
||||
|| rightResult.code == TextFileFormat::ReadEncodingError;
|
||||
|
||||
const bool leftFileExists = (leftResult != TextFileFormat::ReadIOError);
|
||||
const bool rightFileExists = (rightResult != TextFileFormat::ReadIOError);
|
||||
const bool leftFileExists = leftResult.code != TextFileFormat::ReadIOError;
|
||||
const bool rightFileExists = rightResult.code != TextFileFormat::ReadIOError;
|
||||
if (!leftFileExists && rightFileExists)
|
||||
reloadInput.fileOperation = FileData::NewFile;
|
||||
else if (leftFileExists && !rightFileExists)
|
||||
|
@@ -3865,10 +3865,7 @@ IEditor *GitClient::openShowEditor(const FilePath &workingDirectory, const QStri
|
||||
if (content.isEmpty())
|
||||
return nullptr;
|
||||
QByteArray fileContent;
|
||||
if (TextFileFormat::readFileUTF8(path,
|
||||
nullptr,
|
||||
&fileContent,
|
||||
nullptr)
|
||||
if (TextFileFormat::readFileUTF8(path, nullptr, &fileContent).code
|
||||
== TextFileFormat::ReadSuccess) {
|
||||
if (fileContent == content)
|
||||
return nullptr; // open the file for read/write
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#include <QLabel>
|
||||
|
||||
using namespace LanguageServerProtocol;
|
||||
using namespace Utils;
|
||||
|
||||
namespace LanguageClient {
|
||||
|
||||
@@ -277,20 +278,20 @@ bool operator==(const ItemData &id1, const ItemData &id2)
|
||||
return id1.range == id2.range && id1.userData == id2.userData;
|
||||
}
|
||||
|
||||
QStringList SymbolSupport::getFileContents(const Utils::FilePath &filePath)
|
||||
QStringList SymbolSupport::getFileContents(const FilePath &filePath)
|
||||
{
|
||||
QString fileContent;
|
||||
if (TextEditor::TextDocument *document = TextEditor::TextDocument::textDocumentForFilePath(
|
||||
filePath)) {
|
||||
fileContent = document->plainText();
|
||||
} else {
|
||||
Utils::TextFileFormat format;
|
||||
format.lineTerminationMode = Utils::TextFileFormat::LFLineTerminator;
|
||||
QString error;
|
||||
TextFileFormat format;
|
||||
format.lineTerminationMode = TextFileFormat::LFLineTerminator;
|
||||
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
||||
if (Utils::TextFileFormat::readFile(filePath, codec, &fileContent, &format, &error)
|
||||
!= Utils::TextFileFormat::ReadSuccess) {
|
||||
qDebug() << "Failed to read file" << filePath << ":" << error;
|
||||
const TextFileFormat::ReadResult result =
|
||||
TextFileFormat::readFile(filePath, codec, &fileContent, &format);
|
||||
if (result.code != TextFileFormat::ReadSuccess) {
|
||||
qDebug() << "Failed to read file" << filePath << ":" << result.error;
|
||||
}
|
||||
}
|
||||
return fileContent.split("\n");
|
||||
|
@@ -236,10 +236,9 @@ bool PythonBuildSystem::save()
|
||||
if (filePath.fileName() == "pyproject.toml") {
|
||||
Core::BaseTextDocument projectFile;
|
||||
QString pyProjectTomlContent;
|
||||
QString readingError;
|
||||
auto result = projectFile.read(filePath, &pyProjectTomlContent, &readingError);
|
||||
if (result != TextFileFormat::ReadSuccess) {
|
||||
MessageManager::writeDisrupting(readingError);
|
||||
const BaseTextDocument::ReadResult result = projectFile.read(filePath, &pyProjectTomlContent);
|
||||
if (result.code != TextFileFormat::ReadSuccess) {
|
||||
MessageManager::writeDisrupting(result.error);
|
||||
return false;
|
||||
}
|
||||
auto newPyProjectToml = updatePyProjectTomlContent(pyProjectTomlContent, projectFiles);
|
||||
|
@@ -12,6 +12,8 @@
|
||||
|
||||
#include <utils/filepath.h>
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
namespace Python::Internal {
|
||||
|
||||
/*
|
||||
@@ -19,15 +21,14 @@ namespace Python::Internal {
|
||||
\param relativeFilePath The relative path to the file from the testfiles folder
|
||||
\returns The contents of the file
|
||||
*/
|
||||
static Utils::Result<QString> readTestFile(const QString &relativeFilePath)
|
||||
static Result<QString> readTestFile(const QString &relativeFilePath)
|
||||
{
|
||||
const auto filePath = Utils::FilePath::fromUserInput(":/unittests/Python/" + relativeFilePath);
|
||||
const auto filePath = FilePath::fromUserInput(":/unittests/Python/" + relativeFilePath);
|
||||
Core::BaseTextDocument projectFile;
|
||||
QString fileContent;
|
||||
QString readingError;
|
||||
auto result = projectFile.read(filePath, &fileContent, &readingError);
|
||||
if (result != Utils::TextFileFormat::ReadSuccess)
|
||||
return Utils::make_unexpected(readingError);
|
||||
const Core::BaseTextDocument::ReadResult result = projectFile.read(filePath, &fileContent);
|
||||
if (result.code != TextFileFormat::ReadSuccess)
|
||||
return ResultError(result.error);
|
||||
|
||||
return fileContent;
|
||||
}
|
||||
|
@@ -743,14 +743,13 @@ QPair<ProFile *, QStringList> QmakePriFile::readProFile()
|
||||
{
|
||||
QString contents;
|
||||
{
|
||||
QString errorMsg;
|
||||
if (TextFileFormat::readFile(filePath(),
|
||||
const TextFileFormat::ReadResult result =
|
||||
TextFileFormat::readFile(filePath(),
|
||||
Core::EditorManager::defaultTextCodec(),
|
||||
&contents,
|
||||
&m_textFormat,
|
||||
&errorMsg)
|
||||
!= TextFileFormat::ReadSuccess) {
|
||||
QmakeBuildSystem::proFileParseError(errorMsg, filePath());
|
||||
&m_textFormat);
|
||||
if (result.code != TextFileFormat::ReadSuccess) {
|
||||
QmakeBuildSystem::proFileParseError(result.error, filePath());
|
||||
return {includeFile, lines};
|
||||
}
|
||||
lines = contents.split('\n');
|
||||
|
@@ -322,17 +322,14 @@ bool QmlBuildSystem::setFileSettingInProjectFile(const QString &setting,
|
||||
}
|
||||
|
||||
QString fileContent;
|
||||
QString error;
|
||||
Utils::TextFileFormat textFileFormat;
|
||||
TextFileFormat textFileFormat;
|
||||
const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8
|
||||
Utils::TextFileFormat::ReadResult readResult = Utils::TextFileFormat::readFile(qmlProjectFilePath,
|
||||
TextFileFormat::ReadResult readResult = TextFileFormat::readFile(qmlProjectFilePath,
|
||||
codec,
|
||||
&fileContent,
|
||||
&textFileFormat,
|
||||
&error);
|
||||
if (readResult != Utils::TextFileFormat::ReadSuccess) {
|
||||
qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << error;
|
||||
}
|
||||
&textFileFormat);
|
||||
if (readResult.code != TextFileFormat::ReadSuccess)
|
||||
qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << readResult.error;
|
||||
|
||||
const QString settingQmlCode = setting + ":";
|
||||
|
||||
@@ -489,13 +486,12 @@ bool QmlBuildSystem::setMainUiFileInMainFile(const Utils::FilePath &newMainUiFil
|
||||
}
|
||||
|
||||
QString fileContent;
|
||||
QString error;
|
||||
Utils::TextFileFormat textFileFormat;
|
||||
TextFileFormat textFileFormat;
|
||||
const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8
|
||||
if (Utils::TextFileFormat::readFile(mainFilePath(), codec, &fileContent, &textFileFormat, &error)
|
||||
!= Utils::TextFileFormat::ReadSuccess) {
|
||||
qWarning() << "Failed to read file" << mainFilePath() << ":" << error;
|
||||
}
|
||||
const TextFileFormat::ReadResult res =
|
||||
TextFileFormat::readFile(mainFilePath(), codec, &fileContent, &textFileFormat);
|
||||
if (res.code != TextFileFormat::ReadSuccess)
|
||||
qWarning() << "Failed to read file" << mainFilePath() << ":" << res.error;
|
||||
|
||||
const QString currentMain = QString("%1 {").arg(mainUiFilePath().baseName());
|
||||
const QString newMain = QString("%1 {").arg(newMainUiFilePath.baseName());
|
||||
|
@@ -111,15 +111,13 @@ QTextDocument *RefactoringFile::mutableDocument() const
|
||||
if (!m_document) {
|
||||
QString fileContents;
|
||||
if (!m_filePath.isEmpty()) {
|
||||
QString error;
|
||||
QTextCodec *defaultCodec = EditorManager::defaultTextCodec();
|
||||
TextFileFormat::ReadResult result = TextFileFormat::readFile(m_filePath,
|
||||
defaultCodec,
|
||||
&fileContents,
|
||||
&m_textFileFormat,
|
||||
&error);
|
||||
if (result != TextFileFormat::ReadSuccess) {
|
||||
qWarning() << "Could not read " << m_filePath << ". Error: " << error;
|
||||
&m_textFileFormat);
|
||||
if (result.code != TextFileFormat::ReadSuccess) {
|
||||
qWarning() << "Could not read " << m_filePath << ". Error: " << result.error;
|
||||
m_textFileFormat.setCodec(nullptr);
|
||||
}
|
||||
}
|
||||
|
@@ -771,12 +771,11 @@ Result<> TextDocument::openImpl(const FilePath &filePath,
|
||||
bool reload)
|
||||
{
|
||||
QStringList content;
|
||||
QString errorString;
|
||||
|
||||
ReadResult readResult = TextFileFormat::ReadIOError;
|
||||
|
||||
if (!filePath.isEmpty()) {
|
||||
readResult = read(realFilePath, &content, &errorString);
|
||||
readResult = read(realFilePath, &content);
|
||||
const int chunks = content.size();
|
||||
|
||||
// Don't call setUndoRedoEnabled(true) when reload is true and filenames are different,
|
||||
@@ -826,8 +825,8 @@ Result<> TextDocument::openImpl(const FilePath &filePath,
|
||||
d->m_document.setModified(filePath != realFilePath);
|
||||
setFilePath(filePath);
|
||||
}
|
||||
if (readResult == TextFileFormat::ReadIOError)
|
||||
return ResultError(errorString);
|
||||
if (readResult.code == TextFileFormat::ReadIOError)
|
||||
return ResultError(readResult.error);
|
||||
return ResultOk;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user