- use QMutex instead of QRecursiveMutex in SynEdit

- Can use tab / shift-tab as shortcut for indent/unindent
This commit is contained in:
royqh1979@gmail.com 2021-10-06 23:35:45 +08:00
parent 48e97dc15d
commit 3e8200d307
6 changed files with 47 additions and 25 deletions

View File

@ -578,21 +578,7 @@ void Editor::keyPressEvent(QKeyEvent *event)
pMainWindow->functionTip()->hide(); pMainWindow->functionTip()->hide();
return; return;
case Qt::Key_Tab: case Qt::Key_Tab:
if (mUserCodeInTabStops.count()>0) { tab();
handled = true;
int oldLine = caretY();
popUserCodeInTabStops();
if (oldLine!=caretY()) {
invalidateLine(oldLine);
}
invalidateLine(caretY());
} else {
if (mTabStopBegin >= 0) {
handled = true;
mTabStopBegin = -1;
invalidateLine(caretY());
}
}
return; return;
case Qt::Key_Up: case Qt::Key_Up:
if (pMainWindow->functionTip()->isVisible()) { if (pMainWindow->functionTip()->isVisible()) {
@ -3177,6 +3163,26 @@ const PCppParser &Editor::parser()
return mParser; return mParser;
} }
void Editor::tab()
{
if (mUserCodeInTabStops.count()>0) {
int oldLine = caretY();
popUserCodeInTabStops();
if (oldLine!=caretY()) {
invalidateLine(oldLine);
}
invalidateLine(caretY());
return;
} else {
if (mTabStopBegin >= 0) {
mTabStopBegin = -1;
invalidateLine(caretY());
return;
}
}
SynEdit::tab();
}
int Editor::gutterClickedLine() const int Editor::gutterClickedLine() const
{ {
return mGutterClickedLine; return mGutterClickedLine;

View File

@ -161,6 +161,8 @@ public:
const PCppParser &parser(); const PCppParser &parser();
void tab() override;
private slots: private slots:
void onModificationChanged(bool status) ; void onModificationChanged(bool status) ;
void onStatusChanged(SynStatusChanges changes); void onStatusChanged(SynStatusChanges changes);

View File

@ -2899,7 +2899,7 @@ void MainWindow::on_actionUnIndent_triggered()
{ {
Editor * editor = mEditorList->getEditor(); Editor * editor = mEditorList->getEditor();
if (editor != NULL ) { if (editor != NULL ) {
editor->untab(); editor->shifttab();
} }
} }

View File

@ -1264,6 +1264,9 @@
<property name="text"> <property name="text">
<string>Indent</string> <string>Indent</string>
</property> </property>
<property name="shortcut">
<string>Tab</string>
</property>
</action> </action>
<action name="actionUnIndent"> <action name="actionUnIndent">
<property name="icon"> <property name="icon">
@ -1274,6 +1277,9 @@
<property name="text"> <property name="text">
<string>UnIndent</string> <string>UnIndent</string>
</property> </property>
<property name="shortcut">
<string>Shift+Tab</string>
</property>
</action> </action>
<action name="actionToggleComment"> <action name="actionToggleComment">
<property name="text"> <property name="text">

View File

@ -2387,7 +2387,7 @@ void SynEdit::doAddChar(QChar AChar)
} }
} }
} }
setSelText(AChar); doSetSelText(AChar);
mUndoList->EndBlock(); mUndoList->EndBlock();
//DoOnPaintTransient(ttAfter); //DoOnPaintTransient(ttAfter);
@ -2402,7 +2402,7 @@ void SynEdit::doCutToClipboard()
mUndoList->EndBlock(); mUndoList->EndBlock();
}); });
internalDoCopyToClipboard(selText()); internalDoCopyToClipboard(selText());
setSelText(""); doSetSelText("");
} }
void SynEdit::doCopyToClipboard() void SynEdit::doCopyToClipboard()
@ -3535,7 +3535,7 @@ void SynEdit::doAddStr(const QString &s)
BE.Char = BB.Char + s.length(); BE.Char = BB.Char + s.length();
setCaretAndSelection(caretXY(),BB,BE); setCaretAndSelection(caretXY(),BB,BE);
} }
setSelText(s); doSetSelText(s);
} }
void SynEdit::doUndo() void SynEdit::doUndo()
@ -4330,9 +4330,8 @@ void SynEdit::setSelTextPrimitiveEx(SynSelectionMode PasteMode, const QString &V
internalSetCaretY(1); internalSetCaretY(1);
} }
void SynEdit::setSelText(const QString &Value) void SynEdit::doSetSelText(const QString &Value)
{ {
QMutexLocker locker(&mMutex);
mUndoList->BeginBlock(); mUndoList->BeginBlock();
auto action = finally([this]{ auto action = finally([this]{
mUndoList->EndBlock(); mUndoList->EndBlock();
@ -4494,7 +4493,7 @@ int SynEdit::searchReplace(const QString &sSearch, const QString &sReplace, SynS
mUndoList->BeginBlock(); mUndoList->BeginBlock();
dobatchReplace = true; dobatchReplace = true;
} }
setSelText(replaceText); doSetSelText(replaceText);
nReplaceLen = caretX() - nFound; nReplaceLen = caretX() - nFound;
// fix the caret position and the remaining results // fix the caret position and the remaining results
if (!bBackward) { if (!bBackward) {
@ -5906,6 +5905,13 @@ void SynEdit::setSelLength(int Value)
} }
} }
void SynEdit::setSelText(const QString &text)
{
QMutexLocker locker(&mMutex);
doSetSelText(text);
}
BufferCoord SynEdit::blockBegin() const BufferCoord SynEdit::blockBegin() const
{ {
if ((mBlockEnd.Line < mBlockBegin.Line) if ((mBlockEnd.Line < mBlockBegin.Line)

View File

@ -221,8 +221,8 @@ public:
void uncollapseAroundLine(int line); void uncollapseAroundLine(int line);
PSynEditFoldRange foldHidesLine(int line); PSynEditFoldRange foldHidesLine(int line);
void setSelText(const QString& Value);
void setSelLength(int Value); void setSelLength(int Value);
void setSelText(const QString& text);
int searchReplace(const QString& sSearch, const QString& sReplace, SynSearchOptions options, int searchReplace(const QString& sSearch, const QString& sReplace, SynSearchOptions options,
PSynSearchBase searchEngine, SynSearchMathedProc matchedCallback = nullptr); PSynSearchBase searchEngine, SynSearchMathedProc matchedCallback = nullptr);
@ -249,7 +249,7 @@ public:
virtual void zoomOut() { commandProcessor(SynEditorCommand::ecZoomOut);} virtual void zoomOut() { commandProcessor(SynEditorCommand::ecZoomOut);}
virtual void selectAll() { commandProcessor(SynEditorCommand::ecSelectAll);} virtual void selectAll() { commandProcessor(SynEditorCommand::ecSelectAll);}
virtual void tab() { commandProcessor(SynEditorCommand::ecTab);} virtual void tab() { commandProcessor(SynEditorCommand::ecTab);}
virtual void untab() { commandProcessor(SynEditorCommand::ecShiftTab);} virtual void shifttab() { commandProcessor(SynEditorCommand::ecShiftTab);}
virtual void toggleComment() { commandProcessor(SynEditorCommand::ecToggleComment);} virtual void toggleComment() { commandProcessor(SynEditorCommand::ecToggleComment);}
virtual void beginUpdate(); virtual void beginUpdate();
@ -432,6 +432,8 @@ private:
QRect clientRect(); QRect clientRect();
void synFontChanged(); void synFontChanged();
void doOnPaintTransient(SynTransientType TransientType); void doOnPaintTransient(SynTransientType TransientType);
void doSetSelText(const QString& Value);
void updateLastCaretX(); void updateLastCaretX();
void ensureCursorPosVisible(); void ensureCursorPosVisible();
void ensureCursorPosVisibleEx(bool ForceToMiddle); void ensureCursorPosVisibleEx(bool ForceToMiddle);
@ -675,7 +677,7 @@ private:
QString mInputPreeditString; QString mInputPreeditString;
QRecursiveMutex mMutex; QMutex mMutex;
friend class SynEditTextPainter; friend class SynEditTextPainter;