2018-11-23 09:45:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2018 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "languageclientoutline.h"
|
|
|
|
|
|
|
|
|
|
#include "languageclientmanager.h"
|
2019-04-05 10:05:25 +02:00
|
|
|
#include "languageclientutils.h"
|
2018-11-23 09:45:51 +01:00
|
|
|
|
|
|
|
|
#include <coreplugin/find/itemviewfind.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
|
|
|
|
#include <languageserverprotocol/languagefeatures.h>
|
|
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <texteditor/texteditor.h>
|
|
|
|
|
#include <utils/itemviews.h>
|
|
|
|
|
#include <utils/mimetypes/mimedatabase.h>
|
|
|
|
|
#include <utils/treemodel.h>
|
2020-01-27 14:52:46 +01:00
|
|
|
#include <utils/treeviewcombobox.h>
|
2018-11-23 09:45:51 +01:00
|
|
|
#include <utils/utilsicons.h>
|
|
|
|
|
|
|
|
|
|
#include <QBoxLayout>
|
|
|
|
|
|
|
|
|
|
using namespace LanguageServerProtocol;
|
|
|
|
|
|
|
|
|
|
namespace LanguageClient {
|
|
|
|
|
|
|
|
|
|
class LanguageClientOutlineItem : public Utils::TypedTreeItem<LanguageClientOutlineItem>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
LanguageClientOutlineItem() = default;
|
|
|
|
|
LanguageClientOutlineItem(const SymbolInformation &info)
|
|
|
|
|
: m_name(info.name())
|
|
|
|
|
, m_range(info.location().range())
|
|
|
|
|
, m_type(info.kind())
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
LanguageClientOutlineItem(const DocumentSymbol &info)
|
|
|
|
|
: m_name(info.name())
|
|
|
|
|
, m_detail(info.detail().value_or(QString()))
|
|
|
|
|
, m_range(info.range())
|
|
|
|
|
, m_type(info.kind())
|
|
|
|
|
{
|
|
|
|
|
for (const DocumentSymbol &child : info.children().value_or(QList<DocumentSymbol>()))
|
|
|
|
|
appendChild(new LanguageClientOutlineItem(child));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TreeItem interface
|
|
|
|
|
QVariant data(int column, int role) const override
|
|
|
|
|
{
|
|
|
|
|
switch (role) {
|
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
|
return symbolIcon(m_type);
|
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
|
return m_name;
|
|
|
|
|
default:
|
|
|
|
|
return Utils::TreeItem::data(column, role);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Position pos() const { return m_range.start(); }
|
|
|
|
|
bool contains(const Position &pos) const { return m_range.contains(pos); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QString m_name;
|
|
|
|
|
QString m_detail;
|
|
|
|
|
Range m_range;
|
|
|
|
|
int m_type = -1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class LanguageClientOutlineModel : public Utils::TreeModel<LanguageClientOutlineItem>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using Utils::TreeModel<LanguageClientOutlineItem>::TreeModel;
|
|
|
|
|
void setInfo(const QList<SymbolInformation> &info)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
for (const SymbolInformation &symbol : info)
|
|
|
|
|
rootItem()->appendChild(new LanguageClientOutlineItem(symbol));
|
|
|
|
|
}
|
|
|
|
|
void setInfo(const QList<DocumentSymbol> &info)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
for (const DocumentSymbol &symbol : info)
|
|
|
|
|
rootItem()->appendChild(new LanguageClientOutlineItem(symbol));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class LanguageClientOutlineWidget : public TextEditor::IOutlineWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
2019-01-31 12:15:43 +01:00
|
|
|
LanguageClientOutlineWidget(Client *client, TextEditor::BaseTextEditor *editor);
|
2018-11-23 09:45:51 +01:00
|
|
|
|
|
|
|
|
// IOutlineWidget interface
|
|
|
|
|
public:
|
|
|
|
|
QList<QAction *> filterMenuActions() const override;
|
|
|
|
|
void setCursorSynchronization(bool syncWithCursor) override;
|
|
|
|
|
|
|
|
|
|
private:
|
2019-04-04 14:36:28 +02:00
|
|
|
void handleResponse(const DocumentUri &uri, const DocumentSymbolsResult &response);
|
2018-11-23 09:45:51 +01:00
|
|
|
void updateTextCursor(const QModelIndex &proxyIndex);
|
|
|
|
|
void updateSelectionInTree(const QTextCursor ¤tCursor);
|
|
|
|
|
void onItemActivated(const QModelIndex &index);
|
|
|
|
|
|
2019-01-31 12:15:43 +01:00
|
|
|
QPointer<Client> m_client;
|
2018-11-23 09:45:51 +01:00
|
|
|
QPointer<TextEditor::BaseTextEditor> m_editor;
|
|
|
|
|
LanguageClientOutlineModel m_model;
|
|
|
|
|
Utils::TreeView m_view;
|
2019-04-04 14:36:28 +02:00
|
|
|
DocumentUri m_uri;
|
2018-11-23 09:45:51 +01:00
|
|
|
bool m_sync = false;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-31 12:15:43 +01:00
|
|
|
LanguageClientOutlineWidget::LanguageClientOutlineWidget(Client *client,
|
2018-11-23 09:45:51 +01:00
|
|
|
TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: m_client(client)
|
|
|
|
|
, m_editor(editor)
|
|
|
|
|
, m_view(this)
|
2019-09-11 14:34:20 +02:00
|
|
|
, m_uri(DocumentUri::fromFilePath(editor->textDocument()->filePath()))
|
2018-11-23 09:45:51 +01:00
|
|
|
{
|
2019-04-04 14:36:28 +02:00
|
|
|
connect(client->documentSymbolCache(),
|
|
|
|
|
&DocumentSymbolCache::gotSymbols,
|
|
|
|
|
this,
|
|
|
|
|
&LanguageClientOutlineWidget::handleResponse);
|
2019-05-13 15:20:24 +02:00
|
|
|
connect(editor->textDocument(), &TextEditor::TextDocument::contentsChanged, this, [this]() {
|
|
|
|
|
if (m_client)
|
|
|
|
|
m_client->documentSymbolCache()->requestSymbols(m_uri);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-04 14:36:28 +02:00
|
|
|
client->documentSymbolCache()->requestSymbols(m_uri);
|
2018-11-23 09:45:51 +01:00
|
|
|
|
|
|
|
|
auto *layout = new QVBoxLayout;
|
2019-08-29 10:36:01 +02:00
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
2018-11-23 09:45:51 +01:00
|
|
|
layout->setSpacing(0);
|
|
|
|
|
layout->addWidget(Core::ItemViewFind::createSearchableWrapper(&m_view));
|
|
|
|
|
setLayout(layout);
|
|
|
|
|
m_view.setModel(&m_model);
|
|
|
|
|
m_view.setHeaderHidden(true);
|
|
|
|
|
connect(&m_view, &QAbstractItemView::activated,
|
|
|
|
|
this, &LanguageClientOutlineWidget::onItemActivated);
|
|
|
|
|
connect(m_editor->editorWidget(), &TextEditor::TextEditorWidget::cursorPositionChanged,
|
|
|
|
|
this, [this](){
|
|
|
|
|
if (m_sync)
|
|
|
|
|
updateSelectionInTree(m_editor->textCursor());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QAction *> LanguageClientOutlineWidget::filterMenuActions() const
|
|
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LanguageClientOutlineWidget::setCursorSynchronization(bool syncWithCursor)
|
|
|
|
|
{
|
|
|
|
|
m_sync = syncWithCursor;
|
|
|
|
|
if (m_sync && m_editor)
|
|
|
|
|
updateSelectionInTree(m_editor->textCursor());
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 14:36:28 +02:00
|
|
|
void LanguageClientOutlineWidget::handleResponse(const DocumentUri &uri,
|
|
|
|
|
const DocumentSymbolsResult &result)
|
2018-11-23 09:45:51 +01:00
|
|
|
{
|
2019-04-04 14:36:28 +02:00
|
|
|
if (uri != m_uri)
|
|
|
|
|
return;
|
|
|
|
|
if (Utils::holds_alternative<QList<SymbolInformation>>(result))
|
|
|
|
|
m_model.setInfo(Utils::get<QList<SymbolInformation>>(result));
|
|
|
|
|
else if (Utils::holds_alternative<QList<DocumentSymbol>>(result))
|
|
|
|
|
m_model.setInfo(Utils::get<QList<DocumentSymbol>>(result));
|
|
|
|
|
else
|
|
|
|
|
m_model.clear();
|
2018-11-23 09:45:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LanguageClientOutlineWidget::updateTextCursor(const QModelIndex &proxyIndex)
|
|
|
|
|
{
|
|
|
|
|
LanguageClientOutlineItem *item = m_model.itemForIndex(proxyIndex);
|
|
|
|
|
const Position &pos = item->pos();
|
|
|
|
|
// line has to be 1 based, column 0 based!
|
|
|
|
|
m_editor->editorWidget()->gotoLine(pos.line() + 1, pos.character(), true, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LanguageClientOutlineWidget::updateSelectionInTree(const QTextCursor ¤tCursor)
|
|
|
|
|
{
|
|
|
|
|
QItemSelection selection;
|
|
|
|
|
const Position pos(currentCursor);
|
|
|
|
|
m_model.forAllItems([&](const LanguageClientOutlineItem *item) {
|
|
|
|
|
if (item->contains(pos))
|
|
|
|
|
selection.select(m_model.indexForItem(item), m_model.indexForItem(item));
|
|
|
|
|
});
|
|
|
|
|
m_view.selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
|
2019-05-17 08:29:18 +02:00
|
|
|
if (!selection.isEmpty())
|
|
|
|
|
m_view.scrollTo(selection.indexes().first());
|
2018-11-23 09:45:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LanguageClientOutlineWidget::onItemActivated(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid() || !m_editor)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
updateTextCursor(index);
|
|
|
|
|
m_editor->widget()->setFocus();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 14:52:46 +01:00
|
|
|
bool LanguageClientOutlineWidgetFactory::clientSupportsDocumentSymbols(
|
|
|
|
|
const Client *client, const TextEditor::TextDocument *doc)
|
2018-11-23 09:45:51 +01:00
|
|
|
{
|
2019-07-24 13:40:12 +02:00
|
|
|
if (!client)
|
|
|
|
|
return false;
|
2018-11-23 09:45:51 +01:00
|
|
|
DynamicCapabilities dc = client->dynamicCapabilities();
|
|
|
|
|
if (dc.isRegistered(DocumentSymbolsRequest::methodName).value_or(false)) {
|
|
|
|
|
TextDocumentRegistrationOptions options(dc.option(DocumentSymbolsRequest::methodName));
|
|
|
|
|
return !options.isValid(nullptr)
|
|
|
|
|
|| options.filterApplies(doc->filePath(), Utils::mimeTypeForName(doc->mimeType()));
|
|
|
|
|
}
|
|
|
|
|
return client->capabilities().documentSymbolProvider().value_or(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LanguageClientOutlineWidgetFactory::supportsEditor(Core::IEditor *editor) const
|
|
|
|
|
{
|
|
|
|
|
auto doc = qobject_cast<TextEditor::TextDocument *>(editor->document());
|
|
|
|
|
if (!doc)
|
|
|
|
|
return false;
|
2019-07-24 13:40:12 +02:00
|
|
|
return clientSupportsDocumentSymbols(LanguageClientManager::clientForDocument(doc), doc);
|
2018-11-23 09:45:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::IOutlineWidget *LanguageClientOutlineWidgetFactory::createWidget(Core::IEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor);
|
|
|
|
|
QTC_ASSERT(textEditor, return nullptr);
|
2019-07-24 13:40:12 +02:00
|
|
|
Client *client = LanguageClientManager::clientForDocument(textEditor->textDocument());
|
|
|
|
|
if (!client || !clientSupportsDocumentSymbols(client, textEditor->textDocument()))
|
|
|
|
|
return nullptr;
|
|
|
|
|
return new LanguageClientOutlineWidget(client, textEditor);
|
2018-11-23 09:45:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-27 14:52:46 +01:00
|
|
|
class OutlineComboBox : public Utils::TreeViewComboBox
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
OutlineComboBox(Client *client, TextEditor::BaseTextEditor *editor);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void updateModel(const DocumentUri &resultUri, const DocumentSymbolsResult &result);
|
|
|
|
|
void updateEntry();
|
|
|
|
|
void activateEntry();
|
|
|
|
|
void requestSymbols();
|
|
|
|
|
|
|
|
|
|
LanguageClientOutlineModel m_model;
|
|
|
|
|
QPointer<Client> m_client;
|
|
|
|
|
TextEditor::TextEditorWidget *m_editorWidget;
|
|
|
|
|
const DocumentUri m_uri;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Utils::TreeViewComboBox *LanguageClientOutlineWidgetFactory::createComboBox(Client *client,
|
|
|
|
|
Core::IEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor);
|
|
|
|
|
QTC_ASSERT(textEditor, return nullptr);
|
|
|
|
|
TextEditor::TextDocument *document = textEditor->textDocument();
|
|
|
|
|
if (!client || !clientSupportsDocumentSymbols(client, document))
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
return new OutlineComboBox(client, textEditor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OutlineComboBox::OutlineComboBox(Client *client, TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: m_client(client)
|
|
|
|
|
, m_editorWidget(editor->editorWidget())
|
|
|
|
|
, m_uri(DocumentUri::fromFilePath(editor->document()->filePath()))
|
|
|
|
|
{
|
|
|
|
|
setModel(&m_model);
|
|
|
|
|
setMinimumContentsLength(13);
|
|
|
|
|
QSizePolicy policy = sizePolicy();
|
|
|
|
|
policy.setHorizontalPolicy(QSizePolicy::Expanding);
|
|
|
|
|
setSizePolicy(policy);
|
|
|
|
|
setMaxVisibleItems(40);
|
|
|
|
|
|
|
|
|
|
connect(client->documentSymbolCache(), &DocumentSymbolCache::gotSymbols,
|
|
|
|
|
this, &OutlineComboBox::updateModel);
|
|
|
|
|
connect(editor->textDocument(), &TextEditor::TextDocument::contentsChanged,
|
|
|
|
|
this, &OutlineComboBox::requestSymbols);
|
|
|
|
|
connect(m_editorWidget, &TextEditor::TextEditorWidget::cursorPositionChanged,
|
|
|
|
|
this, &OutlineComboBox::updateEntry);
|
|
|
|
|
connect(this, QOverload<int>::of(&QComboBox::activated), this, &OutlineComboBox::activateEntry);
|
2020-02-28 14:46:11 +01:00
|
|
|
|
|
|
|
|
requestSymbols();
|
2020-01-27 14:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OutlineComboBox::updateModel(const DocumentUri &resultUri, const DocumentSymbolsResult &result)
|
|
|
|
|
{
|
|
|
|
|
if (m_uri != resultUri)
|
|
|
|
|
return;
|
|
|
|
|
if (Utils::holds_alternative<QList<SymbolInformation>>(result))
|
|
|
|
|
m_model.setInfo(Utils::get<QList<SymbolInformation>>(result));
|
|
|
|
|
else if (Utils::holds_alternative<QList<DocumentSymbol>>(result))
|
|
|
|
|
m_model.setInfo(Utils::get<QList<DocumentSymbol>>(result));
|
|
|
|
|
else
|
|
|
|
|
m_model.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OutlineComboBox::updateEntry()
|
|
|
|
|
{
|
|
|
|
|
const Position pos(m_editorWidget->textCursor());
|
|
|
|
|
LanguageClientOutlineItem *itemForCursor = m_model.findNonRootItem(
|
|
|
|
|
[&](const LanguageClientOutlineItem *item) { return item->contains(pos); });
|
|
|
|
|
if (itemForCursor)
|
|
|
|
|
setCurrentIndex(m_model.indexForItem(itemForCursor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OutlineComboBox::activateEntry()
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex modelIndex = view()->currentIndex();
|
|
|
|
|
if (modelIndex.isValid()) {
|
|
|
|
|
const Position &pos = m_model.itemForIndex(modelIndex)->pos();
|
|
|
|
|
Core::EditorManager::cutForwardNavigationHistory();
|
|
|
|
|
Core::EditorManager::addCurrentPositionToNavigationHistory();
|
|
|
|
|
// line has to be 1 based, column 0 based!
|
|
|
|
|
m_editorWidget->gotoLine(pos.line() + 1, pos.character(), true, true);
|
|
|
|
|
emit m_editorWidget->activateEditor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OutlineComboBox::requestSymbols()
|
|
|
|
|
{
|
|
|
|
|
if (m_client)
|
|
|
|
|
m_client->documentSymbolCache()->requestSymbols(m_uri);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 09:45:51 +01:00
|
|
|
} // namespace LanguageClient
|