Skip to content

Commit

Permalink
code actions: support cancellation of md5sum command
Browse files Browse the repository at this point in the history
  • Loading branch information
perrinjerome committed Oct 4, 2023
1 parent 0b1a232 commit 1f1faa5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
4 changes: 4 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning][semver].

## Unreleased

## Added

- code actions: support cancellation of md5sum command

## Fixed

- hover: fix crash when opening instance.cfg.in outside of workspace folder
Expand Down
25 changes: 14 additions & 11 deletions server/buildoutls/md5sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def update_md5sum(
ls.progress.begin(
token,
WorkDoneProgressBegin(
cancellable=True, # TODO actually support cancellation
cancellable=True,
title=f"Updating md5sum for {url}",
))

Expand Down Expand Up @@ -64,6 +64,8 @@ async def update_md5sum(
message=f"{percentage:0.2f}% in {elapsed_time:0.2f}s",
percentage=max(0, int(percentage)),
))
if ls.progress.tokens[token].cancelled():
break

hexdigest = m.hexdigest()

Expand All @@ -88,13 +90,14 @@ async def update_md5sum(
)
new_text = f"md5sum = {hexdigest}\n"

ls.progress.end(token, WorkDoneProgressEnd())
ls.apply_edit(
WorkspaceEdit(
changes={
md5sum_location.uri:
[TextEdit(
range=md5sum_location.range,
new_text=new_text,
)]
}))
if not ls.progress.tokens[token].cancelled():
ls.progress.end(token, WorkDoneProgressEnd())
ls.apply_edit(
WorkspaceEdit(
changes={
md5sum_location.uri:
[TextEdit(
range=md5sum_location.range,
new_text=new_text,
)]
}))
3 changes: 3 additions & 0 deletions server/buildoutls/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import collections
import concurrent.futures
import os
import urllib.parse
from typing import Any
Expand Down Expand Up @@ -55,6 +57,7 @@ def __init__(self):
show_message_log = mock.Mock()
apply_edit = mock.Mock()
progress = mock.create_autospec(pygls.progress.Progress)
progress.tokens = collections.defaultdict(concurrent.futures.Future)

server = FakeServer()

Expand Down
51 changes: 51 additions & 0 deletions server/buildoutls/tests/test_code_actions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import collections
import concurrent.futures
import json
import pathlib
import textwrap
Expand Down Expand Up @@ -550,6 +552,55 @@ async def test_update_md5sum_code_action_without_md5sum_option(
}, ), )


async def test_update_md5sum_code_action_cancelled(
server,
example_com_response,
) -> None:

code_action_params = CodeActionParams(
text_document=TextDocumentIdentifier(
uri='file:///code_actions/update_md5sum.cfg'),
range=Range(start=Position(line=1, character=11),
end=Position(line=1, character=12)),
context=CodeActionContext(diagnostics=[]))

code_actions = await lsp_code_action(
server,
_dump_and_load(code_action_params),
)
assert isinstance(code_actions, list)
assert code_actions == [
CodeAction(
title='Update md5sum',
kind=CodeActionKind.QuickFix,
command=Command(
title='Update md5sum',
command=COMMAND_UPDATE_MD5SUM,
arguments=[
UpdateMD5SumCommandParams(
document_uri='file:///code_actions/update_md5sum.cfg',
section_name='section',
),
],
),
)
]
assert isinstance(code_actions[0].command, Command)
assert code_actions[0].command.arguments

def cancelled_future() -> concurrent.futures.Future[None]:
f: concurrent.futures.Future[None] = concurrent.futures.Future()
f.cancel()
return f

server.progress.tokens = collections.defaultdict(cancelled_future)

await command_update_md5sum(
server,
cast(List[UpdateMD5SumCommandParams], code_actions[0].command.arguments))
server.apply_edit.assert_not_called()


@pytest.mark.parametrize(
'range_',
[
Expand Down

0 comments on commit 1f1faa5

Please sign in to comment.