From f7a6db70052298a7f045d8ae3338019343be5b31 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 31 Mar 2022 19:36:46 +0800 Subject: [PATCH] - change: only auto complete symbol '(' when at line end, or there are spaces or right ')' '}' ']'after it - fix: mouse drag may fail when start drag at the right half of the selection's last character --- NEWS.md | 2 ++ RedPandaIDE/editor.cpp | 12 +++++++++++- RedPandaIDE/qsynedit/SynEdit.cpp | 27 ++++++++++++++++++--------- RedPandaIDE/qsynedit/SynEdit.h | 1 + 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2b3dea98..03179dad 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,6 +20,8 @@ Red Panda C++ Version 1.0.2 - fix: vector vars can't be expanded in the watch panel - change: use qt's mingw 8.1 (32bit) and 11.2 (64bit) in distributions, to provide better compatibility with simplified chinese windows. - fix: crash when rename an openned file, and choose "no" when ask if keep the editor open + - change: only auto complete symbol '(' when at line end, or there are spaces or right ')' '}' ']'after it + - fix: mouse drag may fail when start drag at the right half of the selection's last character Red Panda C++ Version 1.0.1 - fix: only convert project icon file when it's filename doesn't end with ".ico" diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 97271a3d..956028b2 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -554,6 +554,10 @@ void Editor::focusOutEvent(QFocusEvent *event) pMainWindow->functionTip()->hide(); } +static bool isSpaceOrRightParenthesis(const QChar& ch) { + return ch.isSpace() || ch==')' || ch=="]" || ch=="}"; +} + void Editor::keyPressEvent(QKeyEvent *event) { bool handled = false; @@ -784,7 +788,6 @@ void Editor::keyPressEvent(QKeyEvent *event) switch (ch.unicode()) { case '"': case '\'': - case '(': case ')': case '{': case '}': @@ -794,6 +797,13 @@ void Editor::keyPressEvent(QKeyEvent *event) case '*': handled = handleSymbolCompletion(ch); return; + case '(': + if (caretX()-1>=lineText().length() + || caretX()<=0 + || isSpaceOrRightParenthesis(lineText().at(caretX()-1))) { + handled = handleSymbolCompletion(ch); + } + return; case '>': if ((caretX() <= 1) || lineText().isEmpty() || lineText()[caretX() - 2] != '-') { diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 948b8952..f9ef6afb 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -608,7 +608,7 @@ bool SynEdit::pointToCharLine(const QPoint &point, BufferCoord &coord) return false; } - coord = displayToBufferPos(pixelsToRowColumn(point.x(),point.y())); + coord = displayToBufferPos(pixelsToNearestRowColumn(point.x(),point.y())); return true; } @@ -622,7 +622,7 @@ bool SynEdit::pointToLine(const QPoint &point, int &line) return false; } - BufferCoord coord = displayToBufferPos(pixelsToRowColumn(point.x(),point.y())); + BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(point.x(),point.y())); line = coord.Line; return true; } @@ -684,7 +684,7 @@ void SynEdit::invalidateGutterLines(int FirstLine, int LastLine) * @return */ -DisplayCoord SynEdit::pixelsToRowColumn(int aX, int aY) const +DisplayCoord SynEdit::pixelsToNearestRowColumn(int aX, int aY) const { return { std::max(1, (int)(mLeftChar + round((aX - mGutterWidth - 2.0) / mCharWidth))), @@ -692,6 +692,15 @@ DisplayCoord SynEdit::pixelsToRowColumn(int aX, int aY) const }; } +DisplayCoord SynEdit::pixelsToRowColumn(int aX, int aY) const +{ + return { + std::max(1, (int)(mLeftChar + (aX - mGutterWidth - 2.0) / mCharWidth)), + std::max(1, mTopLine + (aY / mTextHeight)) + }; + +} + QPoint SynEdit::rowColumnToPixels(const DisplayCoord &coord) const { QPoint result; @@ -1125,7 +1134,7 @@ void SynEdit::processGutterClick(QMouseEvent *event) { int X = event->pos().x(); int Y = event->pos().y(); - DisplayCoord RowColumn = pixelsToRowColumn(X, Y); + DisplayCoord RowColumn = pixelsToNearestRowColumn(X, Y); int Line = rowToLine(RowColumn.Row); // Check if we clicked on a folding thing @@ -1843,7 +1852,7 @@ void SynEdit::doMouseScroll(bool isDragging) iMousePos = QCursor::pos(); iMousePos = mapFromGlobal(iMousePos); - C = pixelsToRowColumn(iMousePos.x(), iMousePos.y()); + C = pixelsToNearestRowColumn(iMousePos.x(), iMousePos.y()); C.Row = minMax(C.Row, 1, displayLineCount()); if (mScrollDeltaX != 0) { setLeftChar(leftChar() + mScrollDeltaX * mMouseSelectionScrollSpeed); @@ -2576,7 +2585,7 @@ void SynEdit::computeCaret() int X=iMousePos.x(); int Y=iMousePos.y(); - DisplayCoord vCaretNearestPos = pixelsToRowColumn(X, Y); + DisplayCoord vCaretNearestPos = pixelsToNearestRowColumn(X, Y); vCaretNearestPos.Row = minMax(vCaretNearestPos.Row, 1, displayLineCount()); setInternalDisplayXY(vCaretNearestPos); } @@ -6288,7 +6297,7 @@ void SynEdit::dragEnterEvent(QDragEnterEvent *event) mDragCaretSave = caretXY(); mDragSelBeginSave = blockBegin(); mDragSelEndSave = blockEnd(); - BufferCoord coord = displayToBufferPos(pixelsToRowColumn(event->pos().x(), + BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(event->pos().x(), event->pos().y())); internalSetCaretXY(coord); setBlockBegin(mDragSelBeginSave); @@ -6302,7 +6311,7 @@ void SynEdit::dropEvent(QDropEvent *event) { //mScrollTimer->stop(); - BufferCoord coord = displayToBufferPos(pixelsToRowColumn(event->pos().x(), + BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(event->pos().x(), event->pos().y())); setCaretXY(coord); if (coord>=mDragSelBeginSave && coord<=mDragSelEndSave) { @@ -6371,7 +6380,7 @@ void SynEdit::dragMoveEvent(QDragMoveEvent *event) iMousePos = mapFromGlobal(iMousePos); int X=iMousePos.x(); int Y=iMousePos.y(); - BufferCoord coord = displayToBufferPos(pixelsToRowColumn(X,Y)); + BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(X,Y)); internalSetCaretXY(coord); setBlockBegin(mDragSelBeginSave); setBlockEnd(mDragSelEndSave); diff --git a/RedPandaIDE/qsynedit/SynEdit.h b/RedPandaIDE/qsynedit/SynEdit.h index 7efd6d25..b6a41671 100644 --- a/RedPandaIDE/qsynedit/SynEdit.h +++ b/RedPandaIDE/qsynedit/SynEdit.h @@ -177,6 +177,7 @@ public: void invalidateGutter(); void invalidateGutterLine(int aLine); void invalidateGutterLines(int FirstLine, int LastLine); + DisplayCoord pixelsToNearestRowColumn(int aX, int aY) const; DisplayCoord pixelsToRowColumn(int aX, int aY) const; QPoint rowColumnToPixels(const DisplayCoord& coord) const; DisplayCoord bufferToDisplayPos(const BufferCoord& p) const;