forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefactService.swift
More file actions
278 lines (241 loc) · 9.78 KB
/
Copy pathRefactService.swift
File metadata and controls
278 lines (241 loc) · 9.78 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import Foundation
import LanguageClient
import LanguageServerProtocol
import Logger
import SuggestionModel
import XcodeInspector
public protocol RefactSuggestionServiceType {
func getCompletions(
fileURL: URL,
content: String,
cursorPosition: CursorPosition,
tabSize: Int,
indentSize: Int,
usesTabsForIndentation: Bool,
ignoreSpaceOnlySuggestions: Bool
) async throws -> [CodeSuggestion]
func notifyAccepted(_ suggestion: CodeSuggestion) async
func notifyOpenTextDocument(fileURL: URL, content: String) async throws
func notifyChangeTextDocument(fileURL: URL, content: String) async throws
func notifyCloseTextDocument(fileURL: URL) async throws
func cancelRequest() async
func terminate()
}
enum RefactError: Error, LocalizedError {
case languageServerNotInstalled
case languageServerOutdated
case languageServiceIsInstalling
var errorDescription: String? {
switch self {
case .languageServerNotInstalled:
return "Language server is not installed. Please install it in the host app."
case .languageServerOutdated:
return "Language server is outdated. Please update it in the host app or update the extension."
case .languageServiceIsInstalling:
return "Language service is installing, please try again later."
}
}
}
public class RefactSuggestionService {
static let sessionId = UUID().uuidString
let projectRootURL: URL
var server: RefactLSP?
var heartbeatTask: Task<Void, Error>?
var requestCounter: UInt64 = 0
var cancellationCounter: UInt64 = 0
let openedDocumentPool = OpenedDocumentPool()
let onServiceLaunched: () -> Void
let languageServerURL: URL
let supportURL: URL
private var ongoingTasks = Set<Task<[CodeSuggestion], Error>>()
init(designatedServer: RefactLSP) {
projectRootURL = URL(fileURLWithPath: "/")
server = designatedServer
onServiceLaunched = {}
languageServerURL = URL(fileURLWithPath: "/")
supportURL = URL(fileURLWithPath: "/")
}
public init(projectRootURL: URL, onServiceLaunched: @escaping () -> Void) throws {
self.projectRootURL = projectRootURL
self.onServiceLaunched = onServiceLaunched
let urls = try RefactSuggestionService.createFoldersIfNeeded()
languageServerURL = urls.executableURL.appendingPathComponent("refact-lsp")
supportURL = urls.supportURL
Task {
try await setupServerIfNeeded()
}
}
@discardableResult
func setupServerIfNeeded() async throws -> RefactLSP {
if let server { return server }
let binaryManager = RefactInstallationManager()
let installationStatus = binaryManager.checkInstallation()
switch installationStatus {
case .notInstalled:
throw RefactError.languageServerNotInstalled
case .installed:
break
}
let tempFolderURL = FileManager.default.temporaryDirectory
let managerDirectoryURL = tempFolderURL
.appendingPathComponent("com.intii.CopilotForXcode")
.appendingPathComponent(UUID().uuidString)
if !FileManager.default.fileExists(atPath: managerDirectoryURL.path) {
try FileManager.default.createDirectory(
at: managerDirectoryURL,
withIntermediateDirectories: true
)
}
let server = RefactLanguageServer(
languageServerExecutableURL: languageServerURL,
managerDirectoryURL: managerDirectoryURL,
supportURL: supportURL
)
server.terminationHandler = { [weak self] in
self?.server = nil
self?.heartbeatTask?.cancel()
self?.requestCounter = 0
self?.cancellationCounter = 0
Logger.refact.info("Language server is terminated, will be restarted when needed.")
}
self.server = server
server.start()
return server
}
public static func createFoldersIfNeeded() throws -> (
applicationSupportURL: URL,
gitHubCopilotURL: URL,
executableURL: URL,
supportURL: URL
) {
let supportURL = FileManager.default.urls(
for: .applicationSupportDirectory,
in: .userDomainMask
).first!.appendingPathComponent(
Bundle.main
.object(forInfoDictionaryKey: "APPLICATION_SUPPORT_FOLDER") as! String
)
if !FileManager.default.fileExists(atPath: supportURL.path) {
try? FileManager.default
.createDirectory(at: supportURL, withIntermediateDirectories: false)
}
let gitHubCopilotFolderURL = supportURL.appendingPathComponent("Refact")
if !FileManager.default.fileExists(atPath: gitHubCopilotFolderURL.path) {
try? FileManager.default
.createDirectory(at: gitHubCopilotFolderURL, withIntermediateDirectories: false)
}
let supportFolderURL = gitHubCopilotFolderURL.appendingPathComponent("support")
if !FileManager.default.fileExists(atPath: supportFolderURL.path) {
try? FileManager.default
.createDirectory(at: supportFolderURL, withIntermediateDirectories: false)
}
let executableFolderURL = gitHubCopilotFolderURL.appendingPathComponent("executable")
if !FileManager.default.fileExists(atPath: executableFolderURL.path) {
try? FileManager.default
.createDirectory(at: executableFolderURL, withIntermediateDirectories: false)
}
return (supportURL, gitHubCopilotFolderURL, executableFolderURL, supportFolderURL)
}
}
extension RefactSuggestionService {
func getRelativePath(of fileURL: URL) -> String {
let filePath = fileURL.path
let rootPath = projectRootURL.path
if let range = filePath.range(of: rootPath),
range.lowerBound == filePath.startIndex
{
let relativePath = filePath.replacingCharacters(
in: filePath.startIndex..<range.upperBound,
with: ""
)
return relativePath
}
return filePath
}
}
extension RefactSuggestionService: RefactSuggestionServiceType {
public func getCompletions(
fileURL: URL,
content: String,
cursorPosition: CursorPosition,
tabSize: Int,
indentSize: Int,
usesTabsForIndentation: Bool,
ignoreSpaceOnlySuggestions: Bool
) async throws -> [CodeSuggestion] {
ongoingTasks.forEach { $0.cancel() }
ongoingTasks.removeAll()
requestCounter += 1
let model = UserDefaults.shared.value(for: \.refactCodeCompletionModel)
let task = Task {
let lines = content.breakLines()
let currentLine = lines[cursorPosition.line]
let leftOfCursor = String(currentLine.prefix(cursorPosition.character))
let multiline = leftOfCursor.replacingOccurrences(of: "\\s", with: "", options: .regularExpression).isEmpty
let requestBody = RefactRequest.GetCompletion.RequestBody(
noCache: false,
client: "xcode",
parameters: .init(temperature: 0.2, maxNewTokens: 50),
model: model ,
inputs: .init(
cursor: .init(
character: cursorPosition.character,
file: fileURL.path,
line: cursorPosition.line
),
sources: [
fileURL.path:content
],
multiline: multiline
)
)
let request = RefactRequest.GetCompletion(
requestBody: requestBody
)
try Task.checkCancellation()
let result = try await (try await setupServerIfNeeded()).sendRequest(request)
try Task.checkCancellation()
let range0 = multiline ? CursorPosition(line: cursorPosition.line, character: 0) : cursorPosition
let range1 = CursorPosition(line: cursorPosition.line, character: currentLine.count)
return result.choices.enumerated().map { (index,item) in
CodeSuggestion(
id: "\(result.snippetTelemetryID)",
text: item.codeCompletion,
position: cursorPosition,
range: .init(start: range0, end: range1)
)
}
}
ongoingTasks.insert(task)
return try await task.value
}
public func cancelRequest() async {}
public func notifyAccepted(_ suggestion: CodeSuggestion) async {
guard let snippetTelemetryID = Int(suggestion.id) else { return }
_ = try? await (try setupServerIfNeeded())
.sendRequest(RefactRequest.SnippetAccepted(requestBody: .init(snippetTelemetryID: snippetTelemetryID)))
}
public func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
let relativePath = getRelativePath(of: fileURL)
await openedDocumentPool.openDocument(
url: fileURL,
relativePath: relativePath,
content: content
)
}
public func notifyChangeTextDocument(fileURL: URL, content: String) async throws {
let relativePath = getRelativePath(of: fileURL)
await openedDocumentPool.updateDocument(
url: fileURL,
relativePath: relativePath,
content: content
)
}
public func notifyCloseTextDocument(fileURL: URL) async throws {
await openedDocumentPool.closeDocument(url: fileURL)
}
public func terminate() {
server?.terminate()
server = nil
}
}