From aad78fa475a3bc46296f06075e59ca35b5f81ab9 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Wed, 23 Jun 2021 08:55:56 +0800 Subject: [PATCH] * fix: use backspace to delete selected text is not correctly handled * done: delete symbol pairs --- RedPandaIDE/editor.cpp | 104 ++++++++++++++---- RedPandaIDE/editor.h | 1 + RedPandaIDE/qsynedit/SynEdit.cpp | 4 +- RedPandaIDE/settings.cpp | 12 +- RedPandaIDE/settings.h | 6 +- .../editorsymbolcompletionwidget.cpp | 4 +- .../editorsymbolcompletionwidget.ui | 16 +-- 7 files changed, 106 insertions(+), 41 deletions(-) diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 01892f53..dfb4e707 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -211,6 +211,56 @@ QTabWidget* Editor::pageControl() noexcept{ return mParentPageControl; } +void Editor::undoSymbolCompletion(int pos) +{ + PSynHighlighterAttribute Attr; + QString Token; + bool tokenFinished; + SynHighlighterTokenType tokenType; + + if (!highlighter()) + return; + if (!pSettings->editor().removeSymbolPairs()) + return; + if (!GetHighlighterAttriAtRowCol(caretXY(), Token, tokenFinished, tokenType, Attr)) + return; + if ((tokenType == SynHighlighterTokenType::Comment) && (!tokenFinished)) + return ; + //convert caret x to string index; + pos--; + + if (pos<0 || pos+1>=lineText().length()) + return; + QChar DeletedChar = lineText()[pos]; + QChar NextChar = lineText()[pos+1]; + if ((tokenType == SynHighlighterTokenType::Character) && (DeletedChar != '\'')) + return; + if (tokenType == SynHighlighterTokenType::StringEscapeSequence) + return; + if (tokenType == SynHighlighterTokenType::String) { + if ((DeletedChar!='"') && (DeletedChar!='(')) + return; + if ((DeletedChar=='"') && (Token!="\"\"")) + return; + if ((DeletedChar=='(') && (!Token.startsWith("R\""))) + return; + } + if ((DeletedChar == '\'') && (tokenType == SynHighlighterTokenType::Number)) + return; + if ((DeletedChar == '<') && + ((tokenType != SynHighlighterTokenType::PreprocessDirective) + || !lineText().startsWith("#include"))) + return; + if ( (pSettings->editor().completeBracket() && (DeletedChar == '[') && (NextChar == ']')) || + (pSettings->editor().completeParenthese() && (DeletedChar == '(') && (NextChar == ')')) || + (pSettings->editor().completeGlobalInclude() && (DeletedChar == '<') && (NextChar == '>')) || + (pSettings->editor().completeBrace() && (DeletedChar == '{') && (NextChar == '}')) || + (pSettings->editor().completeSingleQuote() && (DeletedChar == '\'') && (NextChar == '\'')) || + (pSettings->editor().completeDoubleQuote() && (DeletedChar == '\"') && (NextChar == '\"'))) { + CommandProcessor(SynEditorCommand::ecDeleteChar); + } +} + void Editor::wheelEvent(QWheelEvent *event) { if ( (event->modifiers() & Qt::ControlModifier)!=0) { int size = pSettings->editor().fontSize(); @@ -250,24 +300,38 @@ void Editor::focusOutEvent(QFocusEvent *event) void Editor::keyPressEvent(QKeyEvent *event) { bool handled = false; - QString t = event->text(); - if (!t.isEmpty()) { - QChar ch = t[0]; - switch (ch.unicode()) { - case '"': - case '\'': - case '(': - case ')': - case '{': - case '}': - case '[': - case ']': - case '<': - case '>': - case '*': - handled = handleSymbolCompletion(ch); + switch (event->key()) { + case Qt::Key_Delete: + // remove completed character + //fLastIdCharPressed:=0; + undoSymbolCompletion(caretX()); + break;; + case Qt::Key_Backspace: + // remove completed character + //fLastIdCharPressed:=0; + undoSymbolCompletion(caretX()-1); + break;; + default: { + QString t = event->text(); + if (!t.isEmpty()) { + QChar ch = t[0]; + switch (ch.unicode()) { + case '"': + case '\'': + case '(': + case ')': + case '{': + case '}': + case '[': + case ']': + case '<': + case '>': + case '*': + handled = handleSymbolCompletion(ch); + } } } + } if (!handled) { SynEdit::keyPressEvent(event); } else { @@ -676,7 +740,7 @@ bool Editor::handleSingleQuoteCompletion() } } else { if (status == QuoteStatus::NotQuote) { - if (highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) { + if (ch == 0 || highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) { // insert '' beginUpdate(); CommandProcessor(SynEditorCommand::ecChar,'\''); @@ -696,13 +760,13 @@ bool Editor::handleDoubleQuoteCompletion() QuoteStatus status = getQuoteStatus(); QChar ch = getCurrentChar(); if (ch == '"') { - if (status == QuoteStatus::DoubleQuote && status == QuoteStatus::RawString) { + if (status == QuoteStatus::DoubleQuote || status == QuoteStatus::RawString) { setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over return true; } } else { if (status == QuoteStatus::NotQuote) { - if (highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) { + if ((ch == 0) || highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) { // insert "" beginUpdate(); CommandProcessor(SynEditorCommand::ecChar,'"'); @@ -759,7 +823,7 @@ Editor::QuoteStatus Editor::getQuoteStatus() if (posX >= Line.length()) { posX = Line.length()-1; } - for (int i=0; i<=posX;i++) { + for (int i=0; iendUpdate(); decPaintLock(); }); - BufferCoord BB = mBlockBegin; - BufferCoord BE = mBlockEnd; + BufferCoord BB = blockBegin(); + BufferCoord BE = blockEnd(); if (selAvail()) { DeleteSelection(BB,BE); internalSetCaretXY(BB); diff --git a/RedPandaIDE/settings.cpp b/RedPandaIDE/settings.cpp index 2d1f7c9b..1b77eb63 100644 --- a/RedPandaIDE/settings.cpp +++ b/RedPandaIDE/settings.cpp @@ -293,14 +293,14 @@ void Settings::Editor::setColorScheme(const QString &colorScheme) mColorScheme = colorScheme; } -bool Settings::Editor::removeMathcingSymbol() const +bool Settings::Editor::removeSymbolPairs() const { - return mRemoveMathcingSymbol; + return mRemoveSymbolPairs; } -void Settings::Editor::setRemoveMathcingSymbol(bool removeMathcingSymbol) +void Settings::Editor::setRemoveSymbolPairs(bool value) { - mRemoveMathcingSymbol = removeMathcingSymbol; + mRemoveSymbolPairs = value; } bool Settings::Editor::overwriteSymbols() const @@ -721,7 +721,7 @@ void Settings::Editor::doSave() saveValue("complete_double_quote", mCompleteDoubleQuote); saveValue("complete_global_include", mCompleteGlobalInclude); saveValue("overwrite_symbols", mOverwriteSymbols); - saveValue("remove_matching_symbols",mRemoveMathcingSymbol); + saveValue("remove_symbol_pairs",mRemoveSymbolPairs); } void Settings::Editor::doLoad() @@ -794,7 +794,7 @@ void Settings::Editor::doLoad() mCompleteDoubleQuote = boolValue("complete_double_quote",true); mCompleteGlobalInclude = boolValue("complete_global_include",true); mOverwriteSymbols = boolValue("overwrite_symbols",true); - mRemoveMathcingSymbol = boolValue("remove_matching_symbols",true); + mRemoveSymbolPairs = boolValue("remove_symbol_pairs",true); } SynEditCaretType Settings::Editor::caretForOverwrite() const diff --git a/RedPandaIDE/settings.h b/RedPandaIDE/settings.h index d6f53ceb..1c31b096 100644 --- a/RedPandaIDE/settings.h +++ b/RedPandaIDE/settings.h @@ -245,8 +245,8 @@ public: bool overwriteSymbols() const; void setOverwriteSymbols(bool overwriteSymbols); - bool removeMathcingSymbol() const; - void setRemoveMathcingSymbol(bool removeMathcingSymbol); + bool removeSymbolPairs() const; + void setRemoveSymbolPairs(bool value); private: QByteArray mDefaultEncoding; @@ -317,7 +317,7 @@ public: bool mCompleteDoubleQuote; bool mCompleteGlobalInclude; bool mOverwriteSymbols; - bool mRemoveMathcingSymbol; + bool mRemoveSymbolPairs; // _Base interface protected: diff --git a/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.cpp b/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.cpp index 05e3f50c..fa282d15 100644 --- a/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.cpp +++ b/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.cpp @@ -24,7 +24,7 @@ void EditorSymbolCompletionWidget::doLoad() ui->chkCompleteGlobalInclude->setChecked(pSettings->editor().completeGlobalInclude()); ui->chkCompleteParenthesis->setChecked(pSettings->editor().completeParenthese()); ui->chkCompleteSingleQuotation->setChecked(pSettings->editor().completeSingleQuote()); - ui->chkRemoveMatchingSymbols->setChecked(pSettings->editor().removeMathcingSymbol()); + ui->chkRemoveSymbolPairs->setChecked(pSettings->editor().removeSymbolPairs()); ui->chkSkipMathingSymbols->setChecked(pSettings->editor().overwriteSymbols()); } @@ -38,7 +38,7 @@ void EditorSymbolCompletionWidget::doSave() pSettings->editor().setCompleteGlobalInclude(ui->chkCompleteGlobalInclude->isChecked()); pSettings->editor().setCompleteParenthese(ui->chkCompleteParenthesis->isChecked()); pSettings->editor().setCompleteSingleQuote(ui->chkCompleteSingleQuotation->isChecked()); - pSettings->editor().setRemoveMathcingSymbol(ui->chkRemoveMatchingSymbols->isChecked()); + pSettings->editor().setRemoveSymbolPairs(ui->chkRemoveSymbolPairs->isChecked()); pSettings->editor().setOverwriteSymbols(ui->chkSkipMathingSymbols->isChecked()); pSettings->editor().save(); diff --git a/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.ui b/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.ui index 83631cfc..1cc8a99a 100644 --- a/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.ui +++ b/RedPandaIDE/settingsdialog/editorsymbolcompletionwidget.ui @@ -118,13 +118,6 @@ - - - - Remove matching symbols when delete chars - - - @@ -144,7 +137,7 @@ - + Qt::Vertical @@ -157,6 +150,13 @@ + + + + Remove symbol pairs when delete chars + + +