- fix: Crash when find occurrences in a project that has missing files.

rename FileEndType to NewlineType
This commit is contained in:
Roy Qu 2023-01-04 11:01:34 +08:00
parent 4f2fb8f540
commit 89ee9d8914
13 changed files with 472 additions and 440 deletions

View File

@ -12,6 +12,8 @@ Red Panda C++ Version 2.8
- fix: Error When save project units' encoding settings. - fix: Error When save project units' encoding settings.
- enhancement: Waiting for syntax parsers to finish before saving files, to prevent data lost caused by syntax parsering crash. - enhancement: Waiting for syntax parsers to finish before saving files, to prevent data lost caused by syntax parsering crash.
- fix: Restore main window and cpu info window will set wrong font in the cpu info. - fix: Restore main window and cpu info window will set wrong font in the cpu info.
- enhancement: Let encoding options in the statusbar more explicit.
- fix: Crash when find occurrences in a project that has missing files.
Red Panda C++ Version 2.7 Red Panda C++ Version 2.7

View File

@ -50,12 +50,16 @@ bool CppRefacter::findOccurence(Editor *editor, const QSynedit::BufferCoord &pos
if (!statement) if (!statement)
return false; return false;
if (statement->scope == StatementScope::Local) {
doFindOccurenceInEditor(statement,editor,editor->parser());
} else {
std::shared_ptr<Project> project = pMainWindow->project(); std::shared_ptr<Project> project = pMainWindow->project();
if (editor->inProject() && project) { if (editor->inProject() && project) {
doFindOccurenceInProject(statement,project,editor->parser()); doFindOccurenceInProject(statement,project,editor->parser());
} else { } else {
doFindOccurenceInEditor(statement,editor,editor->parser()); doFindOccurenceInEditor(statement,editor,editor->parser());
} }
}
pMainWindow->searchResultModel()->notifySearchResultsUpdated(); pMainWindow->searchResultModel()->notifySearchResultsUpdated();
return true; return true;
} }
@ -88,10 +92,10 @@ bool CppRefacter::findOccurence(const QString &statementFullname, SearchFileScop
if (!statement) if (!statement)
return false; return false;
if (scope == SearchFileScope::wholeProject) { if (statement->scope == StatementScope::Local || scope == SearchFileScope::currentFile) {
doFindOccurenceInProject(statement,project,parser);
} else if (scope == SearchFileScope::currentFile) {
doFindOccurenceInEditor(statement, editor,parser); doFindOccurenceInEditor(statement, editor,parser);
} else if (scope == SearchFileScope::wholeProject) {
doFindOccurenceInProject(statement,project,parser);
} }
pMainWindow->searchResultModel()->notifySearchResultsUpdated(); pMainWindow->searchResultModel()->notifySearchResultsUpdated();
return true; return true;
@ -163,6 +167,7 @@ void CppRefacter::doFindOccurenceInEditor(PStatement statement , Editor *editor,
); );
PSearchResultTreeItem item = findOccurenceInFile( PSearchResultTreeItem item = findOccurenceInFile(
editor->filename(), editor->filename(),
editor->encodingOption(),
statement, statement,
parser); parser);
if (item && !(item->results.isEmpty())) { if (item && !(item->results.isEmpty())) {
@ -181,6 +186,7 @@ void CppRefacter::doFindOccurenceInProject(PStatement statement, std::shared_ptr
if (isCFile(unit->fileName()) || isHFile(unit->fileName())) { if (isCFile(unit->fileName()) || isHFile(unit->fileName())) {
PSearchResultTreeItem item = findOccurenceInFile( PSearchResultTreeItem item = findOccurenceInFile(
unit->fileName(), unit->fileName(),
unit->encoding(),
statement, statement,
parser); parser);
if (item && !(item->results.isEmpty())) { if (item && !(item->results.isEmpty())) {
@ -192,6 +198,7 @@ void CppRefacter::doFindOccurenceInProject(PStatement statement, std::shared_ptr
PSearchResultTreeItem CppRefacter::findOccurenceInFile( PSearchResultTreeItem CppRefacter::findOccurenceInFile(
const QString &filename, const QString &filename,
const QByteArray& fileEncoding,
const PStatement &statement, const PStatement &statement,
const PCppParser& parser) const PCppParser& parser)
{ {
@ -203,9 +210,16 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
if (pMainWindow->editorList()->getContentFromOpenedEditor( if (pMainWindow->editorList()->getContentFromOpenedEditor(
filename,buffer)){ filename,buffer)){
editor.document()->setContents(buffer); editor.document()->setContents(buffer);
} else if (!fileExists(filename)){
return parentItem;
} else { } else {
QByteArray encoding; QByteArray encoding;
editor.document()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding); try {
editor.document()->loadFromFile(filename,fileEncoding,encoding);
} catch (FileError e) {
//don't handle it;
return parentItem;
}
} }
editor.setSyntaxer(syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP)); editor.setSyntaxer(syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP));
int posY = 0; int posY = 0;

View File

@ -42,6 +42,7 @@ private:
void doFindOccurenceInProject(PStatement statement, std::shared_ptr<Project> project, const PCppParser& parser); void doFindOccurenceInProject(PStatement statement, std::shared_ptr<Project> project, const PCppParser& parser);
PSearchResultTreeItem findOccurenceInFile( PSearchResultTreeItem findOccurenceInFile(
const QString& filename, const QString& filename,
const QByteArray& fileEncoding,
const PStatement& statement, const PStatement& statement,
const PCppParser& parser); const PCppParser& parser);
void renameSymbolInFile( void renameSymbolInFile(

View File

@ -137,9 +137,9 @@ MainWindow::MainWindow(QWidget *parent)
mFileEncodingStatus = new LabelWithMenu(); mFileEncodingStatus = new LabelWithMenu();
mFileModeStatus = new QLabel(); mFileModeStatus = new QLabel();
mFileInfoStatus->setStyleSheet("margin-left:10px; margin-right:10px"); mFileInfoStatus->setStyleSheet("margin-left:5px; margin-right:5px");
mFileEncodingStatus->setStyleSheet("margin-left:10px; margin-right:10px"); mFileEncodingStatus->setStyleSheet("margin-left:5px; margin-right:5px");
mFileModeStatus->setStyleSheet("margin-left:10px; margin-right:10px"); mFileModeStatus->setStyleSheet("margin-left:5px; margin-right:5px");
prepareTabInfosData(); prepareTabInfosData();
prepareTabMessagesData(); prepareTabMessagesData();
ui->statusbar->insertPermanentWidget(0,mFileModeStatus); ui->statusbar->insertPermanentWidget(0,mFileModeStatus);
@ -459,12 +459,12 @@ void MainWindow::updateForEncodingInfo(const Editor* editor, bool clear) {
if (!clear && editor!=nullptr) { if (!clear && editor!=nullptr) {
if (editor->encodingOption() != editor->fileEncoding()) { if (editor->encodingOption() != editor->fileEncoding()) {
mFileEncodingStatus->setText( mFileEncodingStatus->setText(
QString("%1(%2)") QString(" %1(%2) ")
.arg(QString(editor->encodingOption()) .arg(QString(editor->encodingOption())
,QString(editor->fileEncoding()))); ,QString(editor->fileEncoding())));
} else { } else {
mFileEncodingStatus->setText( mFileEncodingStatus->setText(
QString("%1") QString(" %1 ")
.arg(QString(editor->encodingOption())) .arg(QString(editor->encodingOption()))
); );
} }
@ -1323,12 +1323,10 @@ void MainWindow::updateStatusbarForLineCol(const Editor* e, bool clear)
{ {
if (!clear && e!=nullptr) { if (!clear && e!=nullptr) {
int col = e->charToColumn(e->caretY(),e->caretX()); int col = e->charToColumn(e->caretY(),e->caretX());
QString msg = tr("Line:%1 Col:%2 Selected:%3 Lines:%4 Length:%5") QString msg = tr("Line: %1 Col: %2 Lines: %3")
.arg(e->caretY()) .arg(e->caretY())
.arg(col) .arg(col)
.arg(e->selText().length()) .arg(e->document()->count());
.arg(e->document()->count())
.arg(e->document()->getTextLength());
mFileInfoStatus->setText(msg); mFileInfoStatus->setText(msg);
} else { } else {
mFileInfoStatus->setText(""); mFileInfoStatus->setText("");
@ -3350,6 +3348,12 @@ void MainWindow::buildEncodingMenu()
ui->actionEncode_in_UTF_8_BOM->setCheckable(true); ui->actionEncode_in_UTF_8_BOM->setCheckable(true);
} }
void MainWindow::buildNewlineMenu()
{
mMenuNewline = new QMenu(this);
mMenuNewline->setTitle(tr("Newline"));
}
void MainWindow::maximizeEditor() void MainWindow::maximizeEditor()
{ {
if (ui->tabExplorer->isShrinked() && ui->tabExplorer->isShrinked()) { if (ui->tabExplorer->isShrinked() && ui->tabExplorer->isShrinked()) {
@ -9252,6 +9256,7 @@ void MainWindow::on_actionToggle_Readonly_triggered()
editor->setReadOnly(!editor->readOnly()); editor->setReadOnly(!editor->readOnly());
editor->updateCaption(); editor->updateCaption();
updateEditorActions(editor); updateEditorActions(editor);
updateForStatusbarModeInfo(editor);
} }
} }

View File

@ -275,6 +275,7 @@ private:
void initToolButtons(); void initToolButtons();
void buildContextMenus(); void buildContextMenus();
void buildEncodingMenu(); void buildEncodingMenu();
void buildNewlineMenu();
void maximizeEditor(); void maximizeEditor();
QStringList getBinDirsForCurrentEditor(); QStringList getBinDirsForCurrentEditor();
QStringList getDefaultCompilerSetBinDirs(); QStringList getDefaultCompilerSetBinDirs();
@ -778,6 +779,7 @@ private:
LabelWithMenu *mFileEncodingStatus; LabelWithMenu *mFileEncodingStatus;
QLabel *mFileModeStatus; QLabel *mFileModeStatus;
QMenu *mMenuEncoding; QMenu *mMenuEncoding;
QMenu *mMenuNewline;
QMenu *mMenuExport; QMenu *mMenuExport;
QMenu *mMenuEncodingList; QMenu *mMenuEncodingList;
QMenu *mMenuRecentFiles; QMenu *mMenuRecentFiles;

View File

@ -4046,7 +4046,7 @@
</message> </message>
<message> <message>
<source>Line:%1 Col:%2 Selected:%3 Lines:%4 Length:%5</source> <source>Line:%1 Col:%2 Selected:%3 Lines:%4 Length:%5</source>
<translation>Linha:%1 Coluna:%2 Seleção:%3 Linhas:%4 Tamanho:%5</translation> <translation type="vanished">Linha:%1 Coluna:%2 Seleção:%3 Linhas:%4 Tamanho:%5</translation>
</message> </message>
<message> <message>
<source>Read Only</source> <source>Read Only</source>
@ -4924,6 +4924,10 @@
<source>Toggle Readonly</source> <source>Toggle Readonly</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Line: %1 Col: %2 Lines: %3</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>NewClassDialog</name> <name>NewClassDialog</name>

File diff suppressed because it is too large Load Diff

View File

@ -3893,10 +3893,6 @@
<source>Clear History</source> <source>Clear History</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Line:%1 Col:%2 Selected:%3 Lines:%4 Length:%5</source>
<translation type="unfinished"></translation>
</message>
<message> <message>
<source>Read Only</source> <source>Read Only</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
@ -4741,6 +4737,10 @@
<source>Toggle Readonly</source> <source>Toggle Readonly</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Line: %1 Col: %2 Lines: %3</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>NewClassDialog</name> <name>NewClassDialog</name>

View File

@ -43,7 +43,7 @@ Document::Document(const QFont& font, const QFont& nonAsciiFont, QObject *parent
{ {
mAppendNewLineAtEOF = true; mAppendNewLineAtEOF = true;
mFileEndingType = FileEndingType::Windows; mNewlineType = NewlineType::Windows;
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
mUpdateCount = 0; mUpdateCount = 0;
mCharWidth = mFontMetrics.horizontalAdvance("M"); mCharWidth = mFontMetrics.horizontalAdvance("M");
@ -147,12 +147,12 @@ int Document::lengthOfLongestLine() {
QString Document::lineBreak() const QString Document::lineBreak() const
{ {
switch(mFileEndingType) { switch(mNewlineType) {
case FileEndingType::Linux: case NewlineType::Unix:
return "\n"; return "\n";
case FileEndingType::Windows: case NewlineType::Windows:
return "\r\n"; return "\r\n";
case FileEndingType::Mac: case NewlineType::MacOld:
return "\r"; return "\r";
} }
return "\n"; return "\n";
@ -321,7 +321,7 @@ int Document::getTextLength()
int Result = 0; int Result = 0;
foreach (const PDocumentLine& line, mLines ) { foreach (const PDocumentLine& line, mLines ) {
Result += line->fString.length(); Result += line->fString.length();
if (mFileEndingType == FileEndingType::Windows) { if (mNewlineType == NewlineType::Windows) {
Result += 2; Result += 2;
} else { } else {
Result += 1; Result += 1;
@ -542,7 +542,7 @@ void Document::loadFromFile(const QString& filename, const QByteArray& encoding,
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
QFile file(filename); QFile file(filename);
if (!file.open(QFile::ReadOnly )) if (!file.open(QFile::ReadOnly))
throw FileError(tr("Can't open file '%1' for read!").arg(file.fileName())); throw FileError(tr("Can't open file '%1' for read!").arg(file.fileName()));
beginUpdate(); beginUpdate();
internalClear(); internalClear();
@ -573,11 +573,11 @@ void Document::loadFromFile(const QString& filename, const QByteArray& encoding,
codec = QTextCodec::codecForName(ENCODING_UTF8); codec = QTextCodec::codecForName(ENCODING_UTF8);
} }
if (line.endsWith("\r\n")) { if (line.endsWith("\r\n")) {
mFileEndingType = FileEndingType::Windows; mNewlineType = NewlineType::Windows;
} else if (line.endsWith("\n")) { } else if (line.endsWith("\n")) {
mFileEndingType = FileEndingType::Linux; mNewlineType = NewlineType::Unix;
} else if (line.endsWith("\r")) { } else if (line.endsWith("\r")) {
mFileEndingType = FileEndingType::Mac; mNewlineType = NewlineType::MacOld;
} }
internalClear(); internalClear();
while (true) { while (true) {
@ -780,16 +780,16 @@ void Document::internalClear()
} }
} }
FileEndingType Document::getFileEndingType() NewlineType Document::getNewlineType()
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
return mFileEndingType; return mNewlineType;
} }
void Document::setFileEndingType(const FileEndingType &fileEndingType) void Document::setNewlineType(const NewlineType &fileEndingType)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
mFileEndingType = fileEndingType; mNewlineType = fileEndingType;
} }
bool Document::empty() bool Document::empty()

View File

@ -98,8 +98,8 @@ public:
bool getAppendNewLineAtEOF(); bool getAppendNewLineAtEOF();
void setAppendNewLineAtEOF(bool appendNewLineAtEOF); void setAppendNewLineAtEOF(bool appendNewLineAtEOF);
FileEndingType getFileEndingType(); NewlineType getNewlineType();
void setFileEndingType(const FileEndingType &fileEndingType); void setNewlineType(const NewlineType &fileEndingType);
bool empty(); bool empty();
@ -143,7 +143,7 @@ private:
int mCharWidth; int mCharWidth;
//int mCount; //int mCount;
//int mCapacity; //int mCapacity;
FileEndingType mFileEndingType; NewlineType mNewlineType;
bool mAppendNewLineAtEOF; bool mAppendNewLineAtEOF;
int mIndexOfLongestLine; int mIndexOfLongestLine;
int mUpdateCount; int mUpdateCount;

View File

@ -32,7 +32,7 @@ SynExporter::SynExporter(const QByteArray charset):mCharset(charset)
mForegroundColor = QGuiApplication::palette().color(QPalette::Text); mForegroundColor = QGuiApplication::palette().color(QPalette::Text);
mUseBackground = false; mUseBackground = false;
mExportAsText = false; mExportAsText = false;
mFileEndingType = FileEndingType::Windows; mFileEndingType = NewlineType::Windows;
clear(); clear();
setTitle(""); setTitle("");
} }
@ -211,12 +211,12 @@ void SynExporter::setUseBackground(bool Value)
} }
} }
FileEndingType SynExporter::fileEndingType() const NewlineType SynExporter::fileEndingType() const
{ {
return mFileEndingType; return mFileEndingType;
} }
void SynExporter::setFileEndingType(const FileEndingType &fileEndingType) void SynExporter::setFileEndingType(const NewlineType &fileEndingType)
{ {
mFileEndingType = fileEndingType; mFileEndingType = fileEndingType;
} }
@ -389,11 +389,11 @@ void SynExporter::setOnFormatToken(const FormatTokenHandler &onFormatToken)
QString SynExporter::lineBreak() QString SynExporter::lineBreak()
{ {
switch(mFileEndingType) { switch(mFileEndingType) {
case FileEndingType::Linux: case NewlineType::Unix:
return "\n"; return "\n";
case FileEndingType::Windows: case NewlineType::Windows:
return "\r\n"; return "\r\n";
case FileEndingType::Mac: case NewlineType::MacOld:
return "\r"; return "\r";
} }
return "\n"; return "\n";

View File

@ -78,8 +78,8 @@ public:
bool useBackground() const; bool useBackground() const;
void setUseBackground(bool Value); void setUseBackground(bool Value);
FileEndingType fileEndingType() const; NewlineType fileEndingType() const;
void setFileEndingType(const FileEndingType &fileEndingType); void setFileEndingType(const NewlineType &fileEndingType);
/** /**
* @brief The clipboard format the exporter creates as native format. * @brief The clipboard format the exporter creates as native format.
@ -119,7 +119,7 @@ protected:
QMap<QChar,QString> mReplaceReserved; QMap<QChar,QString> mReplaceReserved;
QString mTitle; QString mTitle;
bool mUseBackground; bool mUseBackground;
FileEndingType mFileEndingType; NewlineType mFileEndingType;
QString lineBreak(); QString lineBreak();

View File

@ -36,7 +36,7 @@ class QTextCodec;
#define ENCODING_SYSTEM_DEFAULT "SYSTEM" #define ENCODING_SYSTEM_DEFAULT "SYSTEM"
#define ENCODING_ASCII "ASCII" #define ENCODING_ASCII "ASCII"
enum class FileEndingType { enum class NewlineType {
Windows, Windows,
Unix, Unix,
MacOld MacOld