From b20ffaa10946f8c895955847fcd471c89d073294 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Mon, 27 Jun 2022 09:34:44 +0200 Subject: [PATCH] LanguageClient: sort outline items The order we get from the server might not be in the order of appearance in the document. Fixes: QTCREATORBUG-4346 Change-Id: I0badba7fd40619b2aa20a81b6a86b43dc9e6a1b1 Reviewed-by: Christian Kandeler --- .../languageclient/languageclientoutline.cpp | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/plugins/languageclient/languageclientoutline.cpp b/src/plugins/languageclient/languageclientoutline.cpp index 1ba12cee50a..ba5602e9c26 100644 --- a/src/plugins/languageclient/languageclientoutline.cpp +++ b/src/plugins/languageclient/languageclientoutline.cpp @@ -48,6 +48,23 @@ using namespace LanguageServerProtocol; namespace LanguageClient { +const QList sortedSymbols(const QList &symbols) +{ + auto result = symbols; + Utils::sort(result, [](const SymbolInformation &a, const SymbolInformation &b){ + return a.location().range().start() < b.location().range().start(); + }); + return result; +} +const QList sortedSymbols(const QList &symbols) +{ + auto result = symbols; + Utils::sort(result, [](const DocumentSymbol &a, const DocumentSymbol &b){ + return a.range().start() < b.range().start(); + }); + return result; +} + class LanguageClientOutlineItem : public Utils::TypedTreeItem { public: @@ -65,7 +82,9 @@ public: , m_symbolStringifier(stringifier) , m_type(info.kind()) { - for (const DocumentSymbol &child : info.children().value_or(QList())) + const QList children = sortedSymbols( + info.children().value_or(QList())); + for (const DocumentSymbol &child : children) appendChild(new LanguageClientOutlineItem(child, stringifier)); } @@ -103,13 +122,13 @@ public: void setInfo(const QList &info) { clear(); - for (const SymbolInformation &symbol : info) + for (const SymbolInformation &symbol : sortedSymbols(info)) rootItem()->appendChild(new LanguageClientOutlineItem(symbol)); } void setInfo(const QList &info) { clear(); - for (const DocumentSymbol &symbol : info) + for (const DocumentSymbol &symbol : sortedSymbols(info)) rootItem()->appendChild(new LanguageClientOutlineItem(symbol, m_symbolStringifier)); }