Skip to content

Commit

Permalink
pbek/QOwnNotes#719 Entering a bracket-character should surround the c…
Browse files Browse the repository at this point in the history
…urrently selected text.
  • Loading branch information
sanderboom committed Sep 18, 2017
1 parent ca0b633 commit 67bb24f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion qmarkdowntextedit.cpp
Expand Up @@ -268,8 +268,31 @@ bool QMarkdownTextEdit::handleBracketClosing(QString openingCharacter,
closingCharacter = openingCharacter;
}

// Get the current text from the block (NOTE: inserted character not included).
QTextCursor c = textCursor();
QString selectedText = c.selectedText();

// When user currently has text selected, we prepend the openingCharacter
// and append the closingCharacter. E.g. 'text' -> '(text)'. We keep the
// current selectedText selected.
//
// TODO: how to make ctrl-z keep the selectedText selected?
if (selectedText != "") {
// Insert. The selectedText is overwritten.
c.insertText(openingCharacter);
c.insertText(selectedText);
c.insertText(closingCharacter);

// Re-select the selectedText.
int selectionEnd = c.position() - 1;
int selectionStart = selectionEnd - selectedText.length();
c.setPosition(selectionStart);
c.setPosition(selectionEnd, QTextCursor::KeepAnchor);
this->setTextCursor(c);

return true;
}

// Get the current text from the block (NOTE: inserted character not included).
QString text = c.block().text();

// Remove whitespace at start of string (e.g. in multilevel-lists).
Expand Down

0 comments on commit 67bb24f

Please sign in to comment.