Skip to content

Commit

Permalink
Github Issue #92: The textCursor position is saved before the Markdow…
Browse files Browse the repository at this point in the history
…n symbols are shown. After the Markdown symbols are shown, MkEdit is set with the saved textcursor position
  • Loading branch information
AngryFender committed Aug 2, 2023
1 parent f1bf38d commit 18adb1d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,21 @@ void MkEdit::cursorPositionChangedHandle()
if(!cursor.hasSelection()){
selectRange.start = -1;
selectRange.end = -1;
selectRange.currentposInBlock = cursor.positionInBlock();
selectRange.currentBlockPos = cursor.block().position();
}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);
for(int rep = 0; rep < selectRange.currentposInBlock; rep++){
cursor.movePosition(QTextCursor::NextCharacter,QTextCursor::MoveAnchor);
}
this->setTextCursor(cursor);
}
}
}
14 changes: 11 additions & 3 deletions mktextdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ void MkTextDocument::hideAllFormatSymbolsInTextBlock(QTextBlock &block, FormatDa
QString textBlock = block.text();
bool isLink = false;
int linkEnd = 0, linkStart = 0;
int linkTitleEnd = -1, linkTitleStart = -1;

const QString *symbol;
int symbolPos = -1;
Expand Down Expand Up @@ -501,7 +500,7 @@ void MkTextDocument::showSymbols(QTextBlock &block, const QString &symbol)

}

void MkTextDocument::showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatData *formatData)
void MkTextDocument::showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatData *formatData, SelectRange * selectRange)
{
QString textBlock = block.text();
const int blockPos = block.position();
Expand All @@ -524,6 +523,15 @@ void MkTextDocument::showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatDa

for(QVector<PositionData*>::Iterator it = formatData->pos_begin(); it < formatData->pos_end(); it++)
{
int cursorPos = (*it)->getPos();
int symbolLen = (*it)->getSymbol().length();

if(selectRange){
if(cursorPos< selectRange->currentposInBlock){
selectRange->currentposInBlock = selectRange->currentposInBlock+symbolLen;
}
}

showSymbolsAtPos(textBlock, (*it)->getPos(), (*it)->getSymbol());

if((*it)->getSymbol() == LINK_SYMBOL_URL_START){
Expand Down Expand Up @@ -895,7 +903,7 @@ void MkTextDocument::hideMKSymbolsFromDrawingRect(QRect rect, bool hasSelection,
if(formatData->isHidden()){
formatData->setHidden(false);
resetTextBlockFormat(block);
showAllFormatSymbolsInTextBlock(block, formatData);
showAllFormatSymbolsInTextBlock(block, formatData, editSelectRange);
}
QTextCursor cursor(block);
for(QVector<FragmentData*>::Iterator it = formatData->formats_begin(); it < formatData->formats_end(); it++)
Expand Down
4 changes: 3 additions & 1 deletion mktextdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class FormatCollection{
struct SelectRange{
int start = NO_SELECTION_POS;
int end = NO_SELECTION_POS;
int currentposInBlock = NO_SELECTION_POS;
int currentBlockPos = NO_SELECTION_POS;
int blockStart = NO_SELECTION_POS;
int posInBlockStart = NO_SELECTION_POS;
int blockEnd = NO_SELECTION_POS;
Expand Down Expand Up @@ -171,7 +173,7 @@ public slots:
void hideSymbolsAtPos(QString &text, int pos, const QString &symbol);

void showSymbols(QTextBlock &block,const QString &symbol);
void showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatData *formatData);
void showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatData *formatData, SelectRange * selectRange = nullptr);
void showSymbolsAtPos(QString &text, int pos, const QString &symbol);

void autoCompleteCodeBlock(int blockNumber,bool &success);
Expand Down

0 comments on commit 18adb1d

Please sign in to comment.