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

Remove TextLocation and TextRange class #108

Merged
merged 2 commits into from
Jul 16, 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
6 changes: 0 additions & 6 deletions docs/API/knut/functionsymbol.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Knut
| | Name |
|-|-|
|vector<[FunctionArgument](../knut/functionargument.md)>|**[arguments](#arguments)**|
|[TextRange](../knut/textrange.md)|**[range](#range)**|
|string|**[returnType](#returnType)**|

## Property Documentation
Expand All @@ -24,11 +23,6 @@ import Knut

Returns the list of arguments being passed to this function.

#### <a name="range"></a>[TextRange](../knut/textrange.md) **range**

The range enclosing this function, not including leading/trailing
whitespace but everything else like comments.

#### <a name="returnType"></a>string **returnType**

Returns the return type of this function.
8 changes: 4 additions & 4 deletions docs/API/knut/symbol.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import Knut
|string|**[description](#description)**|
|Kind|**[kind](#kind)**|
|string|**[name](#name)**|
|[TextRange](../knut/textrange.md)|**[range](#range)**|
|[TextRange](../knut/textrange.md)|**[selectionRange](#selectionRange)**|
|[RangeMark](../knut/rangemark.md)|**[range](#range)**|
|[RangeMark](../knut/rangemark.md)|**[selectionRange](#selectionRange)**|

## Methods

Expand Down Expand Up @@ -65,13 +65,13 @@ Return the kind of this symbol. Available symbol kinds are:

Return the name of this symbol.

#### <a name="range"></a>[TextRange](../knut/textrange.md) **range**
#### <a name="range"></a>[RangeMark](../knut/rangemark.md) **range**

The range enclosing this symbol not including leading/trailing whitespace but everything else like comments. This
information is typically used to determine if the clients cursor is inside the symbol to reveal in the symbol in the
UI.

#### <a name="selectionRange"></a>[TextRange](../knut/textrange.md) **selectionRange**
#### <a name="selectionRange"></a>[RangeMark](../knut/rangemark.md) **selectionRange**

The range that should be selected and revealed when this symbol is being picked, e.g. the name of a function. Must be
contained by the `range`.
Expand Down
12 changes: 6 additions & 6 deletions docs/API/knut/textdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[deleteLine](#deleteLine)**(int line = -1)|
||**[deleteNextCharacter](#deleteNextCharacter)**(int count = 1)|
||**[deletePreviousCharacter](#deletePreviousCharacter)**(int count = 1)|
||**[deleteRange](#deleteRange)**([TextRange](../knut/textrange.md) range)|
||**[deleteRange](#deleteRange)**([RangeMark](../knut/rangemark.md) range)|
||**[deleteRegion](#deleteRegion)**(int from, int to)|
||**[deleteSelection](#deleteSelection)**()|
||**[deleteStartOfLine](#deleteStartOfLine)**()|
Expand Down Expand Up @@ -74,7 +74,7 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[remove](#remove)**(int length)|
||**[removeIndent](#removeIndent)**(int count)|
||**[replace](#replace)**(int length, string text)|
||**[replace](#replace)**([TextRange](../knut/textrange.md) range, string text)|
||**[replace](#replace)**([RangeMark](../knut/rangemark.md) range, string text)|
||**[replace](#replace)**(int from, int to, string text)|
|bool |**[replaceAll](#replaceAll)**(string before, string after, int options = TextDocument.NoFindFlags)|
|bool |**[replaceAllInRange](#replaceAllInRange)**(string before, string after, [RangeMark](../knut/rangemark.md) range, int options = TextDocument.NoFindFlags)|
Expand All @@ -90,7 +90,7 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[selectPreviousChar](#selectPreviousChar)**(int count = 1)|
||**[selectPreviousLine](#selectPreviousLine)**(int count = 1)|
||**[selectPreviousWord](#selectPreviousWord)**(int count = 1)|
||**[selectRange](#selectRange)**([TextRange](../knut/textrange.md) range)|
||**[selectRange](#selectRange)**([RangeMark](../knut/rangemark.md) range)|
||**[selectRangeMark](#selectRangeMark)**([RangeMark](../knut/rangemark.md) mark)|
||**[selectRegion](#selectRegion)**(int from, int to)|
||**[selectStartOfLine](#selectStartOfLine)**(int count = 1)|
Expand Down Expand Up @@ -205,7 +205,7 @@ Deletes the next `count` characters.

Deletes the previous `count` characters.

#### <a name="deleteRange"></a>**deleteRange**([TextRange](../knut/textrange.md) range)
#### <a name="deleteRange"></a>**deleteRange**([RangeMark](../knut/rangemark.md) range)

Deletes the range passed in parameter.

Expand Down Expand Up @@ -364,7 +364,7 @@ Indents the current line `count` times. If there's a selection, indent all lines

Replaces `length` characters from the current position with the string `text`.

#### <a name="replace"></a>**replace**([TextRange](../knut/textrange.md) range, string text)
#### <a name="replace"></a>**replace**([RangeMark](../knut/rangemark.md) range, string text)

Replaces the text in the range `range` with the string `text`.

Expand Down Expand Up @@ -471,7 +471,7 @@ Selects the previous line, repeat the operation `count` times.

Selects the previous word, repeat the operation `count` times.

#### <a name="selectRange"></a>**selectRange**([TextRange](../knut/textrange.md) range)
#### <a name="selectRange"></a>**selectRange**([RangeMark](../knut/rangemark.md) range)

Selects the range passed in parameter.

Expand Down
28 changes: 0 additions & 28 deletions docs/API/knut/textlocation.md

This file was deleted.

29 changes: 0 additions & 29 deletions docs/API/knut/textrange.md

This file was deleted.

2 changes: 0 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +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
- ScriptDialog: API/knut/scriptdialog.md
Expand Down
4 changes: 0 additions & 4 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ set(PROJECT_SOURCES
textdocument_p.h
texteditor.h
texteditor.cpp
textlocation.h
textlocation.cpp
textrange.h
textrange.cpp
qtuidocument.h
qtuidocument.cpp
qttsdocument.h
Expand Down
47 changes: 20 additions & 27 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 @@ -95,38 +94,33 @@ Symbol *CodeDocument::currentSymbol(const std::function<bool(const Symbol &)> &f
*/
void CodeDocument::deleteSymbol(const Symbol &symbol)
{
auto range = symbol.range();
const auto range = symbol.range();

// Include any leading whitespace (excluding newlines).
auto leading = Core::TextRange {range.start, range.start + 1};
while (leading.start > 0) {
leading.start--;
leading.end--;
selectRange(leading);
int start = range.start();
while (start > 0) {
selectRegion(start - 1, start);

if (selectedText() != " " && selectedText() != "\t") {
break;
}

range.start--;
start--;
}

// Include a trailing semicolon and up to one trailing newline
auto trailing = Core::TextRange {range.end, range.end + 1};
selectRange(trailing);
int end = range.end();
selectRegion(end, end + 1);
if (selectedText() == ";") {
range.end++;

trailing.start++;
trailing.end++;
selectRange(trailing);
end++;
selectRegion(end, end + 1);
}

if (selectedText() == "\n") {
range.end++;
end++;
}

this->selectRange(range);
this->selectRegion(start, end);
this->deleteSelection();
}

Expand Down Expand Up @@ -203,8 +197,8 @@ QString CodeDocument::hover(int position, std::function<void(const QString &)> a
}
}

std::pair<QString, std::optional<TextRange>> CodeDocument::hoverWithRange(
int position, std::function<void(const QString &, std::optional<TextRange>)> asyncCallback /* = {} */) const
std::pair<QString, std::optional<RangeMark>> CodeDocument::hoverWithRange(
int position, std::function<void(const QString &, std::optional<RangeMark>)> asyncCallback /* = {} */) const
{
spdlog::debug("CodeDocument::hover");

Expand All @@ -217,14 +211,14 @@ std::pair<QString, std::optional<TextRange>> CodeDocument::hoverWithRange(

QPointer<const CodeDocument> safeThis(this);

auto convertResult = [safeThis](const auto &result) -> std::pair<QString, std::optional<TextRange>> {
auto convertResult = [safeThis](const auto &result) -> std::pair<QString, std::optional<RangeMark>> {
if (!std::holds_alternative<Lsp::Hover>(result)) {
return {"", {}};
}

auto hover = std::get<Lsp::Hover>(result);

std::optional<TextRange> range;
std::optional<RangeMark> range;
if (hover.range && !safeThis.isNull()) {
range = Utils::lspToRange(*safeThis, hover.range.value());
}
Expand Down Expand Up @@ -261,7 +255,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 +267,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 Expand Up @@ -374,7 +367,7 @@ Document *CodeDocument::switchDeclarationDefinition()
auto symbolList = symbols();

auto currentFunction = kdalgorithms::find_if(symbolList, [&cursor](const auto &symbol) {
auto isInRange = symbol->range().start <= cursor.position() && cursor.position() <= symbol->range().end;
auto isInRange = symbol->range().contains(cursor.position());
return isInRange && symbol->isFunction();
});

Expand All @@ -383,7 +376,7 @@ Document *CodeDocument::switchDeclarationDefinition()
return nullptr;
}

return followSymbol((*currentFunction)->selectionRange().start);
return followSymbol((*currentFunction)->selectionRange().start());
}

/*!
Expand Down
6 changes: 3 additions & 3 deletions 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 All @@ -98,9 +98,9 @@ public slots:

int revision() const;

std::pair<QString, std::optional<TextRange>>
std::pair<QString, std::optional<RangeMark>>
hoverWithRange(int position,
std::function<void(const QString &, std::optional<TextRange>)> asyncCallback = {}) const;
std::function<void(const QString &, std::optional<RangeMark>)> asyncCallback = {}) const;

private:
bool checkClient() const;
Expand Down
2 changes: 1 addition & 1 deletion src/core/codedocument_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ const QList<Core::Symbol *> &TreeSitterHelper::symbols()
m_symbols.append(enumSymbols());

kdalgorithms::sort_by(m_symbols, [](const auto &symbol) {
return symbol->range().start;
return symbol->range().start();
});

assignSymbolContexts();
Expand Down
12 changes: 6 additions & 6 deletions src/core/cppdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ bool CppDocument::insertCodeInMethod(const QString &methodName, QString code, Po
}

QTextCursor cursor = textEdit()->textCursor();
cursor.setPosition(symbol->range().end);
cursor.setPosition(symbol->range().end());
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
if (cursor.selectedText() != "}") {
spdlog::warn("CppDocument::insertCodeInMethod: {} is not a function definition.", symbol->name());
Expand All @@ -474,7 +474,7 @@ bool CppDocument::insertCodeInMethod(const QString &methodName, QString code, Po

cursor.beginEditBlock();
// Goto the end and move back one character
cursor.setPosition(symbol->range().end);
cursor.setPosition(symbol->range().end());
cursor.movePosition(QTextCursor::PreviousCharacter);

const QString strTab = tab();
Expand Down Expand Up @@ -1001,14 +1001,14 @@ void CppDocument::toggleSection()

cursor.beginEditBlock();
// Start from the end
cursor.setPosition(symbol->range().end);
cursor.setPosition(symbol->range().end());
cursor.movePosition(QTextCursor::StartOfLine);
cursor.movePosition(QTextCursor::Up, QTextCursor::KeepAnchor);

if (cursor.selectedText().startsWith(endifString)) {
// The function is already commented out, remove the comments
int start = textEdit()->document()->find(elseString, cursor, QTextDocument::FindBackward).selectionStart();
if (start > symbol->range().start)
if (start > symbol->range().start())
cursor.setPosition(start, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.setPosition(moveBlock(cursor.position(), QTextCursor::PreviousCharacter));
Expand All @@ -1019,7 +1019,7 @@ void CppDocument::toggleSection()
cursorPos -= ifdefString.length() + 1;
} else {
// Comment out the function with #if/#def, make sure to return something if needed
cursor.setPosition(symbol->range().end);
cursor.setPosition(symbol->range().end());
cursor.movePosition(QTextCursor::PreviousCharacter);

QString text = elseString + newLine;
Expand Down Expand Up @@ -1350,7 +1350,7 @@ void CppDocument::deleteMethodLocal(const QString &methodName, const QString &si
// That way removing a function won't change the position of the other functions.
// This assumes the ranges don't overlap.
auto byRange = [](const auto &symbol1, const auto &symbol2) {
return symbol1->range().start > symbol2->range().start;
return symbol1->range().start() > symbol2->range().start();
};
std::ranges::sort(symbolList, byRange);

Expand Down
Loading
Loading