Skip to content

Commit

Permalink
Add snippets (#75)
Browse files Browse the repository at this point in the history
* feat: add editing snippets in preference window

* chore: add .vscode in .gitignore

* fix(preferencewindow): delete editor in the destructor

* feat(preference): apply setting changes to snippets editor

* feat(preference): focus on editor after switching snippet

* feat: create new snippet when there are none and save is clicked

* feat(preferen): set lang for snippets editor

* refactor(appwindow): add function for get window

* feat: add use snippets

* feat: add hotkey for snippets
  • Loading branch information
ouuan authored and coder3101 committed Jan 10, 2020
1 parent 2261643 commit 77151c4
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 59 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -46,4 +46,7 @@ CMakeLists.txt.user*
build/

# Visual Studio
.vs
.vs

# Visual Studio Code
.vscode
8 changes: 8 additions & 0 deletions include/SettingsManager.hpp
Expand Up @@ -82,6 +82,7 @@ struct SettingsData
QKeySequence hotkeyFormat;
QKeySequence hotkeyKill;
QKeySequence hotkeyViewModeToggler;
QKeySequence hotkeySnippets;

ViewMode viewMode;
};
Expand Down Expand Up @@ -184,12 +185,19 @@ class SettingManager
void setHotkeyRun(QKeySequence sequence);
void setHotkeyCompile(QKeySequence sequence);
void setHotkeyViewModeToggler(QKeySequence sequence);
void setHotkeySnippets(QKeySequence sequence);
QKeySequence getHotkeyFormat();
QKeySequence getHotkeyKill();
QKeySequence getHotkeyCompileRun();
QKeySequence getHotkeyRun();
QKeySequence getHotkeyCompile();
QKeySequence getHotkeyViewModeToggler();
QKeySequence getHotkeySnippets();

QString getSnippet(QString lang, QString name);
void setSnippet(QString lang, QString name, QString content);
void removeSnippet(QString lang, QString name);
QStringList getSnippetsNames(QString lang);

private:
QString mSettingsFile;
Expand Down
5 changes: 5 additions & 0 deletions include/appwindow.hpp
Expand Up @@ -74,6 +74,8 @@ class AppWindow : public QMainWindow

void on_actionKill_Processes_triggered();

void on_actionUse_Snippets_triggered();

void on_actionEditor_Mode_triggered();

void on_actionIO_Mode_triggered();
Expand Down Expand Up @@ -104,6 +106,9 @@ class AppWindow : public QMainWindow
void closeAll();
bool closeTab(int index);
void openTab(QString fileName, bool isCompanionTab = false);

MainWindow *currentWindow();
MainWindow *windowIndex(int index);
};

#endif // APPWINDOW_HPP
3 changes: 3 additions & 0 deletions include/mainwindow.hpp
Expand Up @@ -69,12 +69,15 @@ class MainWindow : public QMainWindow
void maybeLoadTemplate();

void setLanguage(QString lang);
QString getLanguage();
void setSettingsData(Settings::SettingsData data, bool);

MessageLogger *getLogger();
QFile *getOpenFile();
QSplitter *getSplitter();

void insertText(QString text);

private slots:
void onTextChangedTriggered();

Expand Down
18 changes: 18 additions & 0 deletions include/preferencewindow.hpp
Expand Up @@ -2,6 +2,7 @@
#define PREFERENCEWINDOW_HPP

#include "SettingsManager.hpp"
#include <QCodeEditor>
#include <QListWidget>
#include <QMainWindow>

Expand Down Expand Up @@ -41,15 +42,32 @@ class PreferenceWindow : public QMainWindow

void on_java_template_clicked();

void on_save_snippet_clicked();

void on_new_snippet_clicked();

void on_delete_snippet_clicked();

void on_rename_snippet_clicked();

void on_snippets_lang_changed(const QString &lang);

void on_current_snippet_changed(const QString &text);

void applySettingsToEditor();

private:
Ui::PreferenceWindow *ui;
QFont currentFont;
QString cppTemplatePath, pythonTemplatePath, javaTemplatePath;
Settings::SettingManager *manager;
QCodeEditor *editor = nullptr; // for snippets

void extractSettingsFromUi();
void applySettingsToui();
void setConstraints();
void updateSnippets();
void switchToSnippet(const QString &text);
};

#endif // PREFERENCEWINDOW_HPP
33 changes: 33 additions & 0 deletions src/SettingsManager.cpp
Expand Up @@ -346,6 +346,34 @@ QKeySequence SettingManager::getHotkeyViewModeToggler()
{
return QKeySequence::fromString(mSettings->value("hotkey_mode_toggle", "").toString());
}
QKeySequence SettingManager::getHotkeySnippets()
{
return QKeySequence::fromString(mSettings->value("hotkey_snippets", "").toString());
}

QString SettingManager::getSnippet(QString lang, QString name)
{
return mSettings->value("snippets/" + lang + "/" + name, "").toString();
}
void SettingManager::setSnippet(QString lang, QString name, QString content)
{
mSettings->setValue("snippets/" + lang + "/" + name, content);
}
void SettingManager::removeSnippet(QString lang, QString name)
{
mSettings->remove("snippets/" + lang + "/" + name);
}
QStringList SettingManager::getSnippetsNames(QString lang)
{
mSettings->beginGroup("snippets");
mSettings->beginGroup(lang);
auto ret = mSettings->allKeys();
mSettings->endGroup();
mSettings->endGroup();
ret.sort(Qt::CaseInsensitive);
return ret;
}

void SettingManager::setHotkeyViewModeToggler(QKeySequence sequence)
{
mSettings->setValue("hotkey_mode_toggle", sequence.toString());
Expand All @@ -370,6 +398,10 @@ void SettingManager::setHotkeyFormat(QKeySequence sequence)
{
mSettings->setValue("hotkey_format", sequence.toString());
}
void SettingManager::setHotkeySnippets(QKeySequence sequence)
{
mSettings->setValue("hotkey_snippets", sequence.toString());
}

ViewMode SettingManager::getViewMode()
{
Expand Down Expand Up @@ -438,6 +470,7 @@ SettingsData SettingManager::toData()
data.hotkeyKill = getHotkeyKill();
data.hotkeyFormat = getHotkeyFormat();
data.hotkeyViewModeToggler = getHotkeyViewModeToggler();
data.hotkeySnippets = getHotkeySnippets();
data.viewMode = getViewMode();

return data;
Expand Down

0 comments on commit 77151c4

Please sign in to comment.