Commit 1c491bcd authored by Rhys Weatherley's avatar Rhys Weatherley
Browse files

Sort most relevant swizzle orderings to the top

"xyz", "st", and "rgb" are more likely to be what the user
wants than things like "aaar" and "abbb", so sort the
completion list to move common swizzles to the top.
parent 63849faf
......@@ -43,6 +43,10 @@
using namespace GLSLEditor;
using namespace GLSLEditor::Internal;
enum CompletionOrder {
SpecialMemberOrder = -5
};
static bool isIdentifierChar(QChar ch)
{
return ch.isLetterOrNumber() || ch == QLatin1Char('_');
......@@ -326,6 +330,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
const QIcon symbolIcon = iconForColor(Qt::darkCyan);
QStringList members;
QStringList specialMembers;
if (ch == QLatin1Char('.')) {
QTextCursor tc(edit->document());
......@@ -359,6 +364,16 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
if (const GLSL::VectorType *vecTy = exprTy.type->asVectorType()) {
members = vecTy->members();
// Sort the most relevant swizzle orderings to the top.
specialMembers += QLatin1String("xy");
specialMembers += QLatin1String("xyz");
specialMembers += QLatin1String("xyzw");
specialMembers += QLatin1String("rgb");
specialMembers += QLatin1String("rgba");
specialMembers += QLatin1String("st");
specialMembers += QLatin1String("stp");
specialMembers += QLatin1String("stpq");
} else if (const GLSL::Struct *structTy = exprTy.type->asStructType()) {
members = structTy->members();
......@@ -391,6 +406,8 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
TextEditor::CompletionItem item(this);
item.icon = symbolIcon;
item.text = s;
if (specialMembers.contains(s))
item.order = SpecialMemberOrder;
m_completions.append(item);
}
......@@ -442,6 +459,37 @@ bool CodeCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &
return ICompletionCollector::partiallyComplete(completionItems);
}
bool CodeCompletion::glslCompletionItemLessThan(const TextEditor::CompletionItem &l, const TextEditor::CompletionItem &r)
{
if (l.order != r.order)
return l.order < r.order;
return completionItemLessThan(l, r);
}
QList<TextEditor::CompletionItem> CodeCompletion::getCompletions()
{
QList<TextEditor::CompletionItem> completionItems;
completions(&completionItems);
qStableSort(completionItems.begin(), completionItems.end(), glslCompletionItemLessThan);
// Remove duplicates
QString lastKey;
QVariant lastData;
QList<TextEditor::CompletionItem> uniquelist;
foreach (const TextEditor::CompletionItem &item, completionItems) {
if (item.text != lastKey || item.data.type() != lastData.type()) {
uniquelist.append(item);
lastKey = item.text;
lastData = item.data;
}
}
return uniquelist;
}
bool CodeCompletion::shouldRestartCompletion()
{
return m_restartCompletion;
......
......@@ -87,6 +87,7 @@ public:
*/
virtual bool partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems);
virtual QList<TextEditor::CompletionItem> getCompletions();
virtual bool shouldRestartCompletion();
/* Called when it's safe to clean up the completion items.
......@@ -99,6 +100,8 @@ private:
TextEditor::ITextEditable *m_editor;
int m_startPosition;
bool m_restartCompletion;
static bool glslCompletionItemLessThan(const TextEditor::CompletionItem &l, const TextEditor::CompletionItem &r);
};
} // namespace Internal
......
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