Skip to content

Commit

Permalink
Fix for #3425. Keep the indentation while copying.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Dec 5, 2015
1 parent a619ca6 commit 6101972
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
37 changes: 36 additions & 1 deletion OMEdit/OMEditGUI/Editors/BaseEditor.cpp
Expand Up @@ -168,6 +168,19 @@ int TabSettings::spacesLeftFromPosition(const QString &text, int position)
return position - i;
}

/*!
* \brief TabSettings::cursorIsAtBeginningOfLine
* Returns true if cursor is at beginning of line.
* \param cursor
* \return
*/
bool TabSettings::cursorIsAtBeginningOfLine(const QTextCursor &cursor)
{
QString text = cursor.block().text();
int fns = firstNonSpace(text);
return (cursor.position() - cursor.block().position() <= fns);
}

TextBlockUserData::~TextBlockUserData()
{
TextMarks marks = _marks;
Expand Down Expand Up @@ -996,9 +1009,31 @@ QMimeData* BaseEditor::PlainTextEdit::createMimeDataFromSelection() const
// Make sure the text appears pre-formatted
tempCursor.setPosition(0);
tempCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
QTextBlockFormat blockFormat = tempCursor.blockFormat();
blockFormat.setNonBreakableLines(true);
tempCursor.setBlockFormat(blockFormat);
mimeData->setHtml(tempCursor.selection().toHtml());
mimeData->setData(QLatin1String("application/OMEdit.modelica-text"), text.toUtf8());
delete tempDocument;

// Try to figure out whether we are copying an entire block, and store the complete block including indentation
QTextCursor selectedStartCursor = cursor;
selectedStartCursor.setPosition(cursor.selectionStart());
QTextCursor selectedEndCursor = cursor;
selectedEndCursor.setPosition(cursor.selectionEnd());

bool startOk = TabSettings::cursorIsAtBeginningOfLine(selectedStartCursor);
bool multipleBlocks = (selectedEndCursor.block() != selectedStartCursor.block());

if (startOk && multipleBlocks) {
selectedStartCursor.movePosition(QTextCursor::StartOfBlock);
if (TabSettings::cursorIsAtBeginningOfLine(selectedEndCursor)) {
selectedEndCursor.movePosition(QTextCursor::StartOfBlock);
}
cursor.setPosition(selectedStartCursor.position());
cursor.setPosition(selectedEndCursor.position(), QTextCursor::KeepAnchor);
text = plainTextFromSelection(cursor);
mimeData->setData(QLatin1String("application/OMEdit.modelica-text"), text.toUtf8());
}
return mimeData;
}
return 0;
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Editors/BaseEditor.h
Expand Up @@ -68,6 +68,7 @@ class TabSettings

static int firstNonSpace(const QString &text);
static int spacesLeftFromPosition(const QString &text, int position);
static bool cursorIsAtBeginningOfLine(const QTextCursor &cursor);
private:
TabPolicy mTabPolicy;
int mTabSize;
Expand Down

0 comments on commit 6101972

Please sign in to comment.