Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thecoolwinter authored Dec 12, 2023
0 parents commit 4ae2a87
Show file tree
Hide file tree
Showing 56 changed files with 6,450 additions and 0 deletions.
41 changes: 41 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
MIT License



Copyright (c) 2023 CodeEdit



Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:



The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.



THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

SOFTWARE.
50 changes: 50 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"pins" : [
{
"identity" : "mainoffender",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattmassicotte/MainOffender",
"state" : {
"revision" : "343cc3797618c29b48b037b4e2beea0664e75315",
"version" : "0.1.0"
}
},
{
"identity" : "rearrange",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/Rearrange",
"state" : {
"revision" : "8f97f721d8a08c6e01ab9f7460e53819bef72dfa",
"version" : "1.5.3"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
}
},
{
"identity" : "swiftlintplugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/lukepistrol/SwiftLintPlugin",
"state" : {
"revision" : "f69b412a765396d44dc9f4788a5b79919c1ca9e3",
"version" : "0.2.2"
}
},
{
"identity" : "textstory",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/TextStory",
"state" : {
"revision" : "8883fa739aa213e70e6cb109bfbf0a0b551e4cb5",
"version" : "0.8.0"
}
}
],
"version" : 2
}
57 changes: 57 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "CodeEditTextView",
platforms: [.macOS(.v13)],
products: [
// A Fast, Efficient text view for code.
.library(
name: "CodeEditTextView",
targets: ["CodeEditTextView"]
),
],
dependencies: [
// Text mutation, storage helpers
.package(
url: "https://github.com/ChimeHQ/TextStory",
from: "0.8.0"
),
// Useful data structures
.package(
url: "https://github.com/apple/swift-collections.git",
.upToNextMajor(from: "1.0.0")
),
// SwiftLint
.package(
url: "https://github.com/lukepistrol/SwiftLintPlugin",
from: "0.2.2"
)
],
targets: [
// The main text view target.
.target(
name: "CodeEditTextView",
dependencies: [
"TextStory",
.product(name: "Collections", package: "swift-collections")
],
plugins: [
.plugin(name: "SwiftLint", package: "SwiftLintPlugin")
]
),

// Tests for the text view
.testTarget(
name: "CodeEditTextViewTests",
dependencies: [
"CodeEditTextView"
],
plugins: [
.plugin(name: "SwiftLint", package: "SwiftLintPlugin")
]
),
]
)
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<p align="center">
<img src="https://github.com/CodeEditApp/CodeEditTextView/blob/main/.github/[email protected]?raw=true" height="128">
<h1 align="center">CodeEditTextView</h1>
</p>


<p align="center">
<a aria-label="Follow CodeEdit on Twitter" href="https://twitter.com/CodeEditApp" target="_blank">
<img alt="" src="https://img.shields.io/badge/Follow%[email protected]?style=for-the-badge&logo=Twitter">
</a>
<a aria-label="Join the community on Discord" href="https://discord.gg/vChUXVf9Em" target="_blank">
<img alt="" src="https://img.shields.io/badge/Join%20the%20community-black.svg?style=for-the-badge&logo=Discord">
</a>
<a aria-label="Read the Documentation" href="https://codeeditapp.github.io/CodeEditSourceEditor/documentation/codeeditsourceeditor/" target="_blank">
<img alt="" src="https://img.shields.io/badge/Documentation-black.svg?style=for-the-badge&logo=readthedocs&logoColor=blue">
</a>
</p>

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.

![GitHub release](https://img.shields.io/github/v/release/CodeEditApp/CodeEditTextView?color=orange&label=latest%20release&sort=semver&style=flat-square)
![Github Tests](https://img.shields.io/github/actions/workflow/status/CodeEditApp/CodeEditTextView/tests.yml?branch=main&label=tests&style=flat-square)
![Documentation](https://img.shields.io/github/actions/workflow/status/CodeEditApp/CodeEditTextView/build-documentation.yml?branch=main&label=docs&style=flat-square)
![GitHub Repo stars](https://img.shields.io/github/stars/CodeEditApp/CodeEditTextView?style=flat-square)
![GitHub forks](https://img.shields.io/github/forks/CodeEditApp/CodeEditTextView?style=flat-square)
[![Discord Badge](https://img.shields.io/discord/951544472238444645?color=5865F2&label=Discord&logo=discord&logoColor=white&style=flat-square)](https://discord.gg/vChUXVf9Em)

| :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). |
| - |:-|

## Documentation

This package is fully documented [here](https://codeeditapp.github.io/CodeEditTextView/documentation/codeedittextview/).

## Usage

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.

```swift
import CodeEditTextView
import AppKit

/// # ViewController
///
/// An example view controller for displaying a text view embedded in a scroll view.
class ViewController: NSViewController, TextViewDelegate {
private var scrollView: NSScrollView!
private var textView: TextView!

var text: String = "func helloWorld() {\n\tprint(\"hello world\")\n}"
var font: NSFont!
var textColor: NSColor!

override func loadView() {
textView = TextView(
string: text,
font: font,
textColor: textColor,
lineHeightMultiplier: 1.0,
wrapLines: true,
isEditable: true,
isSelectable: true,
letterSpacing: 1.0,
delegate: self
)
textView.translatesAutoresizingMaskIntoConstraints = false

scrollView = NSScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = true
scrollView.documentView = textView
self.view = scrollView
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

textView.updateFrameIfNeeded()
}
}
```

## License

Licensed under the [MIT license](https://github.com/CodeEditApp/CodeEdit/blob/main/LICENSE.md).

## Dependencies

Special thanks to [Matt Massicotte](https://twitter.com/mattie) for the great work he's done!

| Package | Source | Author |
| :---------- | :--------------------------------------------------- | :-------------------------------------------- |
| `TextStory` | [GitHub](https://github.com/ChimeHQ/TextStory) | [Matt Massicotte](https://twitter.com/mattie) |
| `swift-collections` | [GitHub](https://github.com/apple/swift-collections.git) | [Apple](https://github.com/apple) |

## Related Repositories

<table>
<tr>
<td align="center">
<a href="https://github.com/CodeEditApp/CodeEdit">
<img src="https://user-images.githubusercontent.com/806104/163099605-4eaedd33-8441-4125-9ca1-a7ccb2f62a74.png" height="128">
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CodeEdit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
</a>
</td>
<td align="center">
<a href="https://github.com/CodeEditApp/CodeEditSourceEditor">
<img src="https://github.com/CodeEditApp/CodeEditTextView/blob/main/.github/[email protected]?raw=true" height="128">
</a>
<p><a href="https://github.com/CodeEditApp/CodeEditSourceEditor">CodeEditSourceEditor</a></p>
</td>
<td align="center">
<a href="https://github.com/CodeEditApp/CodeEditKit">
<img src="https://user-images.githubusercontent.com/806104/193877051-c60d255d-0b6a-408c-bb21-6fabc5e0e60c.png" height="128">
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CodeEditKit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
</a>
</td>
<td align="center">
<a href="https://github.com/CodeEditApp/CodeEditLanguages">
<img src="https://user-images.githubusercontent.com/806104/201497920-d6aace8d-f0dc-49f6-bcd7-6a3b64cc384c.png" height="128">
<p>CodeEditLanguages</p>
</a>
</td>
<td align="center">
<a href="https://github.com/CodeEditApp/CodeEditCLI">
<img src="https://user-images.githubusercontent.com/806104/205848006-f2654778-21f1-4f97-b292-32849cc1eff6.png" height="128">
<p>&nbsp;&nbsp;&nbsp;&nbsp;CodeEdit&nbsp;CLI&nbsp;&nbsp;&nbsp;&nbsp;</p>
</a>
</td>
</tr>
</table>
12 changes: 12 additions & 0 deletions Sources/CodeEditTextView/CodeEditTextView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// This file is purely for helping in the transition from `CodeEditTextView` to `CodeEditSourceEditor`
/// The struct here is an empty view, and will be removed in a future release.

import SwiftUI

// swiftlint:disable:next line_length
@available(*, unavailable, renamed: "CodeEditSourceEditor", message: "CodeEditTextView has moved to https://github.com/CodeEditApp/CodeEditSourceEditor, please update any dependencies to use this new repository URL.")
struct CodeEditTextView: View {
var body: some View {
EmptyView()
}
}
37 changes: 37 additions & 0 deletions Sources/CodeEditTextView/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ``CodeEditInputView``

A text editor designed to edit code documents.

## Overview

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.

> 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).
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.

## Topics

### Text View

- ``TextView``
- ``CEUndoManager``

### Text Layout

- ``TextLayoutManager``
- ``TextLine``
- ``LineFragment``

### Text Selection

- ``TextSelectionManager``
- ``TextSelectionManager/TextSelection``
- ``CursorView``

### Supporting Types

- ``TextLineStorage``
- ``HorizontalEdgeInsets``
- ``LineEnding``
- ``LineBreakStrategy``
14 changes: 14 additions & 0 deletions Sources/CodeEditTextView/Extensions/NSRange+isEmpty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// NSRange+isEmpty.swift
// CodeEditTextView
//
// Created by Khan Winter on 8/23/23.
//

import Foundation

public extension NSRange {
var isEmpty: Bool {
length == 0
}
}
28 changes: 28 additions & 0 deletions Sources/CodeEditTextView/Extensions/NSTextStorage+getLine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// NSTextStorage+getLine.swift
// CodeEditTextView
//
// Created by Khan Winter on 9/3/23.
//

import AppKit

extension NSString {
func getNextLine(startingAt location: Int) -> NSRange? {
let range = NSRange(location: location, length: 0)
var end: Int = NSNotFound
var contentsEnd: Int = NSNotFound
self.getLineStart(nil, end: &end, contentsEnd: &contentsEnd, for: range)
if end != NSNotFound && contentsEnd != NSNotFound && end != contentsEnd {
return NSRange(location: contentsEnd, length: end - contentsEnd)
} else {
return nil
}
}
}

extension NSTextStorage {
func getNextLine(startingAt location: Int) -> NSRange? {
(self.string as NSString).getNextLine(startingAt: location)
}
}
Loading

0 comments on commit 4ae2a87

Please sign in to comment.