Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format on save with Copilot for Xcode (too many limitations) #6

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
</dict>
</plist>
91 changes: 91 additions & 0 deletions CopilotForXcodeExtension/CopilotForXcodeExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import CopilotForXcodeKit
import Foundation
import OSLog

struct FileState {
var contentHashValue: Int = 0
var isDirty: Bool = false
}

func log(message: String, type: OSLogType = .info) {
os_log(
"%{public}@",
log: .init(subsystem: "com.intii.XccurateFormatter", category: "Auto Formatting"),
type: type,
message as CVarArg
)
}

@main
class Extension: CopilotForXcodeExtension {
var host: HostServer?
var suggestionService: SuggestionServiceType? { nil }
var chatService: ChatServiceType? { nil }
var promptToCodeService: PromptToCodeServiceType? { nil }
var sceneConfiguration: SceneConfiguration { .init() }

required init() {}

@MainActor
var files: [URL: FileState] = [:]
@MainActor
var unhandledFileURLs: Set<URL> = []

@MainActor
func workspace(_ workspace: WorkspaceInfo, didUpdateDocumentAt fileURL: URL, content: String) {
updateFile(at: fileURL, content: content)
}

func workspace(_ workspace: WorkspaceInfo, didSaveDocumentAt fileURL: URL) {
Task { await formatFile(at: fileURL) }
}

@MainActor
func workspace(_ workspace: WorkspaceInfo, didCloseDocumentAt fileURL: URL) {
files[fileURL] = nil
}
}

struct SceneConfiguration: CopilotForXcodeExtensionSceneConfiguration {
typealias ChatPanelSceneGroup = Never
typealias SuggestionPanelSceneGroup = Never
}

extension Extension {
@MainActor
func updateFile(at url: URL, content: String) {
var state = files[url] ?? .init()
state.isDirty = content.hashValue != state.contentHashValue
state.contentHashValue = content.hashValue
files[url] = state
}

@MainActor
func formatFile(at url: URL) async {
guard var state = files[url], state.isDirty else {
unhandledFileURLs.remove(url)
return
}
// Only trigger format if the file is active.
guard let editor = try? await host?.getActiveEditor() else { return }
guard editor.documentURL == url else {
// when user switch to another editor, we should format the file when user switch back.
unhandledFileURLs.insert(url)
return
}

do {
try await host?.triggerExtensionCommand(
extensionName: "Xccurate Formatter",
command: "Format File",
activateXcode: false
)
unhandledFileURLs.remove(url)
state.isDirty = false
files[url] = state
} catch {
log(message: error.localizedDescription, type: .error)
}
}
}

11 changes: 11 additions & 0 deletions CopilotForXcodeExtension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EXAppExtensionAttributes</key>
<dict>
<key>EXExtensionPointIdentifier</key>
<string>com.intii.CopilotForXcode.ExtensionService.Extension</string>
</dict>
</dict>
</plist>
Loading