Commit a40d83eb authored by Mark Nauwelaerts's avatar Mark Nauwelaerts Committed by Christoph Cullmann
Browse files

lspclient: move some reusable view methods to common utils

parent ba36fb44
......@@ -29,6 +29,7 @@ target_sources(
lspclientserver.cpp
lspclientservermanager.cpp
lspclientsymbolview.cpp
lspclientutils.cpp
lspsemantichighlighting.cpp
semantic_tokens_legend.cpp
lsptooltip.cpp
......
......@@ -11,6 +11,7 @@
#include "lspclientplugin.h"
#include "lspclientservermanager.h"
#include "lspclientsymbolview.h"
#include "lspclientutils.h"
#include "lspclient_debug.h"
......@@ -1632,19 +1633,6 @@ public:
}
};
LSPRange transformRange(const QUrl &url, const LSPClientRevisionSnapshot &snapshot, const LSPRange &range)
{
KTextEditor::MovingInterface *miface;
qint64 revision;
auto result = range;
snapshot.find(url, miface, revision);
if (miface) {
miface->transformRange(result, KTextEditor::MovingRange::DoNotExpand, KTextEditor::MovingRange::AllowEmpty, revision);
}
return result;
}
void
fillItemRoles(QStandardItem *item, const QUrl &url, const LSPRange _range, RangeData::KindEnum kind, const LSPClientRevisionSnapshot *snapshot = nullptr)
{
......@@ -2006,34 +1994,7 @@ public:
void applyEdits(KTextEditor::Document *doc, const LSPClientRevisionSnapshot *snapshot, const QList<LSPTextEdit> &edits)
{
KTextEditor::MovingInterface *miface = qobject_cast<KTextEditor::MovingInterface *>(doc);
Q_ASSERT(miface);
// NOTE:
// server might be pretty sloppy wrt edits (e.g. python-language-server)
// e.g. send one edit for the whole document rather than 'surgical edits'
// and that even when requesting format for a limited selection
// ... but then we are but a client and do as we are told
// all-in-all a low priority feature
// all coordinates in edits are wrt original document,
// so create moving ranges that will adjust to preceding edits as they are applied
QVector<KTextEditor::MovingRange *> ranges;
for (const auto &edit : edits) {
auto range = snapshot ? transformRange(doc->url(), *snapshot, edit.range) : edit.range;
KTextEditor::MovingRange *mr = miface->newMovingRange(range);
ranges.append(mr);
}
// now make one transaction (a.o. for one undo) and apply in sequence
if (!ranges.empty()) {
KTextEditor::Document::EditingTransaction transaction(doc);
for (int i = 0; i < ranges.length(); ++i) {
doc->replaceText(ranges.at(i)->toRange(), edits.at(i).newText);
}
}
qDeleteAll(ranges);
::applyEdits(doc, snapshot, edits);
}
void applyEdits(const QUrl &url, const LSPClientRevisionSnapshot *snapshot, const QList<LSPTextEdit> &edits)
......
#include "lspclientutils.h"
#include <KTextEditor/MovingInterface>
LSPRange transformRange(const QUrl &url, const LSPClientRevisionSnapshot &snapshot, const LSPRange &range)
{
KTextEditor::MovingInterface *miface;
qint64 revision;
auto result = range;
snapshot.find(url, miface, revision);
if (miface) {
miface->transformRange(result, KTextEditor::MovingRange::DoNotExpand, KTextEditor::MovingRange::AllowEmpty, revision);
}
return result;
}
void applyEdits(KTextEditor::Document *doc, const LSPClientRevisionSnapshot *snapshot, const QList<LSPTextEdit> &edits)
{
KTextEditor::MovingInterface *miface = qobject_cast<KTextEditor::MovingInterface *>(doc);
Q_ASSERT(miface);
// NOTE:
// server might be pretty sloppy wrt edits (e.g. python-language-server)
// e.g. send one edit for the whole document rather than 'surgical edits'
// and that even when requesting format for a limited selection
// ... but then we are but a client and do as we are told
// all-in-all a low priority feature
// all coordinates in edits are wrt original document,
// so create moving ranges that will adjust to preceding edits as they are applied
QVector<KTextEditor::MovingRange *> ranges;
for (const auto &edit : edits) {
auto range = snapshot ? transformRange(doc->url(), *snapshot, edit.range) : edit.range;
KTextEditor::MovingRange *mr = miface->newMovingRange(range);
ranges.append(mr);
}
// now make one transaction (a.o. for one undo) and apply in sequence
if (!ranges.empty()) {
KTextEditor::Document::EditingTransaction transaction(doc);
for (int i = 0; i < ranges.length(); ++i) {
doc->replaceText(ranges.at(i)->toRange(), edits.at(i).newText);
}
}
qDeleteAll(ranges);
}
/*
SPDX-FileCopyrightText: 2022 Mark Nauwelaerts <mark.nauwelaerts@gmail.com>
SPDX-License-Identifier: MIT
*/
#ifndef LSPCLIENTUTILS_H
#define LSPCLIENTUTILS_H
#include "lspclientservermanager.h"
LSPRange transformRange(const QUrl &url, const LSPClientRevisionSnapshot &snapshot, const LSPRange &range);
void applyEdits(KTextEditor::Document *doc, const LSPClientRevisionSnapshot *snapshot, const QList<LSPTextEdit> &edits);
#endif
Markdown is supported
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