Skip to content

Commit

Permalink
refactor(langserver): move local import to global imports in document…
Browse files Browse the repository at this point in the history
…_symbols
  • Loading branch information
d-biehl committed Dec 31, 2023
1 parent 755daf7 commit 135b0d4
Show file tree
Hide file tree
Showing 153 changed files with 862 additions and 671 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import ast
import itertools
from typing import TYPE_CHECKING, Any, List, Optional, Union, cast
from typing import TYPE_CHECKING, Any, List, Optional, Union

from robot.errors import VariableError
from robot.parsing.lexer.tokens import Token
from robot.parsing.model.blocks import Keyword, Section, TestCase
from robot.parsing.model.statements import Statement
from robot.variables import search_variable
from robotcode.core.lsp.types import DocumentSymbol, SymbolInformation, SymbolKind
from robotcode.core.utils.logging import LoggingDescriptor
from robotcode.robot.utils.ast import range_from_node, range_from_token, tokenize_variables
Expand All @@ -29,7 +33,7 @@ def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
def collect(
self, sender: Any, document: TextDocument
) -> Optional[Union[List[DocumentSymbol], List[SymbolInformation], None]]:
return _Visitor.find_from(self.parent.documents_cache.get_model(document), self)
return _Visitor.find_from(self.parent.documents_cache.get_model(document, False), self)


class _Visitor(Visitor):
Expand All @@ -56,22 +60,14 @@ def find_from(cls, model: ast.AST, parent: RobotDocumentSymbolsProtocolPart) ->

return finder.result if finder.result else None

def visit_Section(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.model.blocks import Section
from robot.parsing.model.statements import SectionHeader

section = cast(Section, node)
if section.header is None:
return

header = cast(SectionHeader, section.header)
if not header.name:
def visit_Section(self, node: Section) -> None: # noqa: N802
if not node.header or not node.header.name:
return

r = range_from_node(section)
r = range_from_node(node)
symbol = DocumentSymbol(
name=header.name.replace("*", "").strip(),
kind=SymbolKind.NAMESPACE,
name=node.header.name,
kind=SymbolKind.MODULE,
range=r,
selection_range=r,
children=[],
Expand All @@ -81,45 +77,33 @@ def visit_Section(self, node: ast.AST) -> None: # noqa: N802

self.generic_visit_current_symbol(node, symbol)

def visit_TestCase(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.model.blocks import TestCase

testcase = cast(TestCase, node)
if testcase.name is None:
def visit_TestCase(self, node: TestCase) -> None: # noqa: N802
if node.name is None:
return

if self.current_symbol is not None and self.current_symbol.children is not None:
r = range_from_node(testcase)
symbol = DocumentSymbol(name=testcase.name, kind=SymbolKind.METHOD, range=r, selection_range=r, children=[])
r = range_from_node(node)
symbol = DocumentSymbol(name=node.name, kind=SymbolKind.METHOD, range=r, selection_range=r, children=[])
self.current_symbol.children.append(symbol)

self.generic_visit_current_symbol(node, symbol)

def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.model.blocks import Keyword

keyword = cast(Keyword, node)
if keyword.name is None:
def visit_Keyword(self, node: Keyword) -> None: # noqa: N802
if node.name is None:
return

if self.current_symbol is not None and self.current_symbol.children is not None:
r = range_from_node(keyword)
symbol = DocumentSymbol(
name=keyword.name, kind=SymbolKind.FUNCTION, range=r, selection_range=r, children=[]
)
r = range_from_node(node)
symbol = DocumentSymbol(name=node.name, kind=SymbolKind.FUNCTION, range=r, selection_range=r, children=[])
self.current_symbol.children.append(symbol)

self.generic_visit_current_symbol(node, symbol)

def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.lexer.tokens import Token as RobotToken
from robot.parsing.model.statements import Arguments

n = cast(Arguments, node)
arguments = n.get_tokens(RobotToken.ARGUMENT)
def visit_Arguments(self, node: Statement) -> None: # noqa: N802
arguments = node.get_tokens(Token.ARGUMENT)

if self.current_symbol is not None and self.current_symbol.children is not None:
for argument_token in (cast(RobotToken, e) for e in arguments):
for argument_token in arguments:
if argument_token.value == "@{}":
continue

Expand All @@ -133,29 +117,22 @@ def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
self.current_symbol.children.append(symbol)

def get_variable_token(self, token: Token) -> Optional[Token]:
from robot.parsing.lexer.tokens import Token as RobotToken

return next(
(
v
for v in itertools.dropwhile(
lambda t: t.type in RobotToken.NON_DATA_TOKENS,
lambda t: t.type in Token.NON_DATA_TOKENS,
tokenize_variables(token, ignore_errors=True),
)
if v.type == RobotToken.VARIABLE
if v.type == Token.VARIABLE
),
None,
)

def visit_KeywordCall(self, node: ast.AST) -> None: # noqa: N802
from robot.errors import VariableError
from robot.parsing.lexer.tokens import Token as RobotToken
from robot.parsing.model.statements import KeywordCall

def visit_KeywordCall(self, node: Statement) -> None: # noqa: N802
# TODO analyse "Set Local/Global/Suite Variable"

keyword_call = cast(KeywordCall, node)
for assign_token in keyword_call.get_tokens(RobotToken.ASSIGN):
for assign_token in node.get_tokens(Token.ASSIGN):
if assign_token is None:
continue

Expand All @@ -175,12 +152,8 @@ def visit_KeywordCall(self, node: ast.AST) -> None: # noqa: N802
except VariableError:
pass

def visit_ForHeader(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.lexer.tokens import Token as RobotToken
from robot.parsing.model.statements import ForHeader

n = cast(ForHeader, node)
variables = n.get_tokens(RobotToken.VARIABLE)
def visit_ForHeader(self, node: Statement) -> None: # noqa: N802
variables = node.get_tokens(Token.VARIABLE)

if self.current_symbol is not None and self.current_symbol.children is not None:
for variable in variables:
Expand All @@ -193,12 +166,8 @@ def visit_ForHeader(self, node: ast.AST) -> None: # noqa: N802
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
self.current_symbol.children.append(symbol)

def visit_ExceptHeader(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.lexer.tokens import Token as RobotToken
from robot.parsing.model.statements import ExceptHeader

n = cast(ExceptHeader, node)
variables = n.get_tokens(RobotToken.VARIABLE)
def visit_ExceptHeader(self, node: Statement) -> None: # noqa: N802
variables = node.get_tokens(Token.VARIABLE)

if self.current_symbol is not None and self.current_symbol.children is not None:
for variable in variables:
Expand All @@ -211,12 +180,8 @@ def visit_ExceptHeader(self, node: ast.AST) -> None: # noqa: N802
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
self.current_symbol.children.append(symbol)

def visit_Var(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.lexer.tokens import Token as RobotToken
from robot.parsing.model.statements import Var

n = cast(Var, node)
variables = n.get_tokens(RobotToken.VARIABLE)
def visit_Var(self, node: Statement) -> None: # noqa: N802
variables = node.get_tokens(Token.VARIABLE)

if self.current_symbol is not None and self.current_symbol.children is not None:
for variable in variables:
Expand All @@ -229,20 +194,14 @@ def visit_Var(self, node: ast.AST) -> None: # noqa: N802
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
self.current_symbol.children.append(symbol)

def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
from robot.parsing.lexer.tokens import Token as RobotToken
from robot.parsing.model.statements import KeywordName

n = cast(KeywordName, node)
nt = n.get_token(RobotToken.KEYWORD_NAME)
if nt is None:
def visit_KeywordName(self, node: Statement) -> None: # noqa: N802
name_token = node.get_token(Token.KEYWORD_NAME)
if name_token is None:
return

name_token = cast(Token, nt)

if self.current_symbol is not None and self.current_symbol.children is not None:
for variable in filter(
lambda e: e.type == RobotToken.VARIABLE,
lambda e: e.type == Token.VARIABLE,
tokenize_variables(name_token, identifiers="$", ignore_errors=True),
):
variable_token = self.get_variable_token(variable)
Expand All @@ -254,14 +213,8 @@ def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
self.current_symbol.children.append(symbol)

def visit_Variable(self, node: ast.AST) -> None: # noqa: N802
from robot.api.parsing import Token as RobotToken
from robot.parsing.model.statements import Variable
from robot.variables import search_variable

variable = cast(Variable, node)

name_token = variable.get_token(RobotToken.VARIABLE)
def visit_Variable(self, node: Statement) -> None: # noqa: N802
name_token = node.get_token(Token.VARIABLE)
name = name_token.value

if name is not None:
Expand All @@ -273,6 +226,6 @@ def visit_Variable(self, node: ast.AST) -> None: # noqa: N802
name = name[:-1].rstrip()

if self.current_symbol is not None and self.current_symbol.children is not None:
r = range_from_node(variable)
r = range_from_node(node)
symbol = DocumentSymbol(name=name, kind=SymbolKind.VARIABLE, range=r, selection_range=r)
self.current_symbol.children.append(symbol)
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ result: !DocumentSymbol
children: []
deprecated: null
detail: null
kind: 3
kind: 2
name: Settings
range:
end:
character: 22
line: 2
character: 1
line: 3
start:
character: 0
line: 0
selection_range:
end:
character: 22
line: 2
character: 1
line: 3
start:
character: 0
line: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ result: !DocumentSymbol
children: []
deprecated: null
detail: null
kind: 3
kind: 2
name: Settings
range:
end:
character: 22
line: 2
character: 1
line: 3
start:
character: 0
line: 0
selection_range:
end:
character: 22
line: 2
character: 1
line: 3
start:
character: 0
line: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ result: !DocumentSymbol
children: []
deprecated: null
detail: null
kind: 3
kind: 2
name: Settings
range:
end:
character: 22
line: 2
character: 1
line: 3
start:
character: 0
line: 0
selection_range:
end:
character: 22
line: 2
character: 1
line: 3
start:
character: 0
line: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ result: !DocumentSymbol
children: []
deprecated: null
detail: null
kind: 3
kind: 2
name: Variables
range:
end:
character: 13
line: 6
character: 1
line: 8
start:
character: 0
line: 4
selection_range:
end:
character: 13
line: 6
character: 1
line: 8
start:
character: 0
line: 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ result: !DocumentSymbol
children: []
deprecated: null
detail: null
kind: 3
kind: 2
name: Variables
range:
end:
character: 13
line: 6
character: 1
line: 8
start:
character: 0
line: 4
selection_range:
end:
character: 13
line: 6
character: 1
line: 8
start:
character: 0
line: 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ result: !DocumentSymbol
children: []
deprecated: null
detail: null
kind: 3
kind: 2
name: Variables
range:
end:
character: 13
line: 6
character: 1
line: 8
start:
character: 0
line: 4
selection_range:
end:
character: 13
line: 6
character: 1
line: 8
start:
character: 0
line: 4
Expand Down
Loading

0 comments on commit 135b0d4

Please sign in to comment.