Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect client's preferred encoding when possible #446

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions pygls/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
logger = logging.getLogger(__name__)
T = TypeVar("T")

_SUPPORTED_ENCODINGS = frozenset(
[
types.PositionEncodingKind.Utf8,
types.PositionEncodingKind.Utf16,
types.PositionEncodingKind.Utf32,
]
)


def get_capability(
client_capabilities: types.ClientCapabilities, field: str, default: Any = None
Expand Down Expand Up @@ -401,16 +409,11 @@ def _with_position_encodings(self):
if encodings is None:
return self

if types.PositionEncodingKind.Utf16 in encodings:
return self

if types.PositionEncodingKind.Utf32 in encodings:
self.server_cap.position_encoding = types.PositionEncodingKind.Utf32
return self

if types.PositionEncodingKind.Utf8 in encodings:
self.server_cap.position_encoding = types.PositionEncodingKind.Utf8
return self
# We match client preference where this an overlap between its and our supported encodings.
for encoding in encodings:
if encoding in _SUPPORTED_ENCODINGS:
self.server_cap.position_encoding = encoding
return self

logger.warning(f"Unknown `PositionEncoding`s: {encodings}")

Expand Down
13 changes: 13 additions & 0 deletions tests/test_feature_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ def server_capabilities(**kwargs):
]
)
),
server_capabilities(position_encoding=lsp.PositionEncodingKind.Utf8),
),
(
lsp.INITIALIZE,
None,
lsp.ClientCapabilities(
general=lsp.GeneralClientCapabilities(
position_encodings=[
lsp.PositionEncodingKind.Utf32,
lsp.PositionEncodingKind.Utf8,
]
)
),
server_capabilities(position_encoding=lsp.PositionEncodingKind.Utf32),
),
(
Expand Down
Loading