forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCommandTemplateProcessor.swift
More file actions
53 lines (47 loc) · 1.75 KB
/
CustomCommandTemplateProcessor.swift
File metadata and controls
53 lines (47 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import AppKit
import Foundation
import SuggestionBasic
import XcodeInspector
public struct CustomCommandTemplateProcessor {
public init() {}
public func process(_ text: String) async -> String {
let info = await getEditorInformation()
let editorContent = info.editorContent
let updatedText = text
.replacingOccurrences(of: "{{selected_code}}", with: """
\(editorContent?.selectedContent.trimmingCharacters(in: .whitespacesAndNewlines) ?? "")
""")
.replacingOccurrences(
of: "{{active_editor_language}}",
with: info.language.rawValue
)
.replacingOccurrences(
of: "{{active_editor_file_url}}",
with: info.documentURL?.path ?? ""
)
.replacingOccurrences(
of: "{{active_editor_file_name}}",
with: info.documentURL?.lastPathComponent ?? ""
)
.replacingOccurrences(
of: "{{clipboard}}",
with: NSPasteboard.general.string(forType: .string) ?? ""
)
return updatedText
}
struct EditorInformation {
let editorContent: SourceEditor.Content?
let language: CodeLanguage
let documentURL: URL?
}
func getEditorInformation() async -> EditorInformation {
let editorContent = await XcodeInspector.shared.safe.focusedEditor?.getContent()
let documentURL = await XcodeInspector.shared.safe.activeDocumentURL
let language = documentURL.map(languageIdentifierFromFileURL) ?? .plaintext
return .init(
editorContent: editorContent,
language: language,
documentURL: documentURL
)
}
}