Skip to content

Added SwiftUITextOutputFormat to output a Text struct#92

Open
andreweades wants to merge 4 commits into
JohnSundell:masterfrom
andreweades:master
Open

Added SwiftUITextOutputFormat to output a Text struct#92
andreweades wants to merge 4 commits into
JohnSundell:masterfrom
andreweades:master

Conversation

@andreweades

Copy link
Copy Markdown

This OutputFormat outputs a Text struct that can be used in a SwiftUI view.
It ignores the font as you normally would apply the font with a .font modifier in SwiftUI.
Either we should convert the theme's font to a SwiftUI.Font and and use that or leave it to the user to style fonts separately from highlighting syntax colours. I favour the later as it fits better with the model of SwiftUI and leaves SwiftUITextOutputFormat to be only concerned with constructing the highlighted text but this might be confusing as it ignores the theme's font. However, the attributed text string is different to the font and they could be considered separate and treated separately as SwiftUi pushes us in that direction and an additional .font modifier would override any theme font anyway. Maybe this should really be a view modifier?

@andreweades

andreweades commented Jan 3, 2020

Copy link
Copy Markdown
Author

Apologies. I had some CI build issues as I was rushing like an idiot and just uploaded the file thinking it would work. It obviously builds on my Catalina set up for SwiftUI development. I'm getting a 404 so I can't tell what the other fail is.

@JohnSundell

Copy link
Copy Markdown
Owner

Hey @andreweades, thanks for contributing to Splash. I'll try to review this as soon as possible. Just to help you with the CI issues before then, rather than wrapping the entire implementation in conditional compilation, I suggest using the @available attribute on the new format. That should give you a cleaner implementation as well. If you want to verify your changes locally, just build Splash for the iOS simulator for an iOS version lower than 12, and you should see the same errors as on macOS Mojave.

@andreweades

andreweades commented Jan 3, 2020 via email

Copy link
Copy Markdown
Author

@JohnSundell

Copy link
Copy Markdown
Owner

Would love for @duemunk to take a look at this, since I know that he's been working on something similar 🙂 Will also need this to be covered with tests before it can be merged in, just a heads-up. But I'll try to review this within the next few days.

@duemunk

duemunk commented Jan 3, 2020

Copy link
Copy Markdown
Contributor

Looks really great!! Much simpler than my implementation. Particularly this is super clever:

texts.reduce(Text(""), +)

Great when SwiftUI can surprise in positive ways like that.

@duemunk duemunk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The implementation doesn't support the theme font.

@andreweades

andreweades commented Jan 3, 2020

Copy link
Copy Markdown
Author

The implementation doesn't support the theme font.

That's right. I had some questions regarding font support as the SwiftUI way would be to use the .font view modifier. We could internalise this in the SwiftUITextOutputFormat or leave it to the user. I noted in my PR that this would be confusing so needs more thought.

Options:

  1. internalise font support and maintain the API
  2. separate colour theme from font selection as SwiftUI would encourage

But then I thought maybe it should just be a view modifier that can be applied to any Text and you would get something like the following:

Text(stringOfCode).syntaxHighlight(.presentation).font(.system(size: 12, design: .monospaced))

which leaves the font selection to the existing modifier.

@andreweades

Copy link
Copy Markdown
Author

After thinking about this a bit more... Maybe do 1 and make a syntaxHighlight view modifier that highlights and applies a font modifier.

@andreweades

Copy link
Copy Markdown
Author

My use case is demoed here.

I highlight the main text and the minimap using the same theme but with a different font applied to each.

@duemunk

duemunk commented Jan 12, 2020

Copy link
Copy Markdown
Contributor

I think using the theme's font is a good approach. The extensions are a nice to have in my opinion.

@andreweades

Copy link
Copy Markdown
Author

I will look into this again when I have some spare time. I will have to schedule some Open Source hours.

@Jeehut

Jeehut commented May 5, 2022

Copy link
Copy Markdown

@andreweades Any plans to get this to completion?

@andreweades

Copy link
Copy Markdown
Author

@andreweades Any plans to get this to completion?

I think SwiftUI support for AttributedString supplants this.

@Jeehut

Jeehut commented May 5, 2022

Copy link
Copy Markdown

@andreweades
But how to change the design of the font? I'd like to use .system(design: .monospaced), but this didn't work:

Text(
  AttributedString(
    SyntaxHighlighter(format: AttributedStringOutputFormat(theme: .sundellsColors(withFont: .init(size: 13))))
      .highlight(viewStore.codePreview)
    )
)
.font(.system(size: 13, design: .monospaced)) // <-- `.monospaced` gets ignored

Or is there a simple way to provide a SwiftUI.Font to the withFont which expects a Splash.Font?

@andreweades

andreweades commented May 5, 2022 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants