summaryrefslogtreecommitdiffstats
path: root/tests/lsp_tests/lsp_tutil.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 20:16:53 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 20:16:53 +0000
commitee65fb850961435a3c8ed89ffb20d16f0e8505a3 (patch)
tree1fe559c0b1c6a2244e0e87e94c1d98222fa02e8c /tests/lsp_tests/lsp_tutil.py
parentAdding upstream version 0.1.24. (diff)
downloaddebputy-ee65fb850961435a3c8ed89ffb20d16f0e8505a3.tar.xz
debputy-ee65fb850961435a3c8ed89ffb20d16f0e8505a3.zip
Adding upstream version 0.1.25.upstream/0.1.25
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/lsp_tests/lsp_tutil.py')
-rw-r--r--tests/lsp_tests/lsp_tutil.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/lsp_tests/lsp_tutil.py b/tests/lsp_tests/lsp_tutil.py
new file mode 100644
index 0000000..1e509af
--- /dev/null
+++ b/tests/lsp_tests/lsp_tutil.py
@@ -0,0 +1,45 @@
+from typing import Tuple
+
+try:
+ from pygls.server import LanguageServer
+ from lsprotocol.types import (
+ TextDocumentItem,
+ Position,
+ )
+except ImportError:
+ pass
+
+
+def _locate_cursor(text: str) -> Tuple[str, "Position"]:
+ lines = text.splitlines(keepends=True)
+ for line_no in range(len(lines)):
+ line = lines[line_no]
+ try:
+ c = line.index("<CURSOR>")
+ except ValueError:
+ continue
+ line = line.replace("<CURSOR>", "")
+ lines[line_no] = line
+ pos = Position(line_no, c)
+ return "".join(lines), pos
+ raise ValueError('Missing "<CURSOR>" marker')
+
+
+def put_doc_with_cursor(
+ ls: "LanguageServer",
+ uri: str,
+ language_id: str,
+ content: str,
+ *,
+ doc_version: int = 1,
+) -> "Position":
+ cleaned_content, cursor_pos = _locate_cursor(content)
+ ls.workspace.put_text_document(
+ TextDocumentItem(
+ uri,
+ language_id,
+ doc_version,
+ cleaned_content,
+ )
+ )
+ return cursor_pos