forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCommandTemplateProcessor.swift
More file actions
46 lines (41 loc) · 1.49 KB
/
CustomCommandTemplateProcessor.swift
File metadata and controls
46 lines (41 loc) · 1.49 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
import Foundation
import SuggestionModel
import XcodeInspector
struct CustomCommandTemplateProcessor {
func process(_ text: String) -> String {
let info = 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 ?? ""
)
return updatedText
}
struct EditorInformation {
let editorContent: SourceEditor.Content?
let language: CodeLanguage
let documentURL: URL?
}
func getEditorInformation() -> EditorInformation {
let editorContent = XcodeInspector.shared.focusedEditor?.content
let documentURL = XcodeInspector.shared.activeDocumentURL
let language = documentURL.map(languageIdentifierFromFileURL) ?? .plaintext
return .init(
editorContent: editorContent,
language: language,
documentURL: documentURL
)
}
}