Skip to content

Commit

Permalink
Editor: Keep indentator/syntax when changing themes - #20
Browse files Browse the repository at this point in the history
When switching themes, keep the same syntax/theme. I still need to know
how to save this metadata into the settings.

refs #20
  • Loading branch information
diegoiast committed Oct 23, 2024
1 parent 8c14acb commit a235b3d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
17 changes: 8 additions & 9 deletions src/plugins/texteditor/texteditor_plg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ void TextEditorPlugin::on_client_merged(qmdiHost *) {
if (!editor) {
return;
}
auto langInfo = ::Qutepart::chooseLanguage({}, {}, editor->mdiClientFileName());

newThemeSelected = false;
chooseTheme->setDisabled(true);
Expand All @@ -190,10 +189,11 @@ void TextEditorPlugin::on_client_merged(qmdiHost *) {
p->setDataModel(model);
p->show();

connect(p, &CommandPalette::didHide, this, [this, p, editor, langInfo]() {
auto langInfo = ::Qutepart::chooseLanguage({}, {}, editor->mdiClientFileName());
connect(p, &CommandPalette::didHide, this, [this, p, editor]() {
if (!newThemeSelected) {
editor->setEditorTheme(this->theme);
editor->setEditorHighlighter(langInfo.id, this->theme);
editor->setEditorHighlighter(editor->getSyntaxID(), this->theme);
} else {
// apply it globally
auto newTheme = const_cast<Qutepart::Theme *>(editor->getEditorTheme());
Expand All @@ -207,9 +207,8 @@ void TextEditorPlugin::on_client_merged(qmdiHost *) {
continue;
}

auto langInfo = ::Qutepart::chooseLanguage({}, {}, e->mdiClientFileName());
e->setEditorTheme(this->theme);
e->setEditorHighlighter(langInfo.id, this->theme);
e->setEditorHighlighter(editor->getSyntaxID(), this->theme);
}

auto themeFileName = themeManager->getNameFromDesc(newTheme->metaData.name);
Expand All @@ -220,7 +219,7 @@ void TextEditorPlugin::on_client_merged(qmdiHost *) {
editor->setFocus();
});
connect(p, &CommandPalette::didSelectItem, this,
[langInfo, editor, this](const QModelIndex index, const QAbstractItemModel *) {
[editor, this](const QModelIndex index, const QAbstractItemModel *) {
auto newTheme = const_cast<Qutepart::Theme *>(editor->getEditorTheme());
if (newTheme != this->theme) {
delete newTheme;
Expand All @@ -235,9 +234,9 @@ void TextEditorPlugin::on_client_merged(qmdiHost *) {
}

editor->setEditorTheme(newTheme);
editor->setEditorHighlighter(langInfo.id, newTheme);
editor->setEditorHighlighter(editor->getSyntaxID(), newTheme);
});
connect(p, &CommandPalette::didChooseItem, this, [editor, this]() {
connect(p, &CommandPalette::didChooseItem, this, [this]() {
// don't restore theme on closing - user choose his new theme
newThemeSelected = true;
});
Expand Down Expand Up @@ -371,7 +370,7 @@ void TextEditorPlugin::applySettings(qmdiEditor *editor) {
if (this->theme != editor->getEditorTheme()) {
auto langInfo = ::Qutepart::chooseLanguage({}, {}, editor->mdiClientFileName());
editor->setEditorTheme(this->theme);
editor->setEditorHighlighter(langInfo.id, this->theme);
editor->setEditorHighlighter(editor->getSyntaxID(), this->theme);
}
}

Expand Down
21 changes: 16 additions & 5 deletions src/widgets/qmdieditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ bool qmdiEditor::loadFile(const QString &newFileName) {
hideBannerMessage();
textEditor->setReadOnly(false);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QApplication::processEvents();
if (!newFileName.isEmpty()) {
QFile file(newFileName);
QFileInfo fileInfo(file);
Expand Down Expand Up @@ -672,7 +671,6 @@ bool qmdiEditor::saveFile(const QString &newFileName) {
file.close();
textEditor->document()->setModified(false);

QApplication::processEvents();
QApplication::restoreOverrideCursor();

this->fileName = newFileName;
Expand Down Expand Up @@ -879,8 +877,17 @@ void qmdiEditor::chooseIndenter(const QAction *action) {
textEditor->setIndentAlgorithm(static_cast<Qutepart::IndentAlg>(j));
}

/**
* @brief qmdiEditor::updateFileDetails
*
* Called when a file is opened, or saved. Then, the "best" indentator, and
* syntax is chosen, and fix the UI to refelect it (the bottom combo box).

Check failure on line 884 in src/widgets/qmdieditor.cpp

View workflow job for this annotation

GitHub Actions / Check for spelling errors

refelect ==> reflect
*
* Called on save and load.
*/
void qmdiEditor::updateFileDetails() {
auto langInfo = ::Qutepart::chooseLanguage(QString(), QString(), fileName);

if (langInfo.isValid()) {
textEditor->setHighlighter(langInfo.id, nullptr);
textEditor->setIndentAlgorithm(langInfo.indentAlg);
Expand Down Expand Up @@ -914,9 +921,13 @@ void qmdiEditor::updateFileDetails() {
}
}

auto isCplusplusSource = (fileName.endsWith(".h", Qt::CaseInsensitive) ||
fileName.endsWith(".cpp") || fileName.endsWith(".cc")) ||
fileName.endsWith(".cxx");
auto isCplusplusSource = fileName.endsWith(".h", Qt::CaseInsensitive) ||
fileName.endsWith(".hh", Qt::CaseInsensitive) ||
fileName.endsWith(".hpp", Qt::CaseInsensitive) ||
fileName.endsWith(".cpp", Qt::CaseInsensitive) ||
fileName.endsWith(".cc", Qt::CaseInsensitive) ||
fileName.endsWith(".c++", Qt::CaseInsensitive) ||
fileName.endsWith(".cxx", Qt::CaseInsensitive);
actionToggleHeader->setEnabled(isCplusplusSource);
}

Expand Down
12 changes: 11 additions & 1 deletion src/widgets/qmdieditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ class qmdiEditor : public QWidget, public qmdiClient {
inline const Qutepart::Theme *getEditorTheme() { return textEditor->getTheme(); }
inline void setEditorTheme(const Qutepart::Theme *theme) { textEditor->setTheme(theme); }
inline void setEditorHighlighter(QString id, const Qutepart::Theme *theme) {
if (this->syntaxLangID == id) {
return;
}
this->syntaxLangID = id;
textEditor->setHighlighter(id, theme);
}

inline const QString &getSyntaxID() const { return this->syntaxLangID; }
inline const QString &getIndentatorID() const { return this->indentationID; }

public slots:
void on_fileChanged(const QString &filename);
void adjustBottomAndTopWidget();
Expand Down Expand Up @@ -113,10 +120,13 @@ class qmdiEditor : public QWidget, public qmdiClient {
void focusInEvent(QFocusEvent *event) override;

private:
QString getShortFileName();

Qutepart::ThemeManager *themeManager = nullptr;
Qutepart::Qutepart *textEditor = nullptr;
TextOperationsWidget *operationsWidget = nullptr;
QString getShortFileName();
QString syntaxLangID;
QString indentationID;

QFileSystemWatcher *fileSystemWatcher;
QWidget *topWidget = nullptr;
Expand Down

0 comments on commit a235b3d

Please sign in to comment.