diff --git a/pygls/capabilities.py b/pygls/capabilities.py index 975e5e24..7e265480 100644 --- a/pygls/capabilities.py +++ b/pygls/capabilities.py @@ -15,7 +15,7 @@ # limitations under the License. # ############################################################################ from functools import reduce -from typing import Any, Dict, List, Optional, Set, Union, TypeVar +from typing import Any, Dict, List, Optional, Set, Union, TypeVar, FrozenSet import logging from lsprotocol import types @@ -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 @@ -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}") diff --git a/tests/test_feature_manager.py b/tests/test_feature_manager.py index f69f12a3..744fcdb1 100644 --- a/tests/test_feature_manager.py +++ b/tests/test_feature_manager.py @@ -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), ), (