Skip to content

Commit

Permalink
Update the auto-complete functionality for instance API (#12104)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Mar 13, 2024
1 parent 5cd2141 commit f02e3af
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
4 changes: 3 additions & 1 deletion OMEdit/OMEditLIB/Editors/ModelicaEditor.cpp
Expand Up @@ -155,7 +155,9 @@ QList<LibraryTreeItem*> ModelicaEditor::getCandidateContexts(QStringList nameCom
QList<LibraryTreeItem*> roots;
LibraryTreeItem *pItem = getModelWidget()->getLibraryTreeItem();
while (pItem) {
roots.append(pItem->getInheritedClassesDeepList());
if (!pItem->isRootItem()) {
roots.append(pItem->getInheritedClassesDeepList());
}
pItem = pItem->parent();
}

Expand Down
2 changes: 0 additions & 2 deletions OMEdit/OMEditLIB/Element/Element.cpp
Expand Up @@ -288,8 +288,6 @@ void ElementInfo::parseElementInfoString(QString value)
}
}



/*!
* \brief ElementInfo::fetchParameterValue
* Fetches the Element parameter value if any.
Expand Down
82 changes: 77 additions & 5 deletions OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -681,6 +681,50 @@ void LibraryTreeItem::removeInheritedClasses()
mInheritedClasses.clear();
}

const QList<LibraryTreeItem*> &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<ModelInstance::Element*> 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<QString> 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*> LibraryTreeItem::getInheritedClassesDeepList()
{
QList<LibraryTreeItem*> result;
Expand All @@ -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<ElementInfo*> &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<ModelInstance::Element*> 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;
}
}
Expand Down
7 changes: 4 additions & 3 deletions OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h
Expand Up @@ -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;}
Expand Down Expand Up @@ -169,7 +169,7 @@ class LibraryTreeItem : public QObject
void moveChild(int from, int to);
void addInheritedClass(LibraryTreeItem *pLibraryTreeItem);
void removeInheritedClasses();
QList<LibraryTreeItem*> getInheritedClasses() const {return mInheritedClasses;}
const QList<LibraryTreeItem*> &getInheritedClasses();
QList<LibraryTreeItem*> getInheritedClassesDeepList();
LibraryTreeItem *getDirectComponentsClass(const QString &name);
LibraryTreeItem *getComponentsClass(const QString &name);
Expand All @@ -194,11 +194,12 @@ class LibraryTreeItem : public QObject

OMCInterface::getClassInformation_res mClassInformation;
SimulationOptions mSimulationOptions;
const QList<ElementInfo *> &getComponentsList();
const QList<ElementInfo*> &getComponentsList();
private:
bool mIsRootItem;
LibraryTreeItem *mpParentLibraryTreeItem = 0;
QList<LibraryTreeItem*> mChildren;
bool mInheritedClassesLoaded = false;
QList<LibraryTreeItem*> mInheritedClasses;
QList<ElementInfo*> mComponents;
bool mComponentsLoaded = false;
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -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;}
Expand Down

0 comments on commit f02e3af

Please sign in to comment.