Skip to content

Commit

Permalink
feat(snippets): show warning and ask for new if name is used (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
ouuan authored and coder3101 committed Jan 11, 2020
1 parent b8be982 commit f4dd557
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions src/preferencewindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,25 @@ void PreferenceWindow::on_save_snippet_clicked()

void PreferenceWindow::on_new_snippet_clicked()
{
auto name = QInputDialog::getText(this, tr("New name"), tr("Name:"));
if (name.isEmpty())
return;
int index = ui->snippets->findText(name);
if (index != -1)
{
ui->snippets->setCurrentIndex(index);
}
else
QString name;
auto lang = ui->snippets_lang->currentText();

while (1)
{
auto lang = ui->snippets_lang->currentText();
manager->setSnippet(lang, name, "");
switchToSnippet(name);
name = QInputDialog::getText(this, tr("New"), tr("Name:"));
if (name.isEmpty())
return;
int index = ui->snippets->findText(name);
if (index != -1)
{
QMessageBox::warning(this, "Name used", "The snippet name " + name + " is used for " + lang);
continue;
}
break;
}

manager->setSnippet(lang, name, "");
switchToSnippet(name);
}

void PreferenceWindow::on_delete_snippet_clicked()
Expand All @@ -360,23 +365,28 @@ void PreferenceWindow::on_rename_snippet_clicked()
{
if (ui->snippets->currentIndex() != -1)
{
auto name = QInputDialog::getText(this, tr("New name"), tr("Name:"));
if (name.isEmpty())
return;
int index = ui->snippets->findText(name);
if (index != -1)
{
ui->snippets->setCurrentIndex(index);
}
else
QString name;
auto lang = ui->snippets_lang->currentText();

while (1)
{
auto content = editor->toPlainText();
auto currentName = ui->snippets->currentText();
auto lang = ui->snippets_lang->currentText();
manager->removeSnippet(lang, currentName);
manager->setSnippet(lang, name, content);
switchToSnippet(name);
name = QInputDialog::getText(this, tr("Rename"), tr("Name:"));
if (name.isEmpty())
return;
int index = ui->snippets->findText(name);
if (index != -1)
{
QMessageBox::warning(this, "Name used", "The snippet name " + name + " is used for " + lang);
continue;
}
break;
}

auto content = editor->toPlainText();
auto currentName = ui->snippets->currentText();
manager->removeSnippet(lang, currentName);
manager->setSnippet(lang, name, content);
switchToSnippet(name);
}
}

Expand Down

0 comments on commit f4dd557

Please sign in to comment.