Skip to content

Commit

Permalink
refactor!: Remove TextRange class
Browse files Browse the repository at this point in the history
Replaced with the existing MarkRange.

Fix #67
  • Loading branch information
narnaud committed Jul 16, 2024
1 parent 70a719a commit aa41a17
Show file tree
Hide file tree
Showing 25 changed files with 78 additions and 230 deletions.
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
29 changes: 0 additions & 29 deletions docs/API/knut/textrange.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
- TextRange: API/knut/textrange.md
- Items:
- Script: API/knut/script.md
- ScriptDialog: API/knut/scriptdialog.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
textrange.h
textrange.cpp
qtuidocument.h
qtuidocument.cpp
qttsdocument.h
Expand Down
39 changes: 17 additions & 22 deletions src/core/codedocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,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 @@ -202,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 @@ -216,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 @@ -372,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 @@ -381,7 +376,7 @@ Document *CodeDocument::switchDeclarationDefinition()
return nullptr;
}

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

/*!
Expand Down
4 changes: 2 additions & 2 deletions src/core/codedocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions src/core/functionsymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ namespace Core {
* \qmlproperty vector<FunctionArgument> FunctionSymbol::arguments
* Returns the list of arguments being passed to this function.
*/
/*!
* \qmlproperty TextRange FunctionSymbol::range
* The range enclosing this function, not including leading/trailing
* whitespace but everything else like comments.
*/

FunctionSymbol::FunctionSymbol(QObject *parent, const QueryMatch &match, Kind kind)
: Symbol(parent, match, kind)
Expand Down
1 change: 0 additions & 1 deletion src/core/functionsymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "document.h"
#include "symbol.h"
#include "textrange.h"

#include <QList>
#include <QString>
Expand Down
9 changes: 5 additions & 4 deletions src/core/lsp_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ int lspToPos(const TextDocument &textDocument, const Lsp::Position &pos)
return 0;
}

TextRange lspToRange(const TextDocument &textDocument, const Lsp::Range &range)
RangeMark lspToRange(const TextDocument &textDocument, const Lsp::Range &range)
{
return {lspToPos(textDocument, range.start), lspToPos(textDocument, range.end)};
// I know, ugly, but that's the easiest to do that here.
auto document = const_cast<TextDocument *>(&textDocument);
return document->createRangeMark(lspToPos(textDocument, range.start), lspToPos(textDocument, range.end));
}

RangeMarkList lspToRangeMarkList(const std::vector<Lsp::Location> &locations)
Expand All @@ -62,8 +64,7 @@ RangeMarkList lspToRangeMarkList(const std::vector<Lsp::Location> &locations)
const auto filepath = url.toLocalFile();

if (auto document = qobject_cast<TextDocument *>(Project::instance()->get(filepath))) {
const auto range = lspToRange(*document, location.range);
rangeMarks.push_back(document->createRangeMark(range.start, range.end));
rangeMarks.push_back(lspToRange(*document, location.range));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/core/lsp_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

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

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

int lspToPos(const TextDocument &textDocument, const Lsp::Position &pos);

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

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

Expand Down
5 changes: 0 additions & 5 deletions src/core/rangemark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ TextDocument *RangeMark::document() const
return d ? d->m_editor : nullptr;
}

TextRange RangeMark::toTextRange() const
{
return {start(), end()};
}

QString RangeMark::text() const
{
// <= here instead of < because m_end is exclusive
Expand Down
Loading

0 comments on commit aa41a17

Please sign in to comment.