diff --git a/examples/servers-next/code_actions.py b/examples/servers-next/code_actions.py new file mode 100644 index 00000000..4c393278 --- /dev/null +++ b/examples/servers-next/code_actions.py @@ -0,0 +1,70 @@ +############################################################################ +# Copyright(c) Open Law Library. All rights reserved. # +# See ThirdPartyNotices.txt in the project root for additional notices. # +# # +# Licensed under the Apache License, Version 2.0 (the "License") # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http: // www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +############################################################################ +import asyncio +import logging +import re + +from lsprotocol import types + +from pygls.lsp.server import LanguageServer + +ADDITION = re.compile(r"^\s*(\d+)\s*\+\s*(\d+)\s*=(?=\s*$)") +server = LanguageServer("code-action-server", "v0.1") + + +@server.feature( + types.TEXT_DOCUMENT_CODE_ACTION, + types.CodeActionOptions(code_action_kinds=[types.CodeActionKind.QuickFix]), +) +def code_actions(params: types.CodeActionParams): + items = [] + document_uri = params.text_document.uri + document = server.workspace.get_document(document_uri) + + start_line = params.range.start.line + end_line = params.range.end.line + + lines = document.lines[start_line : end_line + 1] + for idx, line in enumerate(lines): + match = ADDITION.match(line) + if match is not None: + range_ = types.Range( + start=types.Position(line=start_line + idx, character=0), + end=types.Position(line=start_line + idx, character=len(line) - 1), + ) + + left = int(match.group(1)) + right = int(match.group(2)) + answer = left + right + + text_edit = types.TextEdit( + range=range_, new_text=f"{line.strip()} {answer}!" + ) + + action = types.CodeAction( + title=f"Evaluate '{match.group(0)}'", + kind=types.CodeActionKind.QuickFix, + edit=types.WorkspaceEdit(changes={document_uri: [text_edit]}), + ) + items.append(action) + + return items + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG, filename="server.log", filemode="w") + asyncio.run(server.start_io()) diff --git a/tests/lsp_next/test_code_action.py b/tests/lsp_next/test_code_action.py new file mode 100644 index 00000000..2a79c4fb --- /dev/null +++ b/tests/lsp_next/test_code_action.py @@ -0,0 +1,92 @@ +############################################################################ +# Copyright(c) Open Law Library. All rights reserved. # +# See ThirdPartyNotices.txt in the project root for additional notices. # +# # +# Licensed under the Apache License, Version 2.0 (the "License") # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http: // www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +############################################################################ +import pathlib +import sys +from typing import Tuple + +import pytest_asyncio +from lsprotocol import types + +from pygls import uris +from pygls.lsp._client import LanguageClient + + +@pytest_asyncio.fixture +async def code_actions(): + client = LanguageClient("pygls-test-suite", "v1") + server_dir = pathlib.Path( + __file__, "..", "..", "..", "examples", "servers-next" + ).resolve() + assert server_dir.exists() + root_dir = (server_dir / "workspace").resolve() + + await client.start_io(sys.executable, str(server_dir / "code_actions.py")) + + # Initialize the server + response = await client.initialize( + types.InitializeParams( + capabilities=types.ClientCapabilities(), + root_uri=uris.from_fs_path(root_dir), + ) + ) + assert response is not None + + yield client, response + + await client.shutdown(None) + client.exit(None) + + await client.stop() + + +async def test_code_actions( + code_actions: Tuple[LanguageClient, types.InitializeResult], uri_for +): + """Ensure that the example code action server is working as expected.""" + client, initialize_result = code_actions + + code_action_options = initialize_result.capabilities.code_action_provider + assert code_action_options.code_action_kinds == [types.CodeActionKind.QuickFix] + + test_uri = uri_for("sums.txt") + assert test_uri is not None + + response = await client.text_document_code_action( + types.CodeActionParams( + text_document=types.TextDocumentIdentifier(uri=test_uri), + range=types.Range( + start=types.Position(line=0, character=0), + end=types.Position(line=1, character=0), + ), + context=types.CodeActionContext(diagnostics=[]), + ) + ) + + assert len(response) == 1 + code_action = response[0] + + assert code_action.title == "Evaluate '1 + 1 ='" + assert code_action.kind == types.CodeActionKind.QuickFix + + fix = code_action.edit.changes[test_uri][0] + expected_range = types.Range( + start=types.Position(line=0, character=0), + end=types.Position(line=0, character=7), + ) + + assert fix.range == expected_range + assert fix.new_text == "1 + 1 = 2!"