Editor: cleanup special cases for snippets.

This commit is contained in:
Erik Verbruggen
2010-09-24 13:02:13 +02:00
parent 1825d14a82
commit 5ad1b35a76
5 changed files with 21 additions and 63 deletions

View File

@@ -1822,9 +1822,6 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t
Symbol *symbol = 0; Symbol *symbol = 0;
if (TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget()))
edit->setNextChangeIsSnippetSafe();
if (item.data.isValid()) { if (item.data.isValid()) {
if (item.data.canConvert<QString>()) { if (item.data.canConvert<QString>()) {
TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget()); TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget());

View File

@@ -904,8 +904,6 @@ void CodeCompletion::complete(const TextEditor::CompletionItem &item, QChar type
QString toInsert = item.text; QString toInsert = item.text;
if (QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget())) { if (QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget())) {
edit->setNextChangeIsSnippetSafe();
if (item.data.isValid()) { if (item.data.isValid()) {
QTextCursor tc = edit->textCursor(); QTextCursor tc = edit->textCursor();
tc.setPosition(m_startPosition, QTextCursor::KeepAnchor); tc.setPosition(m_startPosition, QTextCursor::KeepAnchor);

View File

@@ -89,17 +89,6 @@
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 {
@@ -258,10 +247,7 @@ 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; d->m_moveLineUndoHack = false;
d->m_nextChangeIsSnippetSafe = false;
} }
BaseTextEditor::~BaseTextEditor() BaseTextEditor::~BaseTextEditor()
@@ -495,37 +481,12 @@ ITextMarkable *BaseTextEditor::markableInterface() const
return baseTextDocument()->documentMarker(); return baseTextDocument()->documentMarker();
} }
void BaseTextEditor::maybeEmitContentsChangedBecauseOfUndo()
{
if (!d->m_inKeyPressEvent && !d->m_nextChangeIsSnippetSafe) {
d->m_nextChangeIsSnippetSafe = false;
// 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 editableInterface()->contentsChangedBecauseOfUndo();
}
}
BaseTextEditorEditable *BaseTextEditor::editableInterface() const BaseTextEditorEditable *BaseTextEditor::editableInterface() const
{ {
if (!d->m_editable) { if (!d->m_editable) {
d->m_editable = const_cast<BaseTextEditor*>(this)->createEditableInterface(); d->m_editable = const_cast<BaseTextEditor*>(this)->createEditableInterface();
connect(this, SIGNAL(textChanged()), connect(this, SIGNAL(textChanged()),
d->m_editable, SIGNAL(contentsChanged())); d->m_editable, SIGNAL(contentsChanged()));
connect(this, SIGNAL(textChanged()),
this, SLOT(maybeEmitContentsChangedBecauseOfUndo()));
connect(this, SIGNAL(changed()), connect(this, SIGNAL(changed()),
d->m_editable, SIGNAL(changed())); d->m_editable, SIGNAL(changed()));
} }
@@ -683,7 +644,6 @@ void BaseTextEditor::editorContentsChange(int position, int charsRemoved, int ch
if (d->m_animator) if (d->m_animator)
d->m_animator->finish(); d->m_animator->finish();
d->m_contentsChanged = true; d->m_contentsChanged = true;
QTextDocument *doc = document(); QTextDocument *doc = document();
@@ -703,11 +663,19 @@ void BaseTextEditor::editorContentsChange(int position, int charsRemoved, int ch
} }
} }
if (d->m_snippetOverlay->isVisible()) {
QTextCursor cursor = textCursor();
cursor.setPosition(position);
if (!d->m_snippetOverlay->hasCursorInSelection(cursor)) {
d->m_snippetOverlay->hide();
d->m_snippetOverlay->clear();
}
}
if (doc->isRedoAvailable()) if (doc->isRedoAvailable())
emit editableInterface()->contentsChangedBecauseOfUndo(); emit editableInterface()->contentsChangedBecauseOfUndo();
} }
void BaseTextEditor::slotSelectionChanged() void BaseTextEditor::slotSelectionChanged()
{ {
if (d->m_inBlockSelectionMode && !textCursor().hasSelection()) { if (d->m_inBlockSelectionMode && !textCursor().hasSelection()) {
@@ -1108,8 +1076,6 @@ 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);
ToolTip::instance()->hide(); ToolTip::instance()->hide();
@@ -1958,8 +1924,7 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
m_requestAutoCompletionRevision(0), m_requestAutoCompletionRevision(0),
m_requestAutoCompletionPosition(0), m_requestAutoCompletionPosition(0),
m_requestAutoCompletionTimer(0), m_requestAutoCompletionTimer(0),
m_cursorBlockNumber(-1), m_cursorBlockNumber(-1)
m_inKeyPressEvent(false)
{ {
} }
@@ -4084,11 +4049,6 @@ QString BaseTextEditor::insertParagraphSeparator(const QTextCursor &tc) const
return QString(); return QString();
} }
void BaseTextEditor::setNextChangeIsSnippetSafe()
{
d->m_nextChangeIsSnippetSafe = true;
}
QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &textToInsert) const QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
{ {
const bool checkBlockEnd = d->m_allowSkippingOfBlockEnd; const bool checkBlockEnd = d->m_allowSkippingOfBlockEnd;
@@ -5441,6 +5401,12 @@ void BaseTextEditor::insertFromMimeData(const QMimeData *source)
cursor.endEditBlock(); cursor.endEditBlock();
setTextCursor(cursor); setTextCursor(cursor);
ensureCursorVisible(); ensureCursorVisible();
if (d->m_snippetOverlay->isVisible() && lines.count() > 1) {
d->m_snippetOverlay->hide();
d->m_snippetOverlay->clear();
}
return; return;
} }
@@ -5451,8 +5417,11 @@ void BaseTextEditor::insertFromMimeData(const QMimeData *source)
if (CompletionSupport::instance()->isActive()) if (CompletionSupport::instance()->isActive())
setFocus(); setFocus();
if (!text.contains(QLatin1Char('\n')) && !text.contains(QLatin1Char('\t'))) if (d->m_snippetOverlay->isVisible() && (text.contains(QLatin1Char('\n'))
setNextChangeIsSnippetSafe(); || text.contains(QLatin1Char('\t')))) {
d->m_snippetOverlay->hide();
d->m_snippetOverlay->clear();
}
const TabSettings &ts = d->m_document->tabSettings(); const TabSettings &ts = d->m_document->tabSettings();
QTextCursor cursor = textCursor(); QTextCursor cursor = textCursor();

View File

@@ -346,7 +346,6 @@ private slots:
bool inFindScope(const QTextCursor &cursor); bool inFindScope(const QTextCursor &cursor);
bool inFindScope(int selectionStart, int selectionEnd); bool inFindScope(int selectionStart, int selectionEnd);
void currentEditorChanged(Core::IEditor *editor); void currentEditorChanged(Core::IEditor *editor);
void maybeEmitContentsChangedBecauseOfUndo();
private: private:
Internal::BaseTextEditorPrivate *d; Internal::BaseTextEditorPrivate *d;
@@ -462,8 +461,6 @@ public:
// Returns the text that needs to be inserted // Returns the text that needs to be inserted
virtual QString insertParagraphSeparator(const QTextCursor &tc) const; virtual QString insertParagraphSeparator(const QTextCursor &tc) const;
virtual void setNextChangeIsSnippetSafe();
protected: protected:
static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen); static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen);

View File

@@ -271,9 +271,6 @@ public:
QPointer<BaseTextEditorAnimator> m_animator; QPointer<BaseTextEditorAnimator> m_animator;
int m_cursorBlockNumber; int m_cursorBlockNumber;
bool m_inKeyPressEvent;
bool m_nextChangeIsSnippetSafe;
}; };
} // namespace Internal } // namespace Internal