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 ._help_renderables import HelpText
|
||||||
from .tokenize import Token
|
from .tokenize import Token
|
||||||
|
from .tokenizer import TokenError
|
||||||
|
|
||||||
|
|
||||||
class DeclarationError(Exception):
|
class DeclarationError(Exception):
|
||||||
@@ -19,6 +20,10 @@ class StyleTypeError(TypeError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UnresolvedVariableError(TokenError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class StyleValueError(ValueError):
|
class StyleValueError(ValueError):
|
||||||
"""Raised when the value of a style property is not valid
|
"""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 rich import print
|
||||||
|
|
||||||
from textual.css.tokenizer import TokenError
|
from .errors import UnresolvedVariableError
|
||||||
from textual.css.types import Specificity3
|
from .types import Specificity3
|
||||||
from ._styles_builder import StylesBuilder, DeclarationError
|
from ._styles_builder import StylesBuilder, DeclarationError
|
||||||
from .model import (
|
from .model import (
|
||||||
Declaration,
|
Declaration,
|
||||||
@@ -210,24 +210,24 @@ def parse_declarations(css: str, path: str) -> Styles:
|
|||||||
return styles_builder.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.
|
"""Raise a TokenError regarding an unresolved variable.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
variable_name (str): A variable name.
|
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.
|
token (Token): The Token.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
TokenError: Always raises a TokenError.
|
UnresolvedVariableError: Always raises a TokenError.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
message = f"reference to undefined variable '${variable_name}'"
|
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:
|
if suggested_variable:
|
||||||
message += f"; did you mean '{suggested_variable}'?"
|
message += f"; did you mean '${suggested_variable}'?"
|
||||||
|
|
||||||
raise TokenError(
|
raise UnresolvedVariableError(
|
||||||
token.path,
|
token.path,
|
||||||
token.code,
|
token.code,
|
||||||
token.start,
|
token.start,
|
||||||
|
|||||||
@@ -76,11 +76,18 @@ class TokenError(Exception):
|
|||||||
|
|
||||||
errors.append(highlighter(f" {self.path or '<unknown>'}:{line_no}:{col_no}"))
|
errors.append(highlighter(f" {self.path or '<unknown>'}:{line_no}:{col_no}"))
|
||||||
errors.append(self._get_snippet())
|
errors.append(self._get_snippet())
|
||||||
final_message = ""
|
|
||||||
for is_last, message_part in loop_last(message.split(";")):
|
final_message = "\n".join(
|
||||||
end = "" if is_last else "\n"
|
f"• {message_part.strip()}" for message_part in message.split(";")
|
||||||
final_message += f"• {message_part.strip()};{end}"
|
)
|
||||||
errors.append(Padding(highlighter(Text(final_message, "red")), pad=(0, 1)))
|
errors.append(
|
||||||
|
Padding(
|
||||||
|
highlighter(
|
||||||
|
Text(final_message, "red"),
|
||||||
|
),
|
||||||
|
pad=(0, 1),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return Group(*errors)
|
return Group(*errors)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user