@@ -3,7 +3,7 @@ import SuggestionModel
33
44public 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+
4982public 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,
0 commit comments