Commit ec9b8196 authored by Eric Armbruster's avatar Eric Armbruster 🍁
Browse files

lspclient: shorten paths in treeview if possible

Use the base directory from the project plugin to shorten the paths of
treeview if possible.
parent 260b3d47
Pipeline #235708 passed with stage
in 7 minutes and 1 second
......@@ -1662,19 +1662,44 @@ public:
item->setData(static_cast<int>(kind), RangeData::KindRole);
QString getProjectBaseDir()
QObject *project = m_mainWindow->pluginView(QStringLiteral("kateprojectplugin"));
if (project) {
return project->property("projectBaseDir").toString();
return {};
QString shortenPath(QString projectBaseDir, QString url)
if (!projectBaseDir.isEmpty() && url.startsWith(projectBaseDir)) {
QString res = url.mid(projectBaseDir.length());
  • Given project based dir ~/doc and a file at ~/documents/foo.txt (a different directory), wouldn't this code cause shortestPath() to return "uments/foo.txt"? This would not be useful.

Please register or sign in to reply
if (res.startsWith(QLatin1Char('/'))) {
return res.mid(1);
return res;
return url;
void makeTree(const QVector<RangeItem> &locations, const LSPClientRevisionSnapshot *snapshot)
// group by url, assuming input is suitably sorted that way
auto treeModel = new QStandardItemModel();
QString baseDir = getProjectBaseDir();
QUrl lastUrl;
QStandardItem *parent = nullptr;
for (const auto &loc : locations) {
// ensure we create a parent, if not already there (bug 427270) or we have a different url
if (!parent || loc.uri != lastUrl) {
if (parent) {
parent->setText(QStringLiteral("%1: %2").arg(lastUrl.toLocalFile()).arg(parent->rowCount()));
parent->setText(QStringLiteral("%1: %2").arg(shortenPath(baseDir, lastUrl.toLocalFile())).arg(parent->rowCount()));
lastUrl = loc.uri;
parent = new QStandardItem();
......@@ -1687,7 +1712,7 @@ public:
fillItemRoles(item, loc.uri, loc.range, loc.kind, snapshot);
if (parent) {
parent->setText(QStringLiteral("%1: %2").arg(lastUrl.toLocalFile()).arg(parent->rowCount()));
parent->setText(QStringLiteral("%1: %2").arg(shortenPath(baseDir, lastUrl.toLocalFile())).arg(parent->rowCount()));
// plain heuristic; mark for auto-expand all when safe and/or useful to do so
Supports Markdown
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