Skip to content

Commit

Permalink
Do not mix annotation completions and other ones
Browse files Browse the repository at this point in the history
Do not annoy user with keywords when completing a class name
  • Loading branch information
atrosinenko authored and adeas31 committed Sep 3, 2019
1 parent 120858d commit a1c86e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
53 changes: 33 additions & 20 deletions OMEdit/OMEdit/OMEditGUI/Editors/ModelicaEditor.cpp
Expand Up @@ -74,29 +74,36 @@ void ModelicaEditor::popUpCompleter()
mpPlainTextEdit->clearCompleter();

QList<CompleterItem> annotations;
getCompletionAnnotations(stringAfterWord("annotation"), annotations);
bool inAnnotation = getCompletionAnnotations(stringAfterWord("annotation"), annotations);
mpPlainTextEdit->insertCompleterSymbols(annotations, ":/Resources/icons/completerAnnotation.svg");

bool startsWithUpperCase = !word.isEmpty() && word[0].isUpper();

if (!word.contains('.') && annotations.empty()) {
QStringList keywords = ModelicaHighlighter::getKeywords();
mpPlainTextEdit->insertCompleterKeywords(keywords);
if (!word.contains('.') && !inAnnotation) {
// Suppose if user specially entered an upper case first letter,
// it is definitely not a keyword...
if (!startsWithUpperCase) {
QStringList keywords = ModelicaHighlighter::getKeywords();
mpPlainTextEdit->insertCompleterKeywords(keywords);
QList<CompleterItem> codesnippets = getCodeSnippets();
mpPlainTextEdit->insertCompleterCodeSnippets(codesnippets);
}
QStringList types = ModelicaHighlighter::getTypes();
mpPlainTextEdit->insertCompleterTypes(types);
QList<CompleterItem> codesnippets = getCodeSnippets();
mpPlainTextEdit->insertCompleterCodeSnippets(codesnippets);
}
QList<CompleterItem> classes, components;
getCompletionSymbols(word, classes, components);
if (!inAnnotation) {
QList<CompleterItem> classes, components;
getCompletionSymbols(word, classes, components);

std::sort(classes.begin(), classes.end());
classes.erase(std::unique(classes.begin(), classes.end()), classes.end());
std::sort(classes.begin(), classes.end());
classes.erase(std::unique(classes.begin(), classes.end()), classes.end());

std::sort(components.begin(), components.end());
components.erase(std::unique(components.begin(), components.end()), components.end());
std::sort(components.begin(), components.end());
components.erase(std::unique(components.begin(), components.end()), components.end());

mpPlainTextEdit->insertCompleterSymbols(classes, ":/Resources/icons/completerClass.svg");
mpPlainTextEdit->insertCompleterSymbols(components, ":/Resources/icons/completerComponent.svg");

mpPlainTextEdit->insertCompleterSymbols(annotations, ":/Resources/icons/completerAnnotation.svg");
mpPlainTextEdit->insertCompleterSymbols(classes, ":/Resources/icons/completerClass.svg");
mpPlainTextEdit->insertCompleterSymbols(components, ":/Resources/icons/completerComponent.svg");
}

QCompleter *completer = mpPlainTextEdit->completer();
QRect cr = mpPlainTextEdit->cursorRect();
Expand Down Expand Up @@ -258,19 +265,24 @@ void ModelicaEditor::getCompletionAnnotations(const QStringList &stack, QList<Co
* \brief Resolves the annotation under cursor as a stack of nested names and returns completions
* \param str A string starting with the "annotation" word up to the cursor position
* \param annotations Resulting collection of completion items
* \return Whether current cursor position is considered inside an annotation
*/
void ModelicaEditor::getCompletionAnnotations(const QString &str, QList<CompleterItem> &annotations)
bool ModelicaEditor::getCompletionAnnotations(const QString &str, QList<CompleterItem> &annotations)
{
QStringList stack;
int lastWordStart = 0;
bool insideWord = false;

if (str.isEmpty())
return;
return false;

for (int i = 0; i < str.size(); ++i) {
QChar ch = str[i];

// do not prevent other completions in case of unrelated unfinished annotation
if (ch == ';')
return false;

// First, handle string literals
if (ch == '"') {
for (++i; i < str.size() && str[i] != '"'; ++i) {
Expand All @@ -288,7 +300,7 @@ void ModelicaEditor::getCompletionAnnotations(const QString &str, QList<Complete
}
if (ch == ')') {
if (stack.isEmpty()) {
return; // not in an annotation at all
return false; // not in an annotation at all
}
stack.pop_back();
}
Expand All @@ -302,10 +314,11 @@ void ModelicaEditor::getCompletionAnnotations(const QString &str, QList<Complete
}

if (stack.isEmpty()) {
return;
return false;
}
stack.pop_front(); // pop 'annotation'
getCompletionAnnotations(stack, annotations);
return true;
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEdit/OMEditGUI/Editors/ModelicaEditor.h
Expand Up @@ -65,7 +65,7 @@ class ModelicaEditor : public BaseEditor
void getCompletionSymbols(QString word, QList<CompleterItem> &classes, QList<CompleterItem> &components);
LibraryTreeItem *getAnnotationCompletionRoot();
void getCompletionAnnotations(const QStringList &stack, QList<CompleterItem> &annotations);
void getCompletionAnnotations(const QString &str, QList<CompleterItem> &annotations);
bool getCompletionAnnotations(const QString &str, QList<CompleterItem> &annotations);
static QList<CompleterItem> getCodeSnippets();
private:
QString mLastValidText;
Expand Down

0 comments on commit a1c86e5

Please sign in to comment.