mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
simplify suggestions, exception name change
This commit is contained in:
@@ -5,6 +5,7 @@ from rich.traceback import Traceback
|
||||
|
||||
from ._help_renderables import HelpText
|
||||
from .tokenize import Token
|
||||
from .tokenizer import TokenError
|
||||
|
||||
|
||||
class DeclarationError(Exception):
|
||||
@@ -19,6 +20,10 @@ class StyleTypeError(TypeError):
|
||||
pass
|
||||
|
||||
|
||||
class UnresolvedVariableError(TokenError):
|
||||
pass
|
||||
|
||||
|
||||
class StyleValueError(ValueError):
|
||||
"""Raised when the value of a style property is not valid
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ from typing import Iterator, Iterable, NoReturn, Sequence
|
||||
|
||||
from rich import print
|
||||
|
||||
from textual.css.tokenizer import TokenError
|
||||
from textual.css.types import Specificity3
|
||||
from .errors import UnresolvedVariableError
|
||||
from .types import Specificity3
|
||||
from ._styles_builder import StylesBuilder, DeclarationError
|
||||
from .model import (
|
||||
Declaration,
|
||||
@@ -210,24 +210,24 @@ def parse_declarations(css: str, path: str) -> Styles:
|
||||
return styles_builder.styles
|
||||
|
||||
|
||||
def _unresolved(variable_name: str, variables: Sequence[str], token: Token) -> NoReturn:
|
||||
def _unresolved(variable_name: str, variables: Iterable[str], token: Token) -> NoReturn:
|
||||
"""Raise a TokenError regarding an unresolved variable.
|
||||
|
||||
Args:
|
||||
variable_name (str): A variable name.
|
||||
variables (Sequence[str]): Possible choices used to generate suggestion.
|
||||
variables (Iterable[str]): Possible choices used to generate suggestion.
|
||||
token (Token): The Token.
|
||||
|
||||
Raises:
|
||||
TokenError: Always raises a TokenError.
|
||||
UnresolvedVariableError: Always raises a TokenError.
|
||||
|
||||
"""
|
||||
message = f"reference to undefined variable '${variable_name}'"
|
||||
suggested_variable = get_suggestion(variable_name, variables)
|
||||
suggested_variable = get_suggestion(variable_name, list(variables))
|
||||
if suggested_variable:
|
||||
message += f"; did you mean '{suggested_variable}'?"
|
||||
message += f"; did you mean '${suggested_variable}'?"
|
||||
|
||||
raise TokenError(
|
||||
raise UnresolvedVariableError(
|
||||
token.path,
|
||||
token.code,
|
||||
token.start,
|
||||
|
||||
@@ -76,11 +76,18 @@ class TokenError(Exception):
|
||||
|
||||
errors.append(highlighter(f" {self.path or '<unknown>'}:{line_no}:{col_no}"))
|
||||
errors.append(self._get_snippet())
|
||||
final_message = ""
|
||||
for is_last, message_part in loop_last(message.split(";")):
|
||||
end = "" if is_last else "\n"
|
||||
final_message += f"• {message_part.strip()};{end}"
|
||||
errors.append(Padding(highlighter(Text(final_message, "red")), pad=(0, 1)))
|
||||
|
||||
final_message = "\n".join(
|
||||
f"• {message_part.strip()}" for message_part in message.split(";")
|
||||
)
|
||||
errors.append(
|
||||
Padding(
|
||||
highlighter(
|
||||
Text(final_message, "red"),
|
||||
),
|
||||
pad=(0, 1),
|
||||
)
|
||||
)
|
||||
|
||||
return Group(*errors)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user