Skip to content

Commit

Permalink
pytest-lsp: Migrate to pygls v2
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Aug 28, 2024
1 parent f2c7d6a commit 541844e
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 106 deletions.
1 change: 1 addition & 0 deletions lib/pytest-lsp/changes/177.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Migrate to pygls v2
3 changes: 3 additions & 0 deletions lib/pytest-lsp/hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ packages = ["pytest_lsp"]
[envs.hatch-test]
dependencies = ["pytest-asyncio"]

[envs.hatch-test.env-vars]
UV_PRERELEASE="allow"

[[envs.hatch-test.matrix]]
python = ["3.9", "3.10", "3.11", "3.12", "3.13"]
pytest = ["8"]
Expand Down
4 changes: 2 additions & 2 deletions lib/pytest-lsp/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ requires-python = ">=3.9"
license = { text = "MIT" }
authors = [{ name = "Alex Carney", email = "alcarneyme@gmail.com" }]
classifiers = [
"Development Status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
"Framework :: Pytest",
"Programming Language :: Python",
Expand All @@ -25,7 +25,7 @@ classifiers = [
]
dependencies = [
"packaging",
"pygls>=1.1.0",
"pygls>=2.0.0a1",
"pytest",
"pytest-asyncio>=0.23",
]
Expand Down
2 changes: 1 addition & 1 deletion lib/pytest-lsp/pytest_lsp/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def work_done_progress_create(
@check_params_of(method=types.WORKSPACE_CONFIGURATION)
def workspace_configuration(
capabilities: types.ClientCapabilities,
params: types.WorkspaceConfigurationParams,
params: types.ConfigurationParams,
):
"""Ensure that the client has support for ``workspace/configuration`` requests."""
is_supported = get_capability(capabilities, "workspace.configuration", False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
CompletionParams,
InsertTextFormat,
)
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("hello-world", "v1")

Expand Down
38 changes: 17 additions & 21 deletions lib/pytest-lsp/tests/examples/diagnostics/server.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
from lsprotocol.types import (
TEXT_DOCUMENT_DID_OPEN,
Diagnostic,
DidOpenTextDocumentParams,
Position,
Range,
)
from pygls.server import LanguageServer
from lsprotocol import types
from pygls.lsp.server import LanguageServer

server = LanguageServer("diagnostic-server", "v1")


@server.feature(TEXT_DOCUMENT_DID_OPEN)
def did_open(ls: LanguageServer, params: DidOpenTextDocumentParams):
ls.publish_diagnostics(
params.text_document.uri,
[
Diagnostic(
message="There is an error here.",
range=Range(
start=Position(line=1, character=1),
end=Position(line=1, character=10),
),
)
],
@server.feature(types.TEXT_DOCUMENT_DID_OPEN)
def did_open(ls: LanguageServer, params: types.DidOpenTextDocumentParams):
ls.text_document_publish_diagnostics(
types.PublishDiagnosticsParams(
uri=params.text_document.uri,
diagnostics=[
types.Diagnostic(
message="There is an error here.",
range=types.Range(
start=types.Position(line=1, character=1),
end=types.Position(line=1, character=10),
),
)
],
)
)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("hello-world", "v1")

Expand Down
2 changes: 1 addition & 1 deletion lib/pytest-lsp/tests/examples/fixture-scope/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("hello-world", "v1")

Expand Down
22 changes: 12 additions & 10 deletions lib/pytest-lsp/tests/examples/generic-rpc/server.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
from pygls.protocol import JsonRPCProtocol, default_converter
from pygls.server import Server
from pygls.server import JsonRPCServer

server = Server(protocol_cls=JsonRPCProtocol, converter_factory=default_converter)
server = JsonRPCServer(
protocol_cls=JsonRPCProtocol, converter_factory=default_converter
)


@server.lsp.fm.feature("math/add")
def addition(ls: Server, params):
@server.feature("math/add")
def addition(ls: JsonRPCServer, params):
a = params.a
b = params.b

ls.lsp.notify("log/message", dict(message=f"{a=}"))
ls.lsp.notify("log/message", dict(message=f"{b=}"))
ls.protocol.notify("log/message", dict(message=f"{a=}"))
ls.protocol.notify("log/message", dict(message=f"{b=}"))

return dict(total=a + b)


@server.lsp.fm.feature("math/sub")
def subtraction(ls: Server, params):
@server.feature("math/sub")
def subtraction(ls: JsonRPCServer, params):
a = params.a
b = params.b

ls.lsp.notify("log/message", dict(message=f"{a=}"))
ls.lsp.notify("log/message", dict(message=f"{b=}"))
ls.protocol.notify("log/message", dict(message=f"{a=}"))
ls.protocol.notify("log/message", dict(message=f"{b=}"))

return dict(total=b - a)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("hello-world", "v1")

Expand Down
2 changes: 1 addition & 1 deletion lib/pytest-lsp/tests/examples/getting-started/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("hello-world", "v1")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("hello-world", "v1")

Expand Down
2 changes: 1 addition & 1 deletion lib/pytest-lsp/tests/examples/server-stderr/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from lsprotocol import types
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("server-stderr", "v1")

Expand Down
22 changes: 11 additions & 11 deletions lib/pytest-lsp/tests/examples/window-create-progress/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import Mock

from lsprotocol import types
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("window-create-progress", "v1")

Expand All @@ -10,21 +10,21 @@
async def do_progress(ls: LanguageServer, *args):
token = "a-token"

await ls.progress.create_async(token)
await ls.work_done_progress.create_async(token)

# Begin
ls.progress.begin(
ls.work_done_progress.begin(
token,
types.WorkDoneProgressBegin(title="Indexing", percentage=0),
)
# Report
for i in range(1, 4):
ls.progress.report(
ls.work_done_progress.report(
token,
types.WorkDoneProgressReport(message=f"{i * 25}%", percentage=i * 25),
)
# End
ls.progress.end(token, types.WorkDoneProgressEnd(message="Finished"))
ls.work_done_progress.end(token, types.WorkDoneProgressEnd(message="Finished"))

return "a result"

Expand All @@ -34,30 +34,30 @@ async def duplicate_progress(ls: LanguageServer, *args):
token = "duplicate-token"

# Need to stop pygls preventing us from using the progress API wrong.
ls.progress._check_token_registered = Mock()
await ls.progress.create_async(token)
ls.work_done_progress._check_token_registered = Mock()
await ls.work_done_progress.create_async(token)

# pytest-lsp should return an error here.
await ls.progress.create_async(token)
await ls.work_done_progress.create_async(token)


@server.command("no.progress")
async def no_progress(ls: LanguageServer, *args):
token = "undefined-token"

# Begin
ls.progress.begin(
ls.work_done_progress.begin(
token,
types.WorkDoneProgressBegin(title="Indexing", percentage=0, cancellable=False),
)
# Report
for i in range(1, 4):
ls.progress.report(
ls.work_done_progress.report(
token,
types.WorkDoneProgressReport(message=f"{i * 25}%", percentage=i * 25),
)
# End
ls.progress.end(token, types.WorkDoneProgressEnd(message="Finished"))
ls.work_done_progress.end(token, types.WorkDoneProgressEnd(message="Finished"))


if __name__ == "__main__":
Expand Down
17 changes: 11 additions & 6 deletions lib/pytest-lsp/tests/examples/window-log-message-fail/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from lsprotocol import types
from pygls.lsp.server import LanguageServer

server = LanguageServer("window-log-message", "v1")


@server.feature(TEXT_DOCUMENT_COMPLETION)
def completion(ls: LanguageServer, params: CompletionParams):
@server.feature(types.TEXT_DOCUMENT_COMPLETION)
def completion(ls: LanguageServer, params: types.CompletionParams):
items = []

for i in range(10):
ls.show_message_log(f"Suggesting item {i}")
items.append(CompletionItem(label=f"item-{i}"))
ls.window_log_message(
types.LogMessageParams(
message=f"Suggesting item {i}",
type=types.MessageType.Log,
)
)
items.append(types.CompletionItem(label=f"item-{i}"))

return items

Expand Down
16 changes: 10 additions & 6 deletions lib/pytest-lsp/tests/examples/window-log-message/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from lsprotocol import types
from pygls.lsp.server import LanguageServer

server = LanguageServer("window-log-message", "v1")


@server.feature(TEXT_DOCUMENT_COMPLETION)
def completion(ls: LanguageServer, params: CompletionParams):
@server.feature(types.TEXT_DOCUMENT_COMPLETION)
def completion(ls: LanguageServer, params: types.CompletionParams):
items = []

for i in range(10):
ls.show_message_log(f"Suggesting item {i}")
items.append(CompletionItem(label=f"item-{i}"))
ls.window_log_message(
types.LogMessageParams(
message=f"Suggesting item {i}", type=types.MessageType.Log
)
)
items.append(types.CompletionItem(label=f"item-{i}"))

return items

Expand Down
19 changes: 8 additions & 11 deletions lib/pytest-lsp/tests/examples/window-show-document/server.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
from lsprotocol.types import (
TEXT_DOCUMENT_COMPLETION,
CompletionItem,
CompletionParams,
ShowDocumentParams,
)
from pygls.server import LanguageServer
from lsprotocol import types
from pygls.lsp.server import LanguageServer

server = LanguageServer("window-show-document", "v1")


@server.feature(TEXT_DOCUMENT_COMPLETION)
async def completion(ls: LanguageServer, params: CompletionParams):
@server.feature(types.TEXT_DOCUMENT_COMPLETION)
async def completion(ls: LanguageServer, params: types.CompletionParams):
items = []
await ls.show_document_async(ShowDocumentParams(uri=params.text_document.uri))
await ls.window_show_document_async(
types.ShowDocumentParams(uri=params.text_document.uri)
)

for i in range(10):
items.append(CompletionItem(label=f"item-{i}"))
items.append(types.CompletionItem(label=f"item-{i}"))

return items

Expand Down
17 changes: 11 additions & 6 deletions lib/pytest-lsp/tests/examples/window-show-message/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from lsprotocol.types import TEXT_DOCUMENT_COMPLETION, CompletionItem, CompletionParams
from pygls.server import LanguageServer
from lsprotocol import types
from pygls.lsp.server import LanguageServer

server = LanguageServer("window-show-message", "v1")


@server.feature(TEXT_DOCUMENT_COMPLETION)
def completion(ls: LanguageServer, params: CompletionParams):
@server.feature(types.TEXT_DOCUMENT_COMPLETION)
def completion(ls: LanguageServer, params: types.CompletionParams):
items = []

for i in range(10):
ls.show_message(f"Suggesting item {i}")
items.append(CompletionItem(label=f"item-{i}"))
ls.window_show_message(
types.ShowMessageParams(
message=f"Suggesting item {i}",
type=types.MessageType.Log,
)
)
items.append(types.CompletionItem(label=f"item-{i}"))

return items

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from lsprotocol import types
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer("workspace-configuration", "v1")


@server.command("server.configuration")
async def configuration(ls: LanguageServer, *args):
results = await ls.get_configuration_async(
types.WorkspaceConfigurationParams(
results = await ls.workspace_configuration_async(
types.ConfigurationParams(
items=[
types.ConfigurationItem(scope_uri="file://workspace/file.txt"),
types.ConfigurationItem(section="not.found"),
Expand Down
2 changes: 1 addition & 1 deletion lib/pytest-lsp/tests/servers/capabilities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lsprotocol.converters import get_converter
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

converter = get_converter()
server = LanguageServer(name="capabilities-server", version="v1.0")
Expand Down
Loading

0 comments on commit 541844e

Please sign in to comment.