Skip to content

Commit 0d25e6e

Browse files
committed
docs(examples): add comprehensive comments to explain each example's purpose and functionality
1 parent f112232 commit 0d25e6e

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

examples/protocol.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Example: Protocol-based capability checking for LSP clients
2+
#
3+
# This example demonstrates how to use Python's Protocol to define expected
4+
# capabilities for LSP clients and perform runtime checks to ensure clients
5+
# meet those requirements.
6+
17
from __future__ import annotations
28

39
from typing import Protocol, runtime_checkable
@@ -17,14 +23,24 @@ class ExpectClientProtocol(
1723
WithRequestDefinition,
1824
Protocol,
1925
):
20-
"""We expect the client to (at least) support 'references' and 'definition' requests."""
26+
"""Protocol defining minimum expected client capabilities.
27+
28+
This protocol specifies that any client implementing it must support
29+
both 'references' and 'definition' LSP requests. Using @runtime_checkable
30+
allows us to use isinstance() and issubclass() checks at runtime.
31+
"""
2132

2233

2334
class BadClient(
2435
LSPClient,
2536
WithRequestDocumentSymbol,
2637
):
27-
"""The bad client does not meet our expectations."""
38+
"""Client that fails to meet protocol requirements.
39+
40+
This client only implements document symbol capability but lacks
41+
the required references and definition capabilities, making it
42+
incompatible with ExpectClientProtocol.
43+
"""
2844

2945

3046
class GoodClient(
@@ -33,8 +49,18 @@ class GoodClient(
3349
WithRequestDefinition,
3450
WithRequestCallHierarchy,
3551
):
36-
"""The good client meets our expectations."""
52+
"""Client that satisfies protocol requirements.
53+
54+
This client implements both required capabilities (references and
55+
definition) plus an additional capability (call hierarchy), making
56+
it fully compatible with ExpectClientProtocol.
57+
"""
3758

3859

39-
assert not issubclass(BadClient, ExpectClientProtocol)
40-
assert issubclass(GoodClient, ExpectClientProtocol)
60+
# Runtime checks to verify protocol compliance
61+
assert not issubclass(
62+
BadClient, ExpectClientProtocol
63+
) # Should fail - missing required capabilities
64+
assert issubclass(
65+
GoodClient, ExpectClientProtocol
66+
) # Should pass - meets all requirements

examples/pyrefly.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Example: Using Pyrefly LSP server for Python reference finding
2+
#
3+
# This example demonstrates how to use the Pyrefly language server
4+
# to find all references to a symbol in Python code. Pyrefly is a
5+
# Python language server that provides enhanced static analysis.
6+
17
from __future__ import annotations
28

39
import anyio
@@ -10,20 +16,25 @@
1016

1117

1218
async def main():
19+
# Initialize Pyrefly client with local server
1320
async with PyreflyClient(server=PyreflyLocalServer()) as client:
21+
# Request references to PyreflyClient at line 21, column 19
1422
refs = await client.request_references(
1523
file_path="src/lsp_client/clients/pyrefly.py",
1624
position=Position(21, 19),
17-
include_declaration=False,
25+
include_declaration=False, # Don't include the declaration itself
1826
)
1927

2028
if not refs:
2129
print("No references found.")
2230
return
2331

32+
# Print all found references
2433
for ref in refs:
2534
print(f"Reference found at {ref.uri} - Range: {ref.range}")
2635

36+
# Verify that this example file contains a reference to PyreflyClient
37+
# This demonstrates the reference finding functionality works correctly
2738
assert any(
2839
ref.uri.endswith(__file__)
2940
and ref.range
@@ -37,4 +48,4 @@ async def main():
3748

3849

3950
if __name__ == "__main__":
40-
anyio.run(main)
51+
anyio.run(main) # Run the async example

examples/pyright_docker.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Example: Using Pyright LSP server in Docker for Python definition lookup
2+
#
3+
# This example demonstrates how to use Pyright (Microsoft's Python language server)
4+
# running in a Docker container to find definition locations of Python symbols.
5+
# The Docker approach provides isolation and consistent environment setup.
6+
17
from __future__ import annotations
28

39
from pathlib import Path
@@ -9,11 +15,15 @@
915

1016

1117
async def main():
18+
# Set up workspace directory and mount it in Docker
1219
workspace = Path.cwd()
1320
async with PyrightClient(
14-
server=PyrightDockerServer(mounts=[workspace]),
21+
server=PyrightDockerServer(
22+
mounts=[workspace]
23+
), # Mount workspace into container
1524
workspace=workspace,
1625
) as client:
26+
# Find definition of PyrightDockerServer at line 12, column 28
1727
refs = await client.request_definition_locations(
1828
file_path=__file__,
1929
position=Position(12, 28),
@@ -23,9 +33,10 @@ async def main():
2333
print("No definition locations found.")
2434
return
2535

36+
# Display all definition locations found
2637
for ref in refs:
2738
print(f"Definition location found at {ref.uri} - Range: {ref.range}")
2839

2940

3041
if __name__ == "__main__":
31-
anyio.run(main)
42+
anyio.run(main) # Run the async example

examples/rust_analyzer.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Example: Basic Rust Analyzer LSP client setup
2+
#
3+
# This example demonstrates how to initialize a Rust Analyzer client
4+
# and retrieve basic information about the language server configuration.
5+
# Rust Analyzer is the official language server for Rust programming language.
6+
17
from __future__ import annotations
28

39
import anyio
@@ -9,9 +15,12 @@
915

1016

1117
async def main():
18+
# Initialize Rust Analyzer client with local server
1219
async with RustAnalyzerClient(server=RustAnalyzerLocalServer()) as client:
20+
# Get and display the language ID for this client
21+
# This should return "rust" for Rust Analyzer
1322
print(client.get_language_id())
1423

1524

1625
if __name__ == "__main__":
17-
anyio.run(main)
26+
anyio.run(main) # Run the async example

0 commit comments

Comments
 (0)