Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve css error reporting #3582

Merged
merged 9 commits into from
Nov 1, 2023
Merged

Conversation

rodrigogiraoserrao
Copy link
Contributor

This PR fixes #3569.

This also improves error importing by providing the widget name/class variable from where CSS was read in case the CSS is inline CSS.
This is still a draft because I still need to figure out if we can link to a specific line/column for when the CSS comes from an external file.

We already kept track of the file and widget CSS was read from. Now, we also keep track of the class variable it comes from and we create some structure to transfer that information across the program.
Instead of creating the link explicitly, we let terminal emulators auto-link to the file.
This came after a discussion about how/whether we should try to support linking to specific file lines/columns for TCSS files and after some research to see how that would be possible.
We decided to drop this feature when we couldn't find information in the standards for 'file://' regarding how to specify line/column numbers and after we found [this iTerm issue](https://gitlab.com/gnachman/iterm2/-/issues/9376) where the creator/maintainer of iTerm says that there is no standard API for opening a file to a particular line number.
@rodrigogiraoserrao rodrigogiraoserrao marked this pull request as ready for review October 25, 2023 12:14
@rodrigogiraoserrao rodrigogiraoserrao self-assigned this Oct 25, 2023
@rodrigogiraoserrao rodrigogiraoserrao added bug Something isn't working enhancement New feature or request labels Oct 25, 2023
Comment on lines -68 to -80
def _join_tokens(tokens: Iterable[Token], joiner: str = "") -> str:
"""Convert tokens into a string by joining their values

Args:
tokens: Tokens to join
joiner: String to join on.

Returns:
The tokens, joined together to form a string.
"""
return joiner.join(token.value for token in tokens)


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was dead code.

Comment on lines -440 to +448
css: str = base.__dict__.get("DEFAULT_CSS", "").strip()
css: str = base.__dict__.get("DEFAULT_CSS", "")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This messed up the line/column reporting of inline CSS errors.

Copy link
Collaborator

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Couple of requests.

Can you add a screenshot of how these changes look in CSS errors?

@@ -1753,20 +1753,19 @@ def _load_screen_css(self, screen: Screen):

update = False
for path in screen.css_path:
if not self.stylesheet.has_source(path):
if not self.stylesheet.has_source((str(path), "")):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to veto the tuple here. It doesn't read well. Could the second value be a keyword arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that sounds good. See 6c6eecf.

if token.path:
path = Path(token.path)
filename = path.name
if token.read_from:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be an explicit is not None check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked this a bit better so that the checks make more sense.

@davep
Copy link
Contributor

davep commented Oct 31, 2023

Giving this spin with this code:

from textual.app import App, ComposeResult
from textual.widgets import Label

class CSSErrorApp(App[None]):

    CSS = """
    :
    """

    def compose(self) -> ComposeResult:
        yield Label()

if __name__ == "__main__":
    CSSErrorApp().run()

I get this error:

 Error in stylesheet:
 /Users/davep/develop/python/textual-upstream/sandbox/foo.py, CSSErrorApp.CSS:1:4
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│   1 │                                                                                                                                                                                                                      │
│ ❱ 2 │   :                                                                                                                                                                                                                  │
│   3                                                                                                                                                                                                                        │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
 • Expected one of 'comment line', 'comment start', 'selector start', 'selector start class', 'selector start id', 'selector start universal', 'variable name', or 'whitespace'.                                              
 • Did you forget a semicolon at the end of a line?        

This seems to be saying that the error is on line 1, but also on line 2. Am I reading that correctly?

@rodrigogiraoserrao
Copy link
Contributor Author

rodrigogiraoserrao commented Nov 1, 2023

This seems to be saying that the error is on line 1, but also on line 2. Am I reading that correctly?

You're reading that correctly, yes, but that also seems to be an issue in main.
Opened #3625 to keep track of that.

I'd be keen on getting this PR in despite of that other bug, unless you feel strongly that I should solve #3625 as a part of this PR.

@davep
Copy link
Contributor

davep commented Nov 1, 2023

Cool, if it's a pre-existing thing unrelated this makes sense it's handled in a different fix.

@rodrigogiraoserrao rodrigogiraoserrao merged commit ad83e91 into main Nov 1, 2023
23 checks passed
@rodrigogiraoserrao rodrigogiraoserrao deleted the improve-css-error-reporting branch November 1, 2023 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSS errors have a broken path
3 participants