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
37 lines (32 loc) · 1.14 KB
/
CustomCommandTemplateProcessor.swift
File metadata and controls
37 lines (32 loc) · 1.14 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
import Foundation
import SuggestionModel
import XcodeInspector
struct CustomCommandTemplateProcessor {
func process(_ text: String) -> String {
let info = getEditorInformation()
if let editorContent = info.editorContent {
let updatedText = text.replacingOccurrences(of: "{{selected_code}}", with: """
```\(info.language.rawValue)
\(editorContent.selectedContent.trimmingCharacters(in: ["\n"]))
```
""")
return updatedText
} else {
let updatedText = text.replacingOccurrences(of: "{{selected_code}}", with: "")
return updatedText
}
}
struct EditorInformation {
let editorContent: SourceEditor.Content?
let language: CodeLanguage
}
func getEditorInformation() -> EditorInformation {
let editorContent = XcodeInspector.shared.focusedEditor?.content
let documentURL = XcodeInspector.shared.activeDocumentURL
let language = languageIdentifierFromFileURL(documentURL)
return .init(
editorContent: editorContent,
language: language
)
}
}