Skip to content

Commit 4ae2a87

Browse files
Initial Commit
This is the first commit for the new CodeEditTextView repository! If you're looking for the source code editor, it's moved to https://github.com/CodeEditApp/CodeEditSourceEditor.
0 parents  commit 4ae2a87

File tree

56 files changed

+6450
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+6450
-0
lines changed

LICENSE.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
MIT License
2+
3+
4+
5+
Copyright (c) 2023 CodeEdit
6+
7+
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
11+
of this software and associated documentation files (the "Software"), to deal
12+
13+
in the Software without restriction, including without limitation the rights
14+
15+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
17+
copies of the Software, and to permit persons to whom the Software is
18+
19+
furnished to do so, subject to the following conditions:
20+
21+
22+
23+
The above copyright notice and this permission notice shall be included in all
24+
25+
copies or substantial portions of the Software.
26+
27+
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
33+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
35+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
37+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
39+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40+
41+
SOFTWARE.

Package.resolved

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "CodeEditTextView",
8+
platforms: [.macOS(.v13)],
9+
products: [
10+
// A Fast, Efficient text view for code.
11+
.library(
12+
name: "CodeEditTextView",
13+
targets: ["CodeEditTextView"]
14+
),
15+
],
16+
dependencies: [
17+
// Text mutation, storage helpers
18+
.package(
19+
url: "https://github.com/ChimeHQ/TextStory",
20+
from: "0.8.0"
21+
),
22+
// Useful data structures
23+
.package(
24+
url: "https://github.com/apple/swift-collections.git",
25+
.upToNextMajor(from: "1.0.0")
26+
),
27+
// SwiftLint
28+
.package(
29+
url: "https://github.com/lukepistrol/SwiftLintPlugin",
30+
from: "0.2.2"
31+
)
32+
],
33+
targets: [
34+
// The main text view target.
35+
.target(
36+
name: "CodeEditTextView",
37+
dependencies: [
38+
"TextStory",
39+
.product(name: "Collections", package: "swift-collections")
40+
],
41+
plugins: [
42+
.plugin(name: "SwiftLint", package: "SwiftLintPlugin")
43+
]
44+
),
45+
46+
// Tests for the text view
47+
.testTarget(
48+
name: "CodeEditTextViewTests",
49+
dependencies: [
50+
"CodeEditTextView"
51+
],
52+
plugins: [
53+
.plugin(name: "SwiftLint", package: "SwiftLintPlugin")
54+
]
55+
),
56+
]
57+
)

README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<p align="center">
2+
<img src="https://github.com/CodeEditApp/CodeEditTextView/blob/main/.github/CodeEditTextView-Icon-128@2x.png?raw=true" height="128">
3+
<h1 align="center">CodeEditTextView</h1>
4+
</p>
5+
6+
7+
<p align="center">
8+
<a aria-label="Follow CodeEdit on Twitter" href="https://twitter.com/CodeEditApp" target="_blank">
9+
<img alt="" src="https://img.shields.io/badge/Follow%20@CodeEditApp-black.svg?style=for-the-badge&logo=Twitter">
10+
</a>
11+
<a aria-label="Join the community on Discord" href="https://discord.gg/vChUXVf9Em" target="_blank">
12+
<img alt="" src="https://img.shields.io/badge/Join%20the%20community-black.svg?style=for-the-badge&logo=Discord">
13+
</a>
14+
<a aria-label="Read the Documentation" href="https://codeeditapp.github.io/CodeEditSourceEditor/documentation/codeeditsourceeditor/" target="_blank">
15+
<img alt="" src="https://img.shields.io/badge/Documentation-black.svg?style=for-the-badge&logo=readthedocs&logoColor=blue">
16+
</a>
17+
</p>
18+
19+
A text editor specialized for displaying and editing code documents. Features include basic text editing, extremely fast initial layout, support for handling large documents, customization options for code documents.
20+
21+
![GitHub release](https://img.shields.io/github/v/release/CodeEditApp/CodeEditTextView?color=orange&label=latest%20release&sort=semver&style=flat-square)
22+
![Github Tests](https://img.shields.io/github/actions/workflow/status/CodeEditApp/CodeEditTextView/tests.yml?branch=main&label=tests&style=flat-square)
23+
![Documentation](https://img.shields.io/github/actions/workflow/status/CodeEditApp/CodeEditTextView/build-documentation.yml?branch=main&label=docs&style=flat-square)
24+
![GitHub Repo stars](https://img.shields.io/github/stars/CodeEditApp/CodeEditTextView?style=flat-square)
25+
![GitHub forks](https://img.shields.io/github/forks/CodeEditApp/CodeEditTextView?style=flat-square)
26+
[![Discord Badge](https://img.shields.io/discord/951544472238444645?color=5865F2&label=Discord&logo=discord&logoColor=white&style=flat-square)](https://discord.gg/vChUXVf9Em)
27+
28+
| :warning: | This package contains a text view suitable for replacing `NSTextView` in some, ***specific*** cases. If you want a text view that can handle things like: right-to-left text, custom layout elements, or feature parity with the system text view, consider using [STTextView](https://github.com/krzyzanowskim/STTextView) or [NSTextView](https://developer.apple.com/documentation/appkit/nstextview). The ``TextView`` exported by this library is designed to lay out documents made up of lines of text. It also does not attempt to reason about the contents of the document. If you're looking to edit *source code* (indentation, syntax highlighting) consider using the parent library [CodeEditSourceEditor](https://github.com/CodeEditApp/CodeEditSourceEditor). |
29+
| - |:-|
30+
31+
## Documentation
32+
33+
This package is fully documented [here](https://codeeditapp.github.io/CodeEditTextView/documentation/codeedittextview/).
34+
35+
## Usage
36+
37+
This package exports a primary `TextView` class. The `TextView` class is an `NSView` subclass that can be embedded in a scroll view or used standalone. It parses and renders lines of a document and handles mouse and keyboard events for text editing. It also renders styled strings for use cases like syntax highlighting.
38+
39+
```swift
40+
import CodeEditTextView
41+
import AppKit
42+
43+
/// # ViewController
44+
///
45+
/// An example view controller for displaying a text view embedded in a scroll view.
46+
class ViewController: NSViewController, TextViewDelegate {
47+
private var scrollView: NSScrollView!
48+
private var textView: TextView!
49+
50+
var text: String = "func helloWorld() {\n\tprint(\"hello world\")\n}"
51+
var font: NSFont!
52+
var textColor: NSColor!
53+
54+
override func loadView() {
55+
textView = TextView(
56+
string: text,
57+
font: font,
58+
textColor: textColor,
59+
lineHeightMultiplier: 1.0,
60+
wrapLines: true,
61+
isEditable: true,
62+
isSelectable: true,
63+
letterSpacing: 1.0,
64+
delegate: self
65+
)
66+
textView.translatesAutoresizingMaskIntoConstraints = false
67+
68+
scrollView = NSScrollView()
69+
scrollView.translatesAutoresizingMaskIntoConstraints = false
70+
scrollView.hasVerticalScroller = true
71+
scrollView.hasHorizontalScroller = true
72+
scrollView.documentView = textView
73+
self.view = scrollView
74+
NSLayoutConstraint.activate([
75+
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
76+
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
77+
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
78+
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
79+
])
80+
81+
textView.updateFrameIfNeeded()
82+
}
83+
}
84+
```
85+
86+
## License
87+
88+
Licensed under the [MIT license](https://github.com/CodeEditApp/CodeEdit/blob/main/LICENSE.md).
89+
90+
## Dependencies
91+
92+
Special thanks to [Matt Massicotte](https://twitter.com/mattie) for the great work he's done!
93+
94+
| Package | Source | Author |
95+
| :---------- | :--------------------------------------------------- | :-------------------------------------------- |
96+
| `TextStory` | [GitHub](https://github.com/ChimeHQ/TextStory) | [Matt Massicotte](https://twitter.com/mattie) |
97+
| `swift-collections` | [GitHub](https://github.com/apple/swift-collections.git) | [Apple](https://github.com/apple) |
98+
99+
## Related Repositories
100+
101+
<table>
102+
<tr>
103+
<td align="center">
104+
<a href="https://github.com/CodeEditApp/CodeEdit">
105+
<img src="https://user-images.githubusercontent.com/806104/163099605-4eaedd33-8441-4125-9ca1-a7ccb2f62a74.png" height="128">
106+
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CodeEdit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
107+
</a>
108+
</td>
109+
<td align="center">
110+
<a href="https://github.com/CodeEditApp/CodeEditSourceEditor">
111+
<img src="https://github.com/CodeEditApp/CodeEditTextView/blob/main/.github/CodeEditSourceEditor-Icon-128@2x.png?raw=true" height="128">
112+
</a>
113+
<p><a href="https://github.com/CodeEditApp/CodeEditSourceEditor">CodeEditSourceEditor</a></p>
114+
</td>
115+
<td align="center">
116+
<a href="https://github.com/CodeEditApp/CodeEditKit">
117+
<img src="https://user-images.githubusercontent.com/806104/193877051-c60d255d-0b6a-408c-bb21-6fabc5e0e60c.png" height="128">
118+
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CodeEditKit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
119+
</a>
120+
</td>
121+
<td align="center">
122+
<a href="https://github.com/CodeEditApp/CodeEditLanguages">
123+
<img src="https://user-images.githubusercontent.com/806104/201497920-d6aace8d-f0dc-49f6-bcd7-6a3b64cc384c.png" height="128">
124+
<p>CodeEditLanguages</p>
125+
</a>
126+
</td>
127+
<td align="center">
128+
<a href="https://github.com/CodeEditApp/CodeEditCLI">
129+
<img src="https://user-images.githubusercontent.com/806104/205848006-f2654778-21f1-4f97-b292-32849cc1eff6.png" height="128">
130+
<p>&nbsp;&nbsp;&nbsp;&nbsp;CodeEdit&nbsp;CLI&nbsp;&nbsp;&nbsp;&nbsp;</p>
131+
</a>
132+
</td>
133+
</tr>
134+
</table>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// This file is purely for helping in the transition from `CodeEditTextView` to `CodeEditSourceEditor`
2+
/// The struct here is an empty view, and will be removed in a future release.
3+
4+
import SwiftUI
5+
6+
// swiftlint:disable:next line_length
7+
@available(*, unavailable, renamed: "CodeEditSourceEditor", message: "CodeEditTextView has moved to https://github.com/CodeEditApp/CodeEditSourceEditor, please update any dependencies to use this new repository URL.")
8+
struct CodeEditTextView: View {
9+
var body: some View {
10+
EmptyView()
11+
}
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# ``CodeEditInputView``
2+
3+
A text editor designed to edit code documents.
4+
5+
## Overview
6+
7+
A text editor specialized for displaying and editing code documents. Features include basic text editing, extremely fast initial layout, support for handling large documents, customization options for code documents.
8+
9+
> This package contains a text view suitable for replacing `NSTextView` in some, ***specific*** cases. If you want a text view that can handle things like: left-to-right layout, custom layout elements, or feature parity with the system text view, consider using [STTextView](https://github.com/krzyzanowskim/STTextView) or [NSTextView](https://developer.apple.com/documentation/appkit/nstextview). The ``TextView`` exported by this library is designed to lay out documents made up of lines of text. However, it does not attempt to reason about the contents of the document. If you're looking to edit *source code* (indentation, syntax highlighting) consider using the parent library [CodeEditSourceEditor](https://github.com/CodeEditApp/CodeEditSourceEditor).
10+
11+
The ``TextView`` class is an `NSView` subclass that can be embedded in a scroll view or used standalone. It parses and renders lines of a document and handles mouse and keyboard events for text editing. It also renders styled strings for use cases like syntax highlighting.
12+
13+
## Topics
14+
15+
### Text View
16+
17+
- ``TextView``
18+
- ``CEUndoManager``
19+
20+
### Text Layout
21+
22+
- ``TextLayoutManager``
23+
- ``TextLine``
24+
- ``LineFragment``
25+
26+
### Text Selection
27+
28+
- ``TextSelectionManager``
29+
- ``TextSelectionManager/TextSelection``
30+
- ``CursorView``
31+
32+
### Supporting Types
33+
34+
- ``TextLineStorage``
35+
- ``HorizontalEdgeInsets``
36+
- ``LineEnding``
37+
- ``LineBreakStrategy``
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// NSRange+isEmpty.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Khan Winter on 8/23/23.
6+
//
7+
8+
import Foundation
9+
10+
public extension NSRange {
11+
var isEmpty: Bool {
12+
length == 0
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// NSTextStorage+getLine.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Khan Winter on 9/3/23.
6+
//
7+
8+
import AppKit
9+
10+
extension NSString {
11+
func getNextLine(startingAt location: Int) -> NSRange? {
12+
let range = NSRange(location: location, length: 0)
13+
var end: Int = NSNotFound
14+
var contentsEnd: Int = NSNotFound
15+
self.getLineStart(nil, end: &end, contentsEnd: &contentsEnd, for: range)
16+
if end != NSNotFound && contentsEnd != NSNotFound && end != contentsEnd {
17+
return NSRange(location: contentsEnd, length: end - contentsEnd)
18+
} else {
19+
return nil
20+
}
21+
}
22+
}
23+
24+
extension NSTextStorage {
25+
func getNextLine(startingAt location: Int) -> NSRange? {
26+
(self.string as NSString).getNextLine(startingAt: location)
27+
}
28+
}

0 commit comments

Comments
 (0)