-
-
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 52adc05
Showing
15 changed files
with
577 additions
and
118 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
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,138 @@ | ||
// | ||
// CodeEditor.swift | ||
// CodeEdit | ||
// | ||
// Created by Marco Carnevali on 19/03/22. | ||
// | ||
|
||
import Foundation | ||
import AppKit | ||
import SwiftUI | ||
import Highlightr | ||
import Combine | ||
|
||
struct CodeEditor: NSViewRepresentable { | ||
@ObservedObject private var codeFile: CodeFileDocument | ||
private var lineGutter: LineGutter? | ||
|
||
private let highlightr = Highlightr() | ||
private var view = NSView() | ||
private let theme: CodeFileView.Theme | ||
|
||
private var textView: CodeEditorTextView? { | ||
let scrollView = view.subviews.first as? NSScrollView | ||
return scrollView?.documentView as? CodeEditorTextView | ||
} | ||
|
||
private lazy var scrollView: NSScrollView = { | ||
let scrollView = NSScrollView() | ||
scrollView.drawsBackground = true | ||
scrollView.borderType = .noBorder | ||
scrollView.hasVerticalScroller = true | ||
scrollView.hasHorizontalRuler = false | ||
scrollView.autoresizingMask = [.width, .height] | ||
scrollView.translatesAutoresizingMaskIntoConstraints = false | ||
return scrollView | ||
}() | ||
|
||
init( | ||
codeFile: CodeFileDocument, | ||
theme: CodeFileView.Theme | ||
) { | ||
self.codeFile = codeFile | ||
self.theme = theme | ||
|
||
let contentSize = scrollView.contentSize | ||
let textStorage = NSTextStorage() | ||
let layoutManager = NSLayoutManager() | ||
textStorage.addLayoutManager(layoutManager) | ||
let textContainer = NSTextContainer(containerSize: scrollView.frame.size) | ||
textContainer.widthTracksTextView = true | ||
textContainer.containerSize = NSSize( | ||
width: contentSize.width, | ||
height: .greatestFiniteMagnitude | ||
) | ||
layoutManager.addTextContainer(textContainer) | ||
let textView = CodeEditorTextView(frame: .zero, textContainer: textContainer) | ||
textView.autoresizingMask = .width | ||
textView.drawsBackground = true | ||
textView.isEditable = true | ||
textView.isHorizontallyResizable = false | ||
textView.isVerticallyResizable = true | ||
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) | ||
textView.minSize = NSSize(width: 0, height: contentSize.height) | ||
textView.allowsUndo = true | ||
|
||
scrollView.documentView = textView | ||
scrollView.translatesAutoresizingMaskIntoConstraints = false | ||
view.addSubview(scrollView) | ||
NSLayoutConstraint.activate([ | ||
scrollView.topAnchor.constraint(equalTo: view.topAnchor), | ||
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | ||
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | ||
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor) | ||
]) | ||
let lineGutter = LineGutter( | ||
scrollView: scrollView, | ||
width: 20, | ||
font: .systemFont(ofSize: 10), | ||
textColor: .labelColor, | ||
backgroundColor: .windowBackgroundColor | ||
) | ||
scrollView.verticalRulerView = lineGutter | ||
self.lineGutter = lineGutter | ||
scrollView.rulersVisible = true | ||
highlightr?.setTheme(to: theme.rawValue) | ||
} | ||
|
||
var themeBackgroundColor: NSColor? { | ||
highlightr?.theme.themeBackgroundColor as? NSColor | ||
} | ||
|
||
func makeNSView(context: Context) -> some NSView { | ||
guard let highlightr = highlightr, | ||
let highlightedCode = highlightr.highlight(codeFile.content) | ||
else { | ||
return view | ||
} | ||
textView?.textStorage?.setAttributedString(highlightedCode) | ||
return view | ||
} | ||
|
||
func updateNSView(_ nsView: NSViewType, context: Context) { | ||
} | ||
|
||
class Coordinator { | ||
private let highlightr: Highlightr? | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
init(highlightr: Highlightr?) { | ||
self.highlightr = highlightr | ||
observeTextChange() | ||
} | ||
|
||
deinit { | ||
cancellables.forEach { $0.cancel() } | ||
} | ||
|
||
private func observeTextChange() { | ||
NotificationCenter.default | ||
.publisher(for: NSText.didChangeNotification) | ||
// we debounce so that performances while typing are increased | ||
.debounce(for: .seconds(1.5), scheduler: RunLoop.main) | ||
.compactMap { notification in notification.object as? NSTextView } | ||
.sink { [weak self] textView in | ||
if let code = self?.highlightr?.highlight(textView.string) { | ||
let cursor = textView.selectedRange() | ||
textView.textStorage?.setAttributedString(code) | ||
textView.setSelectedRange(cursor) | ||
} | ||
} | ||
.store(in: &cancellables) | ||
} | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(highlightr: highlightr) | ||
} | ||
} |
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
Oops, something went wrong.