Commit 4c185a9a authored by Thomas Hartmann's avatar Thomas Hartmann

QmlDesigner.model: rename id is now a proper refactoring operation

I an id is now changed in the navaigator or property editor,
we now use the qmljseditor to properly refactor.

Also I changed dynamic_cast to qobject_cast

Reviewed-by: Erik Verbruggen
parent 2d504964
......@@ -36,6 +36,7 @@
#include <propertymetainfo.h>
#include <qgraphicswidget.h>
#include <abstractview.h>
#include <rewriterview.h>
#include <invalididexception.h>
#include <rewritingexception.h>
......@@ -297,10 +298,11 @@ void NavigatorTreeModel::handleChangedItem(QStandardItem *item)
ItemRow itemRow = itemRowForNode(node);
if (item == itemRow.idItem) {
try {
node.setId(item->text());
} catch (InvalidIdException &e) {
QMessageBox::warning(0, tr("Invalid Id"), e.description());
if (node.isValidId(item->text())) {
if (node.view()->rewriterView())
node.view()->rewriterView()->renameId(node.id(), item->text());
} else {
QMessageBox::warning(0, tr("Invalid Id"), item->text() + tr(" is an invalid id"));
item->setText(node.id());
}
} else if (item == itemRow.visibilityItem) {
......
......@@ -43,6 +43,7 @@
#include <bindingproperty.h>
#include <nodeabstractproperty.h>
#include <rewriterview.h>
#include "propertyeditorvalue.h"
#include "basiclayouts.h"
......@@ -354,13 +355,13 @@ void PropertyEditor::changeValue(const QString &propertyName)
PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(propertyName)));
const QString newId = value->value().toString();
try {
m_selectedNode.setId(newId);
} catch (InvalidIdException &e) {
if (m_selectedNode.isValidId(newId)) {
if (rewriterView())
rewriterView()->renameId(m_selectedNode.id(), newId);
} else {
value->setValue(m_selectedNode.id());
QMessageBox::warning(0, tr("Invalid Id"), e.description());
QMessageBox::warning(0, tr("Invalid Id"), newId + tr(" is an invalid id"));
}
return;
}
......
......@@ -77,7 +77,7 @@ protected:
{ return m_textModifier; }
bool includeSurroundingWhitespace(int &start, int &end) const;
void includeLeadingEmptyLine(int &start) const;
void includeLeadingEmptyLine(int &start) const;
static QmlJS::AST::UiObjectMemberList *searchMemberToInsertAfter(QmlJS::AST::UiObjectMemberList *members, const QStringList &propertyOrder);
static QmlJS::AST::UiObjectMemberList *searchMemberToInsertAfter(QmlJS::AST::UiObjectMemberList *members, const QString &propertyName, const QStringList &propertyOrder);
......
......@@ -55,6 +55,7 @@ namespace QmlDesigner {
class QmlModelView;
class NodeInstanceView;
class RewriterView;
class CORESHARED_EXPORT AbstractView : public QObject
{
......@@ -141,6 +142,7 @@ public:
void changeRootNodeType(const QString &type, int majorVersion, int minorVersion);
NodeInstanceView *nodeInstanceView() const;
RewriterView *rewriterView() const;
protected:
void setModel(Model * model);
......
......@@ -53,6 +53,8 @@ public:
virtual int indentDepth() const;
virtual bool renameId(const QString &oldId, const QString &newId);
virtual QmlJS::Snapshot getSnapshot() const;
virtual QStringList importPaths() const;
};
......
......@@ -61,6 +61,8 @@ public:
virtual QmlJS::Snapshot getSnapshot() const;
virtual QStringList importPaths() const;
virtual bool renameId(const QString & /* oldId */, const QString & /* newId */) { return false; }
public slots:
void contentsChange(int position, int charsRemoved, int charsAdded);
......
......@@ -78,6 +78,8 @@ public:
virtual QmlJS::Snapshot getSnapshot() const = 0;
virtual QStringList importPaths() const = 0;
virtual bool renameId(const QString & /* oldId */, const QString & /* newId */) { return false; }
protected:
QPlainTextEdit *plainTextEdit() const
{ return m_textEdit; }
......
......@@ -159,6 +159,8 @@ public:
int firstDefinitionInsideLength(const ModelNode &node) const;
bool modificationGroupActive();
bool renameId(const QString& oldId, const QString& newId);
signals:
void errorsChanged(const QList<RewriterView::Error> &errors);
......
......@@ -87,6 +87,8 @@ public:
virtual QmlJS::Snapshot getSnapshot() const = 0;
virtual QStringList importPaths() const = 0;
virtual bool renameId(const QString &oldId, const QString &newId) = 0;
signals:
void textChanged();
......
......@@ -348,6 +348,15 @@ NodeInstanceView *AbstractView::nodeInstanceView() const
}
}
RewriterView *AbstractView::rewriterView() const
{
if (model()) {
return model()->m_d->rewriterView();
} else {
return 0;
}
}
QList<ModelNode> AbstractView::allModelNodes()
{
return toModelNodeList(model()->m_d->allNodes());
......
......@@ -32,6 +32,7 @@
#include <extensionsystem/pluginmanager.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljseditor/qmljseditor.h>
#include <texteditor/tabsettings.h>
using namespace QmlDesigner;
......@@ -46,7 +47,7 @@ void BaseTextEditModifier::indent(int offset, int length)
if (length == 0 || offset < 0 || offset + length >= text().length())
return;
if (TextEditor::BaseTextEditor *bte = dynamic_cast<TextEditor::BaseTextEditor*>(plainTextEdit())) {
if (TextEditor::BaseTextEditor *bte = qobject_cast<TextEditor::BaseTextEditor*>(plainTextEdit())) {
// find the applicable block:
QTextDocument *doc = bte->document();
QTextCursor tc(doc);
......@@ -60,13 +61,23 @@ void BaseTextEditModifier::indent(int offset, int length)
int BaseTextEditModifier::indentDepth() const
{
if (TextEditor::BaseTextEditor *bte = dynamic_cast<TextEditor::BaseTextEditor*>(plainTextEdit())) {
if (TextEditor::BaseTextEditor *bte = qobject_cast<TextEditor::BaseTextEditor*>(plainTextEdit())) {
return bte->tabSettings().m_indentSize;
} else {
return 0;
}
}
bool BaseTextEditModifier::renameId(const QString &oldId, const QString &newId)
{
if (QmlJSEditor::Internal::QmlJSTextEditor *qmljse = qobject_cast<QmlJSEditor::Internal::QmlJSTextEditor*>(plainTextEdit())) {
qmljse->renameId(oldId, newId);
return true;
} else {
return false;
}
}
namespace {
static inline QmlJS::ModelManagerInterface *getModelManager()
{
......
......@@ -539,6 +539,14 @@ bool RewriterView::modificationGroupActive()
return m_modificationGroupActive;
}
bool RewriterView::renameId(const QString& oldId, const QString& newId)
{
if (textModifier())
return textModifier()->renameId(oldId, newId);
return false;
}
void RewriterView::qmlTextChanged()
{
if (inErrorState())
......
......@@ -1045,6 +1045,18 @@ void QmlJSTextEditor::setUpdateSelectedElements(bool value)
m_updateSelectedElements = value;
}
void QmlJSTextEditor::renameId(const QString &oldId, const QString &newId)
{
Utils::ChangeSet changeSet;
foreach (const AST::SourceLocation &loc, m_semanticInfo.idLocations.value(oldId)) {
changeSet.replace(loc.begin(), loc.end(), newId);
}
QTextCursor tc = textCursor();
changeSet.apply(&tc);
}
void QmlJSTextEditor::updateUsesNow()
{
if (document()->revision() != m_semanticInfo.revision()) {
......@@ -1242,14 +1254,7 @@ void QmlJSTextEditor::renameIdUnderCursor()
QLineEdit::Normal,
id, &ok);
if (ok) {
Utils::ChangeSet changeSet;
foreach (const AST::SourceLocation &loc, m_semanticInfo.idLocations.value(id)) {
changeSet.replace(loc.begin(), loc.end(), newId);
}
QTextCursor tc = textCursor();
changeSet.apply(&tc);
renameId(id, newId);
}
}
......
......@@ -243,6 +243,8 @@ public:
bool updateSelectedElements() const;
void setUpdateSelectedElements(bool value);
void renameId(const QString &oldId, const QString &newId);
public slots:
void followSymbolUnderCursor();
void findUsages();
......
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