Commit 18b675d3 authored by Yuchen Deng's avatar Yuchen Deng Committed by Leandro Melo

Double-click to select current block

This is a Eclipse-like feature, for select enclosing element.
e.g.
int test() {| // Here is the cursor position
    ...
}
When Double-click, we can select the block: {...}
I think this is a useful feature.

Change-Id: I4ca7ed04056176195d1622714effda9079ae0e44
Reviewed-by: default avatarLeandro Melo <leandro.melo@nokia.com>
parent 05d8aeae
......@@ -201,7 +201,7 @@ TextBlockUserData::MatchType TextBlockUserData::checkClosedParenthesis(QTextCurs
}
}
bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool select)
bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool select, bool onlyInCurrentBlock)
{
QTextBlock block = cursor->block();
int position = cursor->position();
......@@ -224,6 +224,8 @@ bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool se
}
}
}
if (onlyInCurrentBlock)
return false;
block = block.previous();
}
return false;
......
......@@ -104,7 +104,8 @@ public:
static MatchType checkClosedParenthesis(QTextCursor *cursor, QChar c);
static MatchType matchCursorBackward(QTextCursor *cursor);
static MatchType matchCursorForward(QTextCursor *cursor);
static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false);
static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false,
bool onlyInCurrentBlock = false);
static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false);
static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false);
......
......@@ -865,7 +865,7 @@ static QTextCursor flippedCursor(const QTextCursor &cursor)
return flipped;
}
void BaseTextEditorWidget::selectBlockUp()
bool BaseTextEditorWidget::selectBlockUp()
{
QTextCursor cursor = textCursor();
if (!cursor.hasSelection())
......@@ -873,22 +873,23 @@ void BaseTextEditorWidget::selectBlockUp()
else
cursor.setPosition(cursor.selectionStart());
if (!TextBlockUserData::findPreviousOpenParenthesis(&cursor, false))
return;
return false;
if (!TextBlockUserData::findNextClosingParenthesis(&cursor, true))
return;
return false;
setTextCursor(flippedCursor(cursor));
_q_matchParentheses();
return true;
}
void BaseTextEditorWidget::selectBlockDown()
bool BaseTextEditorWidget::selectBlockDown()
{
QTextCursor tc = textCursor();
QTextCursor cursor = d->m_selectBlockAnchor;
if (!tc.hasSelection() || cursor.isNull())
return;
return false;
tc.setPosition(tc.selectionStart());
forever {
......@@ -904,6 +905,7 @@ void BaseTextEditorWidget::selectBlockDown()
setTextCursor(flippedCursor(cursor));
_q_matchParentheses();
return true;
}
void BaseTextEditorWidget::copyLineUp()
......@@ -4214,6 +4216,18 @@ void BaseTextEditorWidget::mouseReleaseEvent(QMouseEvent *e)
QPlainTextEdit::mouseReleaseEvent(e);
}
void BaseTextEditorWidget::mouseDoubleClickEvent(QMouseEvent *e)
{
QTextCursor cursor = textCursor();
const int position = cursor.position();
if (TextBlockUserData::findPreviousOpenParenthesis(&cursor, false, true)) {
if (position - cursor.position() == 1 && selectBlockUp())
return;
}
QPlainTextEdit::mouseDoubleClickEvent(e);
}
void BaseTextEditorWidget::leaveEvent(QEvent *e)
{
// Clear link emulation when the mouse leaves the editor
......
......@@ -298,8 +298,8 @@ public slots:
void gotoNextWordCamelCase();
void gotoNextWordCamelCaseWithSelection();
void selectBlockUp();
void selectBlockDown();
bool selectBlockUp();
bool selectBlockDown();
void moveLineUp();
void moveLineDown();
......@@ -454,6 +454,7 @@ protected:
void mouseMoveEvent(QMouseEvent *);
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void mouseDoubleClickEvent(QMouseEvent *);
void leaveEvent(QEvent *);
void keyReleaseEvent(QKeyEvent *);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment