Merge pull request #446 from Textualize/fix-parse-error

fix broken align and error logic
This commit is contained in:
Will McGugan
2022-04-29 13:43:50 +01:00
committed by GitHub
6 changed files with 20 additions and 18 deletions

View File

@@ -28,7 +28,7 @@ class BasicApp(App):
Widget(id="uber2-child1"),
Widget(id="uber2-child2"),
)
first_child = Placeholder(id="child1", classes={"list-item"})
first_child = Placeholder(id="child1", classes="list-item")
uber1 = Widget(
Placeholder(id="child1", classes="list-item"),
Placeholder(id="child2", classes="list-item"),

View File

@@ -504,6 +504,7 @@ class App(Generic[ReturnType], DOMNode):
try:
if self.css_file is not None:
self.stylesheet.read(self.css_file)
self.stylesheet.parse()
if self.css is not None:
self.stylesheet.add_source(
self.css, path=f"<{self.__class__.__name__}>"

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
import sys
from dataclasses import dataclass
from typing import Iterable
from textual.css._help_renderables import Example, Bullet, HelpText
from textual.css.constants import (
@@ -229,7 +230,7 @@ def scalar_help_text(
def string_enum_help_text(
property_name: str,
valid_values: list[str],
valid_values: Iterable[str],
context: StylingContext,
) -> HelpText:
"""Help text to show when the user supplies an invalid value for a string

View File

@@ -683,7 +683,7 @@ class StylesBuilder:
if token_vertical.name != "token":
align_error(name, token_vertical)
elif token_horizontal.value not in VALID_ALIGN_VERTICAL:
elif token_vertical.value not in VALID_ALIGN_VERTICAL:
align_error(name, token_horizontal)
name = name.replace("-", "_")
@@ -693,24 +693,26 @@ class StylesBuilder:
def process_align_horizontal(self, name: str, tokens: list[Token]) -> None:
try:
value = self._process_enum(name, tokens, VALID_ALIGN_HORIZONTAL)
self.styles._rules[name.replace("-", "_")] = value
except StyleValueError:
self.error(
name,
tokens[0],
string_enum_help_text(name, VALID_ALIGN_HORIZONTAL, context="css"),
)
else:
self.styles._rules[name.replace("-", "_")] = value
def process_align_vertical(self, name: str, tokens: list[Token]) -> None:
try:
value = self._process_enum(name, tokens, VALID_ALIGN_VERTICAL)
self.styles._rules[name.replace("-", "_")] = value
except StyleValueError:
self.error(
name,
tokens[0],
string_enum_help_text(name, VALID_ALIGN_VERTICAL, context="css"),
)
else:
self.styles._rules[name.replace("-", "_")] = value
process_content_align = process_align
process_content_align_horizontal = process_align_horizontal

View File

@@ -17,7 +17,8 @@ from rich.style import Style
from rich.syntax import Syntax
from rich.text import Text
from textual._loop import loop_last
from .._loop import loop_last
from .. import log
from .errors import StylesheetError
from .match import _check_selectors
from .model import RuleSet
@@ -39,9 +40,9 @@ class StylesheetParseError(StylesheetError):
class StylesheetErrors:
def __init__(
self, stylesheet: "Stylesheet", variables: dict[str, str] | None = None
self, rules: list[RuleSet], variables: dict[str, str] | None = None
) -> None:
self.stylesheet = stylesheet
self.rules = rules
self.variables: dict[str, str] = {}
self._css_variables: dict[str, list[Token]] = {}
if variables:
@@ -69,7 +70,7 @@ class StylesheetErrors:
self, console: Console, options: ConsoleOptions
) -> RenderableType:
error_count = 0
for rule in self.stylesheet.rules:
for rule in self.rules:
for is_last, (token, message) in loop_last(rule.errors):
error_count += 1
@@ -140,10 +141,6 @@ class Stylesheet:
def css(self) -> str:
return "\n\n".join(rule_set.css for rule_set in self.rules)
@property
def error_renderable(self) -> StylesheetErrors:
return StylesheetErrors(self)
def set_variables(self, variables: dict[str, str]) -> None:
"""Set CSS variables.
@@ -226,7 +223,8 @@ class Stylesheet:
for path, css in self.source.items():
css_rules = self._parse_rules(css, path)
if any(rule.errors for rule in css_rules):
raise StylesheetParseError(self.error_renderable)
error_renderable = StylesheetErrors(css_rules)
raise StylesheetParseError(error_renderable)
add_rules(css_rules)
self._rules = rules
self._require_parse = False
@@ -244,7 +242,7 @@ class Stylesheet:
for path, css in self.source.items():
stylesheet.add_source(css, path)
stylesheet.parse()
self.rules = stylesheet.rules
self._rules = stylesheet.rules
self.source = stylesheet.source
@classmethod

View File

@@ -69,7 +69,7 @@ class ScrollBarRender:
position: float = 0,
thickness: int = 1,
vertical: bool = True,
style: StyleType = "ansi_bright_magenta on #555555",
style: StyleType = "bright_magenta on #555555",
) -> None:
self.virtual_size = virtual_size
self.window_size = window_size
@@ -89,7 +89,7 @@ class ScrollBarRender:
thickness: int = 1,
vertical: bool = True,
back_color: Color = Color.parse("#555555"),
bar_color: Color = Color.parse("ansi_bright_magenta"),
bar_color: Color = Color.parse("bright_magenta"),
) -> Segments:
if vertical:
@@ -181,7 +181,7 @@ class ScrollBarRender:
vertical=self.vertical,
thickness=thickness,
back_color=_style.bgcolor or Color.parse("#555555"),
bar_color=_style.color or Color.parse("ansi_bright_magenta"),
bar_color=_style.color or Color.parse("bright_magenta"),
)
yield bar