forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFocusedCodeFinder.swift
More file actions
113 lines (98 loc) · 3.14 KB
/
FocusedCodeFinder.swift
File metadata and controls
113 lines (98 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Foundation
import SuggestionBasic
public struct CodeContext: Equatable {
public typealias ScopeContext = ActiveDocumentContext.FocusedContext.Context
public enum Scope: Equatable {
case file
case top
case scope(signature: [ScopeContext])
}
public var scopeContexts: [ScopeContext] {
switch scope {
case .file:
return []
case .top:
return []
case let .scope(contexts):
return contexts
}
}
public var scope: Scope
public var contextRange: CursorRange
public var smallestContextRange: CursorRange
public var focusedRange: CursorRange
public var focusedCode: String
public var imports: [String]
public var includes: [String]
public static var empty: CodeContext {
.init(
scope: .file,
contextRange: .zero,
smallestContextRange: .zero,
focusedRange: .zero,
focusedCode: "",
imports: [],
includes: []
)
}
public init(
scope: Scope,
contextRange: CursorRange,
smallestContextRange: CursorRange,
focusedRange: CursorRange,
focusedCode: String,
imports: [String],
includes: [String]
) {
self.scope = scope
self.contextRange = contextRange
self.smallestContextRange = smallestContextRange
self.focusedRange = focusedRange
self.focusedCode = focusedCode
self.imports = imports
self.includes = includes
}
}
public struct FocusedCodeFinder {
public let maxFocusedCodeLineCount: Int
public init(maxFocusedCodeLineCount: Int) {
self.maxFocusedCodeLineCount = maxFocusedCodeLineCount
}
public struct Document {
var documentURL: URL
var content: String
var lines: [String]
public init(documentURL: URL, content: String, lines: [String]) {
self.documentURL = documentURL
self.content = content
self.lines = lines
}
}
public func findFocusedCode(
in document: Document,
containingRange: CursorRange,
language: CodeLanguage
) -> CodeContext {
let finder: FocusedCodeFinderType = {
switch language {
case .builtIn(.swift):
return SwiftFocusedCodeFinder(maxFocusedCodeLineCount: maxFocusedCodeLineCount)
case .builtIn(.objc), .builtIn(.objcpp), .builtIn(.c):
#warning(
"TODO: Implement C++ focused code finder, use it for C and metal shading language"
)
return ObjectiveCFocusedCodeFinder(maxFocusedCodeLineCount: maxFocusedCodeLineCount)
default:
return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5)
}
}()
return finder.findFocusedCode(in: document, containingRange: containingRange)
}
}
public protocol FocusedCodeFinderType {
typealias Document = FocusedCodeFinder.Document
func findFocusedCode(
in document: Document,
containingRange: CursorRange
) -> CodeContext
}