Skip to content

Commit

Permalink
refactor(langserver): remove async code from code actions
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Jan 1, 2024
1 parent 135b0d4 commit 87cb416
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import annotations

from asyncio import CancelledError
from concurrent.futures import CancelledError
from itertools import chain
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union, cast

from robotcode.core.async_tools import async_tasking_event
from robotcode.core.event import event
from robotcode.core.lsp.types import (
CodeAction,
CodeActionContext,
Expand All @@ -16,7 +14,7 @@
TextDocumentIdentifier,
)
from robotcode.core.utils.logging import LoggingDescriptor
from robotcode.core.utils.threading import threaded
from robotcode.core.utils.threading import check_thread_canceled, threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import CODE_ACTION_KINDS_ATTR, HasCodeActionKinds, language_id_filter
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
Expand All @@ -29,17 +27,17 @@
class CodeActionProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
def __init__(self, parent: "LanguageServerProtocol") -> None:
super().__init__(parent)

@async_tasking_event
async def collect(
@event
def collect(
sender, document: TextDocument, range: Range, context: CodeActionContext # NOSONAR
) -> Optional[List[Union[Command, CodeAction]]]:
...

@async_tasking_event
async def resolve(sender, code_action: CodeAction) -> Optional[CodeAction]: # NOSONAR
@event
def resolve(sender, code_action: CodeAction) -> Optional[CodeAction]: # NOSONAR
...

def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
Expand All @@ -61,7 +59,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:

@rpc_method(name="textDocument/codeAction", param_type=CodeActionParams)
@threaded
async def _text_document_code_action(
def _text_document_code_action(
self,
text_document: TextDocumentIdentifier,
range: Range,
Expand All @@ -81,13 +79,11 @@ async def _text_document_code_action(
for r in c.related_information:
r.location.range = document.range_from_utf16(r.location.range)

for result in await self.collect(
self,
document,
document.range_from_utf16(range),
context,
callback_filter=language_id_filter(document),
for result in self.collect(
self, document, document.range_from_utf16(range), context, callback_filter=language_id_filter(document)
):
check_thread_canceled()

if isinstance(result, BaseException):
if not isinstance(result, CancelledError):
self._logger.exception(result, exc_info=result)
Expand All @@ -102,15 +98,17 @@ async def _text_document_code_action(

@rpc_method(name="codeAction/resolve", param_type=CodeAction)
@threaded
async def _text_document_code_action_resolve(
def _text_document_code_action_resolve(
self,
params: CodeAction,
*args: Any,
**kwargs: Any,
) -> CodeAction:
results: List[CodeAction] = []

for result in await self.resolve(self, params):
for result in self.resolve(self, params):
check_thread_canceled()

if isinstance(result, BaseException):
if not isinstance(result, CancelledError):
self._logger.exception(result, exc_info=result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def _ensure_http_server_started(self) -> None:
]
)
@_logger.call
async def collect(
def collect(
self,
sender: Any,
document: TextDocument,
Expand All @@ -270,11 +270,8 @@ async def collect(
if CodeActionKind.SOURCE.value in context.only and range in range_from_token(
node.get_token(RobotToken.NAME)
):
url = await self.build_url(
node.name,
node.args if isinstance(node, LibraryImport) else (),
document,
namespace,
url = self.build_url(
node.name, node.args if isinstance(node, LibraryImport) else (), document, namespace
)

return [self.open_documentation_code_action(url)]
Expand Down Expand Up @@ -338,26 +335,14 @@ async def collect(
if entry is None:
return None

url = await self.build_url(
entry.import_name,
entry.args,
document,
namespace,
kw_doc.name,
)
url = self.build_url(entry.import_name, entry.args, document, namespace, kw_doc.name)

return [self.open_documentation_code_action(url)]

if isinstance(node, KeywordName):
name_token = node.get_token(RobotToken.KEYWORD_NAME)
if name_token is not None and range in range_from_token(name_token):
url = await self.build_url(
str(document.uri.to_path().name),
(),
document,
namespace,
name_token.value,
)
url = self.build_url(str(document.uri.to_path().name), (), document, namespace, name_token.value)

return [self.open_documentation_code_action(url)]

Expand All @@ -374,7 +359,7 @@ def open_documentation_code_action(self, url: str) -> CodeAction:
),
)

async def build_url(
def build_url(
self,
name: str,
args: Tuple[Any, ...],
Expand Down Expand Up @@ -423,7 +408,7 @@ async def build_url(

@rpc_method(name="robot/documentationServer/convertUri", param_type=ConvertUriParams)
@threaded
async def _convert_uri(self, uri: str, *args: Any, **kwargs: Any) -> Optional[str]:
def _convert_uri(self, uri: str, *args: Any, **kwargs: Any) -> Optional[str]:
real_uri = Uri(uri)

folder = self.parent.workspace.get_workspace_folder(real_uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def find_keyword_sections(node: ast.AST) -> Optional[List[ast.AST]]:


class CodeActionHelperMixin:
async def create_insert_keyword_workspace_edit(
def create_insert_keyword_workspace_edit(
self, document: TextDocument, model: ast.AST, namespace: Namespace, insert_text: str
) -> Tuple[str, Range]:
keyword_sections = find_keyword_sections(model)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

from collections import defaultdict
from dataclasses import dataclass
from string import Template as StringTemplate
Expand Down Expand Up @@ -91,7 +89,7 @@ class CodeActionData(CodeActionDataBase):
class RobotCodeActionQuickFixesProtocolPart(RobotLanguageServerProtocolPart, ModelHelperMixin, CodeActionHelperMixin):
_logger = LoggingDescriptor()

def __init__(self, parent: RobotLanguageServerProtocol) -> None:
def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
super().__init__(parent)

parent.code_action.collect.add(self.collect)
Expand All @@ -101,12 +99,12 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:

@language_id("robotframework")
@code_action_kinds([CodeActionKind.QUICK_FIX])
async def collect(
def collect(
self, sender: Any, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
result = []
for method in iter_methods(self, lambda m: m.__name__.startswith("code_action_")):
code_actions = await method(document, range, context)
code_actions = method(document, range, context)
if code_actions:
result.extend(code_actions)

Expand All @@ -115,17 +113,17 @@ async def collect(

return None

async def resolve(self, sender: Any, code_action: CodeAction) -> Optional[CodeAction]:
def resolve(self, sender: Any, code_action: CodeAction) -> Optional[CodeAction]:
if code_action.data is not None and isinstance(code_action.data, Mapping):
type = code_action.data.get("type", None)
if type == "quickfix":
method_name = code_action.data.get("method")
method = next(iter_methods(self, lambda m: m.__name__ == f"resolve_code_action_{method_name}"))
await method(code_action, data=from_dict(code_action.data, CodeActionData))
method(code_action, data=from_dict(code_action.data, CodeActionData))

return None

async def code_action_create_keyword(
def code_action_create_keyword(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
result: List[Union[Command, CodeAction]] = []
Expand Down Expand Up @@ -184,9 +182,7 @@ async def code_action_create_keyword(

return result if result else None

async def resolve_code_action_create_keyword(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
def resolve_code_action_create_keyword(self, code_action: CodeAction, data: CodeActionData) -> Optional[CodeAction]:
document = self.parent.documents.get(data.document_uri)
if document is None:
return None
Expand Down Expand Up @@ -240,7 +236,7 @@ async def resolve_code_action_create_keyword(
else:
dest_document = document

code_action.edit, select_range = await self._apply_create_keyword(dest_document, insert_text)
code_action.edit, select_range = self._apply_create_keyword(dest_document, insert_text)

code_action.command = Command(
SHOW_DOCUMENT_SELECT_AND_RENAME_COMMAND,
Expand All @@ -251,13 +247,11 @@ async def resolve_code_action_create_keyword(

return None

async def _apply_create_keyword(self, document: TextDocument, insert_text: str) -> Tuple[WorkspaceEdit, Range]:
def _apply_create_keyword(self, document: TextDocument, insert_text: str) -> Tuple[WorkspaceEdit, Range]:
model = self.parent.documents_cache.get_model(document, False)
namespace = self.parent.documents_cache.get_namespace(document)

insert_text, insert_range = await self.create_insert_keyword_workspace_edit(
document, model, namespace, insert_text
)
insert_text, insert_range = self.create_insert_keyword_workspace_edit(document, model, namespace, insert_text)

we = WorkspaceEdit(
document_changes=[
Expand All @@ -276,7 +270,7 @@ async def _apply_create_keyword(self, document: TextDocument, insert_text: str)

return we, selection_range

async def code_action_disable_robotcode_diagnostics_for_line(
def code_action_disable_robotcode_diagnostics_for_line(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
if (
Expand Down Expand Up @@ -313,7 +307,7 @@ async def code_action_disable_robotcode_diagnostics_for_line(

return None

async def resolve_code_action_disable_robotcode_diagnostics_for_line(
def resolve_code_action_disable_robotcode_diagnostics_for_line(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
Expand Down Expand Up @@ -350,7 +344,7 @@ async def resolve_code_action_disable_robotcode_diagnostics_for_line(

return None

async def code_action_create_local_variable(
def code_action_create_local_variable(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
result: List[Union[Command, CodeAction]] = []
Expand Down Expand Up @@ -405,7 +399,7 @@ async def code_action_create_local_variable(

return result if result else None

async def resolve_code_action_create_local_variable(
def resolve_code_action_create_local_variable(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
Expand Down Expand Up @@ -457,7 +451,7 @@ async def resolve_code_action_create_local_variable(

return None

async def code_action_create_suite_variable(
def code_action_create_suite_variable(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
result: List[Union[Command, CodeAction]] = []
Expand Down Expand Up @@ -499,7 +493,7 @@ async def code_action_create_suite_variable(

return result if result else None

async def resolve_code_action_create_suite_variable(
def resolve_code_action_create_suite_variable(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
Expand Down Expand Up @@ -589,7 +583,7 @@ async def resolve_code_action_create_suite_variable(
return code_action
return None

async def code_action_add_argument(
def code_action_add_argument(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
result: List[Union[Command, CodeAction]] = []
Expand Down Expand Up @@ -638,9 +632,7 @@ async def code_action_add_argument(

return result if result else None

async def resolve_code_action_add_argument(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
def resolve_code_action_add_argument(self, code_action: CodeAction, data: CodeActionData) -> Optional[CodeAction]:
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
document = self.parent.documents.get(data.document_uri)
if document is None:
Expand Down
Loading

0 comments on commit 87cb416

Please sign in to comment.