forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatTabFactory.swift
More file actions
57 lines (52 loc) · 1.91 KB
/
ChatTabFactory.swift
File metadata and controls
57 lines (52 loc) · 1.91 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
53
54
55
56
57
import BuiltinExtension
import ChatGPTChatTab
import ChatService
import ChatTab
import Foundation
import PromptToCodeService
import SuggestionBasic
import SuggestionWidget
import XcodeInspector
enum ChatTabFactory {
static func chatTabBuilderCollection() -> [ChatTabBuilderCollection] {
let chatGPTChatTab = folderIfNeeded(
ChatGPTChatTab.chatBuilders(),
title: ChatGPTChatTab.name
)
let (defaultChatTab, othersChatTabs) = chatTabsFromExtensions()
if let defaultChatTab {
return [defaultChatTab] + othersChatTabs + [chatGPTChatTab].compactMap(\.self)
} else {
return [chatGPTChatTab].compactMap(\.self) + othersChatTabs
}
}
private static func folderIfNeeded(
_ builders: [any ChatTabBuilder],
title: String
) -> ChatTabBuilderCollection? {
if builders.count > 1 {
return .folder(title: title, kinds: builders.map(ChatTabKind.init))
}
if let first = builders.first { return .kind(ChatTabKind(first)) }
return nil
}
static func chatTabsFromExtensions()
-> (default: ChatTabBuilderCollection?, others: [ChatTabBuilderCollection])
{
let extensions = BuiltinExtensionManager.shared.extensions
let chatTabTypes = extensions.flatMap(\.chatTabTypes)
var defaultChatTab: ChatTabBuilderCollection?
var otherChatTabs = [ChatTabBuilderCollection]()
for chatTabType in chatTabTypes {
if chatTabType.isDefaultChatTabReplacement {
defaultChatTab = folderIfNeeded(chatTabType.chatBuilders(), title: chatTabType.name)
} else if let tab = folderIfNeeded(
chatTabType.chatBuilders(),
title: chatTabType.name
) {
otherChatTabs.append(tab)
}
}
return (defaultChatTab, otherChatTabs)
}
}