Skip to content

Commit b3ae4ec

Browse files
committed
Fix that context retriever was incorrect when the context is too long
1 parent 2b317fd commit b3ae4ec

11 files changed

Lines changed: 86 additions & 32 deletions

Pro

Submodule Pro updated from e1670cc to d1282a4

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
8282
selectedCode: "",
8383
selectionRange: .outOfScope,
8484
lineAnnotations: [],
85-
imports: []
85+
imports: [],
86+
includes: []
8687
)
8788

8889
activeDocumentContext.update(info)

Tool/Sources/FocusedCodeFinder/ActiveDocumentContext.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ public struct ActiveDocumentContext {
1111
public var selectionRange: CursorRange
1212
public var lineAnnotations: [EditorInformation.LineAnnotation]
1313
public var imports: [String]
14+
public var includes: [String]
1415

1516
public struct FocusedContext {
1617
public struct Context: Equatable {
1718
public var signature: String
1819
public var name: String
1920
public var range: CursorRange
20-
21+
2122
public init(signature: String, name: String, range: CursorRange) {
2223
self.signature = signature
2324
self.name = name
2425
self.range = range
2526
}
2627
}
27-
28+
2829
public var context: [Context]
2930
public var contextRange: CursorRange
31+
public var smallestContextRange: CursorRange
3032
public var codeRange: CursorRange
3133
public var code: String
3234
public var lineAnnotations: [EditorInformation.LineAnnotation]
@@ -35,13 +37,15 @@ public struct ActiveDocumentContext {
3537
public init(
3638
context: [Context],
3739
contextRange: CursorRange,
40+
smallestContextRange: CursorRange,
3841
codeRange: CursorRange,
3942
code: String,
4043
lineAnnotations: [EditorInformation.LineAnnotation],
4144
otherLineAnnotations: [EditorInformation.LineAnnotation]
4245
) {
4346
self.context = context
4447
self.contextRange = contextRange
48+
self.smallestContextRange = smallestContextRange
4549
self.codeRange = codeRange
4650
self.code = code
4751
self.lineAnnotations = lineAnnotations
@@ -61,6 +65,7 @@ public struct ActiveDocumentContext {
6165
selectionRange: CursorRange,
6266
lineAnnotations: [EditorInformation.LineAnnotation],
6367
imports: [String],
68+
includes: [String],
6469
focusedContext: FocusedContext? = nil
6570
) {
6671
self.documentURL = documentURL
@@ -72,6 +77,7 @@ public struct ActiveDocumentContext {
7277
self.selectionRange = selectionRange
7378
self.lineAnnotations = lineAnnotations
7479
self.imports = imports
80+
self.includes = includes
7581
self.focusedContext = focusedContext
7682
}
7783

@@ -92,15 +98,18 @@ public struct ActiveDocumentContext {
9298
}
9399

94100
public mutating func moveToCodeContainingRange(_ range: CursorRange) {
95-
let finder = FocusedCodeFinder()
96-
101+
let finder = FocusedCodeFinder(
102+
maxFocusedCodeLineCount: UserDefaults.shared.value(for: \.maxFocusedCodeLineCount)
103+
)
104+
97105
let codeContext = finder.findFocusedCode(
98106
in: .init(documentURL: documentURL, content: fileContent, lines: lines),
99107
containingRange: range,
100108
language: language
101109
)
102110

103111
imports = codeContext.imports
112+
includes = codeContext.includes
104113

105114
let startLine = codeContext.focusedRange.start.line
106115
let endLine = codeContext.focusedRange.end.line
@@ -117,6 +126,7 @@ public struct ActiveDocumentContext {
117126
focusedContext = .init(
118127
context: codeContext.scopeContexts,
119128
contextRange: codeContext.contextRange,
129+
smallestContextRange: codeContext.smallestContextRange,
120130
codeRange: codeContext.focusedRange,
121131
code: codeContext.focusedCode,
122132
lineAnnotations: matchedAnnotations,
@@ -144,6 +154,7 @@ public struct ActiveDocumentContext {
144154
selectionRange = info.editorContent?.selections.first ?? .zero
145155
lineAnnotations = info.editorContent?.lineAnnotations ?? []
146156
imports = []
157+
includes = []
147158

148159
if changed {
149160
moveToFocusedCode()

Tool/Sources/FocusedCodeFinder/FocusedCodeFinder.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public struct CodeContext: Equatable {
2323

2424
public var scope: Scope
2525
public var contextRange: CursorRange
26+
public var smallestContextRange: CursorRange
2627
public var focusedRange: CursorRange
2728
public var focusedCode: String
2829
public var imports: [String]
@@ -32,6 +33,7 @@ public struct CodeContext: Equatable {
3233
.init(
3334
scope: .file,
3435
contextRange: .zero,
36+
smallestContextRange: .zero,
3537
focusedRange: .zero,
3638
focusedCode: "",
3739
imports: [],
@@ -42,13 +44,15 @@ public struct CodeContext: Equatable {
4244
public init(
4345
scope: Scope,
4446
contextRange: CursorRange,
47+
smallestContextRange: CursorRange,
4548
focusedRange: CursorRange,
4649
focusedCode: String,
4750
imports: [String],
4851
includes: [String]
4952
) {
5053
self.scope = scope
5154
self.contextRange = contextRange
55+
self.smallestContextRange = smallestContextRange
5256
self.focusedRange = focusedRange
5357
self.focusedCode = focusedCode
5458
self.imports = imports
@@ -57,7 +61,11 @@ public struct CodeContext: Equatable {
5761
}
5862

5963
public struct FocusedCodeFinder {
60-
public init() {}
64+
public let maxFocusedCodeLineCount: Int
65+
66+
public init(maxFocusedCodeLineCount: Int) {
67+
self.maxFocusedCodeLineCount = maxFocusedCodeLineCount
68+
}
6169

6270
public struct Document {
6371
var documentURL: URL
@@ -79,10 +87,12 @@ public struct FocusedCodeFinder {
7987
let finder: FocusedCodeFinderType = {
8088
switch language {
8189
case .builtIn(.swift):
82-
return SwiftFocusedCodeFinder()
90+
return SwiftFocusedCodeFinder(maxFocusedCodeLineCount: maxFocusedCodeLineCount)
8391
case .builtIn(.objc), .builtIn(.objcpp), .builtIn(.c):
84-
#warning("TODO: Implement C++ focused code finder, use it for C and metal shading language")
85-
return ObjectiveCFocusedCodeFinder()
92+
#warning(
93+
"TODO: Implement C++ focused code finder, use it for C and metal shading language"
94+
)
95+
return ObjectiveCFocusedCodeFinder(maxFocusedCodeLineCount: maxFocusedCodeLineCount)
8696
default:
8797
return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
8898
}

Tool/Sources/FocusedCodeFinder/KnownLanguageFocusedCodeFinder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ public extension KnownLanguageFocusedCodeFinderType {
126126

127127
return .init(
128128
scope: scopeContexts.isEmpty ? .file : .scope(signature: scopeContexts),
129-
contextRange: contextRange,
129+
contextRange: contextRange,
130+
smallestContextRange: codeRange,
130131
focusedRange: focusedRange,
131132
focusedCode: code,
132133
imports: contextInfo.imports,

Tool/Sources/FocusedCodeFinder/ObjectiveC/ObjectiveCCodeFinder.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ public class ObjectiveCFocusedCodeFinder: KnownLanguageFocusedCodeFinder<
1414
ASTNode,
1515
TreeSitterTextPosition
1616
> {
17-
override public init(
18-
maxFocusedCodeLineCount: Int = UserDefaults.shared.value(for: \.maxFocusedCodeLineCount)
19-
) {
17+
override public init(maxFocusedCodeLineCount: Int) {
2018
super.init(maxFocusedCodeLineCount: maxFocusedCodeLineCount)
2119
}
2220

@@ -99,7 +97,7 @@ public class ObjectiveCFocusedCodeFinder: KnownLanguageFocusedCodeFinder<
9997
var prefix = ""
10098
/// Generics, super class, etc.
10199
var extra = ""
102-
100+
103101
if let nameNode = node.child(byFieldName: "name") {
104102
name = textProvider(.node(nameNode))
105103
prefix = textProvider(.range(
@@ -135,7 +133,7 @@ public class ObjectiveCFocusedCodeFinder: KnownLanguageFocusedCodeFinder<
135133
prefix = prefix.split(separator: "\n")
136134
.joined(separator: " ")
137135
.trimmingCharacters(in: .whitespacesAndNewlines)
138-
136+
139137
extra = extra.split(separator: "\n")
140138
.joined(separator: " ")
141139
.trimmingCharacters(in: .whitespacesAndNewlines)
@@ -152,7 +150,7 @@ public class ObjectiveCFocusedCodeFinder: KnownLanguageFocusedCodeFinder<
152150
canBeUsedAsCodeRange: true
153151
)
154152
}
155-
153+
156154
func parseMethodDefinitionNode(
157155
_ node: ASTNode,
158156
textProvider: @escaping TextProvider

Tool/Sources/FocusedCodeFinder/Swift/SwiftFocusedCodeFinder.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ public class SwiftFocusedCodeFinder: KnownLanguageFocusedCodeFinder<
1010
SyntaxProtocol,
1111
SyntaxProtocol
1212
> {
13-
override public init(
14-
maxFocusedCodeLineCount: Int = UserDefaults.shared.value(for: \.maxFocusedCodeLineCount)
15-
) {
13+
override public init(maxFocusedCodeLineCount: Int) {
1614
super.init(maxFocusedCodeLineCount: maxFocusedCodeLineCount)
1715
}
1816

Tool/Sources/FocusedCodeFinder/UnknownLanguageFocusCodeFinder.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ public struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinderType {
2828
min(startLineIndex + proposedLineCount - 1, lines.count - 1)
2929
)
3030

31+
if lines.endIndex <= endLineIndex { return .empty }
32+
3133
let focusedLines = lines[startLineIndex...endLineIndex]
3234

3335
let contextStartLine = max(startLineIndex - 5, 0)
3436
let contextEndLine = min(endLineIndex + 5, lines.count - 1)
37+
38+
let contextRange = CursorRange(
39+
start: .init(line: contextStartLine, character: 0),
40+
end: .init(line: contextEndLine, character: lines[contextEndLine].count)
41+
)
3542

3643
return .init(
3744
scope: .top,
38-
contextRange: .init(
39-
start: .init(line: contextStartLine, character: 0),
40-
end: .init(line: contextEndLine, character: lines[contextEndLine].count)
41-
),
45+
contextRange: contextRange,
46+
smallestContextRange: contextRange,
4247
focusedRange: .init(
4348
start: .init(line: startLineIndex, character: 0),
4449
end: .init(line: endLineIndex, character: lines[endLineIndex].count)
@@ -57,16 +62,19 @@ public struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinderType {
5762
let focusedLines = document.lines[startLine...endLine]
5863
let contextStartLine = max(startLine - 3, 0)
5964
let contextEndLine = min(endLine + 3, document.lines.count - 1)
65+
66+
let contextRange = CursorRange(
67+
start: .init(line: contextStartLine, character: 0),
68+
end: .init(
69+
line: contextEndLine,
70+
character: document.lines[contextEndLine].count
71+
)
72+
)
6073

6174
return CodeContext(
6275
scope: .top,
63-
contextRange: .init(
64-
start: .init(line: contextStartLine, character: 0),
65-
end: .init(
66-
line: contextEndLine,
67-
character: document.lines[contextEndLine].count
68-
)
69-
),
76+
contextRange: contextRange,
77+
smallestContextRange: contextRange,
7078
focusedRange: containingRange,
7179
focusedCode: focusedLines.joined(),
7280
imports: [],

Tool/Tests/FocusedCodeFinderTests/ObjectiveCFocusedCodeFinderTests.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
3636
range: .init(startPair: (1, 0), endPair: (5, 1))
3737
),
3838
]),
39-
contextRange: .init(startPair: (0, 0), endPair: (6, 4)),
39+
contextRange: .init(startPair: (0, 0), endPair: (6, 4)),
40+
smallestContextRange: range,
4041
focusedRange: range,
4142
focusedCode: """
4243
NSInteger foo = 0;
@@ -69,6 +70,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
6970
),
7071
]),
7172
contextRange: .init(startPair: (0, 0), endPair: (4, 1)),
73+
smallestContextRange: range,
7274
focusedRange: range,
7375
focusedCode: """
7476
NSLog(@"Hello");
@@ -107,6 +109,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
107109
),
108110
]),
109111
contextRange: .init(startPair: (0, 0), endPair: (7, 4)),
112+
smallestContextRange: range,
110113
focusedRange: range,
111114
focusedCode: """
112115
- (void)fooWith:(NSInteger)foo {
@@ -146,6 +149,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
146149
),
147150
]),
148151
contextRange: .init(startPair: (0, 0), endPair: (4, 4)),
152+
smallestContextRange: range,
149153
focusedRange: range,
150154
focusedCode: """
151155
- (void)fooWith:(NSInteger)foo;
@@ -183,6 +187,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
183187
),
184188
]),
185189
contextRange: .init(startPair: (0, 0), endPair: (4, 4)),
190+
smallestContextRange: range,
186191
focusedRange: range,
187192
focusedCode: """
188193
- (void)fooWith:(NSInteger)foo;
@@ -220,6 +225,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
220225
),
221226
]),
222227
contextRange: .init(startPair: (0, 0), endPair: (4, 4)),
228+
smallestContextRange: range,
223229
focusedRange: range,
224230
focusedCode: """
225231
- (void)fooWith:(NSInteger)foo;
@@ -257,6 +263,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
257263
),
258264
]),
259265
contextRange: .init(startPair: (0, 0), endPair: (4, 1)),
266+
smallestContextRange: range,
260267
focusedRange: range,
261268
focusedCode: """
262269
NSInteger foo;
@@ -294,6 +301,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
294301
),
295302
]),
296303
contextRange: .init(startPair: (0, 0), endPair: (4, 1)),
304+
smallestContextRange: range,
297305
focusedRange: range,
298306
focusedCode: """
299307
foo,
@@ -331,6 +339,7 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
331339
),
332340
]),
333341
contextRange: .init(startPair: (0, 0), endPair: (4, 2)),
342+
smallestContextRange: range,
334343
focusedRange: range,
335344
focusedCode: """
336345
foo,
@@ -372,6 +381,7 @@ final class ObjectiveCFocusedCodeFinder_Focus_Tests: XCTestCase {
372381
),
373382
]),
374383
contextRange: .init(startPair: (0, 0), endPair: (6, 4)),
384+
smallestContextRange: range,
375385
focusedRange: .init(startPair: (1, 0), endPair: (5, 1)),
376386
focusedCode: """
377387
- (void)fooWith:(NSInteger)foo {
@@ -408,6 +418,7 @@ final class ObjectiveCFocusedCodeFinder_Focus_Tests: XCTestCase {
408418
XCTAssertEqual(context, .init(
409419
scope: .file,
410420
contextRange: .init(startPair: (0, 0), endPair: (0, 0)),
421+
smallestContextRange: range,
411422
focusedRange: .init(startPair: (0, 0), endPair: (4, 4)),
412423
focusedCode: """
413424
@interface __GENERICS(NSArray, ObjectType) (BlocksKit)

0 commit comments

Comments
 (0)