Commit 51a33a9f authored by Roberto Raggi's avatar Roberto Raggi

Create completion items for the builtin snippets

parent 72a9a9fc
......@@ -134,6 +134,17 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
}
}
// snippets completion
TextEditor::CompletionItem item(this);
item.text = QLatin1String("Rectangle - declaration");
item.data = QVariant::fromValue(QString("Rectangle {\nwidth: $100$;\nheight: 100;\n$$\n}"));
m_completions.append(item);
item.text = QLatin1String("Item - declaration");
item.data = QVariant::fromValue(QString("Item {\nwidth: $100$;\nheight: 100;\n$$\n}"));
m_completions.append(item);
return pos;
}
......@@ -188,7 +199,23 @@ void QmlCodeCompletion::completions(QList<TextEditor::CompletionItem> *completio
void QmlCodeCompletion::complete(const TextEditor::CompletionItem &item)
{
const QString toInsert = item.text;
QString toInsert = item.text;
if (QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget())) {
if (item.data.isValid()) {
QTextCursor tc = edit->textCursor();
tc.beginEditBlock();
tc.setPosition(m_startPosition);
tc.setPosition(m_editor->position(), QTextCursor::KeepAnchor);
tc.removeSelectedText();
toInsert = item.data.toString();
edit->insertCodeSnippet(toInsert);
tc.endEditBlock();
return;
}
}
const int length = m_editor->position() - m_startPosition;
m_editor->setCurPos(m_startPosition);
m_editor->replace(length, toInsert);
......
......@@ -1246,7 +1246,7 @@ skip_event:
delete e;
}
void BaseTextEditor::universalHelper()
void BaseTextEditor::insertCodeSnippet(const QString &snippet)
{
QList<QTextEdit::ExtraSelection> selections;
......@@ -1254,8 +1254,6 @@ void BaseTextEditor::universalHelper()
const int startCursorPosition = cursor.position();
cursor.beginEditBlock();
const QString snippet = QLatin1String("for ($initializer$; $condition$; $expresssion$) {\n$$\n}\n");
if ((snippet.count('$') % 2) != 0) {
qWarning() << "invalid snippet";
return;
......@@ -1326,6 +1324,12 @@ void BaseTextEditor::universalHelper()
}
}
void BaseTextEditor::universalHelper()
{
const QString snippet = QLatin1String("for ($initializer$; $condition$; $expresssion$) {\n$$\n}\n");
insertCodeSnippet(snippet);
}
void BaseTextEditor::setTextCursor(const QTextCursor &cursor)
{
// workaround for QTextControl bug
......
......@@ -379,6 +379,8 @@ public:
void setTextCursor(const QTextCursor &cursor);
void insertCodeSnippet(const QString &snippet);
public slots:
void setDisplayName(const QString &title);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment