-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathChatSection.swift
More file actions
165 lines (140 loc) · 5.5 KB
/
ChatSection.swift
File metadata and controls
165 lines (140 loc) · 5.5 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import Client
import ComposableArchitecture
import SwiftUI
import Toast
import XcodeInspector
struct ChatSection: View {
@AppStorage(\.autoAttachChatToXcode) var autoAttachChatToXcode
var body: some View {
SettingsSection(title: "Chat Settings") {
// Auto Attach toggle
SettingsToggle(
title: "Auto-attach Chat Window to Xcode",
isOn: $autoAttachChatToXcode
)
Divider()
// Response language picker
ResponseLanguageSetting()
.padding(SettingsToggle.defaultPadding)
Divider()
// Custom instructions
CustomInstructionSetting()
.padding(SettingsToggle.defaultPadding)
}
}
}
struct ResponseLanguageSetting: View {
@AppStorage(\.chatResponseLocale) var chatResponseLocale
// Locale codes mapped to language display names
// reference: https://code.visualstudio.com/docs/configure/locales#_available-locales
private let localeLanguageMap: [String: String] = [
"en": "English",
"zh-cn": "Chinese, Simplified",
"zh-tw": "Chinese, Traditional",
"fr": "French",
"de": "German",
"it": "Italian",
"es": "Spanish",
"ja": "Japanese",
"ko": "Korean",
"ru": "Russian",
"pt-br": "Portuguese (Brazil)",
"tr": "Turkish",
"pl": "Polish",
"cs": "Czech",
"hu": "Hungarian"
]
var selectedLanguage: String {
if chatResponseLocale == "" {
return "English"
}
return localeLanguageMap[chatResponseLocale] ?? "English"
}
// Display name to locale code mapping (for the picker UI)
var sortedLanguageOptions: [(displayName: String, localeCode: String)] {
localeLanguageMap.map { (displayName: $0.value, localeCode: $0.key) }
.sorted { $0.displayName < $1.displayName }
}
var body: some View {
WithPerceptionTracking {
HStack {
VStack(alignment: .leading) {
Text("Response Language")
.font(.body)
Text("This change applies only to new chat sessions. Existing ones won’t be impacted.")
.font(.footnote)
}
Spacer()
Picker("", selection: $chatResponseLocale) {
ForEach(sortedLanguageOptions, id: \.localeCode) { option in
Text(option.displayName).tag(option.localeCode)
}
}
.frame(maxWidth: 200, alignment: .leading)
}
}
}
}
struct CustomInstructionSetting: View {
@State var isGlobalInstructionsViewOpen = false
@Environment(\.toast) var toast
var body: some View {
WithPerceptionTracking {
HStack {
VStack(alignment: .leading) {
Text("Custom Instructions")
.font(.body)
Text("Configure custom instructions for GitHub Copilot to follow during chat sessions.")
.font(.footnote)
}
Spacer()
Button("Current Workspace") {
openCustomInstructions()
}
Button("Global") {
isGlobalInstructionsViewOpen = true
}
}
.sheet(isPresented: $isGlobalInstructionsViewOpen) {
GlobalInstructionsView(isOpen: $isGlobalInstructionsViewOpen)
}
}
}
func openCustomInstructions() {
Task {
let service = try? getService()
let inspectorData = try? await service?.getXcodeInspectorData()
var currentWorkspace: URL? = nil
if let url = inspectorData?.realtimeActiveWorkspaceURL, let workspaceURL = URL(string: url), workspaceURL.path != "/" {
currentWorkspace = workspaceURL
} else if let url = inspectorData?.latestNonRootWorkspaceURL {
currentWorkspace = URL(string: url)
}
// Open custom instructions for the current workspace
if let workspaceURL = currentWorkspace, let projectURL = WorkspaceXcodeWindowInspector.extractProjectURL(workspaceURL: workspaceURL, documentURL: nil) {
let configFile = projectURL.appendingPathComponent(".github/copilot-instructions.md")
// If the file doesn't exist, create one with a proper structure
if !FileManager.default.fileExists(atPath: configFile.path) {
do {
// Create directory if it doesn't exist
try FileManager.default.createDirectory(
at: projectURL.appendingPathComponent(".github"),
withIntermediateDirectories: true
)
// Create empty file
try "".write(to: configFile, atomically: true, encoding: .utf8)
} catch {
toast("Failed to create config file .github/copilot-instructions.md: \(error)", .error)
}
}
if FileManager.default.fileExists(atPath: configFile.path) {
NSWorkspace.shared.open(configFile)
}
}
}
}
}
#Preview {
ChatSection()
.frame(width: 600)
}