Skip to content

Commit c9b3f07

Browse files
committed
Support disabling selected code to be visible by the chat by default
1 parent 51d5739 commit c9b3f07

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

Core/Sources/ChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct ActiveDocumentChatContextCollector: ChatContextCollector {
1919
```
2020
"""
2121
}
22-
22+
2323
if selectionRange.start == selectionRange.end,
2424
UserDefaults.shared.value(for: \.embedFileContentInChatContextIfNoSelection)
2525
{
@@ -34,19 +34,40 @@ public struct ActiveDocumentChatContextCollector: ChatContextCollector {
3434
"""
3535
} else {
3636
return """
37-
File Content Not Available: The file is longer than \(maxLine) lines, \
38-
it can't fit into the context. \
37+
File Content Not Available: '''
38+
The file is longer than \(maxLine) lines, it can't fit into the context. \
3939
You MUST not answer the user about the file content because you don't have it.\
4040
Ask user to select code for explanation.
41+
'''
4142
"""
4243
}
4344
}
4445

46+
if UserDefaults.shared.value(for: \.useSelectionScopeByDefaultInChatContext) {
47+
return """
48+
Selected Code \
49+
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
50+
\(content.selectedContent)
51+
```
52+
"""
53+
}
54+
55+
if prompt.hasPrefix("@selection") {
56+
return """
57+
Selected Code \
58+
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
59+
\(content.selectedContent)
60+
```
61+
"""
62+
}
63+
4564
return """
46-
Selected Code \
47-
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
48-
\(content.selectedContent)
49-
```
65+
Selected Code Not Available: '''
66+
User has disabled default scope. \
67+
You MUST not answer the user about the selected code because you don't have it.\
68+
Ask user to prepend message with `@selection` to enable selected code to be \
69+
visible by you.
70+
'''
5071
"""
5172
}()
5273

Core/Sources/HostApp/FeatureSettings/ChatSettingsView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ struct ChatSettingsView: View {
1414
var embedFileContentInChatContextIfNoSelection
1515
@AppStorage(\.maxEmbeddableFileInChatContextLineCount)
1616
var maxEmbeddableFileInChatContextLineCount
17+
@AppStorage(\.useSelectionScopeByDefaultInChatContext)
18+
var useSelectionScopeByDefaultInChatContext
1719

1820
@AppStorage(\.chatFeatureProvider) var chatFeatureProvider
1921
@AppStorage(\.chatGPTModel) var chatGPTModel
@@ -165,6 +167,10 @@ struct ChatSettingsView: View {
165167
@ViewBuilder
166168
var contextForm: some View {
167169
Form {
170+
Toggle(isOn: $settings.useSelectionScopeByDefaultInChatContext) {
171+
Text("Use selection scope by default in chat context.")
172+
}
173+
168174
Toggle(isOn: $settings.embedFileContentInChatContextIfNoSelection) {
169175
Text("Embed file content in chat context if no code is selected.")
170176
}

Core/Sources/Preferences/Keys.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ public extension UserDefaultPreferenceKeys {
225225
var maxEmbeddableFileInChatContextLineCount: PreferenceKey<Int> {
226226
.init(defaultValue: 100, key: "MaxEmbeddableFileInChatContextLineCount")
227227
}
228+
229+
var useSelectionScopeByDefaultInChatContext: PreferenceKey<Bool> {
230+
.init(defaultValue: true, key: "UseSelectionScopeByDefaultInChatContext")
231+
}
228232
}
229233

230234
// MARK: - Custom Commands

0 commit comments

Comments
 (0)