- fix: save/load bookmark doesn't work

This commit is contained in:
Roy Qu 2022-10-23 00:39:24 +08:00
parent e8300abd65
commit be0c9ad4f5
9 changed files with 110 additions and 64 deletions

View File

@ -2,6 +2,7 @@ Red Panda C++ Version 2.0
- redesign the project parser, more efficient and correct - redesign the project parser, more efficient and correct
- enhancement: todo parser for project - enhancement: todo parser for project
- fix: save/load bookmark doesn't work
Red Panda C++ Version 1.5 Red Panda C++ Version 1.5

View File

@ -310,6 +310,12 @@ void Debugger::setIsForProject(bool newIsForProject)
} }
} }
void Debugger::clearForProject()
{
mBreakpointModel->clear(true);
mWatchModel->clear(true);
}
void Debugger::addBreakpoint(int line, const Editor* editor) void Debugger::addBreakpoint(int line, const Editor* editor)
{ {
addBreakpoint(line,editor->filename(), editor->inProject()); addBreakpoint(line,editor->filename(), editor->inProject());
@ -2197,6 +2203,16 @@ void WatchModel::clear()
endResetModel(); endResetModel();
} }
void WatchModel::clear(bool forProject)
{
if (mIsForProject == forProject)
beginResetModel();
QList<PWatchVar> &vars=(forProject?mProjectWatchVars:mWatchVars);
vars.clear();
if (mIsForProject == forProject)
endResetModel();
}
const QList<PWatchVar> &WatchModel::watchVars() const const QList<PWatchVar> &WatchModel::watchVars() const
{ {
return watchVars(mIsForProject); return watchVars(mIsForProject);

View File

@ -196,6 +196,7 @@ public:
void removeWatchVar(const QString& expression); void removeWatchVar(const QString& expression);
void removeWatchVar(const QModelIndex& index); void removeWatchVar(const QModelIndex& index);
void clear(); void clear();
void clear(bool forProject);
PWatchVar findWatchVar(const QModelIndex& index); PWatchVar findWatchVar(const QModelIndex& index);
PWatchVar findWatchVar(const QString& expr); PWatchVar findWatchVar(const QString& expr);
void resetAllVarInfos(); void resetAllVarInfos();
@ -307,6 +308,7 @@ public:
bool isForProject() const; bool isForProject() const;
void setIsForProject(bool newIsForProject); void setIsForProject(bool newIsForProject);
void clearForProject();
//breakpoints //breakpoints
void addBreakpoint(int line, const Editor* editor); void addBreakpoint(int line, const Editor* editor);

View File

@ -4474,8 +4474,11 @@ void MainWindow::closeProject(bool refreshEditor)
mClassBrowserModel.endUpdate(); mClassBrowserModel.endUpdate();
if (!mQuitting) { if (!mQuitting) {
mBookmarkModel->clear(true);
mBookmarkModel->setIsForProject(false); mBookmarkModel->setIsForProject(false);
mDebugger->clearForProject();
mDebugger->setIsForProject(false); mDebugger->setIsForProject(false);
mTodoModel.clear(true);
mTodoModel.setIsForProject(false); mTodoModel.setIsForProject(false);
// Clear error browser // Clear error browser
clearIssues(); clearIssues();

View File

@ -131,11 +131,10 @@ void TodoThread::doParseFile(const QString &filename, QSynedit::PHighlighter hig
attr = highlighter->getTokenAttribute(); attr = highlighter->getTokenAttribute();
if (attr == commentAttr) { if (attr == commentAttr) {
QString token = highlighter->getToken(); QString token = highlighter->getToken();
qDebug()<<token;
int pos = token.indexOf("TODO:",0,Qt::CaseInsensitive); int pos = token.indexOf("TODO:",0,Qt::CaseInsensitive);
if (pos>=0) { if (pos>=0) {
emit todoFound( emit todoFound(
mFilename, filename,
i+1, i+1,
pos+highlighter->getTokenPos(), pos+highlighter->getTokenPos(),
lines[i].trimmed() lines[i].trimmed()
@ -196,6 +195,16 @@ void TodoModel::clear()
endResetModel(); endResetModel();
} }
void TodoModel::clear(bool forProject)
{
if (mIsForProject == forProject)
beginResetModel();
QList<PTodoItem> &items=getItems(forProject);
items.clear();
if (mIsForProject == forProject)
endResetModel();
}
PTodoItem TodoModel::getItem(const QModelIndex &index) PTodoItem TodoModel::getItem(const QModelIndex &index)
{ {
if (!index.isValid()) if (!index.isValid())

View File

@ -41,6 +41,7 @@ public:
int ch, const QString& line); int ch, const QString& line);
void removeTodosForFile(const QString& filename); void removeTodosForFile(const QString& filename);
void clear(); void clear();
void clear(bool forProject);
PTodoItem getItem(const QModelIndex& index); PTodoItem getItem(const QModelIndex& index);
private: private:
QList<PTodoItem> &getItems(bool forProject); QList<PTodoItem> &getItems(bool forProject);

View File

@ -194,37 +194,31 @@ void BookmarkModel::save(const QString &filename, const QString& projectFolder)
{ {
bool forProject = !projectFolder.isEmpty(); bool forProject = !projectFolder.isEmpty();
qint64 t,fileTimestamp; qint64 t,fileTimestamp;
QList<PBookmark> bookmarks; QHash<QString,int> compareHash;
QList<PBookmark> &list=forProject?mProjectBookmarks:mBookmarks;
if (forProject) { if (forProject) {
t=mLastLoadProjectBookmarksTimestamp; t=mLastLoadProjectBookmarksTimestamp;
foreach (const PBookmark& bookmark, mProjectBookmarks) {
PBookmark newBookmark=std::make_shared<Bookmark>();
newBookmark->description = bookmark->description;
newBookmark->filename = extractRelativePath(projectFolder, bookmark->filename);
newBookmark->line = bookmark->line;
newBookmark->timestamp = bookmark->timestamp;
bookmarks.append(newBookmark);
}
} else { } else {
t=mLastLoadBookmarksTimestamp; t=mLastLoadBookmarksTimestamp;
bookmarks = mBookmarks; }
for (int i=0;i<list.count();i++) {
const PBookmark& bookmark=list[i];
QString filename = forProject?extractRelativePath(projectFolder, bookmark->filename):bookmark->filename;
QString key = QString("%1-%2").arg(filename).arg(bookmark->line);
compareHash.insert(key,i);
} }
QList<PBookmark> fileBookmarks=load(filename, t,&fileTimestamp); QList<PBookmark> fileBookmarks=load(filename, t,&fileTimestamp);
QFile file(filename); QFile file(filename);
if (file.open(QFile::WriteOnly | QFile::Truncate)) { if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QHash<QString,int> compareHash;
QList<PBookmark> saveBookmarks; QList<PBookmark> saveBookmarks;
for (int i=0; i<bookmarks.count(); i++) {
PBookmark& bookmark=bookmarks[i];
QString key = QString("%1-%2").arg(bookmark->filename).arg(bookmark->line);
compareHash.insert(key,i);;
}
QDir dir(projectFolder); QDir dir(projectFolder);
foreach (const PBookmark& bookmark, fileBookmarks) { foreach (const PBookmark& bookmark, fileBookmarks) {
QString key = QString("%1-%2").arg(bookmark->filename).arg(bookmark->line); QString key = QString("%1-%2").arg(bookmark->filename).arg(bookmark->line);
int idx = compareHash.value(key,-1); int idx = compareHash.value(key,-1);
if (idx<0) { if (idx<0) {
int count=forProject?mProjectBookmarks.count():mBookmarks.count(); int count=list.count();
compareHash.insert(key,count); compareHash.insert(key,count);
if (forProject == mIsForProject) { if (forProject == mIsForProject) {
beginInsertRows(QModelIndex(),count,count); beginInsertRows(QModelIndex(),count,count);
@ -232,12 +226,11 @@ void BookmarkModel::save(const QString &filename, const QString& projectFolder)
if (forProject) { if (forProject) {
bookmark->filename = dir.absoluteFilePath(bookmark->filename); bookmark->filename = dir.absoluteFilePath(bookmark->filename);
} }
QList<PBookmark> &list=forProject?mProjectBookmarks:mBookmarks;
list.append(bookmark); list.append(bookmark);
if (forProject == mIsForProject) if (forProject == mIsForProject)
endInsertRows(); endInsertRows();
} else { } else {
const QList<PBookmark> &list=forProject?mProjectBookmarks:mBookmarks;
PBookmark pTemp = list[idx]; PBookmark pTemp = list[idx];
if (pTemp->timestamp<=bookmark->timestamp) { if (pTemp->timestamp<=bookmark->timestamp) {
bookmark->description = pTemp->timestamp; bookmark->description = pTemp->timestamp;
@ -258,10 +251,10 @@ void BookmarkModel::save(const QString &filename, const QString& projectFolder)
QJsonObject rootObj; QJsonObject rootObj;
rootObj["timestamp"]=QString("%1").arg(saveTime); rootObj["timestamp"]=QString("%1").arg(saveTime);
QJsonArray array; QJsonArray array;
const QList<PBookmark> &list=forProject?mProjectBookmarks:mBookmarks;
foreach (const PBookmark& bookmark, list) { foreach (const PBookmark& bookmark, list) {
QJsonObject obj; QJsonObject obj;
obj["filename"]=bookmark->filename; QString filename = forProject?extractRelativePath(projectFolder, bookmark->filename):bookmark->filename;
obj["filename"]=filename;
obj["line"]=bookmark->line; obj["line"]=bookmark->line;
obj["description"]=bookmark->description; obj["description"]=bookmark->description;
obj["timestamp"]=QString("%1").arg(bookmark->timestamp); obj["timestamp"]=QString("%1").arg(bookmark->timestamp);
@ -298,7 +291,7 @@ QList<PBookmark> BookmarkModel::load(const QString& filename, qint64 criteriaTim
.arg(error.offset) .arg(error.offset)
.arg(error.errorString())); .arg(error.errorString()));
} }
QJsonObject rootObj; QJsonObject rootObj=doc.object();
bool ok; bool ok;
qint64 timestamp = rootObj["timestamp"].toString().toLongLong(&ok); qint64 timestamp = rootObj["timestamp"].toString().toLongLong(&ok);
if (!ok || timestamp<=criteriaTimestamp) if (!ok || timestamp<=criteriaTimestamp)
@ -309,6 +302,7 @@ QList<PBookmark> BookmarkModel::load(const QString& filename, qint64 criteriaTim
QJsonValue value = array[i]; QJsonValue value = array[i];
QJsonObject obj=value.toObject(); QJsonObject obj=value.toObject();
qint64 bookmarkTimestamp = obj["timestamp"].toString().toULongLong(&ok); qint64 bookmarkTimestamp = obj["timestamp"].toString().toULongLong(&ok);
qDebug()<<bookmarkTimestamp<<"??"<<criteriaTimestamp;
if (ok && bookmarkTimestamp>criteriaTimestamp) { if (ok && bookmarkTimestamp>criteriaTimestamp) {
PBookmark bookmark = std::make_shared<Bookmark>(); PBookmark bookmark = std::make_shared<Bookmark>();
bookmark->filename = obj["filename"].toString(); bookmark->filename = obj["filename"].toString();
@ -338,7 +332,9 @@ void BookmarkModel::loadProjectBookmarks(const QString &filename, const QString&
mLastLoadProjectBookmarksTimestamp = QDateTime::currentMSecsSinceEpoch(); mLastLoadProjectBookmarksTimestamp = QDateTime::currentMSecsSinceEpoch();
mProjectBookmarks = load(filename,0,&t); mProjectBookmarks = load(filename,0,&t);
QDir folder(projectFolder); QDir folder(projectFolder);
qDebug()<<"load books"<<filename;
foreach (PBookmark bookmark, mProjectBookmarks) { foreach (PBookmark bookmark, mProjectBookmarks) {
qDebug()<<bookmark->filename;
bookmark->filename=folder.absoluteFilePath(bookmark->filename); bookmark->filename=folder.absoluteFilePath(bookmark->filename);
} }
if (mIsForProject) if (mIsForProject)

View File

@ -595,12 +595,12 @@ void Document::loadFromFile(const QString& filename, const QByteArray& encoding,
realEncoding = ENCODING_ASCII; realEncoding = ENCODING_ASCII;
return; return;
} }
realEncoding = pCharsetInfoManager->getDefaultSystemEncoding(); realEncoding = pCharsetInfoManager->getDefaultSystemEncoding();
if (tryLoadFileByEncoding(realEncoding,file)) {
return;
}
QList<PCharsetInfo> charsets = pCharsetInfoManager->findCharsetByLocale(pCharsetInfoManager->localeName()); QList<PCharsetInfo> charsets = pCharsetInfoManager->findCharsetByLocale(pCharsetInfoManager->localeName());
if (!charsets.isEmpty()) { if (!charsets.isEmpty()) {
if (tryLoadFileByEncoding(realEncoding,file)) {
return;
}
QSet<QByteArray> encodingSet; QSet<QByteArray> encodingSet;
for (int i=0;i<charsets.size();i++) { for (int i=0;i<charsets.size();i++) {

View File

@ -37,6 +37,7 @@
#include <QMimeDatabase> #include <QMimeDatabase>
#include <windows.h> #include <windows.h>
#endif #endif
#include "charsetinfo.h"
BaseError::BaseError(const QString &reason): BaseError::BaseError(const QString &reason):
mReason(reason) mReason(reason)
@ -301,51 +302,68 @@ void readFileToLines(const QString &fileName, QTextCodec *codec, LineProcessFunc
} }
} }
static QStringList tryLoadFileByEncoding(QByteArray encodingName, QFile& file, bool* isOk) {
QStringList result;
*isOk=false;
QTextCodec* codec = QTextCodec::codecForName(encodingName);
if (!codec)
return result;
file.reset();
QTextCodec::ConverterState state;
while (true) {
if (file.atEnd()){
break;
}
QByteArray line = file.readLine();
if (line.endsWith("\r\n")) {
line.remove(line.length()-2,2);
} else if (line.endsWith("\r")) {
line.remove(line.length()-1,1);
} else if (line.endsWith("\n")){
line.remove(line.length()-1,1);
}
QString newLine = codec->toUnicode(line.constData(),line.length(),&state);
if (state.invalidChars>0) {
return QStringList();
}
result.append(newLine);
}
*isOk=true;
return result;
}
QStringList readFileToLines(const QString &fileName) QStringList readFileToLines(const QString &fileName)
{ {
QFile file(fileName); QFile file(fileName);
if (file.size()<=0) if (file.size()<=0)
return QStringList(); return QStringList();
QTextCodec* codec = QTextCodec::codecForLocale();
QStringList result; QStringList result;
QTextCodec::ConverterState state;
bool ok = true;
if (file.open(QFile::ReadOnly)) { if (file.open(QFile::ReadOnly)) {
while (!file.atEnd()) { bool ok;
QByteArray array = file.readLine(); result = tryLoadFileByEncoding("UTF-8",file,&ok);
QString s = codec->toUnicode(array,array.length(),&state); if (ok) {
if (state.invalidChars>0) { return result;
ok=false;
break;
}
if (s.endsWith("\r\n")) {
s.remove(s.length()-2,2);
} else if (s.endsWith("\r")) {
s.remove(s.length()-1,1);
} else if (s.endsWith("\n")){
s.remove(s.length()-1,1);
}
result.append(s);
} }
if (!ok) {
file.seek(0); QByteArray realEncoding = pCharsetInfoManager->getDefaultSystemEncoding();
result.clear(); result = tryLoadFileByEncoding(realEncoding,file,&ok);
codec = QTextCodec::codecForName("UTF-8"); if (ok) {
while (!file.atEnd()) { return result;
QByteArray array = file.readLine(); }
QString s = codec->toUnicode(array,array.length(),&state); QList<PCharsetInfo> charsets = pCharsetInfoManager->findCharsetByLocale(pCharsetInfoManager->localeName());
if (state.invalidChars>0) { if (!charsets.isEmpty()) {
result.clear(); QSet<QByteArray> encodingSet;
break; for (int i=0;i<charsets.size();i++) {
encodingSet.insert(charsets[i]->name);
}
encodingSet.remove(realEncoding);
foreach (const QByteArray& encodingName,encodingSet) {
if (encodingName == ENCODING_UTF8)
continue;
result = tryLoadFileByEncoding("UTF-8",file,&ok);
if (ok) {
return result;
} }
if (s.endsWith("\r\n")) {
s.remove(s.length()-2,2);
} else if (s.endsWith("\r")) {
s.remove(s.length()-1,1);
} else if (s.endsWith("\n")){
s.remove(s.length()-1,1);
}
result.append(s);
} }
} }
} }