{"id":20683195,"url":"https://github.com/chimehq/textformation","last_synced_at":"2025-04-22T12:22:37.755Z","repository":{"id":43842023,"uuid":"454080491","full_name":"ChimeHQ/TextFormation","owner":"ChimeHQ","description":"Rules system for live typing completions","archived":false,"fork":false,"pushed_at":"2024-03-02T18:55:38.000Z","size":150,"stargazers_count":42,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-24T08:59:36.014Z","etag":null,"topics":["appkit","nstextview","swift","text"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ChimeHQ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["mattmassicotte"]}},"created_at":"2022-01-31T16:23:18.000Z","updated_at":"2024-08-06T00:34:24.020Z","dependencies_parsed_at":"2023-11-21T15:49:51.295Z","dependency_job_id":"eec81518-8072-444d-8c95-d23b6f54c220","html_url":"https://github.com/ChimeHQ/TextFormation","commit_stats":{"total_commits":69,"total_committers":3,"mean_commits":23.0,"dds":0.02898550724637683,"last_synced_commit":"f07ecbdb8daab6cdb5344a88e8685ae55a7a44c3"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChimeHQ%2FTextFormation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChimeHQ%2FTextFormation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChimeHQ%2FTextFormation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChimeHQ%2FTextFormation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ChimeHQ","download_url":"https://codeload.github.com/ChimeHQ/TextFormation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224974662,"owners_count":17401108,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["appkit","nstextview","swift","text"],"created_at":"2024-11-16T22:15:53.288Z","updated_at":"2024-11-16T22:15:53.837Z","avatar_url":"https://github.com/ChimeHQ.png","language":"Swift","funding_links":["https://github.com/sponsors/mattmassicotte"],"categories":[],"sub_categories":[],"readme":"[![Build Status][build status badge]][build status]\n[![License][license badge]][license]\n[![Platforms][platforms badge]][platforms]\n[![Documentation][documentation badge]][documentation]\n\n# TextFormation\n\nTextFormation is simple rule system that can be used to implement typing completions and whitespace control. Think matching \"}\" with \"{\" and indenting.\n\n## Integration\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/ChimeHQ/TextFormation\", from: \"0.8.0\")\n]\n```\n\n## Concept\n\nTextFormation's core model is a `Filter`. Filters are typically set up once for a given language. From there, changes in the form of a `TextMutation` are fed in. The filter examines a `TextMutation` **before** it has been applied. A filter can have three possible result actions.\n\n- `none` indicates that the mutation should be passed to the next filter in the list\n- `stop` means no further filtering should be applied\n- `discard` is just like stop, but also means the `TextMutation` shouldn't be applied\n\nFilters do not necessarily change the text. You must respect the filter action, ensuring that the mutation is actually applied in the cases of `none` and `stop`. The design of the filters tries hard to allow mutations to occur to help maintain the expected selection and undo behaviors of a standard text view.\n\n## Usage\n\nCareful use of filter nesting, possibly `CompositeFilter`, and these actions can produce some pretty powerful behaviors. Here's an example of a chain that produces typing completions that roughly matches what Xcode does for open/close curly braces:\n\n```swift\n// skip over closings\nlet skip = SkipFilter(matching: \"}\")\n\n// apply whitespace to our close\nlet closeWhitespace = LineLeadingWhitespaceFilter(string: \"}\")\n\n// handle newlines inserted in between opening and closing\nlet newlinePair = NewlineWithinPairFilter(open: \"{\", close: \"}\")\n\n// auto-insert closings after an opening, with special-handling for newlines\nlet closePair = ClosePairFilter(open: \"{\", close: \"}\")\n\n// surround selection-replacements with the pair\nlet openPairReplacement = OpenPairReplacementFilter(open: \"{\", close: \"}\")\n\n// delete a matching close when adjacent and the opening is deleted\nlet deleteClose = DeleteCloseFilter(open: \"{\", close: \"}\")\n\nlet filters: [Filter] = [skip, closeWhitespace, openPairReplacement, newlinePair, closePair, deleteClose]\n\n// treat a \"stop\" as only applying to our local chain\nlet filter = CompositeFilter(filters: filters, handler: { (_, action) in\n    switch action {\n    case .stop, .none:\n        return .none\n    case .discard:\n        return .discard\n    }\n})\n```\n\nThis kind of usage is probably going to be common, so all this behavior is wrapped up in a pre-made filter: `StandardOpenPairFilter`.\n\n```swift\nlet filter = StandardOpenPairFilter(open: \"{\", close: \"}\")\n```\n\nUsing filters:\n\n```swift\n// simple indentation algorithm that uses minimal text context\nlet indenter = TextualIndenter()\n\n// delete any trailing whitespace, and use our indenter to compute\n// any needed leading whitespace using a four-space unit\nlet providers = WhitespaceProviders(leadingWhitespace: indenter.substitionProvider(indentationUnit: \"    \", width: 4),\n                                    trailingWhitespace: { _, _ in return \"\" })\n\nlet action = filter.shouldProcessMutation(mutation, in: textView, with: providers)\n```\n\nThere's also a nice little type called `TextViewFilterApplier` that can make it easier to connect filters up to an `NSTextView` or `UITextView`. All you need to do use one of the stand-in delegate methods:\n\n```swift\npublic func textView(_ textView: NSTextView, shouldChangeTextInRanges affectedRanges: [NSValue], replacementStrings: [String]?) -\u003e Bool\npublic func textView(_ textView: NSTextView, shouldChangeTextInRange affectedRange: NSRange, replacementString: String?) -\u003e Bool\n\npublic func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -\u003e Bool\n```\n\n### Indenting\n\nCorrectly indenting in the general case may require parsing. It also typically needs some understanding of the user's preferences. The included `TextualIndenter` type has a pattern-based system that can perform sufficiently in many situations.\n\nIt also includes pre-defined patterns for some languages:\n\n```swift\nTextualIndenter.rubyPatterns\nTextualIndenter.pythonPatterns\n```\n\n### Suggestions or Feedback\n\nWe'd love to hear from you! Get in touch via an issue or pull request.\n\nPlease note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.\n\n[build status]: https://github.com/ChimeHQ/TextFormation/actions\n[build status badge]: https://github.com/ChimeHQ/TextFormation/workflows/CI/badge.svg\n[license]: https://opensource.org/licenses/BSD-3-Clause\n[license badge]: https://img.shields.io/github/license/ChimeHQ/TextFormation\n[platforms]: https://swiftpackageindex.com/ChimeHQ/TextFormation\n[platforms badge]: https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FChimeHQ%2FTextFormation%2Fbadge%3Ftype%3Dplatforms\n[documentation]: https://swiftpackageindex.com/ChimeHQ/TextFormation/main/documentation\n[documentation badge]: https://img.shields.io/badge/Documentation-DocC-blue\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchimehq%2Ftextformation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchimehq%2Ftextformation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchimehq%2Ftextformation/lists"}