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
This commit is contained in:
mae
2010-03-29 16:44:27 +02:00
parent b96bdb50f9
commit 91dac9385e
4 changed files with 78 additions and 17 deletions

View File

@@ -779,12 +779,10 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
return -1; return -1;
} }
#ifdef QML_WITH_SNIPPETS
if (isQmlFile && (completionOperator.isNull() || completionOperator.isSpace() || isDelimiter(completionOperator))) { if (isQmlFile && (completionOperator.isNull() || completionOperator.isSpace() || isDelimiter(completionOperator))) {
updateSnippets(); updateSnippets();
m_completions.append(m_snippets); m_completions.append(m_snippets);
} }
#endif
if (! m_completions.isEmpty()) if (! m_completions.isEmpty())
return m_startPosition; return m_startPosition;
@@ -817,14 +815,10 @@ void CodeCompletion::complete(const TextEditor::CompletionItem &item)
if (QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget())) { if (QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget())) {
if (item.data.isValid()) { if (item.data.isValid()) {
QTextCursor tc = edit->textCursor(); QTextCursor tc = edit->textCursor();
tc.beginEditBlock();
tc.setPosition(m_startPosition); tc.setPosition(m_startPosition);
tc.setPosition(m_editor->position(), QTextCursor::KeepAnchor); tc.setPosition(m_editor->position(), QTextCursor::KeepAnchor);
tc.removeSelectedText();
toInsert = item.data.toString(); toInsert = item.data.toString();
edit->insertCodeSnippet(toInsert); edit->insertCodeSnippet(tc, toInsert);
tc.endEditBlock();
return; return;
} }
} }
@@ -893,6 +887,29 @@ void CodeCompletion::updateSnippets()
item.text += description; item.text += description;
} }
item.data = QVariant::fromValue(data); 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; item.icon = icon;
m_snippets.append(item); m_snippets.append(item);
break; break;

View File

@@ -82,6 +82,16 @@
using namespace TextEditor; using namespace TextEditor;
using namespace TextEditor::Internal; 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 TextEditor {
namespace Internal { namespace Internal {
@@ -237,6 +247,9 @@ BaseTextEditor::BaseTextEditor(QWidget *parent)
connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)), connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
this, SLOT(currentEditorChanged(Core::IEditor*))); this, SLOT(currentEditorChanged(Core::IEditor*)));
d->m_inKeyPressEvent = false;
d->m_moveLineUndoHack = false;
} }
BaseTextEditor::~BaseTextEditor() BaseTextEditor::~BaseTextEditor()
@@ -534,9 +547,25 @@ ITextMarkable *BaseTextEditor::markableInterface() const
void BaseTextEditor::maybeEmitTextChangedBecauseOfUndo() 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(); emit d->m_editable->contentsChangedBecauseOfUndo();
} }
}
ITextEditable *BaseTextEditor::editableInterface() const ITextEditable *BaseTextEditor::editableInterface() const
{ {
@@ -998,6 +1027,8 @@ void BaseTextEditor::cleanWhitespace()
void BaseTextEditor::keyPressEvent(QKeyEvent *e) void BaseTextEditor::keyPressEvent(QKeyEvent *e)
{ {
Locker inKeyPressEvent(&d->m_inKeyPressEvent);
viewport()->setCursor(Qt::BlankCursor); viewport()->setCursor(Qt::BlankCursor);
QToolTip::hideText(); QToolTip::hideText();
@@ -1310,19 +1341,20 @@ skip_event:
delete e; 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) { if ((snippet.count('$') % 2) != 0) {
qWarning() << "invalid snippet"; qWarning() << "invalid snippet";
return; return;
} }
QList<QTextEdit::ExtraSelection> selections;
QTextCursor cursor = cursor_arg;
cursor.beginEditBlock();
cursor.removeSelectedText();
const int startCursorPosition = cursor.position();
int pos = 0; int pos = 0;
QMap<int, int> positions; QMap<int, int> positions;
@@ -1369,7 +1401,7 @@ void BaseTextEditor::insertCodeSnippet(const QString &snippet)
tc.setPosition(position + length, QTextCursor::KeepAnchor); tc.setPosition(position + length, QTextCursor::KeepAnchor);
QTextEdit::ExtraSelection selection; QTextEdit::ExtraSelection selection;
selection.cursor = tc; selection.cursor = tc;
selection.format.setBackground(length ? Qt::darkCyan : Qt::darkMagenta); selection.format = (length ? d->m_occurrencesFormat : d->m_occurrenceRenameFormat);
selections.append(selection); selections.append(selection);
} }
@@ -1391,6 +1423,7 @@ void BaseTextEditor::insertCodeSnippet(const QString &snippet)
} }
setTextCursor(cursor); setTextCursor(cursor);
} }
} }
void BaseTextEditor::universalHelper() void BaseTextEditor::universalHelper()
@@ -5231,6 +5264,13 @@ void BaseTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
d->m_matchFormat.setForeground(parenthesesFormat.foreground()); d->m_matchFormat.setForeground(parenthesesFormat.foreground());
d->m_rangeFormat.setBackground(parenthesesFormat.background()); 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 slotUpdateExtraAreaWidth(); // Adjust to new font width
updateCurrentLineHighlight(); // Make sure it takes the new color updateCurrentLineHighlight(); // Make sure it takes the new color
} }

View File

@@ -380,7 +380,7 @@ public:
void setTextCursor(const QTextCursor &cursor); void setTextCursor(const QTextCursor &cursor);
void insertCodeSnippet(const QString &snippet); void insertCodeSnippet(const QTextCursor &cursor, const QString &snippet);
public slots: public slots:
void setDisplayName(const QString &title); void setDisplayName(const QString &title);

View File

@@ -196,6 +196,8 @@ public:
TextEditorOverlay *m_searchResultOverlay; TextEditorOverlay *m_searchResultOverlay;
void snippetCheckCursor(const QTextCursor &cursor); void snippetCheckCursor(const QTextCursor &cursor);
void snippetTabOrBacktab(bool forward); void snippetTabOrBacktab(bool forward);
QTextCharFormat m_occurrencesFormat;
QTextCharFormat m_occurrenceRenameFormat;
QBasicTimer collapsedBlockTimer; QBasicTimer collapsedBlockTimer;
int visibleCollapsedBlockNumber; int visibleCollapsedBlockNumber;
@@ -259,6 +261,8 @@ public:
QPointer<BaseTextEditorAnimator> m_animator; QPointer<BaseTextEditorAnimator> m_animator;
int m_cursorBlockNumber; int m_cursorBlockNumber;
bool m_inKeyPressEvent;
}; };
} // namespace Internal } // namespace Internal