Skip to content

Commit

Permalink
fix: compute prepare_provider for RenameOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed May 3, 2024
1 parent 64f0659 commit d636003
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pygls/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,25 @@ def _with_document_on_type_formatting(self):
return self

def _with_rename(self):
value = self._provider_options(types.TEXT_DOCUMENT_RENAME, default=True)
if value is not None:
self.server_cap.rename_provider = value
server_supports_rename = types.TEXT_DOCUMENT_RENAME in self.features
if server_supports_rename is False:
return self

client_prepare_support = get_capability(
self.client_capabilities, "text_document.rename.prepare_support", False
)

# From the spec:
# > RenameOptions may only be specified if the client states that it supports
# > prepareSupport in its initial initialize request.
if not client_prepare_support:
self.server_cap.rename_provider = server_supports_rename

else:
self.server_cap.rename_provider = types.RenameOptions(
prepare_provider=types.TEXT_DOCUMENT_PREPARE_RENAME in self.features
)

return self

def _with_folding_range(self):
Expand Down
54 changes: 54 additions & 0 deletions tests/test_feature_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,60 @@ def _():
assert expected == actual


def test_register_prepare_rename_no_client_support(feature_manager: FeatureManager):

@feature_manager.feature(lsp.TEXT_DOCUMENT_RENAME)
def _():
pass

@feature_manager.feature(lsp.TEXT_DOCUMENT_PREPARE_RENAME)
def _():
pass

expected = server_capabilities(rename_provider=True)

actual = ServerCapabilitiesBuilder(
lsp.ClientCapabilities(),
feature_manager.features.keys(),
feature_manager.feature_options,
[],
None,
None,
).build()

assert expected == actual


def test_register_prepare_rename_with_client_support(feature_manager: FeatureManager):

@feature_manager.feature(lsp.TEXT_DOCUMENT_RENAME)
def _():
pass

@feature_manager.feature(lsp.TEXT_DOCUMENT_PREPARE_RENAME)
def _():
pass

expected = server_capabilities(
rename_provider=lsp.RenameOptions(prepare_provider=True)
)

actual = ServerCapabilitiesBuilder(
lsp.ClientCapabilities(
text_document=lsp.TextDocumentClientCapabilities(
rename=lsp.RenameClientCapabilities(prepare_support=True)
)
),
feature_manager.features.keys(),
feature_manager.feature_options,
[],
None,
None,
).build()

assert expected == actual


def test_register_inlay_hint_resolve(feature_manager: FeatureManager):
@feature_manager.feature(lsp.TEXT_DOCUMENT_INLAY_HINT)
def _():
Expand Down

0 comments on commit d636003

Please sign in to comment.