-
Notifications
You must be signed in to change notification settings - Fork 153
Add experimental code block options: highlight, strikeout, wrap, showLineNumbers #1287
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
base: main
Are you sure you want to change the base?
Conversation
…rd and other code block annotations
… on language line, tests
e2c790d
to
fbe197e
Compare
@swift-ci please test |
@swift-ci please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a few places of either API breaking changes or API that's designed in a way where it's likely to break in the future. That needs to be addressed.
Also, I have serious concerns about the way that the new code block attributes is encoded in the Render Node specification. We don't have a process for handling breaking changes to that specification today, so that format needs to be rethought to ensure that it's flexible and extensible for the type of information we want to encode there.
public var showLineNumbers: Bool = false | ||
public var strikeout: [Int] = [Int]() | ||
|
||
public enum OptionName: String, CaseIterable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that this looks like an enum that will get more cases over time, it shouldn't be source breaking to add new cases.
One way to do that would be to add a case like _nonFrozenEnum_useDefaultCase
(for example like this) to point out to the caller that they can't exhaustively switch over the cases of this enum.
Another way to do that would be to change this to a structure that's RawRepresentable
of a String
and add static
properties for each of the known cases. If the initializer is internal, then callers will only be able to access the known values.
public var copyToClipboard: Bool = true | ||
public var wrap: Int = 100 | ||
public var highlight: [Int] = [Int]() | ||
public var showLineNumbers: Bool = false | ||
public var strikeout: [Int] = [Int]() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is starting to look like quite bit of configuration. Perhaps it makes sense to organize it into a single structure to separatethe annotations from the code block itself.
@@ -124,12 +124,39 @@ public enum RenderBlockContent: Equatable { | |||
public var code: [String] | |||
/// Additional metadata for this code block. | |||
public var metadata: RenderContentMetadata? | |||
public var copyToClipboard: Bool = true | |||
public var wrap: Int = 100 | |||
public var highlight: [Int] = [Int]() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be quite limiting going forward. If we require in public API that the highlight is a list of integers then we can't represent partially highlighted lines without either:
- API breaking changes
- having to different arrays for different types of highlights.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, with this API design, each time we need to add a new type of highlight we need to add a new public property. It would be more flexible if this was represented differently (for example making the style
a property of the "highlight").
|
||
/// Make a new `CodeListing` with the given data. | ||
public init(syntax: String?, code: [String], metadata: RenderContentMetadata?) { | ||
public init(syntax: String?, code: [String], metadata: RenderContentMetadata?, copyToClipboard: Bool, wrap: Int, highlight: [Int], strikeout: [Int], showLineNumbers: Bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like in the previous PR, this is a source breaking change because callers of the previous initializer will now have an error that they're missing parameters.
Considering that each new code block feature is going to encounter the same issue, I feel like we need to approach this differently. One example of how to do that could be to group all the new properties in a structure and pass it to the initializer. This moves the problem of API breaking changes to the structure. The structure in turn can avoid API breaking changes by only having a public initializer that takes no arguments—resulting in the default values for everything. This is how the context handles its configuration. That design then changes the caller to have to create a structure with the default values and one-by-one assign each property before passing the structure to the initializer.
"highlight": { | ||
"type": "array", | ||
"items": { | ||
"type": "integer" | ||
} | ||
}, | ||
"strikeout": { | ||
"type": "array", | ||
"items": { | ||
"type": "integer" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the API design, I have serious concerns about this scaling to either new styles of highlight style or partial highlight ranges.
Unlike the Swift API, we have no process at all for breaking changes to the Render Node specification so we need to really think this through so that the JSON format is flexible and extensible for the type of information we want to encode here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this is rather technical, it might be good to discuss this in the forums to get greater visibility and to get inputs from a wider audience.
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
/// A function that parses array values on code block options from the language line string | ||
public func parseCodeBlockOptionArray(_ value: String?) -> [Int]? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that callers have any reasons to use this, so it shouldn't be public API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general free functions can have a tendency to pollute the API because they're always available in every scope.
The Swift API Design Guidelines calls out "Prefer methods and properties to free functions." in its general conventions.
In this case, it might make sense to check what code needs to call these functions and move them closer to the caller, ideally making them private
if there's only a single caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this method doesn't look like its ever returning nil
so it doesn't need an optional return type.
} | ||
|
||
/// A function that parses the language line options on code blocks, returning the language and tokens, an array of OptionName and option values | ||
public func tokenizeLanguageString(_ input: String?) -> (lang: String?, tokens: [(RenderBlockContent.CodeListing.OptionName, String?)]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like above, I don't think that callers have any reasons to use this, so it shouldn't be public API.
Summary
This PR implements new code block options for line highlighting, line striking, line wrapping, and showing line numbers. When the
enable-experimental-code-block
flag is enabled, authors can specify options in the code block language line to control presentation:highlight=[1, 2, 3]
— highlight the given line numbersstrikeout=[3]
— strikethrough the given line numberswrap=80
— wrap code lines to the specified character widthshowLineNumbers
— turn on line numbersThese options may appear after a specified language or alone.
User Experience
With the
enable-experimental-code-block
flag, authors can annotate code listings with these new options using the language line.
or without a language
highlight
andstrikeout
accept 1-indexed arrayswrap
takes an integer and applies soft wrapping at that widthImplementation Overview
swift-docc
, this change parsesshowLineNumbers
,highlight=[Int]
,strikeout=[Int]
, andwrap=Int
from the language line in triple-backtick code blocks, where language (optionally) should be specified as the first option when it’s included.OptionName
enum on theCodeListing
struct for available options.RenderNode.spec.json
to document the newCodeListing
fields.Utility/ParseLanguageString.swift
for parsing the language string and option arrays.InvalidCodeBlockOption
checker to identify typos in options, suggest an unknown option might be the language (when it’s not placed first), and validate array indices forhighlight
andstrikeout
are within the bounds of a code block.Dependencies
This PR builds on top of #1273 and depends on associated changes in
swift-docc-render
to actually render and handle thehighlight
,strikeout
,wrap
, andshowLineNumbers
options. The associated swift-docc-render PR is here: swiftlang/swift-docc-render#965Testing
Setup
Use the additional-metadata branches for
swift-docc
andswift-docc-render
with the newhighlight
,strikeout
,wrap
andshowLineNumbers
changes.Rebuild documentation using
swift-docc
and the feature flagenable-experimental-code-block
. Serve it using a local build ofswift-docc-render
.How to Test
highlight
, `strikeout`, `wrap` and/or `showLineNumbers` options after the language (optionally) like this:Checklist
./bin/test
script and it succeededRenderNode.spec.json
, holding off on documentation for experimental featuresThese new options looks like the below:


Compared to without these options:
