Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Utilities
Kate
Commits
a40d83eb
Commit
a40d83eb
authored
Feb 12, 2022
by
Mark Nauwelaerts
Committed by
Christoph Cullmann
Feb 13, 2022
Browse files
lspclient: move some reusable view methods to common utils
parent
ba36fb44
Changes
4
Hide whitespace changes
Inline
Side-by-side
addons/lspclient/CMakeLists.txt
View file @
a40d83eb
...
...
@@ -29,6 +29,7 @@ target_sources(
lspclientserver.cpp
lspclientservermanager.cpp
lspclientsymbolview.cpp
lspclientutils.cpp
lspsemantichighlighting.cpp
semantic_tokens_legend.cpp
lsptooltip.cpp
...
...
addons/lspclient/lspclientpluginview.cpp
View file @
a40d83eb
...
...
@@ -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
)
...
...
addons/lspclient/lspclientutils.cpp
0 → 100644
View file @
a40d83eb
#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
);
}
addons/lspclient/lspclientutils.h
0 → 100644
View file @
a40d83eb
/*
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment