Skip to content

Commit

Permalink
refactor!: Remove TextLocation class
Browse files Browse the repository at this point in the history
Replace with the existing RangeMark.

Related-to #67
  • Loading branch information
narnaud committed Jul 16, 2024
1 parent a2f97fb commit 70a719a
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 136 deletions.
28 changes: 0 additions & 28 deletions docs/API/knut/textlocation.md

This file was deleted.

1 change: 0 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ nav:
- TextDocument: API/knut/textdocument.md
- Mark: API/knut/mark.md
- RangeMark: API/knut/rangemark.md
- TextLocation: API/knut/textlocation.md
- TextRange: API/knut/textrange.md
- Items:
- Script: API/knut/script.md
Expand Down
2 changes: 0 additions & 2 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ set(PROJECT_SOURCES
textdocument_p.h
texteditor.h
texteditor.cpp
textlocation.h
textlocation.cpp
textrange.h
textrange.cpp
qtuidocument.h
Expand Down
8 changes: 3 additions & 5 deletions src/core/codedocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "querymatch.h"
#include "rangemark.h"
#include "symbol.h"
#include "textlocation.h"
#include "treesitter/predicates.h"
#include "utils/json.h"
#include "utils/log.h"
Expand Down Expand Up @@ -261,7 +260,7 @@ std::pair<QString, std::optional<TextRange>> CodeDocument::hoverWithRange(
return {"", {}};
}

Core::TextLocationList CodeDocument::references(int position) const
RangeMarkList CodeDocument::references(int position) const
{
spdlog::debug("CodeDocument::references");

Expand All @@ -273,19 +272,18 @@ Core::TextLocationList CodeDocument::references(int position) const
params.textDocument.uri = toUri();
params.position = Utils::lspFromPos(*this, position);

Core::TextLocationList textLocations;
if (auto result = client()->references(std::move(params))) {
const auto &value = result.value();
if (const auto *locations = std::get_if<std::vector<Lsp::Location>>(&value)) {
return Utils::lspToTextLocationList(*locations);
return Utils::lspToRangeMarkList(*locations);
} else {
spdlog::warn("CodeDocument::references: Language server returned unsupported references type!");
}
} else {
spdlog::warn("CodeDocument::references: LSP call to references returned nothing!");
}

return textLocations;
return {};
}

// Follows the symbol under the cursor.
Expand Down
2 changes: 1 addition & 1 deletion src/core/codedocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CodeDocument : public TextDocument
// As they rely on the clangd LSP, they are not reliable enough to use for scripting.
Core::Document *switchDeclarationDefinition();
Core::Document *followSymbol();
Core::TextLocationList references(int position) const;
Core::RangeMarkList references(int position) const;

QString hover(int position, std::function<void(const QString &)> asyncCallback = {}) const;

Expand Down
13 changes: 6 additions & 7 deletions src/core/lsp_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ TextRange lspToRange(const TextDocument &textDocument, const Lsp::Range &range)
return {lspToPos(textDocument, range.start), lspToPos(textDocument, range.end)};
}

TextLocationList lspToTextLocationList(const std::vector<Lsp::Location> &locations)
RangeMarkList lspToRangeMarkList(const std::vector<Lsp::Location> &locations)
{
TextLocationList textLocations;
textLocations.reserve(locations.size());
RangeMarkList rangeMarks;
rangeMarks.reserve(locations.size());

for (const auto &location : locations) {
const auto url = QUrl::fromEncoded(QByteArray::fromStdString(location.uri));
Expand All @@ -61,14 +61,13 @@ TextLocationList lspToTextLocationList(const std::vector<Lsp::Location> &locatio
}
const auto filepath = url.toLocalFile();

if (auto *document = qobject_cast<CodeDocument *>(Project::instance()->get(filepath))) {
if (auto document = qobject_cast<TextDocument *>(Project::instance()->get(filepath))) {
const auto range = lspToRange(*document, location.range);

textLocations.emplace_back(TextLocation {.document = document, .range = range});
rangeMarks.push_back(document->createRangeMark(range.start, range.end));
}
}

return textLocations;
return rangeMarks;
}

QString removeTypeAliasInformation(const QString &typeInfo)
Expand Down
4 changes: 2 additions & 2 deletions src/core/lsp_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#pragma once

#include "lsp/types.h"
#include "textlocation.h"
#include "rangemark.h"
#include "textrange.h"

#include <QString>
Expand All @@ -29,7 +29,7 @@ int lspToPos(const TextDocument &textDocument, const Lsp::Position &pos);

TextRange lspToRange(const TextDocument &textDocument, const Lsp::Range &range);

TextLocationList lspToTextLocationList(const std::vector<Lsp::Location> &locations);
RangeMarkList lspToRangeMarkList(const std::vector<Lsp::Location> &locations);

QString removeTypeAliasInformation(const QString &typeInfo);

Expand Down
6 changes: 3 additions & 3 deletions src/core/symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ Core::TextRange Symbol::selectionRange() const
return m_selectionRange;
}

QList<Core::TextLocation> Symbol::references() const
RangeMarkList Symbol::references() const
{
LOG("Symbol::references");

if (const auto codedocument = document()) {
auto references = codedocument->references(selectionRange().start);
kdalgorithms::erase_if(references, [this](const auto &reference) {
return reference.range == this->selectionRange();
kdalgorithms::erase_if(references, [this](const RangeMark &reference) {
return reference.toTextRange() == m_selectionRange;
});
return references;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#pragma once

#include "querymatch.h"
#include "textlocation.h"
#include "rangemark.h"
#include "textrange.h"

#include <QList>
Expand Down Expand Up @@ -92,7 +92,7 @@ class Symbol : public QObject
// As per KNUT-163, these are no longer public API.
// They are only used internally by the editor/GUI and not available from QML/JS.
// As this relies on the clangd LSP, it is not reliable enough to use for scripting.
QList<Core::TextLocation> references() const;
Core::RangeMarkList references() const;

Q_INVOKABLE void select();

Expand Down
41 changes: 0 additions & 41 deletions src/core/textlocation.cpp

This file was deleted.

40 changes: 0 additions & 40 deletions src/core/textlocation.h

This file was deleted.

8 changes: 4 additions & 4 deletions tests/tst_symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private slots:
QCOMPARE(symbol->kind(), Core::Symbol::Class);

const auto isSymbolRange = [&symbol](const auto &loc) {
return loc.range == symbol->selectionRange();
return loc.toTextRange() == symbol->selectionRange();
};

spdlog::warn("Finding references");
Expand All @@ -209,19 +209,19 @@ private slots:
spdlog::warn("Counting documents");
QCOMPARE(std::ranges::count_if(references,
[](const auto &location) {
return location.document->fileName().endsWith("main.cpp");
return location.document()->fileName().endsWith("main.cpp");
}),
1);

QCOMPARE(std::ranges::count_if(references,
[](const auto &location) {
return location.document->fileName().endsWith("myobject.h");
return location.document()->fileName().endsWith("myobject.h");
}),
2);

QCOMPARE(std::ranges::count_if(references,
[](const auto &location) {
return location.document->fileName().endsWith("myobject.cpp");
return location.document()->fileName().endsWith("myobject.cpp");
}),
6);
}
Expand Down

0 comments on commit 70a719a

Please sign in to comment.