Skip to content

Added editable attributed strings for Skip Lite - #143

Merged
tifroz merged 3 commits into
skiptools:mainfrom
tifroz:tifroz/skiplite-attributed-string
Nov 4, 2026
Merged

tifroz merged 3 commits into
skiptools:mainfrom
tifroz:tifroz/skiplite-attributed-string

Conversation

@tifroz

@tifroz tifroz commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for contributing to the Skip project! Please use this space to describe your change and add any labels (bug, enhancement, documentation, etc.) to help categorize your contribution.

Please review the contribution guide at https://skip.dev/docs/contributing/ for advice and guidance on making high-quality PRs.

Skip Pull Request Checklist:

  • REQUIRED: I have signed the Contributor Agreement
  • REQUIRED: I have tested my change locally with swift test
  • OPTIONAL: I have tested my change on an iOS simulator or device
  • OPTIONAL: I have tested my change on an Android emulator or device

  • AI was used to generate or assist with generating this PR. Please specify below how you used AI to help you, and what steps you have taken to manually verify the changes.

Codex generated the code, the runtime tested on device from the Showcase app.


Summary

Skip Lite's AttributedString currently stores text and a Markdown tree, so callers cannot find a range, edit its attributes, or enumerate the resulting runs. This adds an editable subset of Foundation's API for transpiled code.

Companion SkipUI, and Showcase PRs are forthcoming

Edit attributes with value semantics

Add AttributedSubstring, AttributeContainer, inferred ranges, run enumeration, and attribute-preserving append. Range edits split and merge runs while preserving unrelated attributes. Copies and detached substrings remain independent. Equality and hashing include attributes and do not depend on edit history. Named attribute access lets UI libraries add typed properties without introducing a UI dependency into Foundation.

Source files:

  • Sources/SkipFoundation/AttributedString.swift
  • README.md

Tests:

  • Created Tests/SkipFoundationTests/Formats/EditableAttributedStringTests.swift. Covers overlapping edits and removal, copy independence, substring source coordinates, Unicode ranges, equality/hashing, append, empty values, and writes through array elements. In particular, testCopyAndDetachedSubstringAreIndependent and testWritesThroughAnArrayElementPreserveValueSemantics check transpiled write-back and copying.

Make parsed and localized Markdown editable

Create attribute runs from inline Markdown using the existing CommonMark dependency. Preserve whitespace, nested inline styles, and link destinations. Localized substitutions remain literal instead of being parsed again as Markdown. Retain markdownNode for existing consumers until the value is edited.

Source files:

  • Sources/SkipFoundation/AttributedString.swift
  • Sources/SkipFoundation/Skip/AttributedMarkdown.kt

Tests:

  • Created in Tests/SkipFoundationTests/Formats/EditableAttributedStringTests.swift: testMarkdownCanBeEditedWithoutLosingInlineStyles, testLocalizedInterpolationIsEditable, testLocalizedMarkdownPreservesLiteralPercents, and the Lite-specific testLocalizedSubstitutionDoesNotIntroduceMarkup.
  • Existing coverage in Tests/SkipFoundationTests/Formats/TestMarkdown.swift: eight tests for the existing Markdown tree, including formatting, links, line breaks, interpolation, and percent escaping.

Add platform-backed range searching

Both AttributedString and AttributedSubstring support range(of:options:locale:). Add String.CompareOptions with .caseInsensitive so callers can find "Skip" using "skip" and then edit the returned range.

Searches with .caseInsensitive or an explicit locale use Android's built-in ICU StringSearch. The returned range uses ICU's matched length in the original text, including when the query and match have different Unicode representations. Substring searches retain their source coordinates. Calls with options: [] and locale: nil preserve the existing exact-match behavior.

Source files:

  • Sources/SkipFoundation/AttributedString.swift
  • Sources/SkipFoundation/String.swift
  • README.md

Tests:

  • Added four shared tests in EditableAttributedStringTests covering first-match behavior, missing and empty searches, composed/decomposed accents with different match lengths, substring source coordinates, attribute edits, and an explicit Turkish locale.

Usage Example

var text = AttributedString("Visit Skip")
if let range = text.range(of: "skip", options: .caseInsensitive) {
    text[range].link = URL(string: "https://skip.dev")
    text[range].inlinePresentationIntent = .stronglyEmphasized
}

for run in text.runs {
    let characters = String(text[run.range].characters)
    let destination = run.link
}

API Shape

Selected declarations, with implementation omitted:

// AttributedString
init(_ string: String, attributes: AttributeContainer = AttributeContainer())
init(_ substring: AttributedSubstring)
init(markdown: String, options: MarkdownParsingOptions = MarkdownParsingOptions()) throws
func range(
    of text: String,
    options: String.CompareOptions = [],
    locale: Locale? = nil
) -> AttributedStringRange?
subscript(_ bounds: AttributedStringRange) -> AttributedSubstring { get set }
var runs: [Run] { get }
var characters: String { get }
mutating func append(_ other: AttributedString)

// Available on strings, substrings, and attribute containers;
// readable on runs.
var link: URL? { get set }
var inlinePresentationIntent: InlinePresentationIntent? { get set }

The same search signature is available on AttributedSubstring. String.CompareOptions currently exposes only .caseInsensitive.

Testing

Validation at f6c1b4f:

  • swift build --build-tests --build-system native.
  • swift test --filter EditableAttributedStringTests: 19 tests against Apple Foundation, zero failures.
  • Gradle :SkipFoundation:testDebugUnitTest --tests 'skip.foundation.EditableAttributedStringTests' -Pkotlin.incremental=false: 21 transpiled tests, zero failures or skips.
  • Companion UI validation passed 3 Apple tests and 5 Android tests, including a Compose rendering test that checks the style boundaries after a case-insensitive Unicode search.

The full package test suite was not run. Generated Kotlin was inspected during implementation to verify substring write-back and detached copying.

Risks / Limitations

  • Search accepts [] or .caseInsensitive. Other option flags, including .backwards, .anchored, .diacriticInsensitive, and .regularExpression, are not implemented; unsupported raw option bits fail a precondition rather than being ignored. This does not extend the plain String.range(of:) overload.
  • ICU searches use the supplied locale, or ICU's root locale when locale is nil. They support canonical Unicode equivalence and retain accent distinctions under the selected locale's rules. With no options and no locale, search remains exact.
  • Search follows the device's ICU rules, so it does not promise identical matching to Apple Foundation. For example, the tested ICU case-insensitive search does not match Straße with STRASSE, while Apple Foundation does. Unicode matching is delegated to the platform; no custom matching tables are added.
  • This is a supported subset, not full Foundation parity. Ranges must be inferred from range(of:) or runs. Lite uses AttributedStringRange and UTF-16 indices; explicit Range<AttributedString.Index> slicing and partial ranges are unsupported.
  • Mutable character views, general character insertion/removal, generic attribute keys/scopes, and Codable support are not included. Named attribute helpers are Skip-specific APIs.
  • Markdown parsing supports only .inlineOnlyPreservingWhitespace, also the Lite default. Block formatting and other parsing modes are unsupported.
  • Compatibility changes: .string and .description now contain parsed text without Markdown delimiters; equality and hashing include attributes. Editing invalidates the legacy markdownNode tree, so renderers must consume runs to display edited attributes.
  • UI attributes and rendering require companion SkipUI changes. This Foundation PR has no dependency on those changes and can be merged first.

@cla-bot cla-bot Bot added the cla-signed label Nov 1, 2026
@tifroz

tifroz commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@fhasse95 SkipFoundation is the lowest dependency for the added attributed text support (for SkipLite), I will need to merge it before submitting the PRs for SkipUI and ShowCase.

You can test & preview the updated Showcase app from my showcase repo (#if !SKIP is gone)
You can preview the updated SkipUI lib from my repo as well

Let me know if you have any comments before I merge this one (and then SkipUI, Showcase)

@fhasse95

fhasse95 commented Nov 4, 2026

Copy link
Copy Markdown
Member

@tifroz Thanks for the changes. Overall, it works great! 😊

I just noticed one use case that doesn't seem to be supported in SkipFoundation yet: AttributedString.range(of:options:).

For example, the following code fails in Skip Lite:

private var searchHighlightedText: AttributedString {
    let searchText = "skip"
    var text = AttributedString("Search highlighting in Skip")

    if let range = text.range(of: searchText, options: .caseInsensitive) {
        text[range].foregroundColor = .accentColor
    }

    return text
}

Would it be possible to add support for String.CompareOptions, particularly .caseInsensitive, to SkipFoundation as well?

@fhasse95 fhasse95 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for adding case-insensitive search and the additional tests! Looks good to me. Feel free to merge! 👍

@tifroz

tifroz commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@fhasse95 Thanks for the review, I will merge this SkipFoundation PR as soon as it lands the CI, then submit & merge the ShowCase and SkipUI companion PRs in sequence.

Here is a quick look at what the updated version looks like in Showcase
screenwriter_temp_1793818997634

@tifroz
tifroz merged commit ba10460 into skiptools:main Nov 4, 2026
2 checks passed
@tifroz
tifroz deleted the tifroz/skiplite-attributed-string branch November 4, 2026 20:37
@tifroz tifroz mentioned this pull request Nov 4, 2026
7 tasks done
tifroz added a commit to skiptools/skip-ui that referenced this pull request Nov 5, 2026
RELEASE MANAGEMENT: This PR depends on skiptools/skip-foundation#143

* Added editable attributed text support for Skip Lite
* Verified and documented case-insensitive attributed-text highlighting
tifroz added a commit to skiptools/skipapp-showcase that referenced this pull request Nov 5, 2026
…les + superscript)

Depends on 3 merged PRs - the dependencies are pinned to their respective commit until the next release

- [SkipFoundation #143](skiptools/skip-foundation#143)
- [SkipUI #597](skiptools/skip-ui#597)
- [SkipFuseUI #164](skiptools/skip-fuse-ui#164). 

* Added attributed text examples using merged Skip dependencies
* Added shared attributed-text and search highlighting examples
* Listed editable attributed text in the playground index
* Avoided Swift type-check timeout in the Alert playground
Ceylo added a commit to Ceylo/skip-foundation that referenced this pull request Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants