forked from qt-creator/qt-creator
Editor: multi cursor support
Adding a way to create multiple cursors that can insert/remove text at arbitrary positions in the document. Adding cursors is done by pressing alt + up/down or by clicking into the editor while holding the alt key. Fixes: QTCREATORBUG-16013 Change-Id: I495d27d95a3d277220946616ef30efc241da0120 Reviewed-by: Christian Stenger <christian.stenger@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Artem Sokolovskii <artem.sokolovskii@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -24,10 +24,14 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "uncommentselection.h"
|
||||
|
||||
#include "qtcassert.h"
|
||||
#include "utils/multitextcursor.h"
|
||||
|
||||
#include <QPlainTextEdit>
|
||||
#include <QTextBlock>
|
||||
|
||||
using namespace Utils;
|
||||
namespace Utils {
|
||||
|
||||
CommentDefinition CommentDefinition::CppStyle = CommentDefinition("//", "/*", "*/");
|
||||
CommentDefinition CommentDefinition::HashStyle = CommentDefinition("#");
|
||||
@@ -74,9 +78,9 @@ static bool isComment(const QString &text, int index,
|
||||
}
|
||||
|
||||
|
||||
QTextCursor Utils::unCommentSelection(const QTextCursor &cursorIn,
|
||||
const CommentDefinition &definition,
|
||||
bool preferSingleLine)
|
||||
QTextCursor unCommentSelection(const QTextCursor &cursorIn,
|
||||
const CommentDefinition &definition,
|
||||
bool preferSingleLine)
|
||||
{
|
||||
if (!definition.isValid())
|
||||
return cursorIn;
|
||||
@@ -244,3 +248,31 @@ QTextCursor Utils::unCommentSelection(const QTextCursor &cursorIn,
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
MultiTextCursor unCommentSelection(const MultiTextCursor &cursorIn,
|
||||
const CommentDefinition &definiton,
|
||||
bool preferSingleLine)
|
||||
{
|
||||
if (cursorIn.isNull())
|
||||
return cursorIn;
|
||||
if (!cursorIn.hasMultipleCursors())
|
||||
return MultiTextCursor({unCommentSelection(cursorIn.mainCursor(), definiton, preferSingleLine)});
|
||||
QMap<int, QTextCursor> cursors;
|
||||
for (const QTextCursor &c : cursorIn) {
|
||||
QTextBlock block = c.document()->findBlock(c.selectionStart());
|
||||
QTC_ASSERT(block.isValid(), continue);
|
||||
QTextBlock end = c.document()->findBlock(c.selectionEnd());
|
||||
QTC_ASSERT(end.isValid(), continue);
|
||||
end = end.next();
|
||||
while (block != end && block.isValid()) {
|
||||
if (!cursors.contains(block.blockNumber()))
|
||||
cursors.insert(block.blockNumber(), QTextCursor(block));
|
||||
block = block.next();
|
||||
}
|
||||
}
|
||||
for (const QTextCursor &c : cursors)
|
||||
unCommentSelection(c, definiton, /*always prefer single line for multi cursor*/ true);
|
||||
return cursorIn;
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
|
||||
Reference in New Issue
Block a user