Skip to content

Commit c409699

Browse files
committed
Merge branch 'feature/chat-scope-selection' into develop
2 parents 51d5739 + ee9ab39 commit c409699

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

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

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ You can detach the chat panel by simply dragging it away. Once detached, the cha
193193

194194
The chat panel allows for chat scope to temporarily control the context of the conversation for the latest message. To use a scope, simply prefix the message with `@scope`.
195195

196-
Currently, the only supported scope is `@file`, which will import the content of the file into the system prompt.
196+
| Scope | Description |
197+
|:---:|---|
198+
| `@selection` | Inject the selected code from the active editor into the conversation. This scope will be applied to any message automatically. If you don't want this to be the default behavior, you can turn off the option `Use selection scope by default in chat context.`. |
199+
| `@file` | Inject the content of the file into the conversation. Keep in mind that you may not have enough tokens to inject large files. |
197200

198201
#### Chat Plugins
199202

0 commit comments

Comments
 (0)