-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70f5d71
commit e2f6052
Showing
18 changed files
with
824 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
CodeEditModules/.swiftpm/xcode/xcshareddata/xcschemes/CodeFile.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1330" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "CodeFile" | ||
BuildableName = "CodeFile" | ||
BlueprintName = "CodeFile" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "CodeFile" | ||
BuildableName = "CodeFile" | ||
BlueprintName = "CodeFile" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
17 changes: 0 additions & 17 deletions
17
CodeEditModules/Modules/CodeFile/src/CodeEditor+AppStorage.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// | ||
// CodeEditor.swift | ||
// CodeEdit | ||
// | ||
// Created by Marco Carnevali on 19/03/22. | ||
// | ||
|
||
import Foundation | ||
import AppKit | ||
import SwiftUI | ||
import Highlightr | ||
import Combine | ||
|
||
struct CodeEditor: NSViewRepresentable { | ||
@State private var isCurrentlyUpdatingView: ReferenceTypeBool = .init(value: false) | ||
private var content: Binding<String> | ||
private let language: Language? | ||
private let theme: Binding<CodeFileView.Theme> | ||
private let highlightr = Highlightr() | ||
|
||
init( | ||
content: Binding<String>, | ||
language: Language?, | ||
theme: Binding<CodeFileView.Theme> | ||
) { | ||
self.content = content | ||
self.language = language | ||
self.theme = theme | ||
highlightr?.setTheme(to: theme.wrappedValue.rawValue) | ||
} | ||
|
||
func makeNSView(context: Context) -> NSScrollView { | ||
let scrollView = NSScrollView() | ||
let textView = CodeEditorTextView( | ||
textContainer: buildTextStorage( | ||
language: language, | ||
scrollView: scrollView | ||
) | ||
) | ||
textView.autoresizingMask = .width | ||
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) | ||
textView.minSize = NSSize(width: 0, height: scrollView.contentSize.height) | ||
textView.delegate = context.coordinator | ||
|
||
scrollView.drawsBackground = true | ||
scrollView.borderType = .noBorder | ||
scrollView.hasVerticalScroller = true | ||
scrollView.hasHorizontalRuler = false | ||
scrollView.autoresizingMask = [.width, .height] | ||
|
||
scrollView.documentView = textView | ||
scrollView.verticalRulerView = LineGutter( | ||
scrollView: scrollView, | ||
width: 60, | ||
font: .systemFont(ofSize: 10), | ||
textColor: .labelColor, | ||
backgroundColor: .windowBackgroundColor | ||
) | ||
scrollView.rulersVisible = true | ||
|
||
updateTextView(textView) | ||
return scrollView | ||
} | ||
|
||
func updateNSView(_ scrollView: NSScrollView, context: Context) { | ||
if let textView = scrollView.documentView as? CodeEditorTextView { | ||
updateTextView(textView) | ||
} | ||
} | ||
|
||
final class Coordinator: NSObject, NSTextViewDelegate { | ||
private var content: Binding<String> | ||
init(content: Binding<String>) { | ||
self.content = content | ||
} | ||
|
||
func textDidChange(_ notification: Notification) { | ||
guard let textView = notification.object as? NSTextView else { | ||
return | ||
} | ||
content.wrappedValue = textView.string | ||
} | ||
|
||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(content: content) | ||
} | ||
|
||
private func updateTextView(_ textView: NSTextView) { | ||
guard !isCurrentlyUpdatingView.value else { | ||
return | ||
} | ||
|
||
isCurrentlyUpdatingView.value = true | ||
|
||
defer { | ||
isCurrentlyUpdatingView.value = false | ||
} | ||
|
||
highlightr?.setTheme(to: theme.wrappedValue.rawValue) | ||
|
||
if content.wrappedValue != textView.string { | ||
if let textStorage = textView.textStorage as? CodeAttributedString { | ||
textStorage.language = language?.rawValue | ||
textStorage.replaceCharacters( | ||
in: NSRange(location: 0, length: textStorage.length), | ||
with: content.wrappedValue | ||
) | ||
} else { | ||
textView.string = content.wrappedValue | ||
} | ||
} | ||
} | ||
|
||
private func buildTextStorage(language: Language?, scrollView: NSScrollView) -> NSTextContainer { | ||
// highlightr wrapper that enables real-time highlighting | ||
let textStorage: CodeAttributedString | ||
if let highlightr = highlightr { | ||
textStorage = CodeAttributedString(highlightr: highlightr) | ||
} else { | ||
textStorage = CodeAttributedString() | ||
} | ||
textStorage.language = language?.rawValue | ||
let layoutManager = NSLayoutManager() | ||
textStorage.addLayoutManager(layoutManager) | ||
let textContainer = NSTextContainer(containerSize: scrollView.frame.size) | ||
textContainer.widthTracksTextView = true | ||
textContainer.containerSize = NSSize( | ||
width: scrollView.contentSize.width, | ||
height: .greatestFiniteMagnitude | ||
) | ||
layoutManager.addTextContainer(textContainer) | ||
return textContainer | ||
} | ||
} | ||
|
||
extension CodeEditor { | ||
// A wrapper around a `Bool` that enables updating | ||
// the wrapped value during `View` renders. | ||
private class ReferenceTypeBool { | ||
var value: Bool | ||
|
||
init(value: Bool) { | ||
self.value = value | ||
} | ||
} | ||
} |
Oops, something went wrong.