Skip to content

Commit 0167734

Browse files
committed
Update focus code finder to return more info about contexts
1 parent e1cdd69 commit 0167734

File tree

6 files changed

+48
-14
lines changed

6 files changed

+48
-14
lines changed

Pro

Submodule Pro updated from e35f538 to 970fa1b

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
112112
: """
113113
Focused Context:
114114
```
115-
\(focusedContext.context.joined(separator: "\n"))
115+
\(focusedContext.context.map(\.signature).joined(separator: "\n"))
116116
```
117117
"""
118118

Tool/Sources/FocusedCodeFinder/ActiveDocumentContext.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,27 @@ public struct ActiveDocumentContext {
1313
public var imports: [String]
1414

1515
public struct FocusedContext {
16-
public var context: [String]
16+
public struct Context: Equatable {
17+
public var signature: String
18+
public var name: String
19+
public var range: CursorRange
20+
21+
public init(signature: String, name: String, range: CursorRange) {
22+
self.signature = signature
23+
self.name = name
24+
self.range = range
25+
}
26+
}
27+
28+
public var context: [Context]
1729
public var contextRange: CursorRange
1830
public var codeRange: CursorRange
1931
public var code: String
2032
public var lineAnnotations: [EditorInformation.LineAnnotation]
2133
public var otherLineAnnotations: [EditorInformation.LineAnnotation]
2234

2335
public init(
24-
context: [String],
36+
context: [Context],
2537
contextRange: CursorRange,
2638
codeRange: CursorRange,
2739
code: String,
@@ -109,7 +121,7 @@ public struct ActiveDocumentContext {
109121
}
110122

111123
focusedContext = .init(
112-
context: codeContext.scopeSignatures,
124+
context: codeContext.scopeContexts,
113125
contextRange: codeContext.contextRange,
114126
codeRange: codeContext.focusedRange,
115127
code: codeContext.focusedCode,

Tool/Sources/FocusedCodeFinder/FocusedCodeFinder.swift

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

44
public struct CodeContext: Equatable {
5+
public typealias ScopeContext = ActiveDocumentContext.FocusedContext.Context
6+
57
public enum Scope: Equatable {
68
case file
79
case top
8-
case scope(signature: [String])
10+
case scope(signature: [ScopeContext])
911
}
1012

11-
public var scopeSignatures: [String] {
13+
public var scopeContexts: [ScopeContext] {
1214
switch scope {
1315
case .file:
1416
return []
1517
case .top:
16-
return ["Top level of the file"]
17-
case let .scope(signature):
18-
return signature
18+
return []
19+
case let .scope(contexts):
20+
return contexts
1921
}
2022
}
2123

Tool/Sources/FocusedCodeFinder/SwiftFocusedCodeFinder.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinder {
100100
}
101101

102102
var contextRange = CursorRange.zero
103-
var signature = [String]()
103+
var signature = [CodeContext.ScopeContext]()
104104

105105
while let node = nodes.first {
106106
nodes.removeFirst()
@@ -114,7 +114,11 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinder {
114114

115115
if let context {
116116
contextRange = context.contextRange
117-
signature.insert(context.signature, at: 0)
117+
signature.insert(.init(
118+
signature: context.signature,
119+
name: context.name,
120+
range: context.contextRange
121+
), at: 0)
118122
}
119123

120124
if !more {
@@ -135,6 +139,7 @@ public struct SwiftFocusedCodeFinder: FocusedCodeFinder {
135139
extension SwiftFocusedCodeFinder {
136140
struct ContextInfo {
137141
var signature: String
142+
var name: String
138143
var contextRange: CursorRange
139144
var canBeUsedAsCodeRange: Bool = true
140145
}
@@ -163,6 +168,7 @@ extension SwiftFocusedCodeFinder {
163168
.prefixedModifiers(node.modifierAndAttributeText(extractText))
164169
.suffixedInheritance(node.inheritanceClauseTexts(extractText))
165170
.replacingOccurrences(of: "\n", with: " "),
171+
name: name,
166172
contextRange: convertRange(node)
167173
), false)
168174

@@ -174,6 +180,7 @@ extension SwiftFocusedCodeFinder {
174180
.prefixedModifiers(node.modifierAndAttributeText(extractText))
175181
.suffixedInheritance(node.inheritanceClauseTexts(extractText))
176182
.replacingOccurrences(of: "\n", with: " "),
183+
name: name,
177184
contextRange: convertRange(node)
178185
), false)
179186

@@ -185,6 +192,7 @@ extension SwiftFocusedCodeFinder {
185192
.prefixedModifiers(node.modifierAndAttributeText(extractText))
186193
.suffixedInheritance(node.inheritanceClauseTexts(extractText))
187194
.replacingOccurrences(of: "\n", with: " "),
195+
name: name,
188196
contextRange: convertRange(node)
189197
), false)
190198

@@ -196,6 +204,7 @@ extension SwiftFocusedCodeFinder {
196204
.prefixedModifiers(node.modifierAndAttributeText(extractText))
197205
.suffixedInheritance(node.inheritanceClauseTexts(extractText))
198206
.replacingOccurrences(of: "\n", with: ""),
207+
name: name,
199208
contextRange: convertRange(node)
200209
), false)
201210

@@ -206,6 +215,7 @@ extension SwiftFocusedCodeFinder {
206215
signature: "\(type) \(name)"
207216
.prefixedModifiers(node.modifierAndAttributeText(extractText))
208217
.replacingOccurrences(of: "\n", with: " "),
218+
name: name,
209219
contextRange: convertRange(node)
210220
), false)
211221

@@ -217,6 +227,7 @@ extension SwiftFocusedCodeFinder {
217227
.prefixedModifiers(node.modifierAndAttributeText(extractText))
218228
.suffixedInheritance(node.inheritanceClauseTexts(extractText))
219229
.replacingOccurrences(of: "\n", with: " "),
230+
name: name,
220231
contextRange: convertRange(node)
221232
), false)
222233

@@ -228,6 +239,7 @@ extension SwiftFocusedCodeFinder {
228239
.prefixedModifiers(node.modifierAndAttributeText(extractText))
229240
.suffixedInheritance(node.inheritanceClauseTexts(extractText))
230241
.replacingOccurrences(of: "\n", with: " "),
242+
name: name,
231243
contextRange: convertRange(node)
232244
), false)
233245

@@ -242,6 +254,7 @@ extension SwiftFocusedCodeFinder {
242254
return (.init(
243255
signature: "\(type) \(name)\(signature)"
244256
.prefixedModifiers(node.modifierAndAttributeText(extractText)),
257+
name: name,
245258
contextRange: convertRange(node)
246259
), true)
247260

@@ -254,6 +267,7 @@ extension SwiftFocusedCodeFinder {
254267
signature: "\(type) \(name)\(signature.isEmpty ? "" : "\(signature)")"
255268
.prefixedModifiers(node.modifierAndAttributeText(extractText))
256269
.replacingOccurrences(of: "\n", with: " "),
270+
name: name,
257271
contextRange: convertRange(node)
258272
), true)
259273

@@ -265,6 +279,7 @@ extension SwiftFocusedCodeFinder {
265279
signature: signature
266280
.prefixedModifiers(node.modifierAndAttributeText(extractText))
267281
.replacingOccurrences(of: "\n", with: " "),
282+
name: keyword,
268283
contextRange: convertRange(node)
269284
), true)
270285

@@ -278,6 +293,7 @@ extension SwiftFocusedCodeFinder {
278293
signature: signature
279294
.prefixedModifiers(node.modifierAndAttributeText(extractText))
280295
.replacingOccurrences(of: "\n", with: " "),
296+
name: "subscript",
281297
contextRange: convertRange(node)
282298
), true)
283299

@@ -288,7 +304,7 @@ extension SwiftFocusedCodeFinder {
288304
signature: "\(signature)"
289305
.prefixedModifiers(node.modifierAndAttributeText(extractText))
290306
.replacingOccurrences(of: "\n", with: " "),
291-
307+
name: "init",
292308
contextRange: convertRange(node)
293309
), true)
294310

@@ -299,7 +315,7 @@ extension SwiftFocusedCodeFinder {
299315
signature: signature
300316
.prefixedModifiers(node.modifierAndAttributeText(extractText))
301317
.replacingOccurrences(of: "\n", with: " "),
302-
318+
name: "deinit",
303319
contextRange: convertRange(node)
304320
), true)
305321

@@ -308,6 +324,7 @@ extension SwiftFocusedCodeFinder {
308324

309325
return (.init(
310326
signature: signature.replacingOccurrences(of: "\n", with: " "),
327+
name: "closure",
311328
contextRange: convertRange(node)
312329
), true)
313330

@@ -316,13 +333,15 @@ extension SwiftFocusedCodeFinder {
316333

317334
return (.init(
318335
signature: signature.replacingOccurrences(of: "\n", with: " "),
336+
name: "function call",
319337
contextRange: convertRange(node),
320338
canBeUsedAsCodeRange: false
321339
), true)
322340

323341
case let node as SwitchCaseSyntax:
324342
return (.init(
325343
signature: node.trimmedDescription.replacingOccurrences(of: "\n", with: " "),
344+
name: "switch",
326345
contextRange: convertRange(node)
327346
), true)
328347

Tool/Sources/SuggestionModel/ExportedFromLSP.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import LanguageServerProtocol
22

3+
/// Line starts at 0.
34
public typealias CursorPosition = LanguageServerProtocol.Position
45

56
public extension CursorPosition {

0 commit comments

Comments
 (0)