Commit 91dac938 authored by mae's avatar mae

Re-enable snippets

Fixed colors to use the same as refactoring rename, and a few
fixes for undo and document changes outside the tab areas.

Reviewed-by: Roberto Raggi
parent b96bdb50
......@@ -779,12 +779,10 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
return -1;
}
#ifdef QML_WITH_SNIPPETS
if (isQmlFile && (completionOperator.isNull() || completionOperator.isSpace() || isDelimiter(completionOperator))) {
updateSnippets();
m_completions.append(m_snippets);
}
#endif
if (! m_completions.isEmpty())
return m_startPosition;
......@@ -817,14 +815,10 @@ void CodeCompletion::complete(const TextEditor::CompletionItem &item)
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();
edit->insertCodeSnippet(tc, toInsert);
return;
}
}
......@@ -893,6 +887,29 @@ void CodeCompletion::updateSnippets()
item.text += description;
}
item.data = QVariant::fromValue(data);
QString tooltip = data;
tooltip.replace(QRegExp("\n\\s*"), QLatin1String(" "));
{
QString s = QLatin1String("<nobr>");
int count = 0;
for (int i = 0; i < tooltip.count(); ++i) {
if (tooltip.at(i) != QChar::ObjectReplacementCharacter) {
s += tooltip.at(i);
continue;
}
if (++count % 2) {
s += QLatin1String("<b>");
} else {
s += QLatin1String("</b>");
}
}
tooltip = s;
}
item.details = tooltip; // ###TODO this should not be the normal tooltip
item.icon = icon;
m_snippets.append(item);
break;
......
......@@ -82,6 +82,16 @@
using namespace TextEditor;
using namespace TextEditor::Internal;
namespace {
class Locker {
bool *m_bool;
public:
inline Locker(bool *b):m_bool(b){ *m_bool = true; }
inline ~Locker() { *m_bool = false; }
};
}
namespace TextEditor {
namespace Internal {
......@@ -237,6 +247,9 @@ BaseTextEditor::BaseTextEditor(QWidget *parent)
connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
this, SLOT(currentEditorChanged(Core::IEditor*)));
d->m_inKeyPressEvent = false;
d->m_moveLineUndoHack = false;
}
BaseTextEditor::~BaseTextEditor()
......@@ -534,8 +547,24 @@ ITextMarkable *BaseTextEditor::markableInterface() const
void BaseTextEditor::maybeEmitTextChangedBecauseOfUndo()
{
if (document()->isRedoAvailable())
if (!d->m_inKeyPressEvent) {
// i.e. the document was changed outside key press event
// Possible with undo, cut, paste, etc.
if (d->m_snippetOverlay->isVisible()) {
d->m_snippetOverlay->hide();
d->m_snippetOverlay->clear();
QTextCursor cursor = textCursor();
cursor.clearSelection();
setTextCursor(cursor);
return;
}
}
if (document()->isRedoAvailable()) {
emit d->m_editable->contentsChangedBecauseOfUndo();
}
}
ITextEditable *BaseTextEditor::editableInterface() const
......@@ -998,6 +1027,8 @@ void BaseTextEditor::cleanWhitespace()
void BaseTextEditor::keyPressEvent(QKeyEvent *e)
{
Locker inKeyPressEvent(&d->m_inKeyPressEvent);
viewport()->setCursor(Qt::BlankCursor);
QToolTip::hideText();
......@@ -1310,19 +1341,20 @@ skip_event:
delete e;
}
void BaseTextEditor::insertCodeSnippet(const QString &snippet)
void BaseTextEditor::insertCodeSnippet(const QTextCursor &cursor_arg, const QString &snippet)
{
QList<QTextEdit::ExtraSelection> selections;
QTextCursor cursor = textCursor();
const int startCursorPosition = cursor.position();
cursor.beginEditBlock();
if ((snippet.count('$') % 2) != 0) {
qWarning() << "invalid snippet";
return;
}
QList<QTextEdit::ExtraSelection> selections;
QTextCursor cursor = cursor_arg;
cursor.beginEditBlock();
cursor.removeSelectedText();
const int startCursorPosition = cursor.position();
int pos = 0;
QMap<int, int> positions;
......@@ -1369,7 +1401,7 @@ void BaseTextEditor::insertCodeSnippet(const QString &snippet)
tc.setPosition(position + length, QTextCursor::KeepAnchor);
QTextEdit::ExtraSelection selection;
selection.cursor = tc;
selection.format.setBackground(length ? Qt::darkCyan : Qt::darkMagenta);
selection.format = (length ? d->m_occurrencesFormat : d->m_occurrenceRenameFormat);
selections.append(selection);
}
......@@ -1391,6 +1423,7 @@ void BaseTextEditor::insertCodeSnippet(const QString &snippet)
}
setTextCursor(cursor);
}
}
void BaseTextEditor::universalHelper()
......@@ -5231,6 +5264,13 @@ void BaseTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
d->m_matchFormat.setForeground(parenthesesFormat.foreground());
d->m_rangeFormat.setBackground(parenthesesFormat.background());
// snippests
d->m_occurrencesFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES));
d->m_occurrencesFormat.clearForeground();
d->m_occurrenceRenameFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES_RENAME));
d->m_occurrenceRenameFormat.clearForeground();
slotUpdateExtraAreaWidth(); // Adjust to new font width
updateCurrentLineHighlight(); // Make sure it takes the new color
}
......
......@@ -380,7 +380,7 @@ public:
void setTextCursor(const QTextCursor &cursor);
void insertCodeSnippet(const QString &snippet);
void insertCodeSnippet(const QTextCursor &cursor, const QString &snippet);
public slots:
void setDisplayName(const QString &title);
......
......@@ -196,6 +196,8 @@ public:
TextEditorOverlay *m_searchResultOverlay;
void snippetCheckCursor(const QTextCursor &cursor);
void snippetTabOrBacktab(bool forward);
QTextCharFormat m_occurrencesFormat;
QTextCharFormat m_occurrenceRenameFormat;
QBasicTimer collapsedBlockTimer;
int visibleCollapsedBlockNumber;
......@@ -259,6 +261,8 @@ public:
QPointer<BaseTextEditorAnimator> m_animator;
int m_cursorBlockNumber;
bool m_inKeyPressEvent;
};
} // namespace Internal
......
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