@@ -5,6 +5,12 @@ import SwiftParser
55import SwiftSyntax
66
77struct 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
0 commit comments