Skip to content

Commit

Permalink
refactor: move most of langserver...ast_utils to robotcode.robot.util…
Browse files Browse the repository at this point in the history
…s.ast
  • Loading branch information
d-biehl committed Dec 25, 2023
1 parent c9aaa60 commit bc96805
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 600 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.

## [unreleased]

### Refactor

- **langserver:** Remove async from robotcode langserver analytics ([1ff1e44](https://github.com/d-biehl/robotcode/commit/1ff1e44d91f64671e108b35f8fe9f7005a72be5d))
- **langserver:** Remove AsyncVisitor code ([c9aaa60](https://github.com/d-biehl/robotcode/commit/c9aaa6088a86c67b8dec5cb9794efe6c339cc6a5))
- **robotcode:** Move threaded decorator to the new core.utils.threading module ([96b897b](https://github.com/d-biehl/robotcode/commit/96b897b63bdc13aaa0e383a12e74399e0f8caa86))
- Remove some unneeded code ([65e1775](https://github.com/d-biehl/robotcode/commit/65e1775fd380b7e6b67a88c327f8961929e9cafb))


### Testing

- Add RFW 7.0 to devel and test matrix ([cd35020](https://github.com/d-biehl/robotcode/commit/cd3502086b1505b53a89047d27ca097d3dfce07b))
- Try to stablize regression tests ([19419b3](https://github.com/d-biehl/robotcode/commit/19419b3c10f0a7e079d6b6b7466516f4ec756f88))
- Enable RFW 7.0 tests in github workflow ([6a39f66](https://github.com/d-biehl/robotcode/commit/6a39f66f6113ef8e1437a3bf2ae10d3de6fe0203))
- Another try to stabilize regression tests ([91d4d48](https://github.com/d-biehl/robotcode/commit/91d4d482fdcecc65270f98f715760576ea9f7544))


## [0.68.1](https://github.com/d-biehl/robotcode/compare/v0.68.0..v0.68.1) - 2023-12-21

### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,13 @@
cast,
)

from robot.parsing.lexer.tokens import Token
from robotcode.core.lsp.types import Position, Range
from robotcode.robot.utils import get_robot_version
from robotcode.robot.utils.ast import get_variable_token, range_from_token, strip_variable_token
from robotcode.robot.utils.markdownformatter import MarkDownFormatter

from ..utils.ast_utils import (
HasError,
HasErrors,
Token,
get_variable_token,
range_from_token,
strip_variable_token,
)
from ..utils.ast_utils import HasError, HasErrors
from ..utils.match import normalize, normalize_namespace
from .entities import (
ArgumentDefinition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,23 @@
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union, cast
from urllib.parse import parse_qs, urlparse

from robotcode.core.lsp.types import (
CodeAction,
CodeActionContext,
CodeActionKind,
Command,
Range,
)
from robot.parsing.lexer.tokens import Token
from robotcode.core.lsp.types import CodeAction, CodeActionContext, CodeActionKind, Command, Range
from robotcode.core.uri import Uri
from robotcode.core.utils.dataclasses import CamelSnakeMixin
from robotcode.core.utils.logging import LoggingDescriptor
from robotcode.core.utils.net import find_free_port
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.robot.utils.ast import get_node_at_position, range_from_token

from ...common.decorators import code_action_kinds, language_id
from ...common.text_document import TextDocument
from ..configuration import (
DocumentationServerConfig,
)
from ..configuration import DocumentationServerConfig
from ..diagnostics.entities import LibraryEntry
from ..diagnostics.library_doc import (
get_library_doc,
get_robot_library_html_doc_str,
resolve_robot_variables,
)
from ..diagnostics.library_doc import get_library_doc, get_robot_library_html_doc_str, resolve_robot_variables
from ..diagnostics.model_helper import ModelHelperMixin
from ..diagnostics.namespace import (
Namespace,
)
from ..utils.ast_utils import (
Token,
get_node_at_position,
range_from_token,
)
from ..diagnostics.namespace import Namespace
from .protocol_part import RobotLanguageServerProtocolPart

if TYPE_CHECKING:
Expand Down Expand Up @@ -277,7 +260,7 @@ async def collect(
namespace = self.parent.documents_cache.get_namespace(document)

model = self.parent.documents_cache.get_model(document, False)
node = await get_node_at_position(model, range.start)
node = get_node_at_position(model, range.start)

if context.only and isinstance(node, (LibraryImport, ResourceImport)):
if CodeActionKind.SOURCE.value in context.only and range in range_from_token(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
Position,
Range,
)
from robotcode.language_server.common.text_document import TextDocument
from robotcode.language_server.robotframework.diagnostics.namespace import Namespace
from robotcode.language_server.robotframework.utils.ast_utils import (
range_from_node,
)
from robotcode.language_server.robotframework.utils.async_ast import Visitor
from robotcode.robot.utils.ast import range_from_node

from ...common.text_document import TextDocument
from ..diagnostics.namespace import Namespace
from ..utils.async_ast import Visitor

SHOW_DOCUMENT_SELECT_AND_RENAME_COMMAND = "_robotcode.codeActionShowDocumentSelectAndRename"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

from collections import defaultdict
from dataclasses import dataclass
from string import Template
from string import Template as StringTemplate
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Tuple, Union, cast

from robot.parsing.lexer.tokens import Token
from robot.parsing.model.blocks import Keyword, TestCase, VariableSection
from robot.parsing.model.statements import (
Arguments,
Documentation,
Fixture,
KeywordCall,
KeywordName,
Statement,
Template,
TestCaseName,
TestTemplate,
Variable,
)
from robot.utils.escaping import split_from_equals
from robot.variables.search import contains_variable
from robotcode.core.lsp.types import (
AnnotatedTextEdit,
ChangeAnnotation,
Expand All @@ -23,21 +39,20 @@
from robotcode.core.utils.dataclasses import as_dict, from_dict
from robotcode.core.utils.inspect import iter_methods
from robotcode.core.utils.logging import LoggingDescriptor

from ...common.decorators import code_action_kinds, language_id
from ...common.text_document import TextDocument
from ..diagnostics.errors import DIAGNOSTICS_SOURCE_NAME, Error
from ..diagnostics.model_helper import ModelHelperMixin
from ..utils.ast_utils import (
from robotcode.robot.utils.ast import (
FirstAndLastRealStatementFinder,
Token,
get_node_at_position,
get_nodes_at_position,
get_tokens_at_position,
iter_nodes,
range_from_node,
range_from_token,
)

from ...common.decorators import code_action_kinds, language_id
from ...common.text_document import TextDocument
from ..diagnostics.errors import DIAGNOSTICS_SOURCE_NAME, Error
from ..diagnostics.model_helper import ModelHelperMixin
from .code_action_helper_mixin import (
SHOW_DOCUMENT_SELECT_AND_RENAME_COMMAND,
CodeActionDataBase,
Expand All @@ -55,7 +70,7 @@ class CodeActionData(CodeActionDataBase):
diagnostics_code: Optional[Union[int, str]] = None


KEYWORD_WITH_ARGS_TEMPLATE = Template(
KEYWORD_WITH_ARGS_TEMPLATE = StringTemplate(
"""\
${name}
[Arguments] ${args}
Expand All @@ -64,7 +79,7 @@ class CodeActionData(CodeActionDataBase):
"""
)

KEYWORD_TEMPLATE = Template(
KEYWORD_TEMPLATE = StringTemplate(
"""\
${name}
# TODO: implement keyword "${name}".
Expand Down Expand Up @@ -113,13 +128,6 @@ async def resolve(self, sender: Any, code_action: CodeAction) -> Optional[CodeAc
async def code_action_create_keyword(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
from robot.parsing.model.statements import (
Fixture,
KeywordCall,
Template,
TestTemplate,
)

result: List[Union[Command, CodeAction]] = []

if (context.only and CodeActionKind.QUICK_FIX in context.only) or context.trigger_kind in [
Expand All @@ -135,7 +143,7 @@ async def code_action_create_keyword(
if d.source == DIAGNOSTICS_SOURCE_NAME and d.code == Error.KEYWORD_NOT_FOUND
):
disabled = None
node = await get_node_at_position(model, diagnostic.range.start)
node = get_node_at_position(model, diagnostic.range.start)

if isinstance(node, (KeywordCall, Fixture, TestTemplate, Template)):
tokens = get_tokens_at_position(node, diagnostic.range.start)
Expand Down Expand Up @@ -179,22 +187,12 @@ async def code_action_create_keyword(
async def resolve_code_action_create_keyword(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
from robot.parsing.lexer import Token as RobotToken
from robot.parsing.model.statements import (
Fixture,
KeywordCall,
Template,
TestTemplate,
)
from robot.utils.escaping import split_from_equals
from robot.variables.search import contains_variable

document = self.parent.documents.get(data.document_uri)
if document is None:
return None

model = self.parent.documents_cache.get_model(document, False)
node = await get_node_at_position(model, data.range.start)
node = get_node_at_position(model, data.range.start)

if isinstance(node, (KeywordCall, Fixture, TestTemplate, Template)):
tokens = get_tokens_at_position(node, data.range.start)
Expand Down Expand Up @@ -224,7 +222,7 @@ async def resolve_code_action_create_keyword(

arguments = []

for t in node.get_tokens(RobotToken.ARGUMENT):
for t in node.get_tokens(Token.ARGUMENT):
name, value = split_from_equals(cast(Token, t).value)
if value is not None and not contains_variable(name, "$@&%"):
arguments.append(f"${{{name}}}")
Expand Down Expand Up @@ -355,9 +353,6 @@ async def resolve_code_action_disable_robotcode_diagnostics_for_line(
async def code_action_create_local_variable(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
from robot.parsing.model.blocks import Keyword, TestCase
from robot.parsing.model.statements import Documentation, Fixture, Statement, Template, TestCaseName

result: List[Union[Command, CodeAction]] = []

if (context.only and CodeActionKind.QUICK_FIX in context.only) or context.trigger_kind in [
Expand All @@ -374,7 +369,7 @@ async def code_action_create_local_variable(
and diagnostic.range.start.character < diagnostic.range.end.character
):
model = self.parent.documents_cache.get_model(document, False)
nodes = await get_nodes_at_position(model, range.start)
nodes = get_nodes_at_position(model, range.start)

if not any(n for n in nodes if isinstance(n, (Keyword, TestCase))):
continue
Expand Down Expand Up @@ -413,16 +408,13 @@ async def code_action_create_local_variable(
async def resolve_code_action_create_local_variable(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
from robot.parsing.model.blocks import Keyword, TestCase
from robot.parsing.model.statements import Documentation, Fixture, Statement, Template

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:
return None

model = self.parent.documents_cache.get_model(document, False)
nodes = await get_nodes_at_position(model, data.range.start)
nodes = get_nodes_at_position(model, data.range.start)

if not any(n for n in nodes if isinstance(n, (Keyword, TestCase))):
return None
Expand Down Expand Up @@ -510,16 +502,13 @@ async def code_action_create_suite_variable(
async def resolve_code_action_create_suite_variable(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
from robot.parsing.model.blocks import VariableSection
from robot.parsing.model.statements import Variable

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:
return None

model = self.parent.documents_cache.get_model(document, False)
nodes = await get_nodes_at_position(model, data.range.start)
nodes = get_nodes_at_position(model, data.range.start)

node = nodes[-1] if nodes else None

Expand Down Expand Up @@ -603,8 +592,6 @@ async def resolve_code_action_create_suite_variable(
async def code_action_add_argument(
self, document: TextDocument, range: Range, context: CodeActionContext
) -> Optional[List[Union[Command, CodeAction]]]:
from robot.parsing.model.blocks import Keyword

result: List[Union[Command, CodeAction]] = []

if (context.only and CodeActionKind.QUICK_FIX in context.only) or context.trigger_kind in [
Expand All @@ -628,7 +615,7 @@ async def code_action_add_argument(
continue

model = self.parent.documents_cache.get_model(document, False)
nodes = await get_nodes_at_position(model, range.start)
nodes = get_nodes_at_position(model, range.start)

if not any(n for n in nodes if isinstance(n, Keyword)):
continue
Expand All @@ -654,10 +641,6 @@ async def code_action_add_argument(
async def resolve_code_action_add_argument(
self, code_action: CodeAction, data: CodeActionData
) -> Optional[CodeAction]:
from robot.parsing.lexer.tokens import Token
from robot.parsing.model.blocks import Keyword
from robot.parsing.model.statements import Arguments, Documentation, KeywordName, Statement

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 All @@ -668,7 +651,7 @@ async def resolve_code_action_add_argument(
return None

model = self.parent.documents_cache.get_model(document, False)
nodes = await get_nodes_at_position(model, data.range.start)
nodes = get_nodes_at_position(model, data.range.start)

keyword = next((n for n in nodes if isinstance(n, Keyword)), None)
if keyword is None:
Expand Down
Loading

0 comments on commit bc96805

Please sign in to comment.