Skip to content

Commit 0f39281

Browse files
committed
Update tests
1 parent 16eac03 commit 0f39281

File tree

4 files changed

+160
-16
lines changed

4 files changed

+160
-16
lines changed

Core/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/FocusedCodeFinder/FocusedCodeFinder.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinder {
5555
// search up and down for up to `proposedSearchRange * 2 + 1` lines.
5656
let lines = activeDocumentContext.lines
5757
let proposedLineCount = proposedSearchRange * 2 + 1
58-
var startLineIndex = max(containingRange.start.line - proposedSearchRange, 0)
59-
let endLineIndex = min(containingRange.start.line + proposedSearchRange, lines.count - 1)
60-
if endLineIndex - startLineIndex < proposedLineCount, startLineIndex > 0 {
61-
startLineIndex = max(
62-
startLineIndex - ((proposedSearchRange * 2) - (endLineIndex - startLineIndex)),
63-
0
64-
)
65-
}
58+
let startLineIndex = max(containingRange.start.line - proposedSearchRange, 0)
59+
let endLineIndex = max(
60+
startLineIndex,
61+
min(startLineIndex + proposedLineCount - 1, lines.count - 1)
62+
)
63+
6664
let focusedLines = lines[startLineIndex...endLineIndex]
6765

6866
let contextStartLine = max(startLineIndex - 5, 0)
@@ -78,7 +76,7 @@ struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinder {
7876
start: .init(line: startLineIndex, character: 0),
7977
end: .init(line: endLineIndex, character: lines[endLineIndex].count)
8078
),
81-
focusedCode: focusedLines.joined(separator: "\n"),
79+
focusedCode: focusedLines.joined(),
8280
imports: []
8381
)
8482
}
@@ -102,7 +100,7 @@ struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinder {
102100
)
103101
),
104102
focusedRange: containingRange,
105-
focusedCode: focusedLines.joined(separator: "\n"),
103+
focusedCode: focusedLines.joined(),
106104
imports: []
107105
)
108106
}

Core/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/FocusedCodeFinder/SwiftFocusedCodeFinder.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ struct SwiftFocusedCodeFinder: FocusedCodeFinder {
8080
let centerLine = range.start.line
8181
let relativeCenterLine = centerLine - codeRange.start.line
8282
let startLine = max(0, relativeCenterLine - maxFocusedCodeLineCount / 2)
83-
let endLine = min(result.lines.count - 1, startLine + maxFocusedCodeLineCount)
84-
code = result.lines[startLine...endLine].joined(separator: "\n")
83+
let endLine = max(
84+
startLine,
85+
min(result.lines.count - 1, startLine + maxFocusedCodeLineCount - 1)
86+
)
87+
88+
code = result.lines[startLine...endLine].joined()
8589
codeRange = .init(
8690
start: .init(line: startLine + codeRange.start.line, character: 0),
8791
end: .init(

Core/Tests/ActiveDocumentChatContextCollectorTests/SwiftFocusedCodeFinderTests.swift

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,33 @@ final class SwiftFocusedCodeFinder_FocusedCode_Tests: XCTestCase {
340340
case d
341341
case e
342342
}
343+
344+
func hello() {
345+
print("hello")
346+
print("hello")
347+
}
343348
"""
344349
let range = CursorRange(startPair: (0, 0), endPair: (0, 0))
345-
let context = SwiftFocusedCodeFinder().findFocusedCode(
350+
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: 1000).findFocusedCode(
346351
containingRange: range,
347352
activeDocumentContext: context(code: code)
348353
)
349354
XCTAssertEqual(context, .init(
350355
scope: .top,
351-
contextRange: .init(startPair: (0, 0), endPair: (6, 11)),
352-
focusedRange: .init(startPair: (0, 0), endPair: (3, 11)),
356+
contextRange: .init(startPair: (0, 0), endPair: (13, 2)),
357+
focusedRange: .init(startPair: (0, 0), endPair: (10, 15)),
353358
focusedCode: """
354359
@MainActor
355360
public
356361
indirect enum A {
357362
case a
363+
case b
364+
case c
365+
case d
366+
case e
367+
}
368+
369+
func hello() {
358370
359371
""",
360372
imports: []
@@ -374,7 +386,7 @@ final class SwiftFocusedCodeFinder_FocusedCode_Tests: XCTestCase {
374386
}
375387
"""
376388
let range = CursorRange(startPair: (3, 0), endPair: (3, 0))
377-
let context = SwiftFocusedCodeFinder().findFocusedCode(
389+
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: 1000).findFocusedCode(
378390
containingRange: range,
379391
activeDocumentContext: context(code: code)
380392
)
@@ -397,4 +409,35 @@ final class SwiftFocusedCodeFinder_FocusedCode_Tests: XCTestCase {
397409
imports: []
398410
))
399411
}
412+
413+
func test_get_focused_code_inside_enum_with_limited_max_line_count() {
414+
let code = """
415+
@MainActor
416+
public
417+
indirect enum A {
418+
case a
419+
case b
420+
case c
421+
case d
422+
case e
423+
}
424+
"""
425+
let range = CursorRange(startPair: (3, 0), endPair: (3, 0))
426+
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: 3).findFocusedCode(
427+
containingRange: range,
428+
activeDocumentContext: context(code: code)
429+
)
430+
XCTAssertEqual(context, .init(
431+
scope: .file,
432+
contextRange: .init(startPair: (0, 0), endPair: (0, 0)),
433+
focusedRange: .init(startPair: (2, 0), endPair: (4, 11)),
434+
focusedCode: """
435+
indirect enum A {
436+
case a
437+
case b
438+
439+
""",
440+
imports: []
441+
))
442+
}
400443
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import XCTest
2+
import Foundation
3+
4+
@testable import ActiveDocumentChatContextCollector
5+
6+
class UnknownLanguageFocusedCodeFinderTests: XCTestCase {
7+
func context(code: String) -> ActiveDocumentContext {
8+
.init(
9+
filePath: "",
10+
relativePath: "",
11+
language: .builtIn(.swift),
12+
fileContent: code,
13+
lines: code.components(separatedBy: "\n").map { "\($0)\n" },
14+
selectedCode: "", selectionRange: .zero,
15+
lineAnnotations: [],
16+
imports: []
17+
)
18+
}
19+
20+
func test_the_code_is_long_enough_for_the_search_range() {
21+
let code = stride(from: 0, through: 100, by: 1).map { "\($0)\n" }.joined()
22+
let context = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
23+
.findFocusedCode(
24+
containingRange: .init(startPair: (50, 0), endPair: (50, 0)),
25+
activeDocumentContext: self.context(code: code)
26+
)
27+
XCTAssertEqual(context, .init(
28+
scope: .top,
29+
contextRange: .init(startPair: (40, 0), endPair: (60, 3)),
30+
focusedRange: .init(startPair: (45, 0), endPair: (55, 3)),
31+
focusedCode: stride(from: 45, through: 55, by: 1).map { "\($0)\n" }.joined(),
32+
imports: []
33+
))
34+
}
35+
36+
func test_the_upper_side_is_not_long_enough_expand_the_lower_end() {
37+
let code = stride(from: 0, through: 100, by: 1).map { "\($0)\n" }.joined()
38+
let context = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
39+
.findFocusedCode(
40+
containingRange: .init(startPair: (2, 0), endPair: (2, 0)),
41+
activeDocumentContext: self.context(code: code)
42+
)
43+
XCTAssertEqual(context, .init(
44+
scope: .top,
45+
contextRange: .init(startPair: (0, 0), endPair: (15, 3)),
46+
focusedRange: .init(startPair: (0, 0), endPair: (10, 3)),
47+
focusedCode: stride(from: 0, through: 10, by: 1).map { "\($0)\n" }.joined(),
48+
imports: []
49+
))
50+
}
51+
52+
func test_the_lower_side_is_not_long_enough_do_not_expand_the_upper_end() {
53+
let code = stride(from: 0, through: 100, by: 1).map { "\($0)\n" }.joined()
54+
let context = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
55+
.findFocusedCode(
56+
containingRange: .init(startPair: (99, 0), endPair: (99, 0)),
57+
activeDocumentContext: self.context(code: code)
58+
)
59+
XCTAssertEqual(context, .init(
60+
scope: .top,
61+
contextRange: .init(startPair: (89, 0), endPair: (101, 1)),
62+
focusedRange: .init(startPair: (94, 0), endPair: (101, 1)),
63+
focusedCode: stride(from: 94, through: 100, by: 1).map { "\($0)\n" }.joined() + "\n",
64+
imports: []
65+
))
66+
}
67+
68+
func test_both_sides_are_just_long_enough() {
69+
let code = stride(from: 0, through: 10, by: 1).map { "\($0)\n" }.joined()
70+
let context = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
71+
.findFocusedCode(
72+
containingRange: .init(startPair: (5, 0), endPair: (5, 0)),
73+
activeDocumentContext: self.context(code: code)
74+
)
75+
XCTAssertEqual(context, .init(
76+
scope: .top,
77+
contextRange: .init(startPair: (0, 0), endPair: (11, 1)),
78+
focusedRange: .init(startPair: (0, 0), endPair: (10, 3)),
79+
focusedCode: code,
80+
imports: []
81+
))
82+
}
83+
84+
func test_both_sides_are_not_long_enough() {
85+
let code = stride(from: 0, through: 4, by: 1).map { "\($0)\n" }.joined()
86+
let context = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
87+
.findFocusedCode(
88+
containingRange: .init(startPair: (3, 0), endPair: (3, 0)),
89+
activeDocumentContext: self.context(code: code)
90+
)
91+
XCTAssertEqual(context, .init(
92+
scope: .top,
93+
contextRange: .init(startPair: (0, 0), endPair: (5, 1)),
94+
focusedRange: .init(startPair: (0, 0), endPair: (5, 1)),
95+
focusedCode: code + "\n",
96+
imports: []
97+
))
98+
}
99+
}

0 commit comments

Comments
 (0)