Skip to content

Commit

Permalink
Start to add rename functioinality
Browse files Browse the repository at this point in the history
  • Loading branch information
yeger00 committed Oct 30, 2024
1 parent 5a47454 commit 512859a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
27 changes: 25 additions & 2 deletions pylspclient/lsp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
SymbolInformation,
LocationLink,
Location,
)
from .lsp_pydantic_strcuts import (
Position,
SignatureHelp,
CompletionContext,
CompletionItem,
CompletionList,
WorkspaceEdit,
TextEdit,
)


Expand Down Expand Up @@ -261,3 +261,26 @@ def definition(
return [Location.model_validate(result) for result in result_dict]
except ValidationError:
return [LocationLink.model_validate(result) for result in result_dict]

def rename(self, text_document: TextDocumentIdentifier, position: Position, new_name: str) -> WorkspaceEdit:
"""Send a rename request to the language server at the specified position."""
response = self.lsp_endpoint.call_method(
"textDocument/rename",
textDocument=text_document,
position=position.dict(),
newName=new_name,
)

# Parse response into WorkspaceEdit using Pydantic
changes = {
uri: [
TextEdit(
range_start=Position(**edit["range"]["start"]),
range_end=Position(**edit["range"]["end"]),
new_text=edit["newText"]
)
for edit in edits
]
for uri, edits in response.get("changes", {}).items()
}
return WorkspaceEdit(changes=changes)
2 changes: 1 addition & 1 deletion pylspclient/lsp_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run(self) -> None:
# a call for notify
if method not in self.notify_callbacks:
# Have nothing to do with this.
print("Notify method not found: {method}.".format(method=method))
print("Notify method not found: {method}, {params}".format(method=method, params=params))
else:
self.notify_callbacks[method](params)
else:
Expand Down
10 changes: 9 additions & 1 deletion pylspclient/lsp_pydantic_strcuts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, List, Union
from typing import Dict, Optional, List, Union
from enum import Enum, IntEnum
from pydantic import BaseModel, HttpUrl

Expand Down Expand Up @@ -240,3 +240,11 @@ class CompletionItem(BaseModel):
class CompletionList(BaseModel):
isIncomplete: bool
items: List[CompletionItem]

class TextEdit(BaseModel):
range_start: Position
range_end: Position
new_text: str

class WorkspaceEdit(BaseModel):
changes: Dict[str, List[TextEdit]]
22 changes: 10 additions & 12 deletions tests/test_pylsp_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ def from_uri(path: str) -> str:
return path.replace("uri://", "").replace("uri:", "")


class ReadPipe(threading.Thread):
def __init__(self, pipe):
threading.Thread.__init__(self)
self.pipe = pipe

def run(self):
line = self.pipe.readline().decode('utf-8')
while line:
print(line)
line = self.pipe.readline().decode('utf-8')


@pytest.fixture
def server_process() -> subprocess.Popen:
pylsp_cmd = ["python", "-m", "pylsp"]
Expand Down Expand Up @@ -188,3 +176,13 @@ def test_completion(lsp_client: pylspclient.LspClient):
context = CompletionContext(triggerKind=CompletionTriggerKind.Invoked)
completion_result = lsp_client.completion(TextDocumentIdentifier(uri=uri), position, context)
assert all([i.insertText.startswith(to_complete) for i in completion_result.items])


def test_rename(lsp_client: pylspclient.LspClient):
add_dir(lsp_client, DEFAULT_ROOT)
file_path = "lsp_client.py"
relative_file_path = path.join(DEFAULT_ROOT, file_path)
uri = to_uri(relative_file_path)
file_content = open(relative_file_path, "r").read()
position = string_in_text_to_position(file_content, "call_method")
rename_res = lsp_client.rename(TextDocumentIdentifier(uri=uri), position, "call_method2")

0 comments on commit 512859a

Please sign in to comment.