-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathedit.py
56 lines (48 loc) · 1.82 KB
/
edit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import annotations
from .logging import debug
from .protocol import Position
from .protocol import TextEdit
from .protocol import UINT_MAX
from .protocol import WorkspaceEdit
from typing import Dict, List, Optional, Tuple
import sublime
WorkspaceChanges = Dict[str, Tuple[List[TextEdit], Optional[int]]]
def parse_workspace_edit(workspace_edit: WorkspaceEdit) -> WorkspaceChanges:
changes: WorkspaceChanges = {}
document_changes = workspace_edit.get('documentChanges')
if isinstance(document_changes, list):
for document_change in document_changes:
if 'kind' in document_change:
# TODO: Support resource operations (create/rename/remove)
debug('Ignoring unsupported "resourceOperations" edit type')
continue
text_document = document_change["textDocument"]
uri = text_document['uri']
version = text_document.get('version')
edits = document_change.get('edits')
changes.setdefault(uri, ([], version))[0].extend(edits)
else:
raw_changes = workspace_edit.get('changes')
if isinstance(raw_changes, dict):
for uri, edits in raw_changes.items():
changes[uri] = (edits, None)
return changes
def parse_range(range: Position) -> tuple[int, int]:
return range['line'], min(UINT_MAX, range['character'])
def apply_text_edits(
view: sublime.View,
edits: list[TextEdit] | None,
*,
process_placeholders: bool | None = False,
required_view_version: int | None = None
) -> None:
if not edits:
return
view.run_command(
'lsp_apply_document_edit',
{
'changes': edits,
'process_placeholders': process_placeholders,
'required_view_version': required_view_version,
}
)