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

[WIP] Introduce support for generator feature handlers #516

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "pygls: Debug Server",
Expand Down
51 changes: 26 additions & 25 deletions pygls/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
from functools import reduce
from typing import Any, Dict, List, Optional, Set, Union, TypeVar
import logging
from functools import reduce
from typing import Any, Dict, List, Optional, Set, TypeVar, Union

from lsprotocol import types


logger = logging.getLogger(__name__)
T = TypeVar("T")

Expand Down Expand Up @@ -62,6 +61,7 @@ def __init__(
commands: List[str],
text_document_sync_kind: types.TextDocumentSyncKind,
notebook_document_sync: Optional[types.NotebookDocumentSyncOptions] = None,
position_encoding: types.PositionEncodingKind = types.PositionEncodingKind.Utf16,
):
self.client_capabilities = client_capabilities
self.features = features
Expand All @@ -71,12 +71,35 @@ def __init__(
self.notebook_document_sync = notebook_document_sync

self.server_cap = types.ServerCapabilities()
self.server_cap.position_encoding = position_encoding

def _provider_options(self, feature: str, default: T) -> Optional[Union[T, Any]]:
if feature in self.features:
return self.feature_options.get(feature, default)
return None

@classmethod
def choose_position_encoding(
cls, client_capabilities: types.ClientCapabilities
) -> types.PositionEncodingKind:
server_encoding = types.PositionEncodingKind.Utf16

if (general := client_capabilities.general) is None:
return server_encoding

if (encodings := general.position_encodings) is None:
return server_encoding

# We match client preference where this an overlap between its and our supported encodings.
for client_encoding in encodings:
if client_encoding in _SUPPORTED_ENCODINGS:
server_encoding = client_encoding
return server_encoding

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

return server_encoding

def _with_text_document_sync(self):
open_close = (
types.TEXT_DOCUMENT_DID_OPEN in self.features
Expand Down Expand Up @@ -415,27 +438,6 @@ def _with_inline_value_provider(self):
self.server_cap.inline_value_provider = value
return self

def _with_position_encodings(self):
self.server_cap.position_encoding = types.PositionEncodingKind.Utf16

general = self.client_capabilities.general
if general is None:
return self

encodings = general.position_encodings
if encodings is None:
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}")

return self

def _build(self):
return self.server_cap

Expand Down Expand Up @@ -474,6 +476,5 @@ def build(self):
._with_workspace_capabilities()
._with_diagnostic_provider()
._with_inline_value_provider()
._with_position_encodings()
._build()
)
6 changes: 1 addition & 5 deletions pygls/protocol/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from typing import Any

from collections import namedtuple
from typing import Any

from lsprotocol import converters

Expand All @@ -12,7 +11,6 @@
JsonRPCResponseMessage,
)
from pygls.protocol.language_server import LanguageServerProtocol, lsp_method
from pygls.protocol.lsp_meta import LSPMeta, call_user_feature


def _dict_to_object(d: Any):
Expand Down Expand Up @@ -68,8 +66,6 @@ def default_converter():
"JsonRPCRequestMessage",
"JsonRPCResponseMessage",
"JsonRPCNotification",
"LSPMeta",
"call_user_feature",
"_dict_to_object",
"_params_field_structure_hook",
"_result_field_structure_hook",
Expand Down
Loading
Loading