Skip to content

Commit

Permalink
Merge pull request #446 from Textualize/fix-parse-error
Browse files Browse the repository at this point in the history
fix broken align and error logic
  • Loading branch information
willmcgugan authored Apr 29, 2022
2 parents 884bd6d + 191a6b7 commit 280fd2e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion sandbox/uber.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def on_mount(self):
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"),
Expand Down
1 change: 1 addition & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ async def process_messages(self) -> None:
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__}>"
Expand Down
3 changes: 2 additions & 1 deletion src/textual/css/_help_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
from dataclasses import dataclass
from typing import Iterable

from textual.css._help_renderables import Example, Bullet, HelpText
from textual.css.constants import (
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/textual/css/_styles_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def align_error(name, token):

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("-", "_")
Expand All @@ -693,24 +693,26 @@ def align_error(name, token):
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
Expand Down
18 changes: 8 additions & 10 deletions src/textual/css/stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
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
Expand All @@ -39,9 +40,9 @@ def __rich__(self) -> RenderableType:

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:
Expand Down Expand Up @@ -69,7 +70,7 @@ def __rich_console__(
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

Expand Down Expand Up @@ -140,10 +141,6 @@ def rules(self) -> list[RuleSet]:
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.
Expand Down Expand Up @@ -226,7 +223,8 @@ def parse(self) -> None:
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
Expand All @@ -244,7 +242,7 @@ def reparse(self) -> None:
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
Expand Down
6 changes: 3 additions & 3 deletions src/textual/scrollbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(
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
Expand All @@ -89,7 +89,7 @@ def render_bar(
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:
Expand Down Expand Up @@ -181,7 +181,7 @@ def __rich_console__(
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

Expand Down

0 comments on commit 280fd2e

Please sign in to comment.