Skip to content

Commit 6c4fc82

Browse files
committed
Update ActiveDocumentChatContextCollector to only include selected code when the file is gitignored
1 parent a916e14 commit 6c4fc82

File tree

1 file changed

+47
-35
lines changed

1 file changed

+47
-35
lines changed

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import ASTParser
22
import ChatContextCollector
3+
import Dependencies
34
import FocusedCodeFinder
45
import Foundation
6+
import GitIgnoreCheck
57
import OpenAIService
68
import Preferences
79
import SuggestionModel
@@ -12,22 +14,27 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
1214

1315
public var activeDocumentContext: ActiveDocumentContext?
1416

17+
@Dependency(\.gitIgnoredChecker) var gitIgnoredChecker
18+
1519
public func generateContext(
1620
history: [ChatMessage],
1721
scopes: Set<ChatContext.Scope>,
1822
content: String,
1923
configuration: ChatGPTConfiguration
20-
) -> ChatContext {
24+
) async -> ChatContext {
2125
guard let info = getEditorInformation() else { return .empty }
2226
let context = getActiveDocumentContext(info)
2327
activeDocumentContext = context
2428

25-
guard scopes.contains(.code) else {
29+
let isSensitive = await gitIgnoredChecker.checkIfGitIgnored(fileURL: info.documentURL)
30+
31+
guard scopes.contains(.code)
32+
else {
2633
if scopes.contains(.file) {
2734
var removedCode = context
2835
removedCode.focusedContext = nil
2936
return .init(
30-
systemPrompt: extractSystemPrompt(removedCode),
37+
systemPrompt: extractSystemPrompt(removedCode, isSensitive: isSensitive),
3138
retrievedContent: [],
3239
functions: []
3340
)
@@ -37,36 +44,39 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
3744

3845
var functions = [any ChatGPTFunction]()
3946

40-
// When the bot is already focusing on a piece of code, it can expand the range.
47+
if !isSensitive {
48+
// When the bot is already focusing on a piece of code, it can expand the range.
4149

42-
if context.focusedContext != nil {
43-
functions.append(ExpandFocusRangeFunction(contextCollector: self))
44-
}
50+
if context.focusedContext != nil {
51+
functions.append(ExpandFocusRangeFunction(contextCollector: self))
52+
}
4553

46-
// When the bot is not focusing on any code, or the focusing area is not the user's
47-
// selection, it can move the focus back to the user's selection.
54+
// When the bot is not focusing on any code, or the focusing area is not the user's
55+
// selection, it can move the focus back to the user's selection.
4856

49-
if context.focusedContext == nil ||
50-
!(context.focusedContext?.codeRange.contains(context.selectionRange) ?? false)
51-
{
52-
functions.append(MoveToFocusedCodeFunction(contextCollector: self))
53-
}
57+
if context.focusedContext == nil ||
58+
!(context.focusedContext?.codeRange.contains(context.selectionRange) ?? false)
59+
{
60+
functions.append(MoveToFocusedCodeFunction(contextCollector: self))
61+
}
5462

55-
// When there is a line annotation not in the focused area, the bot can move the focus area
56-
// to the code covering the line of the annotation.
63+
// When there is a line annotation not in the focused area, the bot can move the focus
64+
// area
65+
// to the code covering the line of the annotation.
5766

58-
if let focusedContext = context.focusedContext,
59-
!focusedContext.otherLineAnnotations.isEmpty
60-
{
61-
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
62-
}
67+
if let focusedContext = context.focusedContext,
68+
!focusedContext.otherLineAnnotations.isEmpty
69+
{
70+
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
71+
}
6372

64-
if context.focusedContext == nil, !context.lineAnnotations.isEmpty {
65-
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
73+
if context.focusedContext == nil, !context.lineAnnotations.isEmpty {
74+
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
75+
}
6676
}
6777

6878
return .init(
69-
systemPrompt: extractSystemPrompt(context),
79+
systemPrompt: extractSystemPrompt(context, isSensitive: isSensitive),
7080
retrievedContent: [],
7181
functions: functions
7282
)
@@ -90,7 +100,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
90100
return activeDocumentContext
91101
}
92102

93-
func extractSystemPrompt(_ context: ActiveDocumentContext) -> String {
103+
func extractSystemPrompt(_ context: ActiveDocumentContext, isSensitive: Bool) -> String {
94104
let start = """
95105
## File and Code Scope
96106
@@ -108,7 +118,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
108118
let language = "Language: \(context.language.rawValue)"
109119

110120
if let focusedContext = context.focusedContext {
111-
let codeContext = focusedContext.context.isEmpty
121+
let codeContext = focusedContext.context.isEmpty || isSensitive
112122
? ""
113123
: """
114124
Focused Context:
@@ -119,14 +129,16 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
119129

120130
let codeRange = "Focused Range [line, character]: \(focusedContext.codeRange)"
121131

122-
let code = """
123-
Focused Code (start from line \(focusedContext.codeRange.start.line + 1)):
124-
```\(context.language.rawValue)
125-
\(focusedContext.code)
126-
```
127-
"""
132+
let code = context.selectionRange.isEmpty && isSensitive
133+
? ""
134+
: """
135+
Focused Code (start from line \(focusedContext.codeRange.start.line + 1)):
136+
```\(context.language.rawValue)
137+
\(focusedContext.code)
138+
```
139+
"""
128140

129-
let fileAnnotations = focusedContext.otherLineAnnotations.isEmpty
141+
let fileAnnotations = focusedContext.otherLineAnnotations.isEmpty || isSensitive
130142
? ""
131143
: """
132144
Other Annotations:\"""
@@ -139,7 +151,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
139151
\"""
140152
"""
141153

142-
let codeAnnotations = focusedContext.lineAnnotations.isEmpty
154+
let codeAnnotations = focusedContext.lineAnnotations.isEmpty || isSensitive
143155
? ""
144156
: """
145157
Annotations Inside Focused Range:\"""
@@ -165,7 +177,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
165177
.joined(separator: "\n\n")
166178
} else {
167179
let selectionRange = "Selection Range [line, character]: \(context.selectionRange)"
168-
let lineAnnotations = context.lineAnnotations.isEmpty
180+
let lineAnnotations = context.lineAnnotations.isEmpty || isSensitive
169181
? ""
170182
: """
171183
Line Annotations:\"""

0 commit comments

Comments
 (0)