Skip to content

Commit

Permalink
Refactor: reduce unnecessary reparse when opening file
Browse files Browse the repository at this point in the history
  • Loading branch information
royqh1979 committed Dec 26, 2024
1 parent 05cc6b1 commit fedd877
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 32 deletions.
22 changes: 12 additions & 10 deletions RedPandaIDE/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ static QSet<QString> CppTypeQualifiers {
};

Editor::Editor(QWidget *parent):
Editor{parent,"untitled",ENCODING_AUTO_DETECT,nullptr,true,nullptr}
Editor{parent,"untitled",ENCODING_AUTO_DETECT, FileType::None, QString(), nullptr,true,nullptr}
{
}

Editor::Editor(QWidget *parent, const QString& filename,
const QByteArray& encoding,
Project* pProject, bool isNew,
QTabWidget* parentPageControl):
const QByteArray& encoding, FileType fileType,
const QString& contextFile, Project* pProject,
bool isNew, QTabWidget* parentPageControl):
QSynEdit{parent},
mInited{false},
mEncodingOption{encoding},
Expand All @@ -95,7 +95,7 @@ Editor::Editor(QWidget *parent, const QString& filename,
mHoverModifiedLine{-1},
mWheelAccumulatedDelta{0},
mCtrlClicking{false},
mFileType{FileType::None}
mContextFile{contextFile}
{
mLastFocusOutTime = 0;
mInited=false;
Expand All @@ -107,7 +107,9 @@ Editor::Editor(QWidget *parent, const QString& filename,
if (mFilename.isEmpty()) {
mFilename = QString("untitled%1").arg(getNewFileNumber());
}
doSetFileType(getFileType(mFilename));
if (fileType == FileType::None)
fileType = getFileType(mFilename);
doSetFileType(fileType);
if (mProject && mEncodingOption==ENCODING_PROJECT) {
mEncodingOption=mProject->options().encoding;
}
Expand Down Expand Up @@ -4542,9 +4544,12 @@ FileType Editor::fileType() const

void Editor::setFileType(FileType newFileType)
{
if (newFileType == FileType::None)
newFileType = getFileType(mFilename);
if (mFileType==newFileType)
return;
doSetFileType(newFileType);
applyColorScheme(pSettings->editor().colorScheme());
if (!inProject()) {
initParser();
reparse(false);
Expand Down Expand Up @@ -4583,16 +4588,13 @@ void Editor::doSetFileType(FileType newFileType, bool force)
}

QSynedit::PSyntaxer syntaxer{syntaxerManager.getSyntaxer(mFileType)};
setSyntaxer(syntaxer);
if (syntaxer) {
setSyntaxer(syntaxer);
setFormatter(syntaxerManager.getFormatter(syntaxer->language()));
setUseCodeFolding(true);
} else {
setUseCodeFolding(false);
}

setSyntaxer(syntaxer);
applyColorScheme(pSettings->editor().colorScheme());
}

Editor* Editor::openFileInContext(const QString &filename)
Expand Down
2 changes: 2 additions & 0 deletions RedPandaIDE/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class Editor : public QSynedit::QSynEdit

explicit Editor(QWidget *parent, const QString& filename,
const QByteArray& encoding,
FileType fileType,
const QString& contextFile,
Project* pProject, bool isNew,QTabWidget* parentPageControl);

~Editor();
Expand Down
7 changes: 4 additions & 3 deletions RedPandaIDE/editorlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ EditorList::EditorList(QTabWidget* leftPageWidget,
}

Editor* EditorList::newEditor(const QString& filename, const QByteArray& encoding,
Project *pProject, bool newFile,
QTabWidget* page) {
FileType fileType, const QString& contextFile,
Project *pProject, bool newFile,
QTabWidget* page) {
QTabWidget * parentPageControl = nullptr;
if (page == nullptr)
parentPageControl = getNewEditorPageControl();
Expand All @@ -55,7 +56,7 @@ Editor* EditorList::newEditor(const QString& filename, const QByteArray& encodin
}

// parentPageControl takes the owner ship
Editor * e = new Editor(parentPageControl,filename,encoding,pProject,newFile,parentPageControl);
Editor * e = new Editor(parentPageControl, filename, encoding, fileType, contextFile, pProject, newFile, parentPageControl);
connect(e, &Editor::renamed, this, &EditorList::onEditorRenamed);
updateLayout();
connect(e,&Editor::fileSaved,
Expand Down
1 change: 1 addition & 0 deletions RedPandaIDE/editorlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EditorList : public QObject
QWidget* panel, QObject* parent = nullptr);

Editor* newEditor(const QString& filename, const QByteArray& encoding,
FileType fileType, const QString& contextFile,
Project *pProject, bool newFile,
QTabWidget* page=nullptr);

Expand Down
38 changes: 23 additions & 15 deletions RedPandaIDE/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ void MainWindow::openFiles(const QStringList &files)
}
}

Editor* MainWindow::openFile(QString filename, bool activate, QTabWidget* page)
Editor* MainWindow::openFile(QString filename, bool activate, QTabWidget* page, FileType fileType, const QString& contextFile)
{
if (!fileExists(filename))
return nullptr;
Expand All @@ -1812,6 +1812,8 @@ Editor* MainWindow::openFile(QString filename, bool activate, QTabWidget* page)

Editor* editor = mEditorList->getOpenedEditorByFilename(filename);
if (editor!=nullptr) {
editor->setContextFile(contextFile);
editor->setFileType(fileType);
if (activate) {
editor->activate();
}
Expand All @@ -1837,7 +1839,8 @@ Editor* MainWindow::openFile(QString filename, bool activate, QTabWidget* page)
if (pProject && encoding==ENCODING_PROJECT)
encoding=pProject->options().encoding;
editor = mEditorList->newEditor(filename,encoding,
pProject, false, page);
fileType, contextFile,
pProject, false, page);
// if (mProject) {
// mProject->associateEditorToUnit(editor,unit);
// }
Expand Down Expand Up @@ -3549,6 +3552,18 @@ void MainWindow::loadLastOpens()
if (!fileExists(editorFilename))
continue;
bool onLeft = fileObj["onLeft"].toBool();
QSynedit::BufferCoord pos;
pos.ch = fileObj["caretX"].toInt(1);
pos.line = fileObj["caretY"].toInt(1);
QByteArray encoding;
if (fileObj.contains("encodingOption"))
encoding = fileObj["encodingOption"].toString().toLatin1();
QString contextFile;
if (fileObj.contains("contextFile"))
contextFile = fileObj["contextFile"].toString();
FileType fileType{FileType::None};
if (fileObj.contains("fileType"))
fileType = nameToFileType(fileObj["fileType"].toString());
QTabWidget* page;
if (onLeft)
page = mEditorList->leftPageWidget();
Expand All @@ -3559,12 +3574,14 @@ void MainWindow::loadLastOpens()
unit = mProject->findUnit(editorFilename);
}
bool inProject = (mProject && unit);
QByteArray encoding = unit ? unit->encoding() :
(pSettings->editor().autoDetectFileEncoding()? QByteArray(ENCODING_AUTO_DETECT) : pSettings->editor().defaultEncoding());
if (encoding.isEmpty()) {
encoding = unit ? unit->encoding() :
(pSettings->editor().autoDetectFileEncoding()? QByteArray(ENCODING_AUTO_DETECT) : pSettings->editor().defaultEncoding());
}
Project* pProject = (inProject?mProject.get():nullptr);
if (pProject && encoding==ENCODING_PROJECT)
encoding=pProject->options().encoding;
Editor * editor = mEditorList->newEditor(editorFilename, encoding, pProject,false,page);
Editor * editor = mEditorList->newEditor(editorFilename, encoding, fileType, contextFile, pProject,false,page);

if (inProject && editor) {
mProject->loadUnitLayout(editor);
Expand All @@ -3574,23 +3591,13 @@ void MainWindow::loadLastOpens()
// }
if (!editor)
continue;
QSynedit::BufferCoord pos;
pos.ch = fileObj["caretX"].toInt(1);
pos.line = fileObj["caretY"].toInt(1);
editor->setCaretXY(pos);
editor->setTopPos(
fileObj["top"].toInt(1)
);
editor->setLeftPos(
fileObj["left"].toInt(1)
);
if (fileObj.contains("contextFile"))
editor->setContextFile(fileObj["contextFile"].toString());
if (fileObj.contains("fileType")) {
editor->setFileType(nameToFileType(fileObj["fileType"].toString()));
}
if (fileObj.contains("encodingOption"))
editor->setEncodingOption(fileObj["encodingOption"].toString().toLatin1());
if (fileObj["focused"].toBool(false))
focusedEditor = editor;
//mVisitHistoryManager->removeFile(editorFilename);
Expand Down Expand Up @@ -3670,6 +3677,7 @@ void MainWindow::newEditor(const QString& suffix)
} while(mEditorList->hasFilename(filename));
Editor * editor=mEditorList->newEditor(filename,
pSettings->editor().defaultEncoding(),
FileType::None, QString(),
nullptr,true);
editor->activate();
//updateForEncodingInfo();
Expand Down
2 changes: 1 addition & 1 deletion RedPandaIDE/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class MainWindow : public QMainWindow

TodoModel* todoModel();

Editor* openFile(QString filename, bool activate=true, QTabWidget* page=nullptr);
Editor* openFile(QString filename, bool activate=true, QTabWidget* page=nullptr, FileType fileType=FileType::None, const QString& contextFile = QString());
void openProject(QString filename, bool openFiles = true);
void changeOptions(const QString& widgetName=QString(), const QString& groupName=QString());
void changeProjectOptions(const QString& widgetName=QString(), const QString& groupName=QString());
Expand Down
2 changes: 1 addition & 1 deletion RedPandaIDE/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@
<enum>QTabWidget::North</enum>
</property>
<property name="currentIndex">
<number>2</number>
<number>6</number>
</property>
<property name="iconSize">
<size>
Expand Down
8 changes: 6 additions & 2 deletions RedPandaIDE/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ Editor* Project::openUnit(PProjectUnit& unit, bool forceOpen) {
if (encoding==ENCODING_PROJECT)
encoding=options().encoding;

editor = mEditorList->newEditor(unit->fileName(), encoding, this, false);
editor = mEditorList->newEditor(unit->fileName(), encoding, FileType::None, QString(), this, false);
if (editor) {
//editor->setProject(this);
//unit->setEncoding(encoding);
Expand Down Expand Up @@ -420,7 +420,7 @@ Editor *Project::openUnit(PProjectUnit &unit, const PProjectEditorLayout &layout
encoding = unit->encoding();
if (encoding==ENCODING_PROJECT)
encoding=options().encoding;
editor = mEditorList->newEditor(unit->fileName(), encoding, this, false);
editor = mEditorList->newEditor(unit->fileName(), encoding, FileType::None, QString(), this, false);
if (editor) {
//editor->setInProject(true);
editor->setCaretY(layout->caretY);
Expand Down Expand Up @@ -983,6 +983,8 @@ bool Project::assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, b
Editor * editor = mEditorList->newEditor(
unit->fileName(),
unit->encoding()==ENCODING_PROJECT?options().encoding:unit->encoding(),
FileType::None,
QString(),
this,
false);
editor->activate();
Expand All @@ -1000,6 +1002,8 @@ bool Project::assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, b
Editor * editor = mEditorList->newEditor(
unit->fileName(),
unit->encoding()==ENCODING_PROJECT?options().encoding:unit->encoding(),
FileType::None,
QString(),
this,
true);

Expand Down

0 comments on commit fedd877

Please sign in to comment.