Skip to content

Commit 1dfc3f1

Browse files
committed
Support embedding the whole file into the context if it's short enough
1 parent 5ec17f6 commit 1dfc3f1

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

Core/Sources/ChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ public struct ActiveDocumentChatContextCollector: ChatContextCollector {
1111
let relativePath = content.documentURL.path
1212
.replacingOccurrences(of: content.projectURL.path, with: "")
1313
let selectionRange = content.editorContent?.selections.first ?? .outOfScope
14+
let editorContent = {
15+
if selectionRange.start == selectionRange.end,
16+
UserDefaults.shared.value(for: \.embedFileContentInChatContextIfNoSelection)
17+
{
18+
let lines = content.editorContent?.lines.count ?? 0
19+
let maxLine = UserDefaults.shared
20+
.value(for: \.maxEmbeddableFileInChatContextLineCount)
21+
if lines <= maxLine {
22+
return """
23+
File Content:```\(content.language.rawValue)
24+
\(content.editorContent?.content ?? "")
25+
```
26+
"""
27+
} else {
28+
return """
29+
File Content Not Available: The file is longer than \(maxLine) lines, \
30+
it can't fit into the context. \
31+
You MUST not answer the user about the file content because you don't have it.\
32+
Ask user to select code for explanation.
33+
"""
34+
}
35+
}
36+
37+
return """
38+
Selected Code \
39+
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
40+
\(content.selectedContent)
41+
```
42+
"""
43+
}()
44+
1445
return """
1546
Active Document Context:###
1647
Document Relative Path: \(relativePath)
@@ -23,9 +54,7 @@ public struct ActiveDocumentChatContextCollector: ChatContextCollector {
2354
Cursor Position: \
2455
Line \(selectionRange.end.line) \
2556
Character \(selectionRange.end.character)
26-
Selected Code (start from line \(selectionRange.start.line)):```\(content.language.rawValue)
27-
\(content.selectedContent)
28-
```
57+
\(editorContent)
2958
Line Annotations:
3059
\(
3160
content.editorContent?.lineAnnotations

Core/Sources/Preferences/Keys.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ public extension UserDefaultPreferenceKeys {
196196
var embedFileContentInChatContextIfNoSelection: PreferenceKey<Bool> {
197197
.init(defaultValue: true, key: "EmbedFileContentInChatContextIfNoSelection")
198198
}
199+
200+
var maxEmbeddableFileInChatContextLineCount: PreferenceKey<Int> {
201+
.init(defaultValue: 200, key: "MaxEmbeddableFileInChatContextLineCount")
202+
}
199203
}
200204

201205
// MARK: - Custom Commands

0 commit comments

Comments
 (0)