Skip to content

Commit

Permalink
Github Issue #92: TextCursor is caculated for links as well. New para…
Browse files Browse the repository at this point in the history
…mter called 'isCursorCalculated' is created. Since the cursor is now calculated using blocknumber, empty lines even though they have block number don't appear in the loop which iterates through all blocks. There it was difficult to set the cursor to empty lines. This parameter is checked to see if the cursor is calculated using blocknumber and if yes then set the textcursor or else just leave it as it is.
  • Loading branch information
AngryFender committed Aug 3, 2023
1 parent 18adb1d commit 7e9c9c6
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 3 deletions.
217 changes: 217 additions & 0 deletions CatchTests/MkEdit_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,220 @@ TEST_CASE("MkEdit bold double underscore", "[MkEdit]")
REQUIRE("__abc__" == text);
}

TEST_CASE("MkEdit move cursor to the middle of the characters of first Markdown word", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 2; // '**'
int cursorPosition = 2; // bo
int newLinePos = 26;

doc.setPlainText("**bold** _italic_ \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("bold italic \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("**bold** _italic_ \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

TEST_CASE("MkEdit move cursor to the middle of the characters of second Markdown word", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 2+2+1; // '**' + '**' + '_'
int cursorPosition = 8; // bold ital
int newLinePos = 26;

doc.setPlainText("**bold** _italic_ \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("bold italic \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("**bold** _italic_ \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

TEST_CASE("MkEdit move cursor to the middle of the characters of third Markdown word", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 2+2+1+1+2; // '**' + '**' + '_' + '_' + '~~'
int cursorPosition = 16; // bold italic stri
int newLinePos = 36;

doc.setPlainText("**bold** _italic_ ~~strike~~ \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("bold italic strike \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("**bold** _italic_ ~~strike~~ \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

TEST_CASE("MkEdit move cursor to the middle of the characters of fourth Markdown word which is link", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 2+2+1+1+2+2+1; // '**' + '**' + '_' + '_' + '~~' + '~~' + '['
int cursorPosition = 22; // bold italic strike goo
int newLinePos = 71;

doc.setPlainText("**bold** _italic_ ~~strike~~ [google](https://www.google.com/) \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("bold italic strike google \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("**bold** _italic_ ~~strike~~ [google](https://www.google.com/) \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

TEST_CASE("MkEdit move cursor to the middle of the characters of fifth Markdown word which is another link", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 2+2+1+1+2+2+1+26+1; // '**' + '**' + '_' + '_' + '~~' + '~~' + '[' + '](https://www.google.com/)' + '['
int cursorPosition = 32; // bold italic strike google youtub
int newLinePos = 106;

doc.setPlainText("**bold** _italic_ ~~strike~~ [google](https://www.google.com/) [youtube](https://www.youtube.com/) \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("bold italic strike google youtube \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("**bold** _italic_ ~~strike~~ [google](https://www.google.com/) [youtube](https://www.youtube.com/) \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

TEST_CASE("MkEdit move cursor to the middle of the characters of 6 word where first 5 are bold", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 4*5; // '**' + '**' *5
int cursorPosition = 28; // google google goo
int newLinePos = 59;

doc.setPlainText("**bold** **bold** **bold** **bold** **bold** hello \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("bold bold bold bold bold hello \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("**bold** **bold** **bold** **bold** **bold** hello \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

TEST_CASE("MkEdit move cursor to the middle of the characters of 6 word where first 5 are links", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 1+26+1+26+1; // '[' + '](https://www.google.com/)' + '[' + '](https://www.google.com/)' + '['
int cursorPosition = 17; // google google goo
int newLinePos = 185;

doc.setPlainText("[google](https://www.google.com/) [google](https://www.google.com/) [google](https://www.google.com/) [google](https://www.google.com/) [google](https://www.google.com/) hello \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("google google google google google hello \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("[google](https://www.google.com/) [google](https://www.google.com/) [google](https://www.google.com/) [google](https://www.google.com/) [google](https://www.google.com/) hello \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

10 changes: 8 additions & 2 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MkEdit::MkEdit(QWidget *parent):QTextEdit(parent){

fileSaveTimer.setInterval(FILE_SAVE_TIMEOUT);
regexUrl.setPattern("(https?|ftp|file)://[\\w\\d._-]+(?:\\.[\\w\\d._-]+)+[\\w\\d._-]*(?:(?:/[\\w\\d._-]+)*/?)?(?:\\?[\\w\\d_=-]+(?:&[\\w\\d_=-]+)*)?(?:#[\\w\\d_-]+)?");
savedBlockNumber= -1;

this->setContextMenuPolicy(Qt::CustomContextMenu);
undoAction.setText("Undo Ctrl+Z");
Expand Down Expand Up @@ -606,15 +607,20 @@ void MkEdit::cursorPositionChangedHandle()
selectRange.end = -1;
selectRange.currentposInBlock = cursor.positionInBlock();
selectRange.currentBlockPos = cursor.block().position();
selectRange.isCursorCaculated = false;
}else{
selectRange.start = cursor.selectionStart();
selectRange.end = cursor.selectionEnd();
}



emit cursorPosChanged( textCursor().hasSelection(), currentBlockNumber, getVisibleRect(), &selectRange);

if(!cursor.hasSelection()){
cursor.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor,selectRange.currentBlockPos);
if(!cursor.hasSelection() && selectRange.isCursorCaculated){
cursor.setPosition(this->document()->findBlockByNumber(selectRange.currentBlockNo).position());
cursor.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);

for(int rep = 0; rep < selectRange.currentposInBlock; rep++){
cursor.movePosition(QTextCursor::NextCharacter,QTextCursor::MoveAnchor);
}
Expand Down
15 changes: 14 additions & 1 deletion mktextdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void MkTextDocument::showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatDa
{
QString textBlock = block.text();
const int blockPos = block.position();
int index = 0;
int index = 0;
for(QString::Iterator cp = textBlock.begin(); cp != textBlock.end(); cp++){
if(*cp == u'' || *cp == u''){
checkMarkPositions.removeAll(blockPos + index);
Expand Down Expand Up @@ -536,13 +536,26 @@ void MkTextDocument::showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatDa

if((*it)->getSymbol() == LINK_SYMBOL_URL_START){
int pos = (*it)->getPos();

if(selectRange){
if(pos< selectRange->currentposInBlock){
selectRange->currentposInBlock = selectRange->currentposInBlock+formatData->getLinkUrl(pos).length();
}
}

textBlock.insert(pos+1,formatData->getLinkUrl(pos));
}
}
QTextCursor cursor(block);
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.movePosition(QTextCursor::EndOfBlock,QTextCursor::KeepAnchor);
cursor.insertText(textBlock);
cursor.movePosition(QTextCursor::StartOfBlock);

if((selectRange)){
selectRange->isCursorCaculated = true;
selectRange->currentBlockNo = block.blockNumber();
}
}

void MkTextDocument::showSymbolsAtPos(QString &text, int pos, const QString &symbol)
Expand Down
2 changes: 2 additions & 0 deletions mktextdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ struct SelectRange{
int end = NO_SELECTION_POS;
int currentposInBlock = NO_SELECTION_POS;
int currentBlockPos = NO_SELECTION_POS;
int currentBlockNo = NO_SELECTION_POS;
int blockStart = NO_SELECTION_POS;
int posInBlockStart = NO_SELECTION_POS;
int blockEnd = NO_SELECTION_POS;
int posInBlockEnd = NO_SELECTION_POS;
bool isCursorCaculated = false;
};

class MkTextDocument : public QTextDocument
Expand Down

0 comments on commit 7e9c9c6

Please sign in to comment.