Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions modules/editor/texteditor/editor/codehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,34 @@ bool CodeHandler::keyPress(QKeyEvent *event) {
if(event == QKeySequence::InsertParagraphSeparator) {
cursor.beginEditBlock();

QString text = cursor.block().text();
QString text;

int32_t indentRemain = firstNonIndent(cursor.block().text());
QTextBlock block = cursor.block();
while(block.isValid() && text.trimmed().isEmpty()) {
text = block.text();
block = block.previous();
}

int32_t indentRemain = firstNonIndent(text);
if(m_widget->isFoldable(cursor.block())) {
indentRemain += (m_widget->useSpaces()) ? m_widget->spaceIndent() : 1;
indentRemain += m_widget->useSpaces() ? m_widget->spaceIndent() : 1;
}

cursor.insertBlock();
text.fill((m_widget->useSpaces()) ? ' ' : '\t', indentRemain);
if(m_widget->useSpaces()) {
text.fill(' ', indentRemain * m_widget->spaceIndent());
} else {
text.fill('\t', indentRemain);
}
cursor.insertText(text);

cursor.endEditBlock();

event->accept();
return true;
} else if(event == QKeySequence::MoveToStartOfLine || event == QKeySequence::SelectStartOfLine) {
int32_t pos = cursor.block().position() + firstNonIndent(cursor.block().text());
int32_t indentRemain = firstNonIndent(cursor.block().text()) * (m_widget->useSpaces() ? m_widget->spaceIndent() : 1);
int32_t pos = cursor.block().position() + indentRemain;
if(cursor.position() > pos) {
cursor.setPosition(pos, event == QKeySequence::SelectStartOfLine ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);

Expand Down Expand Up @@ -607,9 +618,15 @@ void CodeHandler::insertIntoBlockSelection(const QString &text) {

int32_t CodeHandler::firstNonIndent(const QString &text) const {
int32_t i = 0;
int32_t spaceIndent = std::max(m_widget->spaceIndent(), 1);
bool isSpace = false;
while(i < text.size()) {
if(text.at(i) != ' ' && text.at(i) != '\t') {
return i;
if(text.at(i) != ' ') {
if(text.at(i) != '\t') {
return i / (isSpace ? spaceIndent : 1);
}
} else {
isSpace = true;
}
++i;
}
Expand All @@ -629,9 +646,8 @@ void CodeHandler::setCursorToColumn(QTextCursor &cursor, int column, QTextCursor

if(offset < 0) {
cursor.setPosition(cursor.block().position() + pos - 1, QTextCursor::KeepAnchor);
cursor.insertText(indentationString(
m_widget->column(cursor.block().text(), pos - 1),
m_widget->column(cursor.block().text(), pos), 0, cursor.block()));
cursor.insertText(indentationString( m_widget->column(cursor.block().text(), pos - 1),
m_widget->column(cursor.block().text(), pos), 0, cursor.block()));
} else {
cursor.insertText(indentationString(m_widget->column(cursor.block().text(), pos), column, 0, cursor.block()));
}
Expand Down