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

Parse, validate, and highlight code blocks #574

Open
xsebek opened this issue Jul 20, 2022 · 9 comments
Open

Parse, validate, and highlight code blocks #574

xsebek opened this issue Jul 20, 2022 · 9 comments
Assignees
Labels
C-Project A larger project, more suitable for experienced contributors. L-Parsing Parsing the Swarm language from a string into an AST. L-Pretty-printing Pretty-printing ASTs or values into a string representation. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. T-Testing Involves the testing suite - unit and integration tests, also benchmarks. T-UI Involves the user interface. Z-Documentation This issue is about README or Wikis or Haddock or... Z-Feature A new feature to be added to the game. Z-User Experience This issue seeks to make the game more enjoyable to play.

Comments

@xsebek
Copy link
Member

xsebek commented Jul 20, 2022

As a quick improvement, we should be able to parse markdown code blocks and highlight those in the game UI.

If the first and last line of Text is ```, the lines in the middle should be (parsed as swarm code and) highlighted.

Before:

  - 'Example:'
  - '  move; turn left; move; turn right'

After:

  - 'Example:'
  - |
    ```
    move; turn left; move; turn right
    ```

As a bonus, we could easily check that the code parses in the CI. #309

@xsebek xsebek added Z-User Experience This issue seeks to make the game more enjoyable to play. Z-Feature A new feature to be added to the game. C-Moderate Effort Should take a moderate amount of time to address. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. L-Parsing Parsing the Swarm language from a string into an AST. L-Pretty-printing Pretty-printing ASTs or values into a string representation. T-UI Involves the user interface. T-Testing Involves the testing suite - unit and integration tests, also benchmarks. Z-Documentation This issue is about README or Wikis or Haddock or... labels Jul 20, 2022
@byorgey
Copy link
Member

byorgey commented Jul 21, 2022

As I mentioned in #545, I do not actually think highlighting code blocks will be easy, although I would be very happy to be proven wrong. But I agree we should do this and at least get #309 .

@xsebek
Copy link
Member Author

xsebek commented Jul 21, 2022

Well for start I would not semantically highlight the code - just make it bold or italic or something like that - simple attribute of the txt block. 🙂 And of course drop the ```.

Highlighting code semantically would require highlighting individual words like in #545, so that is harder even without the extra work of matching parsed elements to code spans and highlighting those.

@byorgey
Copy link
Member

byorgey commented Jul 21, 2022

Ah, that makes sense, yes, that should be possible.

@xsebek
Copy link
Member Author

xsebek commented Oct 9, 2022

Turns out there is a library for this:

In particular, this function to which we can pass tokenized lines:

renderRawSource
  :: (T.Text -> Widget n) -- just put txt function here
  -> [Sky.SourceLine]     -- [SourceLine] == [[Token]] == [[(TokenType, Text)]] 
  -> Widget n

So we can use our already processed terms and just label them.

@xsebek
Copy link
Member Author

xsebek commented Oct 19, 2022

Well, it also needs to work with our pretty printing. 🤔

The way to do that seems to be to:

  1. annotate the Doc.
  2. produce a SimpleDocStream (we can specify line length)
  3. turn that into a list (lines) of lists of text segments each with an assigned TokenType.

@xsebek xsebek added C-Project A larger project, more suitable for experienced contributors. and removed C-Moderate Effort Should take a moderate amount of time to address. labels Oct 21, 2022
@xsebek
Copy link
Member Author

xsebek commented Oct 21, 2022

Since I think the implementation is clear now, I will do semantic highlighting too.

Notes:

  1. start by splitting the descriptions into plaintext paragraphs and code blocks
  2. parse the code blocks
    • if it does not keep the original text so we can print it plainly (e.g. while developing)
  3. pretty print the code blocks (consider line length)
  4. set up an integration test to check that all code blocks are valid
  5. follow the steps in the comment above to highlight the output
  6. profit

xsebek added a commit that referenced this issue Feb 13, 2023
@byorgey byorgey changed the title Highlight code blocks Parse, validate, and highlight code blocks May 27, 2023
xsebek added a commit that referenced this issue Aug 2, 2023
- closes #309
- part of #545 and #574

Restyled by fourmolu (#1107)

Co-authored-by: Restyled.io <commits@restyled.io>
@xsebek
Copy link
Member Author

xsebek commented Aug 3, 2023

I looked into this and ended up quite confused. 😕 (See WIP in 98ee109)

Adding annotations is simple enough, but there are more steps to the Doc TokenType -> Widget Name conversion:

  • we already produce SimpleDocStream
  • but I have no idea how to easily turn it to [[(TokenType, Text)]]
  • for the brick library to highlight our code, we would need some Style definition, which attrMappingsForStyle would turn to brick attribute tree that we would add to our current one
    • given style needs to be loaded from XML with loadSyntaxFromFile
    • this seems like it should be configurable by users, but with default provided in our data

@xsebek
Copy link
Member Author

xsebek commented Aug 3, 2023

@byorgey if you have some idea how to approach this, I would be glad to hear it. 😅

The good news is that once we figure this out, we should get nicely coloured code widgets everywhere.
Also the Parse and Validate parts will be done as part of #1106.

The bad news is that highlighting is more work than I expected and I will not implement it in #1106. I will probably have to render inline code simply and blocks can get properly highlighted later. @byorgey so I will probably rename this issue back to "Highlight code blocks". 😅

@byorgey
Copy link
Member

byorgey commented Aug 4, 2023

The bad news is that highlighting is more work than I expected and I will not implement it in #1106. I will probably have to render inline code simply and blocks can get properly highlighted later. @byorgey so I will probably rename this issue back to "Highlight code blocks". 😅

Makes sense. I don't have any particular ideas off the top of my head. If you wanted to schedule a meeting sometime to think about it together I'd be happy to do that.

mergify bot pushed a commit that referenced this issue Aug 5, 2023
* use CommonMark to parse simple markdown AST parametrised on inline/block code
* validate swarm code (`Document Text -> Document Syntax`)
* update descriptions to use markdown with following conventions:
  - `move` - valid swarm code (the easy to write default)
  - `wedge`{=entity} - for swarm entities
  - `unit`{=type} - for swarm types
  - `require <a> <b>`{=snippet} - raw snippets for invalid code
  - **Alt-G** - bold for keyboard shortcuts
- highlight code in brick widgets
- closes #309
- closes #545
- precedes #574
- precedes #1406
- precedes #1407
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Project A larger project, more suitable for experienced contributors. L-Parsing Parsing the Swarm language from a string into an AST. L-Pretty-printing Pretty-printing ASTs or values into a string representation. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. T-Testing Involves the testing suite - unit and integration tests, also benchmarks. T-UI Involves the user interface. Z-Documentation This issue is about README or Wikis or Haddock or... Z-Feature A new feature to be added to the game. Z-User Experience This issue seeks to make the game more enjoyable to play.
Projects
None yet
Development

No branches or pull requests

2 participants