forked from qt-creator/qt-creator
Create completion items for the builtin snippets
This commit is contained in:
@@ -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;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +199,23 @@ void QmlCodeCompletion::completions(QList<TextEditor::CompletionItem> *completio
|
|||||||
|
|
||||||
void QmlCodeCompletion::complete(const TextEditor::CompletionItem &item)
|
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;
|
const int length = m_editor->position() - m_startPosition;
|
||||||
m_editor->setCurPos(m_startPosition);
|
m_editor->setCurPos(m_startPosition);
|
||||||
m_editor->replace(length, toInsert);
|
m_editor->replace(length, toInsert);
|
||||||
|
|||||||
@@ -1246,7 +1246,7 @@ skip_event:
|
|||||||
delete e;
|
delete e;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseTextEditor::universalHelper()
|
void BaseTextEditor::insertCodeSnippet(const QString &snippet)
|
||||||
{
|
{
|
||||||
QList<QTextEdit::ExtraSelection> selections;
|
QList<QTextEdit::ExtraSelection> selections;
|
||||||
|
|
||||||
@@ -1254,8 +1254,6 @@ void BaseTextEditor::universalHelper()
|
|||||||
const int startCursorPosition = cursor.position();
|
const int startCursorPosition = cursor.position();
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
|
|
||||||
const QString snippet = QLatin1String("for ($initializer$; $condition$; $expresssion$) {\n$$\n}\n");
|
|
||||||
|
|
||||||
if ((snippet.count('$') % 2) != 0) {
|
if ((snippet.count('$') % 2) != 0) {
|
||||||
qWarning() << "invalid snippet";
|
qWarning() << "invalid snippet";
|
||||||
return;
|
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)
|
void BaseTextEditor::setTextCursor(const QTextCursor &cursor)
|
||||||
{
|
{
|
||||||
// workaround for QTextControl bug
|
// workaround for QTextControl bug
|
||||||
|
|||||||
@@ -379,6 +379,8 @@ public:
|
|||||||
|
|
||||||
void setTextCursor(const QTextCursor &cursor);
|
void setTextCursor(const QTextCursor &cursor);
|
||||||
|
|
||||||
|
void insertCodeSnippet(const QString &snippet);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setDisplayName(const QString &title);
|
void setDisplayName(const QString &title);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user