Commit 22519583 authored by Eike Ziller's avatar Eike Ziller

TextEditors: Avoid changing document after construction.

Also when duplicating editors, we don't want to change the document
after construction. Actually at some places (e.g. CppEditorSupport
creation) we don't handle document changes correctly, and we are only
lucky that things still (more or less?) work. Get rid of
BaseTextEditorWidget::duplicateFrom and use copy-constructor style instead.

Change-Id: I7f688b7fcc51d1bb5e222bb333f0d28479b597a6
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 9ce8bcd1
......@@ -66,9 +66,8 @@ CMakeEditor::CMakeEditor(CMakeEditorWidget *editor)
Core::IEditor *CMakeEditor::duplicate()
{
CMakeEditorWidget *w = qobject_cast<CMakeEditorWidget*>(widget());
CMakeEditorWidget *ret = new CMakeEditorWidget();
ret->duplicateFrom(w);
CMakeEditorWidget *ret = new CMakeEditorWidget(
qobject_cast<CMakeEditorWidget *>(editorWidget()));
TextEditor::TextEditorSettings::initializeEditor(ret);
return ret->editor();
}
......@@ -118,8 +117,18 @@ void CMakeEditor::build()
CMakeEditorWidget::CMakeEditorWidget(QWidget *parent)
: BaseTextEditorWidget(new CMakeDocument(), parent)
{
baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter);
ctor();
}
CMakeEditorWidget::CMakeEditorWidget(CMakeEditorWidget *other)
: BaseTextEditorWidget(other)
{
ctor();
}
void CMakeEditorWidget::ctor()
{
baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter);
m_commentDefinition.clearCommentStyles();
m_commentDefinition.singleLine = QLatin1Char('#');
}
......
......@@ -70,6 +70,7 @@ class CMakeEditorWidget : public TextEditor::BaseTextEditorWidget
public:
CMakeEditorWidget(QWidget *parent = 0);
CMakeEditorWidget(CMakeEditorWidget *other);
bool save(const QString &fileName = QString());
......@@ -83,6 +84,8 @@ public slots:
void unCommentSelection();
private:
CMakeEditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
Utils::CommentDefinition m_commentDefinition;
};
......
......@@ -513,15 +513,27 @@ Q_GLOBAL_STATIC(CppTools::SymbolFinder, symbolFinder)
CPPEditorWidget::CPPEditorWidget(QWidget *parent)
: TextEditor::BaseTextEditorWidget(parent)
, m_currentRenameSelection(NoCurrentRenameSelection)
, m_inRename(false)
, m_inRenameChanged(false)
, m_firstRenameChange(false)
, m_objcEnabled(false)
, m_commentsSettings(CppTools::CppToolsSettings::instance()->commentsSettings())
, m_followSymbolUnderCursor(new FollowSymbolUnderCursor(this))
, m_preprocessorButton(0)
{
ctor();
}
CPPEditorWidget::CPPEditorWidget(CPPEditorWidget *other)
: TextEditor::BaseTextEditorWidget(other)
{
ctor();
}
void CPPEditorWidget::ctor()
{
m_currentRenameSelection = NoCurrentRenameSelection;
m_inRename = false;
m_inRenameChanged = false;
m_firstRenameChange = false;
m_objcEnabled = false;
m_commentsSettings = CppTools::CppToolsSettings::instance()->commentsSettings();
m_followSymbolUnderCursor.reset(new FollowSymbolUnderCursor(this));
m_preprocessorButton = 0;
qRegisterMetaType<SemanticInfo>("CppTools::SemanticInfo");
setParenthesesMatchingEnabled(true);
......@@ -640,7 +652,8 @@ void CPPEditorWidget::createToolBar(CPPEditor *editor)
connect(m_outlineCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateOutlineToolTip()));
// set up slots to document changes
updateContentsChangedSignal();
connect(document(), SIGNAL(contentsChange(int,int,int)),
this, SLOT(onContentsChanged(int,int,int)));
// set up function declaration - definition link
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateFunctionDeclDefLink()));
......@@ -1501,10 +1514,8 @@ void CPPEditorWidget::keyPressEvent(QKeyEvent *e)
Core::IEditor *CPPEditor::duplicate()
{
CPPEditorWidget *newEditor = new CPPEditorWidget();
newEditor->duplicateFrom(editorWidget());
// A new QTextDocument was set, so update our signal/slot connection to the new document
newEditor->updateContentsChangedSignal();
CPPEditorWidget *newEditor = new CPPEditorWidget(
qobject_cast<CPPEditorWidget *>(editorWidget()));
CppEditorPlugin::instance()->initializeEditor(newEditor);
return newEditor->editor();
}
......@@ -1860,12 +1871,6 @@ void CPPEditorWidget::applyDeclDefLinkChanges(bool jumpToMatch)
updateFunctionDeclDefLink();
}
void CPPEditorWidget::updateContentsChangedSignal()
{
connect(document(), SIGNAL(contentsChange(int,int,int)),
this, SLOT(onContentsChanged(int,int,int)));
}
FollowSymbolUnderCursor *CPPEditorWidget::followSymbolUnderCursorDelegate()
{
return m_followSymbolUnderCursor.data();
......
......@@ -98,6 +98,7 @@ public:
typedef TextEditor::TabSettings TabSettings;
CPPEditorWidget(QWidget *parent = 0);
CPPEditorWidget(CPPEditorWidget *other);
~CPPEditorWidget();
void unCommentSelection();
......@@ -130,8 +131,6 @@ public:
QSharedPointer<FunctionDeclDefLink> declDefLink() const;
void applyDeclDefLinkChanges(bool jumpToMatch);
void updateContentsChangedSignal();
FollowSymbolUnderCursor *followSymbolUnderCursorDelegate(); // exposed for tests
Q_SIGNALS:
......@@ -191,6 +190,8 @@ private Q_SLOTS:
void onCommentsSettingsChanged(const CppTools::CommentsSettings &settings);
private:
CPPEditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
void markSymbols(const QTextCursor &tc, const CppTools::SemanticInfo &info);
bool sortedOutline() const;
......
......@@ -93,9 +93,8 @@ bool ProjectFilesEditor::duplicateSupported() const
Core::IEditor *ProjectFilesEditor::duplicate()
{
ProjectFilesEditorWidget *parentEditor = qobject_cast<ProjectFilesEditorWidget *>(editorWidget());
ProjectFilesEditorWidget *editor = new ProjectFilesEditorWidget();
editor->duplicateFrom(parentEditor);
ProjectFilesEditorWidget *editor = new ProjectFilesEditorWidget(
qobject_cast<ProjectFilesEditorWidget *>(editorWidget()));
TextEditorSettings::initializeEditor(editor);
return editor->editor();
}
......@@ -111,6 +110,11 @@ ProjectFilesEditorWidget::ProjectFilesEditorWidget(QWidget *parent)
{
}
ProjectFilesEditorWidget::ProjectFilesEditorWidget(ProjectFilesEditorWidget *other)
: BaseTextEditorWidget(other)
{
}
BaseTextEditor *ProjectFilesEditorWidget::createEditor()
{
return new ProjectFilesEditor(this);
......
......@@ -70,8 +70,12 @@ class ProjectFilesEditorWidget : public TextEditor::BaseTextEditorWidget
public:
ProjectFilesEditorWidget(QWidget *parent = 0);
ProjectFilesEditorWidget(ProjectFilesEditorWidget *other);
TextEditor::BaseTextEditor *createEditor();
private:
ProjectFilesEditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
};
} // namespace Internal
......
......@@ -140,10 +140,21 @@ void Document::addRange(const QTextCursor &cursor, GLSL::Scope *scope)
_cursors.append(c);
}
GLSLTextEditorWidget::GLSLTextEditorWidget(QWidget *parent) :
TextEditor::BaseTextEditorWidget(parent),
m_outlineCombo(0)
GLSLTextEditorWidget::GLSLTextEditorWidget(QWidget *parent)
: TextEditor::BaseTextEditorWidget(parent)
{
ctor();
}
GLSLTextEditorWidget::GLSLTextEditorWidget(GLSLTextEditorWidget *other)
: TextEditor::BaseTextEditorWidget(other)
{
ctor();
}
void GLSLTextEditorWidget::ctor()
{
m_outlineCombo = 0;
setParenthesesMatchingEnabled(true);
setMarksVisible(true);
setCodeFoldingSupported(true);
......@@ -189,8 +200,8 @@ bool GLSLTextEditorWidget::isOutdated() const
Core::IEditor *GLSLEditorEditable::duplicate()
{
GLSLTextEditorWidget *newEditor = new GLSLTextEditorWidget();
newEditor->duplicateFrom(editorWidget());
GLSLTextEditorWidget *newEditor = new GLSLTextEditorWidget(
qobject_cast<GLSLTextEditorWidget *>(editorWidget()));
TextEditor::TextEditorSettings::initializeEditor(newEditor);
return newEditor->editor();
}
......
......@@ -87,6 +87,7 @@ class GLSLTextEditorWidget : public TextEditor::BaseTextEditorWidget
public:
GLSLTextEditorWidget(QWidget *parent = 0);
GLSLTextEditorWidget(GLSLTextEditorWidget *other);
~GLSLTextEditorWidget();
virtual void unCommentSelection();
......@@ -110,6 +111,8 @@ protected:
void createToolBar(Internal::GLSLEditorEditable *editable);
private:
GLSLTextEditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
void setSelectedElements();
QString wordUnderCursor() const;
......
......@@ -60,10 +60,8 @@ PythonEditor::~PythonEditor()
Core::IEditor *PythonEditor::duplicate()
{
EditorWidget *widget = new EditorWidget();
widget->duplicateFrom(editorWidget());
EditorWidget *widget = new EditorWidget(qobject_cast<EditorWidget *>(editorWidget()));
TextEditor::TextEditorSettings::initializeEditor(widget);
return widget->editor();
}
......
......@@ -48,7 +48,18 @@ namespace PythonEditor {
namespace Internal {
EditorWidget::EditorWidget(QWidget *parent)
:TextEditor::BaseTextEditorWidget(parent)
: TextEditor::BaseTextEditorWidget(parent)
{
ctor();
}
EditorWidget::EditorWidget(EditorWidget *other)
: TextEditor::BaseTextEditorWidget(other)
{
ctor();
}
void EditorWidget::ctor()
{
m_commentDefinition.multiLineStart.clear();
m_commentDefinition.multiLineEnd.clear();
......
......@@ -42,6 +42,7 @@ class EditorWidget : public TextEditor::BaseTextEditorWidget
public:
EditorWidget(QWidget *parent = 0);
EditorWidget(EditorWidget *other);
virtual ~EditorWidget();
virtual void unCommentSelection();
......@@ -53,6 +54,8 @@ protected:
TextEditor::BaseTextEditor *createEditor();
private:
EditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
Utils::CommentDefinition m_commentDefinition;
};
......
......@@ -61,8 +61,8 @@ ProFileEditor::ProFileEditor(ProFileEditorWidget *editor)
Core::IEditor *ProFileEditor::duplicate()
{
ProFileEditorWidget *ret = new ProFileEditorWidget();
ret->duplicateFrom(editorWidget());
ProFileEditorWidget *ret = new ProFileEditorWidget(
qobject_cast<ProFileEditorWidget*>(editorWidget()));
TextEditor::TextEditorSettings::initializeEditor(ret);
return ret->editor();
}
......@@ -83,6 +83,17 @@ TextEditor::CompletionAssistProvider *ProFileEditor::completionAssistProvider()
ProFileEditorWidget::ProFileEditorWidget(QWidget *parent)
: BaseTextEditorWidget(new ProFileDocument(), parent)
{
ctor();
}
ProFileEditorWidget::ProFileEditorWidget(ProFileEditorWidget *other)
: BaseTextEditorWidget(other)
{
ctor();
}
void ProFileEditorWidget::ctor()
{
baseTextDocument()->setSyntaxHighlighter(new ProFileHighlighter);
m_commentDefinition.clearCommentStyles();
......
......@@ -59,6 +59,7 @@ class ProFileEditorWidget : public TextEditor::BaseTextEditorWidget
public:
ProFileEditorWidget(QWidget *parent = 0);
ProFileEditorWidget(ProFileEditorWidget *other);
void unCommentSelection();
......@@ -69,6 +70,8 @@ protected:
void contextMenuEvent(QContextMenuEvent *);
private:
ProFileEditorWidget(BaseTextEditorWidget *); // avoid stupidity
void ctor();
Utils::CommentDefinition m_commentDefinition;
};
......
......@@ -450,15 +450,26 @@ protected:
QmlJSTextEditorWidget::QmlJSTextEditorWidget(QWidget *parent) :
TextEditor::BaseTextEditorWidget(parent),
m_outlineCombo(0),
m_outlineModel(new QmlOutlineModel(this)),
m_modelManager(0),
m_futureSemanticInfoRevision(0),
m_contextPane(0),
m_findReferences(new FindReferences(this)),
m_semanticHighlighter(new SemanticHighlighter(this))
TextEditor::BaseTextEditorWidget(parent)
{
ctor();
}
QmlJSTextEditorWidget::QmlJSTextEditorWidget(QmlJSTextEditorWidget *other)
: TextEditor::BaseTextEditorWidget(other)
{
ctor();
}
void QmlJSTextEditorWidget::ctor()
{
m_outlineCombo = 0;
m_outlineModel = new QmlOutlineModel(this);
m_futureSemanticInfoRevision = 0;
m_contextPane = 0;
m_findReferences = new FindReferences(this);
m_semanticHighlighter = new SemanticHighlighter(this);
m_semanticInfoUpdater = new SemanticInfoUpdater(this);
m_semanticInfoUpdater->start();
......@@ -583,8 +594,8 @@ QModelIndex QmlJSTextEditorWidget::outlineModelIndex()
IEditor *QmlJSEditor::duplicate()
{
QmlJSTextEditorWidget *newEditor = new QmlJSTextEditorWidget();
newEditor->duplicateFrom(editorWidget());
QmlJSTextEditorWidget *newEditor = new QmlJSTextEditorWidget(
qobject_cast<QmlJSTextEditorWidget *>(editorWidget()));
TextEditor::TextEditorSettings::initializeEditor(newEditor);
return newEditor->editor();
}
......
......@@ -100,6 +100,7 @@ class QMLJSEDITOR_EXPORT QmlJSTextEditorWidget : public TextEditor::BaseTextEdit
public:
QmlJSTextEditorWidget(QWidget *parent = 0);
QmlJSTextEditorWidget(QmlJSTextEditorWidget *other);
~QmlJSTextEditorWidget();
virtual void unCommentSelection();
......@@ -166,6 +167,8 @@ protected:
QString foldReplacementText(const QTextBlock &block) const;
private:
QmlJSTextEditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
bool isClosingBrace(const QList<QmlJS::Token> &tokens) const;
void setSelectedElements();
......
......@@ -200,6 +200,12 @@ BaseTextEditorWidget::BaseTextEditorWidget(BaseTextDocument *doc, QWidget *paren
ctor(QSharedPointer<BaseTextDocument>(doc));
}
BaseTextEditorWidget::BaseTextEditorWidget(BaseTextEditorWidget *other)
{
ctor(other->d->m_document);
d->m_revisionsVisible = other->d->m_revisionsVisible;
}
void BaseTextEditorWidget::ctor(const QSharedPointer<BaseTextDocument> &doc)
{
d = new BaseTextEditorWidgetPrivate;
......@@ -214,7 +220,7 @@ void BaseTextEditorWidget::ctor(const QSharedPointer<BaseTextDocument> &doc)
d->m_refactorOverlay = new RefactorOverlay(this);
d->m_document = doc;
d->setupDocumentSignals(d->m_document);
d->setupDocumentSignals();
d->m_lastScrollPos = -1;
......@@ -2079,17 +2085,6 @@ bool BaseTextEditorWidget::event(QEvent *e)
return QPlainTextEdit::event(e);
}
void BaseTextEditorWidget::duplicateFrom(BaseTextEditorWidget *widget)
{
if (this == widget)
return;
d->m_revisionsVisible = widget->d->m_revisionsVisible;
if (d->m_document == widget->d->m_document)
return;
d->setupDocumentSignals(widget->d->m_document);
d->m_document = widget->d->m_document;
}
void BaseTextEditorWidget::documentAboutToBeReloaded()
{
//memorize cursor position
......@@ -2456,16 +2451,9 @@ BaseTextEditorWidgetPrivate::~BaseTextEditorWidgetPrivate()
{
}
void BaseTextEditorWidgetPrivate::setupDocumentSignals(const QSharedPointer<BaseTextDocument> &document)
void BaseTextEditorWidgetPrivate::setupDocumentSignals()
{
BaseTextDocument *oldDocument = q->baseTextDocument();
if (oldDocument) {
q->disconnect(oldDocument->document(), 0, q, 0);
q->disconnect(oldDocument, 0, q, 0);
q->disconnect(q, 0, oldDocument, 0);
}
QTextDocument *doc = document->document();
QTextDocument *doc = m_document->document();
q->setDocument(doc);
q->setCursorWidth(2); // Applies to the document layout
......@@ -2477,8 +2465,8 @@ void BaseTextEditorWidgetPrivate::setupDocumentSignals(const QSharedPointer<Base
QObject::connect(doc, SIGNAL(modificationChanged(bool)), q, SIGNAL(changed()));
QObject::connect(doc, SIGNAL(contentsChange(int,int,int)), q,
SLOT(editorContentsChange(int,int,int)), Qt::DirectConnection);
QObject::connect(document.data(), SIGNAL(aboutToReload()), q, SLOT(documentAboutToBeReloaded()));
QObject::connect(document.data(), SIGNAL(reloadFinished(bool)), q, SLOT(documentReloadFinished(bool)));
QObject::connect(m_document.data(), SIGNAL(aboutToReload()), q, SLOT(documentAboutToBeReloaded()));
QObject::connect(m_document.data(), SIGNAL(reloadFinished(bool)), q, SLOT(documentReloadFinished(bool)));
q->slotUpdateExtraAreaWidth();
}
......
......@@ -132,6 +132,7 @@ class TEXTEDITOR_EXPORT BaseTextEditorWidget : public QPlainTextEdit
public:
BaseTextEditorWidget(QWidget *parent = 0);
BaseTextEditorWidget(BaseTextDocument *doc, QWidget *parent = 0);
BaseTextEditorWidget(BaseTextEditorWidget *other);
~BaseTextEditorWidget();
const Utils::ChangeSet &changeSet() const;
......@@ -356,9 +357,6 @@ private:
void updateCannotDecodeInfo();
void collectToCircularClipboard();
public:
void duplicateFrom(BaseTextEditorWidget *editor);
protected:
void setDefaultPath(const QString &defaultPath);
......
......@@ -102,7 +102,7 @@ public:
BaseTextEditorWidgetPrivate();
~BaseTextEditorWidgetPrivate();
void setupDocumentSignals(const QSharedPointer<BaseTextDocument> &document);
void setupDocumentSignals();
void updateLineSelectionColor();
void print(QPrinter *printer);
......
......@@ -69,6 +69,12 @@ PlainTextEditorWidget::PlainTextEditorWidget(BaseTextDocument *doc, QWidget *par
ctor();
}
PlainTextEditorWidget::PlainTextEditorWidget(PlainTextEditorWidget *other)
: BaseTextEditorWidget(other)
{
ctor();
}
void PlainTextEditorWidget::ctor()
{
m_isMissingSyntaxDefinition = false;
......@@ -88,8 +94,8 @@ void PlainTextEditorWidget::ctor()
IEditor *PlainTextEditor::duplicate()
{
PlainTextEditorWidget *newWidget = new PlainTextEditorWidget();
newWidget->duplicateFrom(editorWidget());
PlainTextEditorWidget *newWidget = new PlainTextEditorWidget(
qobject_cast<PlainTextEditorWidget *>(editorWidget()));
TextEditorSettings::initializeEditor(newWidget);
return newWidget->editor();
}
......
......@@ -61,6 +61,7 @@ class TEXTEDITOR_EXPORT PlainTextEditorWidget : public BaseTextEditorWidget
public:
PlainTextEditorWidget(QWidget *parent = 0);
PlainTextEditorWidget(BaseTextDocument *doc, QWidget *parent = 0);
PlainTextEditorWidget(PlainTextEditorWidget *other);
void configure(const QString& mimeType);
void configure(const Core::MimeType &mimeType);
......@@ -81,6 +82,7 @@ protected:
virtual BaseTextEditor *createEditor() { return new PlainTextEditor(this); }
private:
PlainTextEditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
bool m_isMissingSyntaxDefinition;
......
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