Skip to content

Commit 16eac03

Browse files
committed
Support setting max focused code line count
1 parent 7a34ee1 commit 16eac03

4 files changed

Lines changed: 64 additions & 27 deletions

File tree

Core/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct ActiveDocumentContext {
229229
case .builtIn(.swift):
230230
return SwiftFocusedCodeFinder()
231231
default:
232-
return UnknownLanguageFocusedCodeFinder()
232+
return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
233233
}
234234
}()
235235

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ struct CodeContext: Equatable {
1414
return []
1515
case .top:
1616
return ["Top level of the file"]
17-
case .scope(let signature):
17+
case let .scope(signature):
1818
return signature
1919
}
2020
}
21+
2122
var scope: Scope
2223
var contextRange: CursorRange
2324
var focusedRange: CursorRange
@@ -37,6 +38,12 @@ protocol FocusedCodeFinder {
3738
}
3839

3940
struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinder {
41+
let proposedSearchRange: Int
42+
43+
init(proposedSearchRange: Int) {
44+
self.proposedSearchRange = proposedSearchRange
45+
}
46+
4047
func findFocusedCode(
4148
containingRange: CursorRange,
4249
activeDocumentContext: ActiveDocumentContext
@@ -45,25 +52,32 @@ struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinder {
4552

4653
// when user is not selecting any code.
4754
if containingRange.start == containingRange.end {
48-
// search up and down for up to 7 lines.
55+
// search up and down for up to `proposedSearchRange * 2 + 1` lines.
4956
let lines = activeDocumentContext.lines
50-
var startLineIndex = max(containingRange.start.line - 3, 0)
51-
let endLineIndex = min(containingRange.start.line + 3, lines.count - 1)
52-
if endLineIndex - startLineIndex <= 6, startLineIndex > 0 {
53-
startLineIndex = max(startLineIndex - (6 - (endLineIndex - startLineIndex)), 0)
57+
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+
)
5465
}
5566
let focusedLines = lines[startLineIndex...endLineIndex]
5667

57-
let contextStartLine = max(startLineIndex - 3, 0)
58-
let contextEndLine = min(endLineIndex + 3, lines.count - 1)
68+
let contextStartLine = max(startLineIndex - 5, 0)
69+
let contextEndLine = min(endLineIndex + 5, lines.count - 1)
5970

6071
return .init(
6172
scope: .top,
6273
contextRange: .init(
6374
start: .init(line: contextStartLine, character: 0),
64-
end: .init(line: contextEndLine, character: 0)
75+
end: .init(line: contextEndLine, character: lines[contextEndLine].count)
76+
),
77+
focusedRange: .init(
78+
start: .init(line: startLineIndex, character: 0),
79+
end: .init(line: endLineIndex, character: lines[endLineIndex].count)
6580
),
66-
focusedRange: containingRange,
6781
focusedCode: focusedLines.joined(separator: "\n"),
6882
imports: []
6983
)
@@ -82,7 +96,10 @@ struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinder {
8296
scope: .top,
8397
contextRange: .init(
8498
start: .init(line: contextStartLine, character: 0),
85-
end: .init(line: contextEndLine, character: 0)
99+
end: .init(
100+
line: contextEndLine,
101+
character: activeDocumentContext.lines[contextEndLine].count
102+
)
86103
),
87104
focusedRange: containingRange,
88105
focusedCode: focusedLines.joined(separator: "\n"),

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import SwiftParser
55
import SwiftSyntax
66

77
struct SwiftFocusedCodeFinder: FocusedCodeFinder {
8+
let maxFocusedCodeLineCount: Int
9+
10+
init(maxFocusedCodeLineCount: Int = UserDefaults.shared.value(for: \.maxFocusedCodeLineCount)) {
11+
self.maxFocusedCodeLineCount = maxFocusedCodeLineCount
12+
}
13+
814
func findFocusedCode(
915
containingRange range: CursorRange,
1016
activeDocumentContext: ActiveDocumentContext
@@ -27,8 +33,7 @@ struct SwiftFocusedCodeFinder: FocusedCodeFinder {
2733

2834
var nodes = visitor.findScopeHierarchy()
2935

30-
let code: String
31-
let codeRange: CursorRange
36+
var codeRange: CursorRange
3237

3338
func convertRange(_ node: SyntaxProtocol) -> CursorRange {
3439
.init(sourceRange: node.sourceRange(converter: locationConverter))
@@ -52,10 +57,11 @@ struct SwiftFocusedCodeFinder: FocusedCodeFinder {
5257
}
5358
}
5459
guard let focusedNode else {
55-
var result = UnknownLanguageFocusedCodeFinder().findFocusedCode(
56-
containingRange: range,
57-
activeDocumentContext: activeDocumentContext
58-
)
60+
var result = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
61+
.findFocusedCode(
62+
containingRange: range,
63+
activeDocumentContext: activeDocumentContext
64+
)
5965
result.imports = visitor.imports
6066
return result
6167
}
@@ -64,8 +70,26 @@ struct SwiftFocusedCodeFinder: FocusedCodeFinder {
6470
codeRange = range
6571
}
6672

67-
code = EditorInformation
68-
.code(in: activeDocumentContext.lines, inside: codeRange, ignoreColumns: true).code
73+
let result = EditorInformation
74+
.code(in: activeDocumentContext.lines, inside: codeRange, ignoreColumns: true)
75+
76+
var code = result.code
77+
78+
if range.isEmpty, result.lines.count > maxFocusedCodeLineCount {
79+
// if the focused code is too long, truncate it to be shorter
80+
let centerLine = range.start.line
81+
let relativeCenterLine = centerLine - codeRange.start.line
82+
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")
85+
codeRange = .init(
86+
start: .init(line: startLine + codeRange.start.line, character: 0),
87+
end: .init(
88+
line: endLine + codeRange.start.line,
89+
character: result.lines[endLine].count
90+
)
91+
)
92+
}
6993

7094
var contextRange = CursorRange.zero
7195
var signature = [String]()
@@ -219,7 +243,7 @@ extension SwiftFocusedCodeFinder {
219243
let signature = node.bindings.first?.typeAnnotation?.trimmedDescription ?? ""
220244

221245
return (.init(
222-
signature: "\(type) \(name)\(signature.isEmpty ? "" : ": \(signature)")"
246+
signature: "\(type) \(name)\(signature.isEmpty ? "" : "\(signature)")"
223247
.prefixedModifiers(node.modifierAndAttributeText(extractText))
224248
.replacingOccurrences(of: "\n", with: " "),
225249
contextRange: convertRange(node)
@@ -471,10 +495,6 @@ extension SwiftFocusedCodeFinder {
471495

472496
// skip if possible
473497

474-
override func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
475-
skipChildrenIfPossible(node)
476-
}
477-
478498
override func visit(_ node: MemberDeclBlockSyntax) -> SyntaxVisitorContinueKind {
479499
skipChildrenIfPossible(node)
480500
}
@@ -486,7 +506,7 @@ extension SwiftFocusedCodeFinder {
486506
// capture if possible
487507

488508
override func visit(_ node: ImportDeclSyntax) -> SyntaxVisitorContinueKind {
489-
imports.append(node.trimmedDescription)
509+
imports.append(node.path.trimmedDescription)
490510
return .skipChildren
491511
}
492512

Tool/Sources/SuggestionModel/EditorInformation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct EditorInformation {
1111
public struct SourceEditorContent {
1212
/// The content of the source editor.
1313
public var content: String
14-
/// The content of the source editor in lines.
14+
/// The content of the source editor in lines. Every line should ends with `\n`.
1515
public var lines: [String]
1616
/// The selection ranges of the source editor.
1717
public var selections: [CursorRange]

0 commit comments

Comments
 (0)