From f02e3af2331c0ff96c8ec5590a5ffd43c70ade68 Mon Sep 17 00:00:00 2001 From: Adeel Asghar Date: Wed, 13 Mar 2024 15:47:32 +0100 Subject: [PATCH] Update the auto-complete functionality for instance API (#12104) --- OMEdit/OMEditLIB/Editors/ModelicaEditor.cpp | 4 +- OMEdit/OMEditLIB/Element/Element.cpp | 2 - .../OMEditLIB/Modeling/LibraryTreeWidget.cpp | 82 +++++++++++++++++-- OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h | 7 +- .../OMEditLIB/Modeling/ModelWidgetContainer.h | 1 + 5 files changed, 85 insertions(+), 11 deletions(-) diff --git a/OMEdit/OMEditLIB/Editors/ModelicaEditor.cpp b/OMEdit/OMEditLIB/Editors/ModelicaEditor.cpp index c86aaf4c783..40af2d6c21b 100644 --- a/OMEdit/OMEditLIB/Editors/ModelicaEditor.cpp +++ b/OMEdit/OMEditLIB/Editors/ModelicaEditor.cpp @@ -155,7 +155,9 @@ QList ModelicaEditor::getCandidateContexts(QStringList nameCom QList roots; LibraryTreeItem *pItem = getModelWidget()->getLibraryTreeItem(); while (pItem) { - roots.append(pItem->getInheritedClassesDeepList()); + if (!pItem->isRootItem()) { + roots.append(pItem->getInheritedClassesDeepList()); + } pItem = pItem->parent(); } diff --git a/OMEdit/OMEditLIB/Element/Element.cpp b/OMEdit/OMEditLIB/Element/Element.cpp index 02d6761f979..314be26f289 100644 --- a/OMEdit/OMEditLIB/Element/Element.cpp +++ b/OMEdit/OMEditLIB/Element/Element.cpp @@ -288,8 +288,6 @@ void ElementInfo::parseElementInfoString(QString value) } } - - /*! * \brief ElementInfo::fetchParameterValue * Fetches the Element parameter value if any. diff --git a/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp b/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp index c4ec9cb6d2e..65de4a432f9 100644 --- a/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp +++ b/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp @@ -681,6 +681,50 @@ void LibraryTreeItem::removeInheritedClasses() mInheritedClasses.clear(); } +const QList &LibraryTreeItem::getInheritedClasses() +{ + // This section is used by autocompletion when instance API is enabled. + /*! @todo We should use the Language Server Protocol. */ + if (MainWindow::instance()->isNewApi()) { + if (mpModelWidget && mpModelWidget->isDiagramViewLoaded()) { + QList elements = mpModelWidget->getModelInstance()->getElements(); + // reuse the mInheritedClasses list + mInheritedClasses.clear(); + foreach (auto pElement, elements) { + if (pElement->isExtend()) { + LibraryTreeItem *pInheritedLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItem(pElement->getType()); + if (pInheritedLibraryTreeItem) { + mInheritedClasses.append(pInheritedLibraryTreeItem); + } + } + } + return mInheritedClasses; + } else { + if (!mInheritedClassesLoaded) { + mInheritedClasses.clear(); + // get the inherited classes of the class + QList inheritedClasses = MainWindow::instance()->getOMCProxy()->getInheritedClasses(getNameStructure()); + foreach (QString inheritedClass, inheritedClasses) { + /* If the inherited class is one of the builtin type such as Real we can + * stop here, because the class cannot contain any classes, etc. + * Also check for cyclic loops. + */ + if (!(MainWindow::instance()->getOMCProxy()->isBuiltinType(inheritedClass) || inheritedClass.compare(getNameStructure()) == 0)) { + LibraryTreeItem *pInheritedLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItem(inheritedClass); + if (pInheritedLibraryTreeItem) { + mInheritedClasses.append(pInheritedLibraryTreeItem); + } + } + } + mInheritedClassesLoaded = true; + } + return mInheritedClasses; + } + } else { + return mInheritedClasses; + } +} + QList LibraryTreeItem::getInheritedClassesDeepList() { QList result; @@ -698,15 +742,43 @@ void LibraryTreeItem::setModelWidget(ModelWidget *pModelWidget) mComponentsLoaded = false; } +#define FETCH_COMPONENTS() \ + if (!mComponentsLoaded) { \ + mComponents = MainWindow::instance()->getOMCProxy()->getElements(getNameStructure()); \ + mComponentsLoaded = true; \ + } + const QList &LibraryTreeItem::getComponentsList() { if (mpModelWidget) { - return mpModelWidget->getComponentsList(); - } else { - if (!mComponentsLoaded) { - mComponents = MainWindow::instance()->getOMCProxy()->getElements(getNameStructure()); - mComponentsLoaded = true; + if (mpModelWidget->isNewApi()) { + if (mpModelWidget->isDiagramViewLoaded()) { + QList elements = mpModelWidget->getModelInstance()->getElements(); + // reuse the mComponents list + mComponents.clear(); + foreach (auto pElement, elements) { + if (pElement->isComponent()) { + /* construct the ElementInfo from the new instance API Element + * We only need the name, type and comment. + */ + ElementInfo *pElementInfo = new ElementInfo(); + pElementInfo->setParentClassName(getNameStructure()); + pElementInfo->setName(pElement->getName()); + pElementInfo->setClassName(pElement->getType()); + pElementInfo->setComment(pElement->getComment()); + mComponents.append(pElementInfo); + } + } + return mComponents; + } else { + FETCH_COMPONENTS(); + return mComponents; + } + } else { + return mpModelWidget->getComponentsList(); } + } else { + FETCH_COMPONENTS(); return mComponents; } } diff --git a/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h b/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h index 79e0d8f2436..5d3202b6ada 100644 --- a/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h +++ b/OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h @@ -91,7 +91,7 @@ class LibraryTreeItem : public QObject void setName(QString name) {mName = name;} const QString& getName() const {return mName;} void setNameStructure(QString nameStructure) {mNameStructure = nameStructure;} - const QString& getNameStructure() {return mNameStructure;} + const QString& getNameStructure() const {return mNameStructure;} QString getWhereToMoveFMU(); void updateClassInformation(); void setFileName(QString fileName) {mFileName = fileName;} @@ -169,7 +169,7 @@ class LibraryTreeItem : public QObject void moveChild(int from, int to); void addInheritedClass(LibraryTreeItem *pLibraryTreeItem); void removeInheritedClasses(); - QList getInheritedClasses() const {return mInheritedClasses;} + const QList &getInheritedClasses(); QList getInheritedClassesDeepList(); LibraryTreeItem *getDirectComponentsClass(const QString &name); LibraryTreeItem *getComponentsClass(const QString &name); @@ -194,11 +194,12 @@ class LibraryTreeItem : public QObject OMCInterface::getClassInformation_res mClassInformation; SimulationOptions mSimulationOptions; - const QList &getComponentsList(); + const QList &getComponentsList(); private: bool mIsRootItem; LibraryTreeItem *mpParentLibraryTreeItem = 0; QList mChildren; + bool mInheritedClassesLoaded = false; QList mInheritedClasses; QList mComponents; bool mComponentsLoaded = false; diff --git a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h index 32798ea7f6d..74fb3d4394a 100644 --- a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h +++ b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h @@ -562,6 +562,7 @@ class ModelWidget : public QWidget GraphicsView* getIconGraphicsView() {return mpIconGraphicsView;} UndoStack* getUndoStack() {return mpUndoStack;} BaseEditor* getEditor() {return mpEditor;} + bool isDiagramViewLoaded() const {return mDiagramViewLoaded;} void setModelClassPathLabel(QString path) {mpModelClassPathLabel->setText(path);} void setModelFilePathLabel(QString path) {mpModelFilePathLabel->setText(path);} QVBoxLayout* getMainLayout() {return mpMainLayout;}