|
|
@@ -18,7 +18,8 @@ QVector<QChar> VSnippet::s_allShortcuts;
|
|
|
|
|
|
VSnippet::VSnippet()
|
|
|
: m_type(Type::PlainText),
|
|
|
- m_cursorMark(c_defaultCursorMark)
|
|
|
+ m_cursorMark(c_defaultCursorMark),
|
|
|
+ m_autoIndent(false)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
@@ -27,13 +28,15 @@ VSnippet::VSnippet(const QString &p_name,
|
|
|
const QString &p_content,
|
|
|
const QString &p_cursorMark,
|
|
|
const QString &p_selectionMark,
|
|
|
- QChar p_shortcut)
|
|
|
+ QChar p_shortcut,
|
|
|
+ bool p_autoIndent)
|
|
|
: m_name(p_name),
|
|
|
m_type(p_type),
|
|
|
m_content(p_content),
|
|
|
m_cursorMark(p_cursorMark),
|
|
|
m_selectionMark(p_selectionMark),
|
|
|
- m_shortcut(p_shortcut)
|
|
|
+ m_shortcut(p_shortcut),
|
|
|
+ m_autoIndent(p_autoIndent)
|
|
|
{
|
|
|
Q_ASSERT(m_selectionMark != m_cursorMark);
|
|
|
}
|
|
|
@@ -43,7 +46,8 @@ bool VSnippet::update(const QString &p_name,
|
|
|
const QString &p_content,
|
|
|
const QString &p_cursorMark,
|
|
|
const QString &p_selectionMark,
|
|
|
- QChar p_shortcut)
|
|
|
+ QChar p_shortcut,
|
|
|
+ bool p_autoIndent)
|
|
|
{
|
|
|
bool updated = false;
|
|
|
if (m_name != p_name) {
|
|
|
@@ -76,6 +80,11 @@ bool VSnippet::update(const QString &p_name,
|
|
|
updated = true;
|
|
|
}
|
|
|
|
|
|
+ if (m_autoIndent != p_autoIndent) {
|
|
|
+ m_autoIndent = p_autoIndent;
|
|
|
+ updated = true;
|
|
|
+ }
|
|
|
+
|
|
|
qDebug() << "snippet" << m_name << "updated" << updated;
|
|
|
|
|
|
return updated;
|
|
|
@@ -103,6 +112,7 @@ QJsonObject VSnippet::toJson() const
|
|
|
snip[SnippetConfig::c_cursorMark] = m_cursorMark;
|
|
|
snip[SnippetConfig::c_selectionMark] = m_selectionMark;
|
|
|
snip[SnippetConfig::c_shortcut] = m_shortcut.isNull() ? "" : QString(m_shortcut);
|
|
|
+ snip[SnippetConfig::c_autoIndent] = m_autoIndent;
|
|
|
|
|
|
return snip;
|
|
|
}
|
|
|
@@ -120,7 +130,8 @@ VSnippet VSnippet::fromJson(const QJsonObject &p_json)
|
|
|
"",
|
|
|
p_json[SnippetConfig::c_cursorMark].toString(),
|
|
|
p_json[SnippetConfig::c_selectionMark].toString(),
|
|
|
- shortcut);
|
|
|
+ shortcut,
|
|
|
+ p_json[SnippetConfig::c_autoIndent].toBool());
|
|
|
|
|
|
return snip;
|
|
|
}
|
|
|
@@ -193,11 +204,15 @@ bool VSnippet::apply(QTextCursor &p_cursor) const
|
|
|
|
|
|
content += secondPart;
|
|
|
|
|
|
+ // Auto indent.
|
|
|
+ QTextBlock startBlock = p_cursor.block();
|
|
|
+ QTextBlock endBlock;
|
|
|
+ QString indentation = VEditUtils::fetchIndentSpaces(startBlock);
|
|
|
+
|
|
|
// Insert it.
|
|
|
switch (m_type) {
|
|
|
case Type::Html:
|
|
|
p_cursor.insertHtml(content);
|
|
|
- // TODO: set the position of the cursor.
|
|
|
break;
|
|
|
|
|
|
case Type::PlainText:
|
|
|
@@ -205,10 +220,34 @@ bool VSnippet::apply(QTextCursor &p_cursor) const
|
|
|
|
|
|
default:
|
|
|
p_cursor.insertText(content);
|
|
|
- p_cursor.setPosition(pos);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
+ endBlock = p_cursor.block();
|
|
|
+
|
|
|
+ if (m_autoIndent
|
|
|
+ && startBlock != endBlock
|
|
|
+ && !indentation.isEmpty()) {
|
|
|
+ // Indent (startBlock, endBlock].
|
|
|
+ startBlock = startBlock.next();
|
|
|
+ while (startBlock.isValid()) {
|
|
|
+ int tmpPos = startBlock.position();
|
|
|
+ p_cursor.setPosition(tmpPos);
|
|
|
+ VEditUtils::indentBlock(p_cursor, indentation, false);
|
|
|
+ if (tmpPos <= pos) {
|
|
|
+ pos += indentation.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (startBlock == endBlock) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ startBlock = startBlock.next();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ p_cursor.setPosition(pos);
|
|
|
+
|
|
|
p_cursor.endEditBlock();
|
|
|
return true;
|
|
|
}
|