Skip to content

Commit

Permalink
autocompletion for codesnippets
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 authored and adeas31 committed May 2, 2017
1 parent 1b6ac20 commit e74c088
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 10 deletions.
77 changes: 69 additions & 8 deletions OMEdit/OMEditGUI/Editors/BaseEditor.cpp
Expand Up @@ -685,7 +685,7 @@ PlainTextEdit::PlainTextEdit(BaseEditor *pBaseEditor)
mpCompleter->setWrapAround(false);
mpCompleter->setWidget(this);
mpCompleter->setCompletionMode(QCompleter::PopupCompletion);
connect(mpCompleter, SIGNAL(activated(QString)), this, SLOT(insertCompletion(QString)));
connect(mpCompleter, SIGNAL(activated(QModelIndex)), this, SLOT(insertCompletionItem(QModelIndex)));
updateLineNumberAreaWidth(0);
updateHighlights();
updateCursorPosition();
Expand All @@ -701,6 +701,24 @@ PlainTextEdit::PlainTextEdit(BaseEditor *pBaseEditor)
connect(this, SIGNAL(customContextMenuRequested(QPoint)), mpBaseEditor, SLOT(showContextMenu(QPoint)));
}

/*!
* \class CompleterItem
* The constructor is set from outside depending on which editor is used (eg.) MetaModelicaEditor,
* ModelicaEditor,CEditor etc..
*/

CompleterItem::CompleterItem()
{

}

CompleterItem::CompleterItem(QString key, QString value, QString select)
{
mKey=key;
mValue=value;
mSelect=select;
}

/*!
* \brief PlainTextEdit::insertCompleterKeywords
* add Keyword list to the QStandardItemModel which will be used by the Completer
Expand All @@ -714,7 +732,8 @@ void PlainTextEdit::insertCompleterKeywords(QStringList keywords)
QStandardItem *pStandardItem = new QStandardItem(keywords[i]);
pStandardItem->setIcon(QIcon(":/Resources/icons/completerkeyword.svg"));
pStandardItem->setToolTip("keywords");
mpStandardItemModel->setItem(i, 0, pStandardItem);
pStandardItem->setData(QVariant::fromValue(CompleterItem(keywords[i],keywords[i],"")),Qt::UserRole);
mpStandardItemModel->appendRow(pStandardItem);
}
}

Expand All @@ -731,6 +750,19 @@ void PlainTextEdit::insertCompleterTypes(QStringList types)
QStandardItem *pStandardItem = new QStandardItem(types[k]);
pStandardItem->setIcon(QIcon(":/Resources/icons/completerType.svg"));
pStandardItem->setToolTip("types");
pStandardItem->setData(QVariant::fromValue(CompleterItem(types[k],types[k],"")),Qt::UserRole);
mpStandardItemModel->appendRow(pStandardItem);
}
}

void PlainTextEdit::insertCompleterCodeSnippets(QList<CompleterItem> items)
{
for (int var = 0; var < items.length(); ++var)
{
QStandardItem *pStandardItem = new QStandardItem(items[var].mKey);
pStandardItem->setIcon(QIcon(":/Resources/icons/completerCodeSnippets.svg"));
pStandardItem->setToolTip("codeSnippets");
pStandardItem->setData(QVariant::fromValue(items[var]),Qt::UserRole);
mpStandardItemModel->appendRow(pStandardItem);
}
}
Expand Down Expand Up @@ -1442,16 +1474,45 @@ QCompleter *PlainTextEdit::completer()
return mpCompleter;
}

/* insert the keywords from the completer popup */
void PlainTextEdit::insertCompletion(const QString& completion)
/*!
* PlainTextEdit::insertCompletionItem
* insert the completerItems from the completer popup
*/
void PlainTextEdit::insertCompletionItem(const QModelIndex &index)
{
QVariant value = index.data(Qt::UserRole);
CompleterItem completerItem = qvariant_cast<CompleterItem>(value);
QString selectiontext = completerItem.mSelect;
QStringList completionlength = completerItem.mValue.split("\n");
QTextCursor cursor = textCursor();
int extra = completion.length() - mpCompleter->completionPrefix().length();
//cursor.movePosition(QTextCursor::Left);
int extra = completionlength[0].length() - mpCompleter->completionPrefix().length();
cursor.movePosition(QTextCursor::EndOfWord);
cursor.insertText(completion.right(extra));
cursor.insertText(completionlength[0].right(extra));
// store the cursor position to be used for selecting text when inserting code snippets
int currentpos = cursor.position();
int startpos = currentpos-completionlength[0].length();
setTextCursor(cursor);
}
// To insert CodeSnippets
if (completionlength.length()>1)
{
// Calculate the indentation spaces for the inserted text
TabSettings tabSettings = OptionsDialog::instance()->getTabSettings();
cursor.insertText("\n");
const QTextBlock previousBlock = cursor.block().previous();
QString indentText = previousBlock.text();
cursor.deletePreviousChar();
for (int var = 1; var < completionlength.length(); ++var) {
cursor.insertText("\n");
cursor.insertText(indentText.left(tabSettings.firstNonSpace(indentText)));
cursor.insertText(completionlength[var]);
}
// set the cursor to appropriate selection text
int indexpos=completionlength[0].indexOf(selectiontext,0); //find the index position of the selection text from the word
cursor.setPosition(startpos+indexpos,QTextCursor::MoveAnchor);
cursor.setPosition(startpos+indexpos+selectiontext.length(), QTextCursor::KeepAnchor);
setTextCursor(cursor);
}
}

QString PlainTextEdit::textUnderCursor() const
{
Expand Down
15 changes: 14 additions & 1 deletion OMEdit/OMEditGUI/Editors/BaseEditor.h
Expand Up @@ -210,6 +210,18 @@ class BaseEditorDocumentLayout : public QPlainTextDocumentLayout
bool mHasBreakpoint;
};

class CompleterItem
{
public:
CompleterItem();
CompleterItem(QString key , QString value , QString select);
QString mKey;
QString mValue;
QString mSelect;
};

Q_DECLARE_METATYPE(CompleterItem)

class BaseEditor;
class QCompleter;
class PlainTextEdit : public QPlainTextEdit
Expand All @@ -220,6 +232,7 @@ class PlainTextEdit : public QPlainTextEdit
LineNumberArea* getLineNumberArea() {return mpLineNumberArea;}
void insertCompleterKeywords(QStringList keywords);
void insertCompleterTypes(QStringList types);
void insertCompleterCodeSnippets(QList<CompleterItem> items);
void setCanHaveBreakpoints(bool canHaveBreakpoints);
bool canHaveBreakpoints() {return mCanHaveBreakpoints;}
int lineNumberAreaWidth();
Expand Down Expand Up @@ -249,7 +262,7 @@ class PlainTextEdit : public QPlainTextEdit
void toggleBlockVisible(const QTextBlock &block);
QString textUnderCursor() const;
private slots:
void insertCompletion(const QString &completion);
void insertCompletionItem(const QModelIndex & index);
public slots:
void updateLineNumberAreaWidth(int newBlockCount);
void updateLineNumberArea(const QRect &rect, int dy);
Expand Down
30 changes: 29 additions & 1 deletion OMEdit/OMEditGUI/Editors/MetaModelicaEditor.cpp
Expand Up @@ -45,6 +45,8 @@ MetaModelicaEditor::MetaModelicaEditor(QWidget *pParent)
mpDocumentMarker = new DocumentMarker(mpPlainTextEdit->document());
QStringList keywords = MetaModelicaHighlighter::getKeywords();
QStringList types = MetaModelicaHighlighter::getTypes();
QList<CompleterItem> codesnippets = getCodeSnippets();
mpPlainTextEdit->insertCompleterCodeSnippets(codesnippets);
mpPlainTextEdit->insertCompleterKeywords(keywords);
mpPlainTextEdit->insertCompleterTypes(types);
}
Expand Down Expand Up @@ -77,6 +79,32 @@ void MetaModelicaEditor::popUpCompleter()
completer->complete(cr);
}

/*!
* \brief MetaModelicaEditor::getCodeSnippets()
* returns the list of CompleterItem to the autocompleter
*/

QList<CompleterItem> MetaModelicaEditor::getCodeSnippets()
{
QList<CompleterItem> codesnippetslist;
codesnippetslist << CompleterItem("match" ,"match (control)\n" " case (condition) then (value);\n" " case (condition) then (value);\n" "end match;", "control")
<< CompleterItem("matchcontinue" ,"matchcontinue (control)\n" " case (condition) then (value);\n" " case (condition) then (value);\n" "end matchcontinue;", "control")
<< CompleterItem("function" ,"function name\n" "end name;", "name")
<< CompleterItem("block" ,"block name\n" "end name;", "name")
<< CompleterItem("model" ,"model name\n" "end name;", "name")
<< CompleterItem("class" ,"class name\n" "end name;", "name")
<< CompleterItem("connector" ,"connector name\n" "end name;", "name")
<< CompleterItem("package" ,"package name\n" "end name;", "name")
<< CompleterItem("record" ,"record name\n" "end name;", "name")
<< CompleterItem("while" ,"while condition loop\n" "end while;", "condition")
<< CompleterItem("if" ,"if condition then\n" "end if;", "condition")
<< CompleterItem("if" ,"if condition then\n" "elseif condition then\n" "else\n" "end if;", "condition")
<< CompleterItem("for" ,"for condition loop\n" "end for;", "condition")
<< CompleterItem("when", "when condition then\n" "end when;", "condition")
<< CompleterItem("when", "when condition then\n" "elsewhen condition then\n" "end when;", "condition");
return codesnippetslist;
}

/*!
* \brief MetaModelicaEditor::showContextMenu
* Create a context menu.
Expand Down Expand Up @@ -240,7 +268,7 @@ QStringList MetaModelicaHighlighter::getKeywords()
<< "when"
<< "while"
<< "within"
/* MetaModelica specific keywords */
/* MetaModelica specific keywords */
<< "as"
<< "case"
<< "continue"
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Editors/MetaModelicaEditor.h
Expand Up @@ -45,6 +45,7 @@ class MetaModelicaEditor : public BaseEditor
MetaModelicaEditor(QWidget *pParent);
void setPlainText(const QString &text);
virtual void popUpCompleter();
static QList<CompleterItem> getCodeSnippets();
private slots:
virtual void showContextMenu(QPoint point);
public slots:
Expand Down
24 changes: 24 additions & 0 deletions OMEdit/OMEditGUI/Editors/ModelicaEditor.cpp
Expand Up @@ -58,6 +58,8 @@ ModelicaEditor::ModelicaEditor(QWidget *pParent)
mpDocumentMarker = new DocumentMarker(mpPlainTextEdit->document());
QStringList keywords = ModelicaHighlighter::getKeywords();
QStringList types = ModelicaHighlighter::getTypes();
QList<CompleterItem> codesnippets = getCodeSnippets();
mpPlainTextEdit->insertCompleterCodeSnippets(codesnippets);
mpPlainTextEdit->insertCompleterKeywords(keywords);
mpPlainTextEdit->insertCompleterTypes(types);
}
Expand All @@ -74,6 +76,28 @@ void ModelicaEditor::popUpCompleter()
completer->complete(cr);
}

/*!
* \brief ModelicaEditor::getCodeSnippets()
* returns the list of CompleterItems to the autocompleter
*/
QList<CompleterItem> ModelicaEditor::getCodeSnippets()
{
QList<CompleterItem> codesnippetslist;
codesnippetslist << CompleterItem("function" ,"function name\n" "end name;", "name")
<< CompleterItem("block" ,"block name\n" "end name;", "name")
<< CompleterItem("model" ,"model name\n" "end name;", "name")
<< CompleterItem("class" ,"class name\n" "end name;", "name")
<< CompleterItem("connector" ,"connector name\n" "end name;", "name")
<< CompleterItem("package" ,"package name\n" "end name;", "name")
<< CompleterItem("record" ,"record name\n" "end name;", "name")
<< CompleterItem("while" ,"while condition loop\n" "end while;", "condition")
<< CompleterItem("if" ,"if condition then\n" "end if;", "condition")
<< CompleterItem("if" ,"if condition then\n" "elseif condition then\n" "else\n" "end if;", "condition")
<< CompleterItem("for" ,"for condition loop\n" "end for;", "condition")
<< CompleterItem("when", "when condition then\n" "end when;", "condition")
<< CompleterItem("when", "when condition then\n" "elsewhen condition then\n" "end when;", "condition");
return codesnippetslist;
}
/*!
* \brief ModelicaEditor::getClassNames
* Uses the OMC parseString API to check the class names inside the Modelica Text
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Editors/ModelicaEditor.h
Expand Up @@ -58,6 +58,7 @@ class ModelicaEditor : public BaseEditor
void setTextChanged(bool changed) {mTextChanged = changed;}
bool isTextChanged() {return mTextChanged;}
virtual void popUpCompleter();
static QList<CompleterItem> getCodeSnippets();
private:
QString mLastValidText;
bool mTextChanged;
Expand Down
63 changes: 63 additions & 0 deletions OMEdit/OMEditGUI/Resources/icons/completerCodeSnippets.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/resource_omedit.qrc
Expand Up @@ -177,5 +177,6 @@
<file>Resources/icons/traceability.svg</file>
<file>Resources/icons/completerkeyword.svg</file>
<file>Resources/icons/completerType.svg</file>
<file>Resources/icons/completerCodeSnippets.svg</file>
</qresource>
</RCC>

0 comments on commit e74c088

Please sign in to comment.