Skip to content

Commit 9c47de5

Browse files
committed
Simplify interface of focused code finder
1 parent 6e0895c commit 9c47de5

File tree

6 files changed

+74
-44
lines changed

6 files changed

+74
-44
lines changed

Pro

Submodule Pro updated from 14b2333 to c916c13

Tool/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ let package = Package(
228228
dependencies: [
229229
"Preferences",
230230
"ASTParser",
231+
"SuggestionModel",
231232
.product(name: "SwiftSyntax", package: "swift-syntax"),
232233
.product(name: "SwiftParser", package: "swift-syntax"),
233234
]

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
7474

7575
func getActiveDocumentContext(_ info: EditorInformation) -> ActiveDocumentContext {
7676
var activeDocumentContext = activeDocumentContext ?? .init(
77-
filePath: "",
77+
documentURL: .init(fileURLWithPath: "/"),
7878
relativePath: "",
7979
language: .builtIn(.swift),
8080
fileContent: "",

Tool/Sources/FocusedCodeFinder/ActiveDocumentContext.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import SuggestionModel
33

44
public struct ActiveDocumentContext {
5-
public var filePath: String
5+
public var documentURL: URL
66
public var relativePath: String
77
public var language: CodeLanguage
88
public var fileContent: String
@@ -52,7 +52,7 @@ public struct ActiveDocumentContext {
5252
public var focusedContext: FocusedContext?
5353

5454
public init(
55-
filePath: String,
55+
documentURL: URL,
5656
relativePath: String,
5757
language: CodeLanguage,
5858
fileContent: String,
@@ -63,7 +63,7 @@ public struct ActiveDocumentContext {
6363
imports: [String],
6464
focusedContext: FocusedContext? = nil
6565
) {
66-
self.filePath = filePath
66+
self.documentURL = documentURL
6767
self.relativePath = relativePath
6868
self.language = language
6969
self.fileContent = fileContent
@@ -92,18 +92,12 @@ public struct ActiveDocumentContext {
9292
}
9393

9494
public mutating func moveToCodeContainingRange(_ range: CursorRange) {
95-
let finder: FocusedCodeFinderType = {
96-
switch language {
97-
case .builtIn(.swift):
98-
return SwiftFocusedCodeFinder()
99-
default:
100-
return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
101-
}
102-
}()
103-
95+
let finder = FocusedCodeFinder()
96+
10497
let codeContext = finder.findFocusedCode(
98+
in: .init(documentURL: documentURL, content: fileContent, lines: lines),
10599
containingRange: range,
106-
activeDocumentContext: self
100+
language: language
107101
)
108102

109103
imports = codeContext.imports
@@ -141,7 +135,7 @@ public struct ActiveDocumentContext {
141135
return false
142136
}()
143137

144-
filePath = info.documentURL.path
138+
documentURL = info.documentURL
145139
relativePath = info.relativePath
146140
language = info.language
147141
fileContent = info.editorContent?.content ?? ""

Tool/Sources/FocusedCodeFinder/FocusedCodeFinder.swift

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SuggestionModel
33

44
public struct CodeContext: Equatable {
55
public typealias ScopeContext = ActiveDocumentContext.FocusedContext.Context
6-
6+
77
public enum Scope: Equatable {
88
case file
99
case top
@@ -46,10 +46,45 @@ public struct CodeContext: Equatable {
4646
}
4747
}
4848

49+
public struct FocusedCodeFinder {
50+
public init() {}
51+
52+
public struct Document {
53+
var documentURL: URL
54+
var content: String
55+
var lines: [String]
56+
57+
public init(documentURL: URL, content: String, lines: [String]) {
58+
self.documentURL = documentURL
59+
self.content = content
60+
self.lines = lines
61+
}
62+
}
63+
64+
public func findFocusedCode(
65+
in document: Document,
66+
containingRange: CursorRange,
67+
language: CodeLanguage
68+
) -> CodeContext {
69+
let finder: FocusedCodeFinderType = {
70+
switch language {
71+
case .builtIn(.swift):
72+
return SwiftFocusedCodeFinder()
73+
default:
74+
return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
75+
}
76+
}()
77+
78+
return finder.findFocusedCode(in: document, containingRange: containingRange)
79+
}
80+
}
81+
4982
public protocol FocusedCodeFinderType {
83+
typealias Document = FocusedCodeFinder.Document
84+
5085
func findFocusedCode(
51-
containingRange: CursorRange,
52-
activeDocumentContext: ActiveDocumentContext
86+
in document: Document,
87+
containingRange: CursorRange
5388
) -> CodeContext
5489
}
5590

@@ -61,15 +96,15 @@ public struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinderType {
6196
}
6297

6398
public func findFocusedCode(
64-
containingRange: CursorRange,
65-
activeDocumentContext: ActiveDocumentContext
99+
in document: Document,
100+
containingRange: CursorRange
66101
) -> CodeContext {
67-
guard !activeDocumentContext.lines.isEmpty else { return .empty }
102+
guard !document.lines.isEmpty else { return .empty }
68103

69104
// when user is not selecting any code.
70105
if containingRange.start == containingRange.end {
71106
// search up and down for up to `proposedSearchRange * 2 + 1` lines.
72-
let lines = activeDocumentContext.lines
107+
let lines = document.lines
73108
let proposedLineCount = proposedSearchRange * 2 + 1
74109
let startLineIndex = max(containingRange.start.line - proposedSearchRange, 0)
75110
let endLineIndex = min(
@@ -102,21 +137,21 @@ public struct UnknownLanguageFocusedCodeFinder: FocusedCodeFinderType {
102137
}
103138

104139
let startLine = max(containingRange.start.line, 0)
105-
let endLine = min(containingRange.end.line, activeDocumentContext.lines.count - 1)
140+
let endLine = min(containingRange.end.line, document.lines.count - 1)
106141

107142
if endLine < startLine { return .empty }
108143

109-
let focusedLines = activeDocumentContext.lines[startLine...endLine]
144+
let focusedLines = document.lines[startLine...endLine]
110145
let contextStartLine = max(startLine - 3, 0)
111-
let contextEndLine = min(endLine + 3, activeDocumentContext.lines.count - 1)
146+
let contextEndLine = min(endLine + 3, document.lines.count - 1)
112147

113148
return CodeContext(
114149
scope: .top,
115150
contextRange: .init(
116151
start: .init(line: contextStartLine, character: 0),
117152
end: .init(
118153
line: contextEndLine,
119-
character: activeDocumentContext.lines[contextEndLine].count
154+
character: document.lines[contextEndLine].count
120155
)
121156
),
122157
focusedRange: containingRange,

Tool/Sources/FocusedCodeFinder/SwiftFocusedCodeFinder.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinderType {
1616
}
1717

1818
public func findFocusedCode(
19-
containingRange range: CursorRange,
20-
activeDocumentContext: ActiveDocumentContext
19+
in document: Document,
20+
containingRange range: CursorRange
2121
) -> CodeContext {
22-
let source = activeDocumentContext.fileContent
22+
let source = document.content
2323
#warning("TODO: cache the tree")
2424
let tree = Parser.parse(source: source)
2525

2626
let locationConverter = SourceLocationConverter(
27-
file: activeDocumentContext.filePath,
27+
file: document.documentURL.path,
2828
tree: tree
2929
)
3030

@@ -52,8 +52,8 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinderType {
5252
node,
5353
parentNodes: nodes,
5454
tree: tree,
55-
activeDocumentContext: activeDocumentContext,
56-
locationConverter: locationConverter
55+
locationConverter: locationConverter,
56+
in: document
5757
)
5858
if context?.canBeUsedAsCodeRange ?? false {
5959
focusedNode = node
@@ -62,10 +62,7 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinderType {
6262
}
6363
guard let focusedNode else {
6464
var result = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 8)
65-
.findFocusedCode(
66-
containingRange: range,
67-
activeDocumentContext: activeDocumentContext
68-
)
65+
.findFocusedCode(in: document, containingRange: range)
6966
result.imports = visitor.imports
7067
return result
7168
}
@@ -74,8 +71,11 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinderType {
7471
codeRange = range
7572
}
7673

77-
let result = EditorInformation
78-
.code(in: activeDocumentContext.lines, inside: codeRange, ignoreColumns: true)
74+
let result = EditorInformation.code(
75+
in: document.lines,
76+
inside: codeRange,
77+
ignoreColumns: true
78+
)
7979

8080
var code = result.code
8181

@@ -108,8 +108,8 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinderType {
108108
node,
109109
parentNodes: nodes,
110110
tree: tree,
111-
activeDocumentContext: activeDocumentContext,
112-
locationConverter: locationConverter
111+
locationConverter: locationConverter,
112+
in: document
113113
)
114114

115115
if let context {
@@ -148,15 +148,15 @@ extension SwiftFocusedCodeFinder {
148148
_ node: SyntaxProtocol,
149149
parentNodes: [SyntaxProtocol],
150150
tree: SourceFileSyntax,
151-
activeDocumentContext: ActiveDocumentContext,
152-
locationConverter: SourceLocationConverter
151+
locationConverter: SourceLocationConverter,
152+
in document: Document
153153
) -> (context: ContextInfo?, more: Bool) {
154154
func convertRange(_ node: SyntaxProtocol) -> CursorRange {
155155
.init(sourceRange: node.sourceRange(converter: locationConverter))
156156
}
157157

158158
func extractText(_ node: SyntaxProtocol) -> String {
159-
EditorInformation.code(in: activeDocumentContext.lines, inside: convertRange(node)).code
159+
EditorInformation.code(in: document.lines, inside: convertRange(node)).code
160160
}
161161

162162
switch node {

0 commit comments

Comments
 (0)