Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [symbol] Add Outline for current document #849

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 146 additions & 97 deletions assets/translations/en_US.ts

Large diffs are not rendered by default.

249 changes: 149 additions & 100 deletions assets/translations/zh_CN.ts

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions src/common/lsp/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,23 +512,21 @@ bool ClientPrivate::symbolResult(const QJsonObject &jsonObj)
auto filePath = requestSave.value(calledID).file;
requestSave.remove(calledID);

QList<DocumentSymbol> docSymbols;
QList<SymbolInformation> symbolInfos;
auto value = jsonObj.value(K_RESULT);
if (value.isArray()) {
auto array = value.toArray();
if (!array.isEmpty()) {
auto arrayObj = array.first().toObject();
if (arrayObj.contains("range")) {
docSymbols = parseDocumentSymbol(array);
const auto &docSymbols = parseDocumentSymbol(array);
emit q->symbolResult(docSymbols, filePath);
} else {
symbolInfos = parseDocumentSymbolInfo(array);
const auto &symbolInfos = parseDocumentSymbolInfo(array);
emit q->symbolResult(symbolInfos, filePath);
}
}
}

emit q->symbolResult(docSymbols, filePath);
emit q->symbolResult(symbolInfos, filePath);
return true;
}
return false;
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/codeeditor/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "utils/editorutils.h"
#include "status/statusinfomanager.h"
#include "symbol/symbollocator.h"
#include "symbol/symbolwidget.h"

#include "base/abstractmenu.h"
#include "base/abstractaction.h"
Expand Down Expand Up @@ -65,7 +66,7 @@ bool CodeEditor::start()
initButtonBox();
initEditorService();
initOptionService();
initLocator();
initSymbol();
registerVariables();

return true;
Expand Down Expand Up @@ -215,7 +216,7 @@ void CodeEditor::initOptionService()
optionService->implGenerator<EditorSettingsWidgetGenerator>(option::GROUP_GENERAL, EditorSettingsWidgetGenerator::kitName());
}

void CodeEditor::initLocator()
void CodeEditor::initSymbol()
{
auto locatorSrv = dpfGetService(LocatorService);
if (!locatorSrv)
Expand All @@ -224,6 +225,8 @@ void CodeEditor::initLocator()
SymbolLocator *locator = new SymbolLocator(workspaceWidget);
locator->setWorkspaceWidget(workspaceWidget);
locatorSrv->registerLocator(locator);

SymbolWidgetGenerator::instance()->registerToDock(workspaceWidget);
}

void CodeEditor::registerVariables()
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codeeditor/codeeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CodeEditor : public dpf::Plugin
void initEditorService();
void initWindowService();
void initOptionService();
void initLocator();
void initSymbol();
void registerVariables();

private:
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/codeeditor/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "private/tabwidget_p.h"
#include "transceiver/codeeditorreceiver.h"
#include "common/common.h"
#include "settings/settingsdefine.h"
#include "base/abstractaction.h"
#include "recent/recentopenwidget.h"
#include "symbol/symbolwidget.h"

#include "services/window/windowservice.h"

Expand Down Expand Up @@ -124,7 +123,7 @@ void TabWidgetPrivate::initUI()

mainLayout->addWidget(tabBar, 0, Qt::AlignTop);
mainLayout->addWidget(symbolBar, 0, Qt::AlignTop);
mainLayout->addLayout(editorLayout);
mainLayout->addLayout(editorLayout, 1);

openedWidget = new RecentOpenWidget(q);
openedWidget->hide();
Expand Down Expand Up @@ -362,6 +361,7 @@ void TabWidgetPrivate::onTabSwitched(const QString &fileName)
if (!editorMng.contains(fileName))
return;

SymbolWidgetGenerator::instance()->symbolWidget()->setEditor(editorMng[fileName]);
symbolBar->setPath(fileName);
editorLayout->setCurrentWidget(editorMng[fileName]);
recentOpenedFiles.removeOne(fileName);
Expand Down Expand Up @@ -890,6 +890,7 @@ void TabWidget::openFile(const QString &fileName)
d->tabBar->setFileName(fileName);
TextEditor *editor = d->createEditor(fileName);

SymbolWidgetGenerator::instance()->symbolWidget()->setEditor(editor);
d->editorLayout->addWidget(editor);
d->editorLayout->setCurrentWidget(editor);
d->changeFocusProxy();
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/codeeditor/gui/workspacewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "settings/editorsettings.h"
#include "settings/settingsdefine.h"
#include "base/abstractaction.h"
#include "symbol/symbolwidget.h"
#include "texteditor.h"

#include <DDialog>

Expand Down Expand Up @@ -355,6 +357,8 @@ void WorkspaceWidgetPrivate::onCloseRequested()
if (!tabWidgetList.isEmpty()) {
tabWidgetList.last()->setFocus();
editor.switchedFile(tabWidgetList.last()->currentFile());
auto symbolWidget = SymbolWidgetGenerator::instance()->symbolWidget();
symbolWidget->setEditor(qobject_cast<TextEditor *>(tabWidgetList.last()->currentWidget()));
}

if (tabWidgetList.size() == 1)
Expand Down Expand Up @@ -523,6 +527,8 @@ void WorkspaceWidgetPrivate::onFocusChanged(QWidget *old, QWidget *now)

focusTabWidget = tabWidget;
editor.switchedFile(focusTabWidget->currentFile());
auto symbolWidget = SymbolWidgetGenerator::instance()->symbolWidget();
symbolWidget->setEditor(qobject_cast<TextEditor *>(tabWidget->currentWidget()));
}

void WorkspaceWidgetPrivate::onZoomValueChanged()
Expand Down
29 changes: 21 additions & 8 deletions src/plugins/codeeditor/symbol/symbolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ void CurmbItem::setText(const QString &text)
update();
}

QString CurmbItem::text() const
{
return displayText;
}

void CurmbItem::setIcon(const QIcon &icon)
{
if (!icon.isNull())
Expand Down Expand Up @@ -187,8 +192,8 @@ void SymbolBar::setPath(const QString &path)
if (i == itemList.size() - 1)
item->setIcon(DFileIconProvider::globalProvider()->icon(path));

QString absolutePath = workspaceDir + QDir::separator() + itemList.mid(0, i).join(QDir::separator());
item->setUserData(absolutePath);
QString filePath = workspaceDir + QDir::separator() + itemList.mid(0, i + 1).join(QDir::separator());
item->setUserData(filePath);
layout->addWidget(item, 0, Qt::AlignVCenter | Qt::AlignLeft);
connect(item, &CurmbItem::clicked, this, &SymbolBar::curmbItemClicked);
}
Expand Down Expand Up @@ -227,7 +232,8 @@ void SymbolBar::updateSymbol(int line, int index)
connect(symbolItem, &CurmbItem::clicked, this, &SymbolBar::curmbItemClicked);
}

symbolItem->setUserData(editor->getFile());
QVariantList list { editor->getFile(), line, index };
symbolItem->setUserData(list);
symbolItem->setText(info.first);
symbolItem->setToolTip(info.first);
symbolItem->setIcon(SymbolManager::instance()->iconFromKind(static_cast<SymbolManager::SymbolKind>(info.second)));
Expand All @@ -240,7 +246,10 @@ void SymbolBar::curmbItemClicked()
return;

if (!symbolView) {
symbolView = new SymbolView(this);
symbolView = new SymbolView(SymbolView::Click, this);
symbolView->setFixedWidth(400);
symbolView->setWindowFlags(Qt::Popup);
symbolView->setClickToHide(true);
connect(symbolView, &SymbolView::hidden, this, &SymbolBar::resetCurmbItemState);
}

Expand All @@ -250,15 +259,19 @@ void SymbolBar::curmbItemClicked()
switch (item->curmbType()) {
case CurmbItem::FilePath: {
const auto &path = item->userData().toString();
symbolView->setRootPath(path);
QFileInfo info(path);
symbolView->setRootPath(info.absolutePath());
symbolView->select(path);
symbolView->show(pos);
} break;
case CurmbItem::Symbol: {
const auto &path = item->userData().toString();
if (symbolView->setSymbolPath(path))
const auto &data = item->userData().toList();
if (data.size() == 3 && symbolView->setSymbolPath(data[0].toString())) {
symbolView->selectSymbol(item->text(), data[1].toInt(), data[2].toInt());
symbolView->show(pos);
else
} else {
item->setSelected(false);
}
} break;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/plugins/codeeditor/symbol/symbolbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CurmbItem : public QWidget

CurmbType curmbType() const;
void setText(const QString &text);
QString text() const;
void setIcon(const QIcon &icon);
void setSelected(bool selected);
bool isSelected() const;
Expand Down
26 changes: 17 additions & 9 deletions src/plugins/codeeditor/symbol/symbolmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ QPair<QString, int> SymbolManagerPrivate::findSymbol(const newlsp::DocumentSymbo
return qMakePair(name, symbol.kind);
}

SymbolManager::SymbolManager()
: d(new SymbolManagerPrivate(this))
SymbolManager::SymbolManager(QObject *parent)
: QObject(parent),
d(new SymbolManagerPrivate(this))
{
}

Expand All @@ -60,6 +61,7 @@ SymbolManager *SymbolManager::instance()
void SymbolManager::setDocumentSymbols(const QString &file, const QList<newlsp::DocumentSymbol> &docSymbols)
{
d->docSymbolHash.insert(file, docSymbols);
Q_EMIT symbolChanged(file);
}

QList<newlsp::DocumentSymbol> SymbolManager::documentSymbols(const QString &file) const
Expand All @@ -70,6 +72,7 @@ QList<newlsp::DocumentSymbol> SymbolManager::documentSymbols(const QString &file
void SymbolManager::setSymbolInformations(const QString &file, const QList<newlsp::SymbolInformation> &symbolInfos)
{
d->symbolInfoHash.insert(file, symbolInfos);
Q_EMIT symbolChanged(file);
}

QList<newlsp::SymbolInformation> SymbolManager::symbolInformations(const QString &file) const
Expand Down Expand Up @@ -149,14 +152,19 @@ QIcon SymbolManager::iconFromKind(SymbolKind kind)

QString SymbolManager::displayNameFromDocumentSymbol(SymbolKind kind, const QString &name, const QString &detail)
{
QString tmpName = name;
int index = tmpName.indexOf("::");
if (index != -1)
tmpName = tmpName.mid(index + 2);

switch (kind) {
case SymbolKind::Constructor:
return name + detail;
return tmpName + detail;
case SymbolKind::Method:
case SymbolKind::Function: {
const int lastParenIndex = detail.lastIndexOf(')');
if (lastParenIndex == -1)
return name;
return tmpName;
int leftParensNeeded = 1;
int i = -1;
for (i = lastParenIndex - 1; i >= 0; --i) {
Expand All @@ -174,16 +182,16 @@ QString SymbolManager::displayNameFromDocumentSymbol(SymbolKind kind, const QStr
break;
}
if (leftParensNeeded > 0)
return name;
return name + detail.mid(i) + " -> " + detail.left(i);
return tmpName;
return tmpName + detail.mid(i) + " -> " + detail.left(i);
}
case SymbolKind::Variable:
case SymbolKind::Field:
case SymbolKind::Constant:
if (detail.isEmpty())
return name;
return name + " -> " + detail;
return tmpName;
return tmpName + " -> " + detail;
default:
return name;
return tmpName;
}
}
8 changes: 6 additions & 2 deletions src/plugins/codeeditor/symbol/symbolmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include "common/lsp/protocol/new/languagefeatures.h"

class SymbolManagerPrivate;
class SymbolManager
class SymbolManager : public QObject
{
Q_OBJECT
public:
enum SymbolKind {
File = 1,
Expand Down Expand Up @@ -53,8 +54,11 @@ class SymbolManager
QIcon iconFromKind(SymbolKind kind);
QString displayNameFromDocumentSymbol(SymbolKind kind, const QString &name, const QString &detail);

Q_SIGNALS:
void symbolChanged(const QString &file);

private:
SymbolManager();
SymbolManager(QObject *parent = nullptr);
~SymbolManager();

SymbolManagerPrivate *const d;
Expand Down
Loading
Loading